# Font for Plots: subscripts and special symbols in labels

**URL:** <https://discourse.julialang.org/t/font-for-plots-subscripts-and-special-symbols-in-labels/89185>\
**Category:** Visualization\
**Created:** [October 24, 2022, 8:53am UTC](https://discourse.julialang.org/t/font-for-plots-subscripts-and-special-symbols-in-labels/89185 "2022-10-24T08:53:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [October 24, 2022, 8:53am UTC](https://discourse.julialang.org/t/font-for-plots-subscripts-and-special-symbols-in-labels/89185/1 "2022-10-24T08:53:42Z")

</div>

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:

```julia
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.)

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [October 24, 2022, 9:41am UTC](https://discourse.julialang.org/t/font-for-plots-subscripts-and-special-symbols-in-labels/89185/2 "2022-10-24T09:41:04Z")

</div>

Hi! I wish all simple questions had simple answers… 🙂

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.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [October 24, 2022, 11:48am UTC](https://discourse.julialang.org/t/font-for-plots-subscripts-and-special-symbols-in-labels/89185/3 "2022-10-24T11:48:36Z")

</div>

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.

> <https://github.com/MakieOrg/Makie.jl/pull/2321>
>
> \# Description
> 
> Adds a rich text data structure which allows to plot strings wi…th colors, subscripts, superscripts, different fonts and fontsizes specified internally. Also adds the ability to specify fonts by symbols such as \`:regular\` and passing a top-level dictionary that resolves these symbols.
> 
> This PR is breaking in two ways:
> \- Symbols are not resolved equivalently to strings anymore. This should mostly not matter because all our examples used strings and the symbol behavior was not advertised.
> \- Doing \`Figure(..., font = xyz)\` will not have the expected effect anymore, because all the \`Block\`s like \`Axis\` which previously inherited their fonts from this field do not do this anymore. Instead, they are set to \`:regular\` or \`:bold\`. The new way to change fonts should therefore be \`Figure(..., fonts = (; regular = xyz, bold = abc))\`.
> 
> Example:
> 
> \`\`\`julia
> using CairoMakie
> 
> f = Figure(fonts = (regular = "TeX Gyre Heros Italic Makie", bold = "Comic Sans Bold"))
> ax = Axis(f\[1, 1\],
> title = rich(
> "H",
> subscript("2", color = :green),
> "O",
> superscript("2", color = :red),
> subscript("3", subscript("xy", color = :green)),
> " hello"
> ), titlesize = 26, titlefont = :regular)
> 
> text!(ax, 1, 2, text = "Hi!", font = :bold)
> 
> f
> \`\`\`
> 
> !\[image\](https://user-images.githubusercontent.com/22495855/197478355-0e929308-a1e0-4825-acc5-7bc3c617b3a8.png)
> 
> Delete options that do not apply:
> 
> \- \[x\] Breaking change (fix or feature that would cause existing functionality to not work as expected)
> 
> \## Checklist
> 
> \- \[\] Added an entry in NEWS.md (for new features and breaking changes)
> \- \[\] Added or changed relevant sections in the documentation
> \- \[\] Added unit tests for new algorithms, conversion methods, etc.
> \- \[\] Added reference image tests for new plotting functions, recipes, visual options, etc.

---

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [October 25, 2022, 5:41am UTC](https://discourse.julialang.org/t/font-for-plots-subscripts-and-special-symbols-in-labels/89185/4 "2022-10-25T05:41:00Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [October 25, 2022, 9:43am UTC](https://discourse.julialang.org/t/font-for-plots-subscripts-and-special-symbols-in-labels/89185/5 "2022-10-25T09:43:35Z")

</div>

> 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](https://github.com/JuliaPlots/Plots.jl) is a visualization interface and toolset. It provides a common API across various [backends](https://docs.juliaplots.org/latest/backends/), like [GR.jl](https://github.com/jheinen/GR.jl), [PyPlot.jl](https://github.com/JuliaPy/PyPlot.jl), and [PlotlyJS.jl](https://github.com/JuliaPlots/PlotlyJS.jl). [Makie.jl](https://github.com/JuliaPlots/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](https://github.com/GiovineItalia/Gadfly.jl). [VegaLite.jl](https://github.com/queryverse/VegaLite.jl) provides the [Vega-Lite](https://vega.github.io/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](https://github.com/Evizero/UnicodePlots.jl).

A good place to start is [that second link](https://docs.juliaplots.org/latest/backends/#backends).

> 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… 🙂

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 25, 2022, 11:58am UTC](https://discourse.julialang.org/t/font-for-plots-subscripts-and-special-symbols-in-labels/89185/6 "2022-10-25T11:58:20Z")

</div>

Maybe this helps: [Nice fonts with Plots, GR and LaTeXStrings](https://discourse.julialang.org/t/nice-fonts-with-plots-gr-and-latexstrings/60037)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 25, 2022, 1:12pm UTC](https://discourse.julialang.org/t/font-for-plots-subscripts-and-special-symbols-in-labels/89185/7 "2022-10-25T13:12:04Z")

</div>

> [@ryofurue](#):
>
> `p = plot(a; xlabel=L"volume [$10^{-3}\mathrm{m}^3$]")`

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`.
