Combining LaTeX with variables in PyPlot title

This is somewhat similar to PyPlot: how to include a variable in a title, but with the added complication of including LaTeX in the title too. See I have this code:

PyPlot.title(L"Plot of the 1st eigenfunction, corresponding to $\lambda=$ $(eigenvalue_approx[1])")

and as you can probably guess, I want lambda to be rendered as the appropriate lower case Greek letter and the first element of eigenvalue_approx to be displayed after the equal sign. But currently I just get:

. Any ideas how to get around this issue?

You can use latexstring(args...):

PyPlot.title(latexstring(
"Plot of the 1st eigenfunction, corresponding to \$\\lambda=\$ $(eigenvalue_approx[1])"
))

Note that you need to escape the LaTeX commands in this case.
From https://github.com/stevengj/LaTeXStrings.jl#usage:

You can also use the lower-level constructor latexstring(args...) , which works much like string(args...) except that it produces a LaTeXString result and automatically puts $ at the beginning and end of the string if an unescaped $ is not already present. Note that with latexstring(...) you do have to escape $ and \ : for example, latexstring("an equation: \$1 + \\alpha^2\$") . One reason you might want to use latexstring instead of L"..." is that only the former supports string interpolation (inserting the values of other variables into your string).

1 Like

You can also supply a mixture of String and LaTeXString args to latexstring to avoid the need for multiple escape characters:

julia> latexstring("Plot of the ", L"1^\mathrm{st}", " eigenfunction, corresponding to ", L"\lambda = ", rand(1:.1:10))

L"Plot of the $1^\mathrm{st}$ eigenfunction, corresponding to $\lambda = $4.5"
2 Likes

Is there a way to do what I asked originally AND specify the number of digits in eigenvalue_approx[1] that are shown? eigenvalue_approx[1] is only accurate to at most 10 digits, so showing 16 isn’t particularly helpful.

julia> using Printf

julia> latexstring("Plot of the ", L"1^\mathrm{st}", " eigenfunction, corresponding to ", L"\lambda = ", 
                           @sprintf("%.10f", eigenvalue_approx[1]))

…or you could round the value: round(eigenvalue_approx[1], sigdigits=10)

1 Like

And how do I incorporate that in my plot title? I ask because I tried the obvious, that is:

PyPlot.title(latexstring("Plot of the ", L"1^\mathrm{st}", " eigenfunction, corresponding to ", L"\lambda = ", 
                           @printf("%.10f", eigenvalue_approx[1])))

and got this plot:
'

Sorry, I added a ninja-edit fix to my earlier response - you need to use @sprintf, not @printf.

PyPlot supports LaTeXStrings, and according to its README, the solution is as simple as

title(L"Plot of the 1st eigenfunction, corresponding to $\lambda = %$(eigenvalue_approx[1])$")

The point is to use %$ for string interpolation in order to suppress $ from being used as LaTeX equation markers.

4 Likes

Yep, that’s been an option for about a year now thanks to this PR: https://github.com/stevengj/LaTeXStrings.jl/pull/40

1 Like