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:



With part1.tex looking like this:

    group style= {  columns=3,xlabels at=edge bottom,
                y descriptions at=edge left,
                horizontal sep=2cm,group name=plots},
    legend columns=3]
    \nextgroupplot[xlabel=site 1,legend to name=grouplegend]
    \addplot coordinates {(1,20)(2,23)(3,24.5)};
    \addlegendentry {AaAaA,BbBbB,CcCcC}%
    \nextgroupplot[xlabel=site 2]
    \addplot coordinates {(1,14)(2,16)(3,17)};
    \nextgroupplot[xlabel=site 3]
    \addplot coordinates {(1,10)(2,11.5)(3,12.25)};
  % Legend
    \node at (plots c2r1.north) [anchor=south] {\ref{grouplegend}};

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

Side note on your question:
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 https://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).


  \begin{axis}[legend to name=fig:1:legend] % Define legend reference
    \addplot coordinates {(1,20)(2,23)(3,24.5)};
  \node at (0,-2) {\ref{fig:1:legend}}; % reference legend

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
  • 1
    In case of legends, this answer offers a much simpler solution. Maybe you want to add this. – thewaywewalk Jul 16 '15 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.