# TikZ Externalize: Referenced legends from PGFPlots are not included

The following MWE without TikZ externalization works. However, the legend at the bottom is lost with externalization. Since the \ref in this example does not refer to anything outside tikzpicture environment, I thought this should work. However, I understand that latex needs to loop twice to resolve the reference and is not able to do it here.

MWE:

\documentclass{standalone}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.6}
\usetikzlibrary{external}
\tikzexternalize
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
legend entries={$\sin(x)$},
legend to name = named,
name = plt
]
\end{axis}
\node[at=(plt.south),anchor=north] {\ref{named}};
\end{tikzpicture}
\end{document}


If I move \ref{named} outside the tikzpicture environment, I can generate two separate PDF files using list and make mode. However, I would like to keep the legends with my plot.

PS: MWE is only representative. This will be used for legend entries with multiple subplots.

-
Did you try exhausting all the options stated in the manual? There is a whole section about how and why specifically adressing cross references.. –  percusse Dec 7 '12 at 19:10
Could you add answer with \pgfplotslegendfromname{name} instead of \ref{name}? I figured it out, but didn't know how to let people know. –  devendra Dec 7 '12 at 19:31
Just wait a little bit(!) and the system will allow you to answer your own question. –  percusse Dec 7 '12 at 19:35

I figured out the solution. \ref is not really required in this case. If \ref{named} is substituted with \pgfplotslegendfromname{named} then the MWE works. Though \ref can be used to put the legend, it is not a good usage.

-
The failure of \ref sounds more like a bug to me... but good to know that there is a working solution. perhaps the externalization has to be run twice just a other latex documents? –  Christian Feuersänger Dec 8 '12 at 9:25
I am trying to accomplish practically the same thing, only within a groupplot. Though the manual says that the groupplot environment should function basically the same as the axis environment, it doesn't seem to with regard to names. So where you used name=plt so that you could later use \node[at=(plt.south) I can't seem to replicate for a groupplot. No matter what I try, I get the error No shape named plt is known. –  jerH May 17 at 21:58