Kolaru / MathTeXEngine.jl

A latex math mode engine in pure Julia.
MIT License
96 stars 20 forks source link

superscript/subscript with nothing on the left #30

Closed briochemc closed 3 years ago

briochemc commented 3 years ago

Would it be possible to allow for things like $^2$ without anything on the left? It does not seem possible currently... Minimal throwing example:

julia> tex = L"^2"
L"$^2$"

julia> generate_tex_elements(tex)
ERROR: TeXParseError: unexpected error while parsing
at position 1 (string index 1)
^2
^
Stack before
[1] TeXExpr :expr

Stacktrace:
  [1] texparse(data::String; showdebug::Bool)
    @ MathTeXEngine ~/.julia/packages/MathTeXEngine/CUYXS/src/parser/parser.jl:277
  [2] texparse
    @ ~/.julia/packages/MathTeXEngine/CUYXS/src/parser/parser.jl:267 [inlined]
  [3] _broadcast_getindex_evalf
    @ ./broadcast.jl:648 [inlined]
  [4] _broadcast_getindex
    @ ./broadcast.jl:621 [inlined]
  [5] _getindex
    @ ./broadcast.jl:644 [inlined]
  [6] _broadcast_getindex
    @ ./broadcast.jl:620 [inlined]
  [7] getindex
    @ ./broadcast.jl:575 [inlined]
  [8] copy
    @ ./broadcast.jl:922 [inlined]
  [9] materialize
    @ ./broadcast.jl:883 [inlined]
 [10] generate_tex_elements(str::LaTeXString, fontset::MathTeXEngine.FontSet)
    @ MathTeXEngine ~/.julia/packages/MathTeXEngine/CUYXS/src/engine/layout.jl:273
 [11] generate_tex_elements(str::LaTeXString)
    @ MathTeXEngine ~/.julia/packages/MathTeXEngine/CUYXS/src/engine/layout.jl:267
 [12] top-level scope
    @ REPL[9]:1

caused by: ArgumentError: array must be non-empty
Stacktrace:
  [1] pop!(a::Vector{Any})
    @ Base ./array.jl:1180
  [2] _setup_decorated!(stack::DataStructures.Stack{Any}, p::Int64, data::String)
    @ MathTeXEngine ~/.julia/packages/MathTeXEngine/CUYXS/src/parser/parser.jl:184
  [3] macro expansion
    @ ~/.julia/packages/MathTeXEngine/CUYXS/src/parser/parser.jl:253 [inlined]
  [4] macro expansion
    @ ~/.julia/packages/Automa/1KOLQ/src/codegen.jl:149 [inlined]
  [5] texparse(data::String; showdebug::Bool)
    @ MathTeXEngine ~/.julia/packages/MathTeXEngine/CUYXS/src/parser/parser.jl:275
  [6] texparse
    @ ~/.julia/packages/MathTeXEngine/CUYXS/src/parser/parser.jl:267 [inlined]
  [7] _broadcast_getindex_evalf
    @ ./broadcast.jl:648 [inlined]
  [8] _broadcast_getindex
    @ ./broadcast.jl:621 [inlined]
  [9] _getindex
    @ ./broadcast.jl:644 [inlined]
 [10] _broadcast_getindex
    @ ./broadcast.jl:620 [inlined]
 [11] getindex
    @ ./broadcast.jl:575 [inlined]
 [12] copy
    @ ./broadcast.jl:922 [inlined]
 [13] materialize
    @ ./broadcast.jl:883 [inlined]
 [14] generate_tex_elements(str::LaTeXString, fontset::MathTeXEngine.FontSet)
    @ MathTeXEngine ~/.julia/packages/MathTeXEngine/CUYXS/src/engine/layout.jl:273
 [15] generate_tex_elements(str::LaTeXString)
    @ MathTeXEngine ~/.julia/packages/MathTeXEngine/CUYXS/src/engine/layout.jl:267
 [16] top-level scope
    @ REPL[9]:1

I stumbled upon this error trying to write unit exponents in LaTeX, e.g., "3.2 km hr$^{-1}$" (where the km and hr are in text mode). Maybe this just needs \mathrm (and \textrm?) to be implemented as a work around?

fingolfin commented 3 years ago

Indeed this also appears in regular formulas occasionally to allow for super-/subscripts left of a symbol. E.g. with so-called "twisted type groups" in abstract algebra, one has things like a group of type $^2D_4$ appear (see e.g. here: https://irandrus.files.wordpress.com/2012/06/periodic-table-of-groups.pdf). So in that sense, a symbol can have four "decorations", not just two. Of course in LaTeX this can't in general be expressed / distinguished cleanly...

briochemc commented 3 years ago

Another common scientific use is isotopes, like 14C often LaTeX'd with $^{14}$C for example.

Kolaru commented 3 years ago

Would it be possible to allow for things like $^2$ without anything on the left? It does not seem possible currently... Minimal throwing example:

This is possible, I did expect it to be valid LaTeX though ^^'. I will need to experiment a bit to see what work well.

It look like the sub/superscript are not associated to the following character but to an empty space:

$_3^2 C_4$

$_3^2 V_4$

$_3^2 L_4$

$_3^2 A_4$

$M^2_3 C_4$

$V^2_3 C_4$

$V {}^2_3 C_4$

$L^2_3 C_4$

gives me

image

Maybe this just needs \mathrm (and \textrm?) to be implemented as a work around?

This was implemented in #27, but I forgot to release a new version. This should be available once the release get to the general registry.

briochemc commented 3 years ago

Maybe this just needs \mathrm (and \textrm?) to be implemented as a work around?

This was implemented in #27

I think only \mathrm was implemented and not \textrm, right?

Kolaru commented 3 years ago

I think only \mathrm was implemented and not \textrm, right?

Hum... I implemented \mathrm that just put stuff in roman font.

How is \textrm supposed to differ? Quick search show me that it preserves space, but I may be missing something more subtle.

briochemc commented 3 years ago

Not really much difference I agree. You could make \textrm behave the same as \mathrm I guess? 🤷

FWIW, I know that some specific LaTeX configurations make them look different, e.g., if you look carefully at the subscript A below (using an EGU journal template):

Screen Shot 2021-09-22 at 10 16 17 pm