Font for Plots: subscripts and special symbols in labels

When I searched this forum, I found a lot of posts about the problem, but I still don’t know answers to my simple question(s).

I’m quite satisfied with Julia’s standard 1D graphing functions in the Plots package. Then, what’s the best way to have subscripts, superscripts, and special symbols that match the surrounding font?

As a strawman, let me use this example:

using Plots
using LaTeXStrings
a = rand(Float64, 10)
p = plot(a; xlabel=L"volume [$10^{-3}\mathrm{m}^3$]")
savefig(p,"tmp.pdf")

This plot is perfect, except that the font for the units don’t match the surrounding default font and that the superscript “3” is too high.

If possible, I’d like to use the default (Helvetica-like) font for the units, too.

I don’t mind trying other packages and other methods of specifying sub/superscripts and special symbols as long as their usage isn’t very different from the standard Plots.

(By the way, I loaded the generated PDF file into Adobe Acrobat Reader and found that its font information is empty. That means that the fonts aren’t included in the PDF file and that the characters have been converted to curves.)

Hi! I wish all simple questions had simple answers… :slight_smile:

My understanding is that (apart from PGFPlots.jl) all the LaTeX-like formatting you see in Julia plotting packages is pseudo-LaTeX or faux-LaTeX rather than genuine \LaTeX (!), and the fonts are restricted and/or hard-coded. So the main font might be DejaVu but the LaTeX markup will be rendered in a “LaTeX font”.

I loaded the generated PDF file into Adobe Acrobat Reader and found that its font information is empty. That means that the fonts aren’t included in the PDF file and that the characters have been converted to curves.

The default engine for Plots.jl is GR.jl, which has a specific approach to font specification and PDF rendering. But with Makie.jl, for example, you’ll find a different approach. For example, a Makie-generated PDF has editable text and the fonts are embedded - but I don’t think you can have your pseudo-LaTeX text strings rendered in Helvetica - it will be NewComputerModern.

1 Like

I’m working on a PR for Makie that will add rich text with subscripts and superscripts in any font, as I think there’s a large set of use cases that aren’t quite full LaTeX and where people don’t want Computer Modern as the font, but which can be covered quite ok with just these basic building blocks.

2 Likes

Thank you both for your responses! So, I’ve got an impression that these things are still under development.

Actually I’m not familiar with how graphics packages are organized and I’m not sure if I’m asking the right questions, but . . .

  1. Do you switch engines without changing plotting function calls? For example, would plot(xs, ys) still work and give more or less the same result with a non-default engine?

  2. How do you switch engines?

  3. Is the handling of fonts and special symbols and sub-/super-scripts totally different between different engines? Is there a common interface planned?

these things are still under development.

I think the Julia ecosystem, like all ecosystems, is always changing…

how graphics packages are organized

I think the Julia home page is a pretty good introduction:

Plots.jl is a visualization interface and toolset. It provides a common API across various backends, like GR.jl, PyPlot.jl, and PlotlyJS.jl. Makie.jl is a sophisticated package for complex graphics and animations. Users who are used to “grammar of graphics” plotting APIs should take a look at Gadfly.jl. VegaLite.jl provides the Vega-Lite grammar of interactive graphics interface as a Julia package. For those who do not wish to leave the comfort of the terminal, there is also UnicodePlots.jl.

A good place to start is that second link.

Is the handling of fonts and special symbols and sub-/super-scripts totally different between different engines?

Different.

Is there a common interface planned?

Don’t know, sorry, I don’t use any of them… :slight_smile:

Maybe this helps: Nice fonts with Plots, GR and LaTeXStrings

2 Likes

You can use Unicode for simple things like numeric sub/superscripts and Greek letters. This will match the surrounding font if your font has sufficient Unicode coverage (otherwise it may switch to a fallback font or give you a □). e.g. xlabel="volume [10⁻³ m³]".

Most Julia environments can tab-complete a subset of LaTeX or LaTeX-like sequences to Unicode these days. e.g. 10⁻³ can be typed in the REPL by tab-completing 10\^-3.

2 Likes