1

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?

Spinbar
  • 11
  • 1
    This problem has to do with preserving links when inserting pdfs -- which due to the nature of the pdf is not trivial. I had posted several questions related to this and sort of had a working solution, but can't find those questions at the moment. – Peter Grill Jan 30 '21 at 18:37
  • @PeterGrill, do you maybe remember what tags you've given to those questions? (I couldn't find them in your questions tagged "pdf" or "pgfplot") – Spinbar Feb 01 '21 at 15:24
  • When externalizing as laid out in the quoted answer, you can load the package xr or xr-hyper for importing cross-referencing data from your main .tex-file to the standalone .tex. This way you can have values of counters of cross-ref'ed items of sectioning displayed correctly but you won't get hyperlinks to the cross-ref'ed items of sectioning because when compiling standalone .tex named destinations of main.tex/main.pdf are unknown and will be replaced by fixed-ones by pdfTeX. (A circumstance which drives me crazy and probably can be worked around going the dvi-route with grfpaste/dvipaste.) – Ulrich Diez Feb 01 '21 at 22:55
  • @Spinbar: I will try to look for them again later this week. It was not specfic to pgfplots, but rather a generic question about inserting pdfs and maintaining links. I recall the answer had some javascript. – Peter Grill Feb 02 '21 at 06:37
  • @Spinbar: Finally found the question. Reason I could not find it earlier was because I had offered a bounty on someone else's question that was similar, thus it did not show up under questions I had asked. Anyway, here is the reference Merging multiple pdf files without breaking hyperlinks. – Peter Grill Mar 22 '21 at 04:58

0 Answers0