Nice fonts with Plots, GR and LaTeXStrings

This not a question, just something that might be helpful to others. With Plots 1.12 and GR0.57.4 (at least), and using these options below, one can get a plot with homogeneous fonts (“Computer Modern”) in every element.

Edit: the default function is working nicely to set the default font to all elements, thus no need to set `PLOTS_DEFAULTS". Simpler way to do it:

using Plots
using LaTeXStrings

plot_font = "Computer Modern"
default(fontfamily=plot_font,
        linewidth=2, framestyle=:box, label=nothing, grid=false)

plot(sort(rand(10)),sort(rand(10)),label="Legend")
plot!(xlabel=L"\textrm{Standard text}(r) / \mathrm{cm^3}")
plot!(ylabel="Same font as everything")
annotate!(0.2,0.8,text("My note",plot_font,12))

savefig("./plot.pdf")

Previously
const plot_font = "Computer Modern"
const PLOTS_DEFAULTS = Dict(:theme=>:default,
                            :fontfamily => plot_font,
                            :linewidth => 2,
                            :framestyle => :box,
                            :label => "",
                            :grid => false)
using Plots
using LaTeXStrings

plot(sort(rand(10)),sort(rand(10)),label="Legend")
plot!(xlabel=L"\textrm{Standard text}(r) / \mathrm{cm^3}")
plot!(ylabel="Same font as everything")
annotate!(0.2,0.8,text("My note",plot_font,12))

savefig("./plot.pdf")

Additionally, some basic latex is good to know:

  • \textm removes the math mode from a latex string.
  • \mathrm removes the italics (not math mode) from the latex string, such that, for example, the exponent looks nicely placed in a non-italic string.

What I would like sometimes is to change everything to sffamily fonts. But that I was unable to do consistently yet.

18 Likes
using Unitful, Plots, Latexify, UnitfulRecipes, UnitfulLatexify
default(; # Plots defaults
    fontfamily="Computer modern",
    label="" # only explicit legend entries
    )
set_default(; # Latexify defaults
    #unitformat=:slash # in case you want `m/s`
    )
x = rand(10)u"m"
v = x./(4u"s") .+ 0.3*randn(10)u"m/s"

plot(x, v; xguide="x", yguide="v", unitformat=latexify)
4 Likes

random

3 Likes

Note that it does not produce the correct placement for the exponent of the unit (the difference between using mathrm and textrm):

that example:

image

using \mathrm{ms^{-1}}:

image

(not that everyone needs to have obsessive compulsive disorder, though, but if you are a developer of that, maybe that is something to improve in the packages).

1 Like

It’s actually $v\;/\;\mathrm{m}\,\mathrm{s}^{-1}$ which I would consider the more correct behaviour.

Well, I don’t agree that produced the correct output there. Actually here all look the same, and correspond to what I think is the correct placement:

ms^{-1}: ms^{-1}

\mathrm{ms^{-1}}: \mathrm{ms^{-1}}

\mathrm{ms}^{-1}: \mathrm{ms}^{-1}

\textrm{ms}^{-1}: \textrm{ms}^{-1}

(in the plot above the -1 is clearly moved upwards).

Staring at them like this, I’m becoming more and more convinced they are both the wrong height.

Yes, that seems to be a bug in GR’s LaTeX rendering. Syntactically I still think your version is wrong, but I guess I could implement it in UnitfulLatexify as a workaround.

1 Like

In latex they all look the same to me:

image

thus, it is a rendering issue indeed in GR.

1 Like

Anyway, you made me notice that the default keyword is properly setting the fonts to all elements (that was not working in some previous version). Thus, the initial example can be made simpler:

using Plots
using LaTeXStrings

plot_font = "Computer Modern"
default(fontfamily=plot_font,
        linewidth=2, framestyle=:box, label=nothing, grid=false)

plot(sort(rand(10)),sort(rand(10)),label="Legend")
plot!(xlabel=L"\textrm{Standard text}(r) / \mathrm{cm^3}")
plot!(ylabel="Same font as everything")
annotate!(0.2,0.8,text("My note",plot_font,12))

savefig("./plot.pdf")

I opened an issue here. It seems like the gull wings are to blame:

2 Likes

It would be nice if text would inherit the default font from default so that the last line could be simply
annotate!(0.2,0.8,text("My note",12))

2 Likes

@leandromartinez98, thanks for this very useful post.
In case it might be of interest, the following example provides some additional formatting options:

using LaTeXStrings, Printf, Plots; gr()

plot_font = "Computer Modern";
default(fontfamily=plot_font,framestyle=:box, label=nothing, grid=false, tickfontsize=7)

u = 15  # persistent wind speed in m/s  (~30 knot)
st0 =  "Significant wave height vs persistent wind speed ("
st1 =  @sprintf(":  u = %i m/s → ", u)
st2 =  @sprintf(" = %.1f m", 0.24*u^2/9.8)
plot(title=L"\textbf{%$st0} \mathbf{H_s \approx 0.24 u^2/g} ) \textit{%$st1} {H_s} \textit{%$st2}", titlefont=font(8,plot_font))
plot!(ylims=(0,30))

To produce:

2 Likes