# Using reference with externalized pgfplot figure

I am currently writing a document with a lot of plots. I draw them with pgfplots. which uses a lot of ressources. To manage that, I use the externalization feature of TikZ.

In the file myarticle.tex, there is:

\documentclass{article}
\usepackage{pgfplots}
\pgfrealjobname{myarticle}

\begin{document}
\begin{figure}[htbp]
\centering
\beginpgfgraphicnamed{myplot}
\begin{tikzpicture}
\begin{axis}
(0,0)
(0.5,1)
(1,2)
}; \label{myplotOne}
\end{axis}
\end{tikzpicture}
\endpgfgraphicnamed
\caption{A beautiful line: \ref{myplotOne}.}
\end{figure}

\end{document}


I then compile it with:

pdflatex --jobname myplot myarticle
pdflatex myarticle
pdflatex myarticle


Problem is it forbids me to use the label/ref mechanism of pgfplots. I know there is a remark about this on the pgfplots manual (section 4.4.2, page 30): "note that you can’t use the label/ref mechanism in conjunction with image externalization as this will (naturally) lead to undeﬁned references."

Is there a way to trick LaTeX/TikZ in order to use them in conjunction? Maybe by storing the displayed reference in a pdf file?

• Which such example files it is best (actually mandatory) to also mention how it must be compiled, i.e.: is -shell-escape required? Must this file be named myarticle.tex? This way also people which do not know pgfplots much but know a lot about references are able to help you. – Martin Scharrer Mar 1 '11 at 16:49
• @Martin: I add the compilation information, thanks for the advice. – Drude Mar 1 '11 at 20:51

In general you should be able to do this using the xr package which allows to reference labels in other LaTeX documents.

But the pgfplot package seem to be able to do this now out-of-the-box (manual v1.4.1, section p. 241):

Support for Labels and References In External Files The external library comes with extra support for \label and \ref (and other commands which usually store information in the .aux file) inside of external files.

You seem to use an outdated version. At least the mentioned text in section 4.4.2, page 30 isn't there in my (freshly updated) version. There 4.4.2 is on page 42 and is about "Smooth plots".

• It seems you are right: I was working with version 4.2. I'll study the new version, and edit my question accordingly. – Drude Mar 1 '11 at 20:56

as Martin said, the restriction has been lifted in more recent versions of pgfplots: it offers the

\usepgfplotslibrary{external}


which can be activated by means of

\tikzexternalize

...
\begin{document}
...


When you type pdflatex -shell-escape <maindocument>, all images in the (complete) document will be externalized automatically; including any references.

You can find details about the procedure in http://pgfplots.sourceforge.net/pgfplots.pdf in section "Import/Export from other Formats".

The sourceforge page also contains links to the most recent pgfplots stable (which is 1.4.1 at the time of this writing).

Best regards

Christian

• @Feuersaenger: You need to indent code blocks with four spaces (or use the '101010' button) and mark inline code using back-ticks . Also normally we don't put greetings and signatures into the posts. Your name is anyway displayed on the lower right. Really nice package BTW! – Martin Scharrer Mar 1 '11 at 21:24
• @Feuersaenger: The \tikzexternalize/shell-escape method works great with label/references, thanks! However, is it possible to do the same with the \beginpgfgraphicnamed/jobname method? Or is this obsolete and should be used? – Drude Mar 1 '11 at 21:45
• Important detail (which I for one missed): when shipping a document with externalized figures, you need to pack the figure .pdf and also the .dpth`. See also here, – Raphael Jun 1 '15 at 14:38