Calligraphic LaTeX fonts in Makie

Hi all,

I’ve run into a small problem regarding mismatch of LaTeX fonts between Makie and the LaTeX documents where I am placing such figures. This is really a problem with the calligraphic fonts that are used in math mode. Below is a simple example:

using CairoMakie, LaTeXStrings

f = Figure()
ax = Axis(f[1,1], title=L"\mathcal{ABCDEFGHIJKLMOPQRSTUVWXYZ}", titlesize=30)
save("makie_mathcal.png", f)
f

The LaTeX calligraphic fonts I am used to look like the ones that are rendered in a jupyter notebook by constructing that same LaTeXString, as shown below

My main concern is in regards to a single one of these letters, \mathcal{M}. This is a common symbol in my field, and not being able to render it as expected is the single annoyance I have while switching all my plotting from python to julia+Makie. After digging a lot I have not been able to find any workaround for this. Is there any possibility to adjust this? Barring other options I would just post-process all pdfs to use the symbol I want, but that is very cumbersome if I need to produce small modifications to figures.

For reference, I’m using julia 1.9.0 with the following package versions:
[13f3f980] CairoMakie v0.10.6
[b964fa9f] LaTeXStrings v1.3.0
[91a5bcdd] Plots v1.38.16

And one small extra detail, I get the same calligraphic fonts if I do this with Plots.jl

using Plots, LaTeXStrings

x = range(0, 10, length=100)
y = sin.(x)
Plots.plot(x, y)
title!(L"\mathcal{ABCDEFGHIJKLMOPQRSTUVWXYZ}")
savefig("plots_mathcal.png")

plots_mathcal

I’m not sure I can help you very much, but here are my thoughts:

The MathTeXEngine.jl (via Makie) uses NewComputer Modern (NewCMMath-Regular):

julia> using MathTeXEngine
julia-1.9> generate_tex_elements(L"\scrM")
1-element Vector{Any}:
 (TeXChar 'ℳ' [index 694 in NewComputerModern Math - Regular], [0.0, 0.0], 1.0)

for U+2133.

In CMUSerif-Roman (which I think Plots.jl/GR uses), this glyph is also stored at U+2133. I think CMUSerif and NewComputerModern are basically the same, at least in this area.

In ComputerModern (cmr10?), this Unicode point doesn’t exist, so I think the symbol is found in cmsy10.ttf, and presumably \LaTeX knows to look there…

So I think the solutions are:

  • use the same font(s) in both your Julia packages and your \LaTeX packages

  • open issues for the Julia packages to allow a way of accessing alternate fonts for a specific \LaTeX character

The Makie docs say:

Makie can render LaTeX strings from the LaTeXStrings.jl package using MathTeXEngine.jl.

The README.md of the latter package says:

Currently the only font set supported is Computer Modern.

I guess that if you’re including plots in a LaTeX document, the best approach would be to use PGFPlotsX, so that you could include actual TeX code into the document seamlessly and have a complete TeX engine and LaTeX implementation at your disposal.

Thanks for the info! Sounds like I will just need to live with this one. I knew MathTeXEngine states in their README that they’re limited to Computer Modern, but thought the calligraphic M symbol I wanted was contained in there. Here I’m a bit confused on the status of MathTeXEngine.jl. The git history on the README says they swapped to ComputerModern from NewComputerModern about two years ago, but on the current version generate_tex_elements produces results using NewComputerModern. Perhaps things were swapped back and the readme file is outdated right now.

@nsajko, being that this is my single remaining issue while creating plots with Makie and I’ve gotten quite used to it, switching to PGFPlotsX would not make much sense. I will probably need to live with a small inconsistency regarding this symbol.

1 Like

I also find LaTeX fonts to be a pain point for using Makie. There is MakieTex which I’ve been meaning to take a look at. In principle, you could just render any label?

Maybe this is just a bug? Perhaps it could be worth it to report it, if it’s easily fixable.

MakieTeX used to be wonderful (MathTeXEngine is faster but younger thus and more limited),
especially for 2D figures where speed does not matter,
and would be the perfect workaround here.
But I just tested it with

  [13f3f980] CairoMakie v0.10.6
  [ee78f7c6] Makie v0.19.6
  [6d554a22] MakieTeX v0.3.1

and it is still broken

1 Like

It’s unrealistic to expect MathTeXEngine to ever approach anything resembling a full TeX engine. It’s more a MathJax than a TeX.

1 Like

Agreed.

Fixing MathTeX seems a little tricky, needing some thought. It broke when text stopped using the actual text object as a positional argument. More like scatter with marker it’s now text with text attribute. But that means that the opportunity to dispatch away from using GlyphCollections and linesegments is gone for now, which was the mechanism MakieTeX used to insert its functionality into anything text-related. It would be relatively easy I think to get a separate recipe going again. It’s more difficult to reuse text but I guess that would be the most practical thing, as it’s used by axis titles, labels, ticks, etc.