Description
Bug summary
If we \input
the pgf file created by matplotlib with pgf backend, it assumes \mathdefault
to exist. But unfortunately, this command is not provided by any of the recommended packages as far as I see: to produce the actual pdf
file, matplotlib seems to actually define it manually like:
\def\mathdefault#1{#1}
as can be read from the automatically generated .pgf file… Manually adding this line in the .tex file before the \input
works as a workaround, but this seems like a dirty workaround as this is not documented as far as I can see.
Would it be possible to either document it or provide these commands in a properly packaged latex library?
See also this discussion leo-colisson/robust-externalize#31
Code for reproduction
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('pgf')
plt.rcParams.update({
"text.usetex": True,
})
plt.rcParams.update({
"pgf.preamble": "\n".join([
r"\usepackage{fontspec}",
r"\usepackage{amsmath}",
r"\usepackage{amssymb}",
]),
})
plt.rcParams['text.latex.preamble'] = r'\newcommand{\mathdefault}[1][]{}' # doesn't seem to work.
year = [2014, 2015, 2016, 2017, 2018, 2019]
tutorial_count = [39, 117, 111, 110, 67, 29]
plt.plot(year, tutorial_count, color="#6c3376", linewidth=2)
plt.title("Simple plot")
plt.xlabel('Year')
plt.ylabel('Number of futurestud.io Tutorials')
plt.savefig("foo.pgf", bbox_inches="tight")
Actual outcome
I would expect \input
of the pgf file to work, but it is not. Here is the full file I use to both compile the python and include it (using the recent robust-externalize library, you might need to copy the .sty from to install it and you might want to compile with -shell-escape
for simplicity)
\documentclass{article}
\usepackage{lipsum}
\usepackage{tikz}
\usepackage{robust-externalize}
\robExtConfigure{enable fallback to manual mode} % prints a helpful message in the document if you forgot --shell-escape
%% If you do not add this line, this will return an error:
%\def\mathdefault#1{#1}
\begin{document}
\lipsum[1]
\begin{figure}[ht]
\centering
\begin{CacheMeCode}{python, custom include command={\input{\robExtAddCachePathAndName{\robExtFinalHash.pgf}}}, set placeholder eval={__LINEWIDTH__}{\lenToCmNoUnit[in]{\linewidth}}}
import matplotlib.pyplot as plt
import matplotlib
from matplotlib.pyplot import figure
matplotlib.use("pgf")
# See this link for details on how to preview the image in jupyter
# https://matplotlib.org/stable/users/explain/text/pgf.html
matplotlib.rcParams.update({
"font.family": "serif",
"font.serif": [], # Use LaTeX default serif font.
"text.usetex": True, # use inline math for ticks
})
figure(figsize=(__LINEWIDTH__, 0.7*__LINEWIDTH__))
year = [2014, 2015, 2016, 2017, 2018, 2019]
tutorial_count = [39, 117, 111, 110, 67, 29]
plt.plot(year, tutorial_count, color="#6c3376", linewidth=2)
plt.title("Simple plot for $\delta = 2$")
plt.xlabel('Year')
plt.ylabel('Number of futurestud.io Tutorials')
print(get_filename_from_extension(".pgf"))
# https://stackoverflow.com/a/52587591/4987648
plt.savefig(get_filename_from_extension(".pgf"), bbox_inches="tight")
\end{CacheMeCode}
\caption{Test}%
\end{figure}
\end{document}
% Local Variables:
% TeX-command-extra-options: "--shell-escape -halt-on-error"
% End:
Expected outcome
I expect the use of \def\mathdefault#1{#1}
to be at least documented, ideally automatically defined in the .pgf
file if the command does not already exist with something like:
\providecommand\mathdefault[1]{#1}
Additional information
This seems to be a regression, as it used not to be a problem on 3.7 I think.
Operating system
NixOs
Matplotlib Version
3.8.2
Matplotlib Backend
pgf
Python version
3.11.8
Jupyter version
Not using jupyter
Installation
Linux package manager