Latex in GLMakie, no spaces

When trying to use Latex with GLMakie in this code:

using GLMakie

f = Figure(backgroundcolor = "#f5f0f5")
ax = Axis(f[1, 1], 
    title = L"A Makie Axis",
    xlabel = L"The x label",
    ylabel = L"The y label \omega"
)
display(f)

This is the output:

There is no space. I added a scientific symbol and it’s useful to note that it doesn’t need $$ to be displayed. This means all the text is considered as an equation.

Is there a way to set the text as default and use $$ to delimit symbols?

A L".." string behaves much like an $...$ expression in LaTeX, e.g. it displays inline math for which spaces are ignored.
For ordinary text I would just use a normal string.
Or do what @p_f suggsted below.

Latex is in math mode here, you can use \text{} for latex text

E.g. L"\text{normal text } x^2"

2 Likes

What if I need to use ordinary text alongside equation, just as in the first example of documentation: LaTeX · Makie which doesn’t work as intended with GLMakie (didn’t try with CairoMakie)

If I use \text{} like:

ax = Axis(f[1, 1], 
    title = L"\text{A Makie Axis}",
    xlabel = L"\text{The x label}",
    ylabel = L"\text{The y label} \omega"
)

It throws a huge error:

ERROR: LoadError: TeXParseError: unexpected error while parsing
at position 7 (string index 7)
\text{The x label}
      ^
with stack (length 1):
[1] TeXExpr :expr


Stacktrace:
  [1] texparse(data::String; showdebug::Bool)
    @ MathTeXEngine ~/.julia/packages/MathTeXEngine/b206G/src/parser/parser.jl:232
  [2] texparse
    @ ~/.julia/packages/MathTeXEngine/b206G/src/parser/parser.jl:221 [inlined]
  [3] generate_tex_elements(str::String, fontset::MathTeXEngine.FontSet)
    @ MathTeXEngine ~/.julia/packages/MathTeXEngine/b206G/src/engine/layout.jl:256
  [4] generate_tex_elements
    @ ~/.julia/packages/MathTeXEngine/b206G/src/engine/layout.jl:256 [inlined]
  [5] texelems_and_glyph_collection(str::LaTeXStrings.LaTeXString, fontscale_px::Float32, halign::Symbol, valign::Symbol, rotation::Quaternionf0, color::Symbol, strokecolor::Tuple{…}, strokewidth::Int64)
    @ Makie ~/.julia/packages/Makie/xbI6d/src/basic_recipes/text.jl:191
  [6] (::Makie.var"#983#989")(latexstring::LaTeXStrings.LaTeXString, ts::Float32, al::Tuple{…}, rot::Float32, mo::StaticArraysCore.SMatrix{…}, color::Symbol, scolor::Tuple{…}, swidth::Int64, ::Point{…})
    @ Makie ~/.julia/packages/Makie/xbI6d/src/basic_recipes/text.jl:117
  [7] lift(::Function, ::Observable{LaTeXStrings.LaTeXString}, ::Observable{Any}, ::Vararg{Observable{Any}}; kw::@Kwargs{})
    @ Makie ~/.julia/packages/Makie/xbI6d/src/interaction/nodes.jl:13
  [8] lift(::Function, ::Observable{LaTeXStrings.LaTeXString}, ::Observable{Any}, ::Observable{Any}, ::Vararg{Observable{…}})
    @ Makie ~/.julia/packages/Makie/xbI6d/src/interaction/nodes.jl:9
  [9] plot!(plot::MakieCore.Text{Tuple{LaTeXStrings.LaTeXString}})
    @ Makie ~/.julia/packages/Makie/xbI6d/src/basic_recipes/text.jl:95
 [10] plot!(scene::Scene, P::Type{…}, attributes::Attributes, input::Tuple{…}, args::Observable{…})
    @ Makie ~/.julia/packages/Makie/xbI6d/src/interfaces.jl:428
 [11] plot!(scene::Scene, P::Type{MakieCore.Text}, attributes::Attributes, args::Observable{Any}; kw_attributes::@Kwargs{})
    @ Makie ~/.julia/packages/Makie/xbI6d/src/interfaces.jl:339
 [12] plot!
    @ ~/.julia/packages/Makie/xbI6d/src/interfaces.jl:307 [inlined]
 [13] #plot!#157
    @ ~/.julia/packages/Makie/xbI6d/src/interfaces.jl:291 [inlined]
 [14] text!(::Scene, ::Vararg{…}; attributes::@Kwargs{…})
    @ MakieCore ~/.julia/packages/MakieCore/S8PkO/src/recipes.jl:35
 [15] Makie.MakieLayout.LineAxis(parent::Scene; kwargs::@Kwargs{…})
    @ Makie.MakieLayout ~/.julia/packages/Makie/xbI6d/src/makielayout/lineaxis.jl:174
 [16] layoutable(::Type{…}, fig_or_scene::Figure; bbox::Nothing, kwargs::@Kwargs{…})
    @ Makie.MakieLayout ~/.julia/packages/Makie/xbI6d/src/makielayout/layoutables/axis.jl:211
 [17] layoutable
    @ ~/.julia/packages/Makie/xbI6d/src/makielayout/layoutables/axis.jl:8 [inlined]
 [18] #_layoutable#11
    @ ~/.julia/packages/Makie/xbI6d/src/makielayout/layoutables.jl:69 [inlined]
 [19] _layoutable
    @ ~/.julia/packages/Makie/xbI6d/src/makielayout/layoutables.jl:68 [inlined]
 [20] _layoutable(::Type{…}, ::GridPosition; kwargs::@Kwargs{…})
    @ Makie.MakieLayout ~/.julia/packages/Makie/xbI6d/src/makielayout/layoutables.jl:64
 [21] _layoutable
    @ ~/.julia/packages/Makie/xbI6d/src/makielayout/layoutables.jl:57 [inlined]
 [22] #_#9
    @ ~/.julia/packages/Makie/xbI6d/src/makielayout/layoutables.jl:49 [inlined]
 [23] top-level scope
    @ ~/Desktop/Programmi/MODULO_05/executable_and_libraries/bin/aaa.jl:27
 [24] include(fname::String)
    @ Base.MainInclude ./client.jl:489
 [25] top-level scope
    @ REPL[2]:1
in expression starting at /home/matteobacci/Desktop/Programmi/MODULO_05/executable_and_libraries/bin/aaa.jl:27

caused by: TeXParseError: unsupported command \text
at position 7 (string index 7)
\text{The x label}
      ^
with stack (length 1):
[1] TeXExpr :expr


Stacktrace:
  [1] macro expansion
    @ ~/.julia/packages/MathTeXEngine/b206G/src/parser/parser.jl:212 [inlined]
  [2] macro expansion
    @ ~/.julia/packages/Automa/Aq8Mq/src/codegen.jl:142 [inlined]
  [3] texparse(data::String; showdebug::Bool)
    @ MathTeXEngine ~/.julia/packages/MathTeXEngine/b206G/src/parser/parser.jl:230
  [4] texparse
    @ ~/.julia/packages/MathTeXEngine/b206G/src/parser/parser.jl:221 [inlined]
  [5] generate_tex_elements(str::String, fontset::MathTeXEngine.FontSet)
    @ MathTeXEngine ~/.julia/packages/MathTeXEngine/b206G/src/engine/layout.jl:256
  [6] generate_tex_elements
    @ ~/.julia/packages/MathTeXEngine/b206G/src/engine/layout.jl:256 [inlined]
  [7] texelems_and_glyph_collection(str::LaTeXStrings.LaTeXString, fontscale_px::Float32, halign::Symbol, valign::Symbol, rotation::Quaternionf0, color::Symbol, strokecolor::Tuple{…}, strokewidth::Int64)
    @ Makie ~/.julia/packages/Makie/xbI6d/src/basic_recipes/text.jl:191
  [8] (::Makie.var"#983#989")(latexstring::LaTeXStrings.LaTeXString, ts::Float32, al::Tuple{…}, rot::Float32, mo::StaticArraysCore.SMatrix{…}, color::Symbol, scolor::Tuple{…}, swidth::Int64, ::Point{…})
    @ Makie ~/.julia/packages/Makie/xbI6d/src/basic_recipes/text.jl:117
  [9] lift(::Function, ::Observable{LaTeXStrings.LaTeXString}, ::Observable{Any}, ::Vararg{Observable{Any}}; kw::@Kwargs{})
    @ Makie ~/.julia/packages/Makie/xbI6d/src/interaction/nodes.jl:13
 [10] lift(::Function, ::Observable{LaTeXStrings.LaTeXString}, ::Observable{Any}, ::Observable{Any}, ::Vararg{Observable{…}})
    @ Makie ~/.julia/packages/Makie/xbI6d/src/interaction/nodes.jl:9
 [11] plot!(plot::MakieCore.Text{Tuple{LaTeXStrings.LaTeXString}})
    @ Makie ~/.julia/packages/Makie/xbI6d/src/basic_recipes/text.jl:95
 [12] plot!(scene::Scene, P::Type{…}, attributes::Attributes, input::Tuple{…}, args::Observable{…})
    @ Makie ~/.julia/packages/Makie/xbI6d/src/interfaces.jl:428
 [13] plot!(scene::Scene, P::Type{MakieCore.Text}, attributes::Attributes, args::Observable{Any}; kw_attributes::@Kwargs{})
    @ Makie ~/.julia/packages/Makie/xbI6d/src/interfaces.jl:339
 [14] plot!
    @ ~/.julia/packages/Makie/xbI6d/src/interfaces.jl:307 [inlined]
 [15] #plot!#157
    @ ~/.julia/packages/Makie/xbI6d/src/interfaces.jl:291 [inlined]
 [16] text!(::Scene, ::Vararg{…}; attributes::@Kwargs{…})
    @ MakieCore ~/.julia/packages/MakieCore/S8PkO/src/recipes.jl:35
 [17] Makie.MakieLayout.LineAxis(parent::Scene; kwargs::@Kwargs{…})
    @ Makie.MakieLayout ~/.julia/packages/Makie/xbI6d/src/makielayout/lineaxis.jl:174
 [18] layoutable(::Type{…}, fig_or_scene::Figure; bbox::Nothing, kwargs::@Kwargs{…})
    @ Makie.MakieLayout ~/.julia/packages/Makie/xbI6d/src/makielayout/layoutables/axis.jl:211
 [19] layoutable
    @ ~/.julia/packages/Makie/xbI6d/src/makielayout/layoutables/axis.jl:8 [inlined]
 [20] #_layoutable#11
    @ ~/.julia/packages/Makie/xbI6d/src/makielayout/layoutables.jl:69 [inlined]
 [21] _layoutable
    @ ~/.julia/packages/Makie/xbI6d/src/makielayout/layoutables.jl:68 [inlined]
 [22] _layoutable(::Type{…}, ::GridPosition; kwargs::@Kwargs{…})
    @ Makie.MakieLayout ~/.julia/packages/Makie/xbI6d/src/makielayout/layoutables.jl:64
 [23] _layoutable
    @ ~/.julia/packages/Makie/xbI6d/src/makielayout/layoutables.jl:57 [inlined]
 [24] #_#9
    @ ~/.julia/packages/Makie/xbI6d/src/makielayout/layoutables.jl:49 [inlined]
 [25] top-level scope
    @ ~/Desktop/Programmi/MODULO_05/executable_and_libraries/bin/aaa.jl:27
 [26] include(fname::String)
    @ Base.MainInclude ./client.jl:489
 [27] top-level scope
    @ REPL[2]:1
Some type information was truncated. Use `show(err)` to see complete types.

Which version of GLMakie are you using?
It works for me on v0.9.9.

1 Like

I assumed that after installing it the version was the latest, but it seemed i was wrong here.

Updating solved the issue

I think you can also escape the space with a backslash: L"The\ x\ label"

Is there a way to set the text as default and use $$ to delimit symbols?

Apologies didn’t actually read this bit before. Just had a look at latexstrings.jl and if you include $$, it will interpret the rest as text, so

f = Figure()
ax = Axis(f[1,1],xlabel=L"Text $a = b$")

works. And if you just want text you can do L"Text$$". Not sure why I’ve been using \text all this time…