6.4 Using LaTeXStrings.jl

LaTeX support in Makie.jl is also available. Simple use cases are shown below (Figure 18). A basic example includes LaTeX strings for x-y labels and legends:

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

using LaTeXStrings
function multiple_lines()
    x = collect(0:10)

    fig = Figure(resolution = (600,400), font="CMU Serif")
    ax = Axis(fig[1,1], xlabel = L"x", ylabel = L"f(x,a)")
    for i in 0:10
        lines!(ax, x, i .* x; label = latexstring("$(i) x"))
    end
    axislegend(L"f(x)"; position = :lt, nbanks = 2, labelsize = 14)
    text!(L"f(x,a) = ax", position = (4,80))
    fig
end
multiple_lines()
Figure 19: Multiple lines.

But, some lines have repeated colors, so thats no good. Adding some markers and line styles usually helps. So, let’s do that using Cycles for these types:

function multiple_scatters_and_lines()
    x = collect(0:10)

    cycle = Cycle([:color, :linestyle, :marker], covary = true)
    set_theme!(Lines = (cycle = cycle,), Scatter = (cycle = cycle,))

    fig = Figure(resolution = (600,400), font="CMU Serif")
    ax = Axis(fig[1,1], xlabel = L"x", ylabel = L"f(x,a)")
    for i in x
        lines!(ax, x, i .* x; label = latexstring("$(i) x"))
        scatter!(ax, x, i .* x; markersize = 13,
            strokewidth = 0.25, label = latexstring("$(i) x"))
    end
    axislegend(L"f(x)"; merge = true,position = :lt,nbanks=2,labelsize=14)
    text!(L"f(x,a) = ax", position = (4,80))
    set_theme!() # reset to default theme
    fig
end
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.



CC BY-NC-SA 4.0 Jose Storopoli, Rik Huijzer and Lazaro Alonso