# References in externalized pgfplots

I was writing on an document with a lot of pgfplots in it. Because of the memory limitation in LaTeX and in order to save some compiling time I tried out the externalize command. With most of the plots it perfectly worked out. But when I use the \ref command, namely \ref{<legendname>} the legend gets lost and instead there is a ??. I think this question in a way is similar to Referencing externalized pgfplots with subfig and cleverref and Using reference with externalized pgfplot figure. But no answer to these questions could solve my problem. Even the use of xr and zref packages showed no effect.

Am I just missing something?

MWE of the maindocument:

\documentclass{article}
\usepackage{zref-user}
\usepackage{xr-hyper}
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.groupplots}
\usepgfplotslibrary{external}
\tikzexternalize
\externaldocument{part1}

\begin{document}
\input{part1}
\end{document}


With part1.tex looking like this:

\begin{tikzpicture}
\begin{groupplot}[
group style= {  columns=3,xlabels at=edge bottom,
y descriptions at=edge left,
horizontal sep=2cm,group name=plots},
width=0.4\textwidth,
xtick=data,
legend columns=3]
\nextgroupplot[xlabel=site 1,legend to name=grouplegend]
\nextgroupplot[xlabel=site 2]
\nextgroupplot[xlabel=site 3]
\end{groupplot}
% Legend
\node at (plots c2r1.north) [anchor=south] {\ref{grouplegend}};
\end{tikzpicture}


I'm writing with MikTeX 2.9 and the latest updates of the packages used. For compiling I used the command pdflatex -enable-write18 <file name>.

I hope the MWE is short enough ;) and some knows how to solve this problem.

-
There is no \label{grouplegend}, so it doesn't know what to reference to. Or am I missing something? –  Psirus Jan 10 '12 at 16:03
@Psirus This one does that: legend to name=grouplegend –  zeroth Jan 10 '12 at 18:20

Firstly I would like to show you my idea of your case in a MWE. I have cut out your groupplot because (as I said in http://tex.stackexchange.com/q/40511/7049 the problem does not lie in groupplot). You should test your MWE, then remove something, test again, does the problem still occur?, then remove some more. Keep doing this until you have found exactly what is causing your problem! This will not only help us, but will help yourself in becoming better at finding what might be the problem.

Ok, so here is a MWE for your problem. I can see that you have tried with the other packages, but for this, the problem still occurs. (I disregarded the use of zref-user and xr-hyper).

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{external}
\tikzexternalize

\begin{document}
\begin{tikzpicture}
\begin{axis}[legend to name=fig:1:legend] % Define legend reference
\end{axis}
\node at (0,-2) {\ref{fig:1:legend}}; % reference legend
\end{tikzpicture}
\end{document}


## The problem

What you have as a problem is stated in the pgf/tikz manual as case a) under section Support for Labels and References In External Files.

There are, however, some points which need your attention when you try to use
a) \ref to something in the main document inside an externalized graphics or
b) \label in the externalized graphics which is referenced in the main document.

In this section your case is also described:

There is just one special case: if a \label/\ref combination is realized itself by a tikzpicture which should be externalized, you need to proceed as for case a) since mode=convert with system call can't handle that stuff on its own. Thus, \label works automatically, just translate the main document often enough.

Your solution is as stated in the document, you need to call pdflatex once more.

However all this is also written in your log, the external library is so smart that it even tells the users that you need to do manual post-processing.
It would write that you still have some undefined references. Which in this case is for your label in the externalized picture.

## Solution 1

So the answer is that you manually have to call the following command, suit to your needs:
pdflatex -shell-escape -interaction=batchmode -jobname "test-figure0" "\def\tikzexternalrealjob{<insert your document name>}\input {<insert your document name>}"

## Solution 2

Another method is to use the pgfplots specialized command \pgfplotslegendfromname{<ref>} which is a work around for explicit references in pgfplots environment in the legend.

Courtesy of @thewaywewalk

-
Thanks for the effort you expended on that. It surprisingly really works it out. Just a bit laborious... –  Lorzen Jan 11 '12 at 9:17
@Lorzen, you are welcome. Yes, it is annoying that one have to do this manually. But you could set up a BAT script which does this for you. –  zeroth Jan 11 '12 at 9:20
In case of legends, this answer offers a much simpler solution. Maybe you want to add this. –  thewaywewalk Jul 16 at 16:12