When using Python's matplotlib
to produce plots, I sometimes require the lualatex
engine using the pgf backend for matplotlib when producing "heavier" plots like hexbin, otherwise the TeX memory is exceeded and pdflatex
fails. When using lualatex
The math font seems to change according to my pgf.preamble
, however, the rest of the font does not, and stays as CM Roman. With pdflatex
, the font behaves how I want. Note that if I go to www.overleaf.com and use the same preamble and compile with pdflatex
or lualatex
, the document shows the fonts I desire, in both math, and main body. EDIT: the main body font actually does not work in overleaf, I was mistaken.
Update: xelatex
behaves the same as lualatex
, ie. only math fonts are changed.
Update: Solved by using \usepackage{fontspec}\usepackage[T1]{fontenc}
at the start of the preamble. I thought fontspec
would not do anything with the T1 font encoding, but it's needed and it works.
This question is similar to: How do I use kpfonts with fontspec?
Code:
import matplotlib as mpl
eng = 'lua' # issue with lua only <<<<<<<<
# eng = 'pdf' # no issue here
# eng = 'xe' # update: issue here as well
mpl.use('pgf')
mpl.rc('font', family='serif')
mpl.rcParams.update({
"pgf.rcfonts" : False,
"pgf.texsystem": eng + "latex",
"pgf.preamble" : '\\usepackage[utf8x]{inputenc}\\usepackage[light]{kpfonts}',
})
import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(0,6,100)
x = np.sin(t)
fig, ax = plt.subplots()
ax.plot(t, x, label='A line')
plt.title('A function, $f(x) = \sin(x)$')
plt.legend()
plt.savefig('example.pdf')