Nice fonts with Plots, GR and LaTeXStrings

lmiq
Mon, 26 Apr 2021 14:41:09 +0000
Visualization

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.

using Plots
using LaTeXStrings

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

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 (click for more details)
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.

20 posts - 6 participants