[Tex/LaTex] pgfplots: externalization and legend referencing

legendpgfplotstikz-external

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 the mode=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 and make -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:

  1. as explained in the log file of the "crossref picture", the automatic externalization of the small \ref pictures does not work with the default conversion mode:
! Package tikz Error: Sorry, image externalization failed: the resulting image was EMPTY. I tried to externalize 'P-figure_crossref0', but it seems there is no such image in the document!?  
   You are currently using 'mode=convert with system call'. This problem can happen if the image (or one of the images preceeding it) was declared inside of a \label{} (i.e. in the .aux file): 'convert with system call' has no access to the main aux file.
   Possible solutions in this case:
   a) Try using 'mode=list and make',
   b) Issue the externalization command 'pdflatex -shell-escape -halt-on-error -interaction=batchmode -jobname "P-figure_crossref0" "\def\tikzexternalrealjob{P}\input{P}"' *manually* (also check the preceeding externalized images, perhaps the file name sequence is not correct).
   Repeat: the resulting image was EMPTY, your attention is required .

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.

  1. As we have seen, the default conversion mode fails. But if force remake is in effect, the default conversion mode is always applied, even if you followed step (b). Thus, there is the choice to deactivate force remake or to use mode=list and make.