LaTeXParseError when trying to make plot title using LaTeXStrings

I’m trying to create a LaTeXString title for a Makie plot that uses string interpolation, but I’m getting some weird behavior. Some of the LaTeXStrings that I’ve tried have worked, but others have not, and give me an error. I suspect it may have to do with the strings being too long for the size of the plot, though this issue only seems to occur with LaTeXStrings and not regular strings. I have tried changing the plot dimensions and I have been unable to resolve the problem. Here’s my code:

using GLMakie

C = 0.50
ℓ = 1.45
α = 0.10
N = 20
seed = 1234

C_str = @sprintf("%4.2f", C)
ℓ_str = @sprintf("%4.2f", ℓ)
α_str = @sprintf("%.2f", α)
N_str = @sprintf("%d", N)

#These work
#title_string = L"C = %$(C_str)"
#title_string = L"C = %$(C_str), \quad ℓ = %$(ℓ_str)" 
#title_string = L"C = %$(C_str), \quad ℓ = %$(ℓ_str), \quad α = %$(α_str)" 

#This gives a parsing error 
#title_string = L"C = %$(C_str), \quad ℓ = %$(ℓ_str), \quad α = %$(α_str), \quad  N = %$(N_str)" 

fig = Figure(resolution = (400,400))
ax = Axis(fig[1, 1]; title = title_string)
limits!(ax, (-10,10), (-10,10))
display(fig)

Any suggestions for how to fix this? As mentioned above, I suspect it has to do with the length of the LaTeXString, but if I put a really long regular string for the title (e.g. title_string = "adfadfadfadfadfadfadfadfadfadfadfadfadfadfadfadffadfafadfadfadfadfadfadfadfadfadfadfadfadfadfadfadfadffadfaf"), the title does correctly appear (albeit truncated to fit the plot width).

Also, in experimenting with the length of the strings, I noticed that \quad seems to be the only LaTeX command that can increase spacing in a LaTeXString plot title without GLMakie throwing an error. I have tried replacing \quad with \;, \,, \:, but they all give LaTeX parse errors. Are there any other commands besides \quad that will allow me to add smaller spaces in my LaTeXStrings in a way that is compatible with GLMakie?

The full error message is below:

TeXParseError: unexpected error while parsing
at position 55 (string index 52)
C = 0.50, \quad ℓ = 1.45, \quad α = 0.10, \quad  N = 20
                                                   ^
with stack (length 1):
[1] TeXExpr :expr
├─ 'C'
├─ TeXExpr :spaced
│  └─ '='
├─ '0'
├─ '.'
├─ '5'
├─ '0'
├─ ','
├─ TeXExpr :symbol
│  ├─ ' '
│  └─ "\\quad"
├─ TeXExpr :symbol
│  ├─ 'ℓ'
│  └─ "\\ell"
├─ TeXExpr :spaced
│  └─ '='
├─ '1'
├─ '.'
├─ '4'
├─ '5'
├─ ','
├─ TeXExpr :symbol
│  ├─ ' '
│  └─ "\\quad"
├─ TeXExpr :symbol
│  ├─ 'α'
│  └─ "\\alpha"
├─ TeXExpr :spaced
│  └─ '='
├─ '0'
├─ '.'
├─ '1'
├─ '0'
└─ ','



Stacktrace:
  [1] texparse(data::String; showdebug::Bool)
    @ MathTeXEngine C:\Users\mbarm\.julia\packages\MathTeXEngine\b206G\src\parser\parser.jl:232
  [2] texparse
    @ C:\Users\mbarm\.julia\packages\MathTeXEngine\b206G\src\parser\parser.jl:221 [inlined]
  [3] generate_tex_elements(str::String, fontset::MathTeXEngine.FontSet) (repeats 2 times)
    @ MathTeXEngine C:\Users\mbarm\.julia\packages\MathTeXEngine\b206G\src\engine\layout.jl:256
  [4] texelems_and_glyph_collection(str::LaTeXString, fontscale_px::Float32, halign::Symbol, valign::Symbol, rotation::Quaternionf0, color::Symbol, strokecolor::Tuple{Symbol, Float64}, strokewidth::Int64)
    @ Makie C:\Users\mbarm\.julia\packages\Makie\xbI6d\src\basic_recipes\text.jl:191
  [5] (::Makie.var"#983#989")(latexstring::LaTeXString, ts::Float32, al::Tuple{Symbol, Symbol}, rot::Float64, mo::SMatrix{4, 4, Float32, 16}, color::Symbol, scolor::Tuple{Symbol, Float64}, swidth::Int64, ::Point2{Float64})
    @ Makie C:\Users\mbarm\.julia\packages\Makie\xbI6d\src\basic_recipes\text.jl:117
  [6] lift(::Function, ::Observable{LaTeXString}, ::Observable{Any}, ::Vararg{Observable{Any}}; kw::@Kwargs{})
    @ Makie C:\Users\mbarm\.julia\packages\Makie\xbI6d\src\interaction\nodes.jl:13
  [7] lift(::Function, ::Observable{LaTeXString}, ::Observable{Any}, ::Observable{Any}, ::Vararg{Observable{Any}})
    @ Makie C:\Users\mbarm\.julia\packages\Makie\xbI6d\src\interaction\nodes.jl:9
  [8] plot!(plot::MakieCore.Text{Tuple{LaTeXString}})
    @ Makie C:\Users\mbarm\.julia\packages\Makie\xbI6d\src\basic_recipes\text.jl:95
  [9] plot!(scene::Scene, P::Type{MakieCore.Text{Tuple{LaTeXString}}}, attributes::Attributes, input::Tuple{Observable{Any}}, args::Observable{Tuple{LaTeXString}})
    @ Makie C:\Users\mbarm\.julia\packages\Makie\xbI6d\src\interfaces.jl:428
 [10] plot!(scene::Scene, P::Type{MakieCore.Text}, attributes::Attributes, args::Observable{Any}; kw_attributes::@Kwargs{})
    @ Makie C:\Users\mbarm\.julia\packages\Makie\xbI6d\src\interfaces.jl:339
 [11] plot!
    @ Makie C:\Users\mbarm\.julia\packages\Makie\xbI6d\src\interfaces.jl:307 [inlined]
 [12] #plot!#157
    @ Makie C:\Users\mbarm\.julia\packages\Makie\xbI6d\src\interfaces.jl:291 [inlined]
 [13] text!(::Scene, ::Vararg{Any}; attributes::@Kwargs{position::Observable{Point2{Float64}}, visible::Observable{Any}, textsize::Observable{Any}, align::Observable{Tuple{Symbol, Symbol}}, font::Observable{Any}, color::Observable{Any}, space::Symbol, show_axis::Bool, inspectable::Bool})
    @ MakieCore C:\Users\mbarm\.julia\packages\MakieCore\S8PkO\src\recipes.jl:35
 [14] layoutable(::Type{Axis}, fig_or_scene::Figure; bbox::Nothing, kwargs::@Kwargs{title::LaTeXString})
    @ Makie.MakieLayout C:\Users\mbarm\.julia\packages\Makie\xbI6d\src\makielayout\layoutables\axis.jl:328
 [15] layoutable
    @ C:\Users\mbarm\.julia\packages\Makie\xbI6d\src\makielayout\layoutables\axis.jl:8 [inlined]
 [16] #_layoutable#11
    @ C:\Users\mbarm\.julia\packages\Makie\xbI6d\src\makielayout\layoutables.jl:69 [inlined]
 [17] kwcall(::NamedTuple, ::typeof(Makie.MakieLayout._layoutable), T::Type{<:Makie.MakieLayout.Layoutable}, gp::Union{GridPosition, GridSubposition}, args::Vararg{Any})
    @ Makie.MakieLayout C:\Users\mbarm\.julia\packages\Makie\xbI6d\src\makielayout\layoutables.jl:68 [inlined]
 [18] _layoutable(::Type{Axis}, ::GridPosition; kwargs::@Kwargs{title::LaTeXString})
    @ Makie.MakieLayout C:\Users\mbarm\.julia\packages\Makie\xbI6d\src\makielayout\layoutables.jl:64
 [19] _layoutable
    @ Makie.MakieLayout C:\Users\mbarm\.julia\packages\Makie\xbI6d\src\makielayout\layoutables.jl:57 [inlined]
 [20] #_#9
    @ Makie.MakieLayout C:\Users\mbarm\.julia\packages\Makie\xbI6d\src\makielayout\layoutables.jl:49 [inlined]
 [21] top-level scope
    @ In[208]:20

The package that handles this is MathTeXEngine.jl, so you should open an issue there if you think you’ve found a parsing bug or would like more spacing commands to be supported.

Thanks for your reply. Opening an issue sounds like a good idea. But before doing that, is it possible to pair down my code to a MWE that isolates the issue with MathTeXEngine (without the GLMakie stuff)?

Sure just like it shows in the readme GitHub - Kolaru/MathTeXEngine.jl: A latex math mode engine in pure Julia.