[Tex/LaTex] Using reference with externalized pgfplot figure

cross-referencingpgfplotstikz-pgf

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}
\addplot[mark=*,color=blue,  solid] plot coordinates {
(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 undefined 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?

Best Answer

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".

Related Question