6.4 Using LaTeXStrings.jl

LaTeX support in Makie.jl is also available via LaTeXStrings.jl:

  1. using LaTeXStrings

Simple use cases are shown below (Figure 18). A basic example includes LaTeX strings for x-y labels and legends:

  1. function LaTeX_Strings()
  2. x = 0:0.05:4π
  3. lines(x, x -> sin(3x) / (cos(x) + 2) / x; label=L"\frac{\sin(3x)}{x(\cos(x)+2)}",
  4. figure=(; resolution=(600, 400)), axis=(; xlabel=L"x"))
  5. lines!(x, x -> cos(x) / x; label=L"\cos(x)/x")
  6. lines!(x, x -> exp(-x); label=L"e^{-x}")
  7. limits!(-0.5, 13, -0.6, 1.05)
  8. axislegend(L"f(x)")
  9. current_figure()
  10. end
  1. with_theme(LaTeX_Strings, publication_theme())

Figure 18: Plot with LaTeX strings.

Figure 18: Plot with LaTeX strings.

A more involved example will be one with some equation as text and increasing legend numbering for curves in a plot:

  1. function multiple_lines()
  2. x = collect(0:10)
  3. fig = Figure(resolution=(600, 400), font="CMU Serif")
  4. ax = Axis(fig[1, 1], xlabel=L"x", ylabel=L"f(x,a)")
  5. for i = 0:10
  6. lines!(ax, x, i .* x; label=latexstring("$(i) x"))
  7. end
  8. axislegend(L"f(x)"; position=:lt, nbanks=2, labelsize=14)
  9. text!(L"f(x,a) = ax", position=(4, 80))
  10. fig
  11. end
  12. JDS.multiple_lines()

Figure 19: Multiple lines.

Figure 19: Multiple lines.

Where latexstring from LaTeXStrings.jl has been used to parse the string. An alternative to this simple case is L"%$i x", which is used in the next example.

But, before that there is another problem, some lines have repeated colors and that’s no good. Adding some markers and line styles usually helps. So, let’s do that using Cycles for these types. Setting covary=true allows to cycle all elements together:

  1. function multiple_scatters_and_lines()
  2. x = collect(0:10)
  3. cycle = Cycle([:color, :linestyle, :marker], covary=true)
  4. set_theme!(Lines=(cycle=cycle,), Scatter=(cycle=cycle,))
  5. fig = Figure(resolution=(600, 400), font="CMU Serif")
  6. ax = Axis(fig[1, 1], xlabel=L"x", ylabel=L"f(x,a)")
  7. for i in x
  8. lines!(ax, x, i .* x; label=L"%$i x")
  9. scatter!(ax, x, i .* x; markersize=13, strokewidth=0.25,
  10. label=L"%$i x")
  11. end
  12. axislegend(L"f(x)"; merge=true, position=:lt, nbanks=2, labelsize=14)
  13. text!(L"f(x,a) = ax", position=(4, 80))
  14. set_theme!() # reset to default theme
  15. fig
  16. end
  17. JDS.multiple_scatters_and_lines()

Figure 20: Multiple Scatters and Lines.

Figure 20: Multiple Scatters and Lines.

And voilà. A publication quality plot is here. What more can we ask for? Well, what about different default colors or palettes. In our next section, we will see how to use again Cycles and know a little bit more about them, plus some additional keywords in order to achieve this.

6.4 Using LaTeXStrings.jl - 图4 Support this project
CC BY-NC-SA 4.0 Jose Storopoli, Rik Huijzer, Lazaro Alonso