1

Consider the following piece of code:

import matplotlib as mpl
mpl.use("pgf")
preamble = r'\usepackage{newtxtext,newtxmath}'
mpl.rcParams.update({
    "pgf.texsystem": 'lualatex',
    "pgf.preamble": preamble,
    'text.latex.preamble': preamble,
    'text.usetex': True,
    'font.size': 9,
    'lines.markersize' : 3
})

import matplotlib.pyplot as plt
fig = plt.figure(figsize=(4.5, 2.5))
ax = fig.gca()
ax.plot(range(5))
ax.set_xlabel(u"µ is not $\\mu$")
fig.tight_layout()
fig.savefig("test.pdf")

The code fails to run with the lualatex error:

Package fontspec Error: The font "DejaVuSerif" cannot be found.

This is odd, I did neither load fontspec nor did I specify DejaVuSerif as the font to use in my preamble. If I issue "pgf.texsystem": 'pdflatex' instead, the code produces a pdf with the expected output. Compiling a tex document with just that preamble and some random text and math with lualatex also gives the expected output. This is why I think the pgf backend of matplotlib messes something up here.

Bubaya
  • 615
  • 3
  • 13
  • Not a `pgf` expert, so excuse me if I'm wrong... is it possible to save the LaTeX source, and compile it so that you can (maybe) have a better understanding of the problem? – gboffi May 31 '23 at 13:48
  • @gboffi Yes, that would be great. I've found no option so far. And I think the issue is more intricate: matplotlib runs latex on all the labels in the plot, in order to measure their size to position them accurately. This information is then used to build the final latex source that's passed to latex/pgf. At least, that's how I understood the workflow. – Bubaya Jun 01 '23 at 07:02

0 Answers0