I have some figures as PGF files that include cross-references in them. Each time I compile my main document, these figures are recompiled too. Therefore I would like to externalize these figures.
The problem is that if externalize through the standalone class, as laid out in this answer by Torbjørn T., the references do not work anymore. (The problem of the standalone class not working with cross-references is also described here and here, no answers.)
The figures are made in Matplotlib via the PGF backend. An example of how to make such a figure with a cross-reference is given in this blogpost by Matt Hancock, and I'll add that code here:
Matplotlib (Python) code:
import numpy as np
import matplotlib as mpl
mpl.use('pgf')
pgf_with_custom_preamble = {
"text.usetex": True, # use inline math for ticks
"pgf.rcfonts": False, # don't setup fonts from rc parameters
"pgf.preamble": [
"\\usepackage{amsmath}", # load additional packages
]
}
mpl.rcParams.update(pgf_with_custom_preamble)
import matplotlib.pyplot as plt
plt.figure(figsize=(4,4))
plt.plot(np.random.randn(10), label="Equation \eqref{eq:foo}")
plt.grid(ls=":")
plt.legend(loc=2)
plt.savefig("figure.pgf", bbox_inches="tight")
LaTeX code:
\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{pgf}
\begin{document}
\begin{equation}
\label{eq:foo}
e^{i\pi} + 1 = 0
\end{equation}
\begin{figure}
\centering
\resizebox{3in}{3in}{
\input{figure.pgf}
}
\caption{This is the caption.}
\end{figure}
\end{document}
Result:

How could I externalize this figure, whilst keeping the cross-reference in the figure working?