There are some issues when using the pgfplots option legend to name
together with externalization. When I disable externalization, everything works fine. When I enable externalization, I get an error
! Undefined control sequence.
l.1 ...every legend to name picture/.try]\pgfplots@legend@to@name
and my document wont compile. Without the legend to name
option, externalization works fine on the other hand. I am using version 1.5 of pgfplots according to \pgfplotsversion
. A short example follows.
\documentclass{report}
% begin of externalization
\usepackage{pgfplots}
\usetikzlibrary{external}
\usepgfplotslibrary{external}
\tikzset{external/force remake}
\tikzexternalize
% end of externalization
\begin{document}
% example from manual, page 166
\pgfplotsset{footnotesize,samples=10}
\begin{center}% note that \centering uses less vspace...
\begin{tikzpicture}
\begin{axis}[
legend columns=-1,
legend entries={$(x+0)^k$;,$(x+1)^k$;,$(x+2)^k$;,$(x+3)^k$},
legend to name=named, % critical line
title={$k=1$}]
\addplot {x};
\addplot {x+1};
\addplot {x+2};
\addplot {x+3};
\end{axis}
\end{tikzpicture}
%
\begin{tikzpicture}
\begin{axis}[title={$k=2$}]
\addplot {x^2};
\addplot {(x+1)^2};
\addplot {(x+2)^2};
\addplot {(x+3)^2};
\end{axis}
\end{tikzpicture}
%
\begin{tikzpicture}
\begin{axis}[title={$k=3$}]
\addplot {x^3};
\addplot {(x+1)^3};
\addplot {(x+2)^3};
\addplot {(x+3)^3};
\end{axis}
\end{tikzpicture}
\\
\ref{named}
\end{center}
% end of example
\end{document}
Am I missing something?
Best Answer
The problem is caused by shortcomings of the default conversion mode.
As explained in some of the comments, disabling the automatic conversion is one approach to solve the problem (compare the good comments of Hendrik).
However, image externalization of the small legend pictures is possible, although not by means of the default conversion mode.
Here is another solution which allows to externalize the small picture as well:
Call the externalization for the
\ref
picture command either manually (in this case,force remake
needs to be disabled) or using themode=list and make
.From my point of view, this approach is preferrable over the locally disabled externalization -- but only if you are willing to use use
mode=list and make
andmake -f <texfile>.makefile
to convert your images.If you are using windows or if invoking
make -f <texfile>.makefile
is undesired, you should stick with the answer of Hendrik.For the records and for the technically interested audience, I will summarize what's going on here:
\ref
pictures does not work with the default conversion mode:In your example, I issued the command shown in (b) and it worked.
See the pgfplots manual for details about the cause of this problem.
force remake
is in effect, the default conversion mode is always applied, even if you followed step (b). Thus, there is the choice to deactivateforce remake
or to usemode=list and make
.