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.


% begin of externalization
\tikzset{external/force remake}
% end of externalization


% example from manual, page 166
\begin{center}% note that \centering uses less vspace...
legend columns=-1,
legend entries={$(x+0)^k$;,$(x+1)^k$;,$(x+2)^k$;,$(x+3)^k$},
legend to name=named, % critical line
\addplot {x};
\addplot {x+1};
\addplot {x+2};
\addplot {x+3};
\addplot {x^2};
\addplot {(x+1)^2};
\addplot {(x+2)^2};
\addplot {(x+3)^2};
\addplot {x^3};
\addplot {(x+1)^3};
\addplot {(x+2)^3};
\addplot {(x+3)^3};
% end of example


Am I missing something?

  • 4
    I found a solution here. Just use \tikzexternaldisable\ref{named}\tikzexternalenable instead of \ref{named}.
    – Hendrik
    Commented Dec 7, 2011 at 16:45
  • 2
    Put it in as an answer. I didn't know this so I reckon it'll be a good addition.
    – qubyte
    Commented Dec 7, 2011 at 17:01
  • 2
    Externalization can not handle referenced legends, so one has to turn it off for such cases. First possibility: Instead of \cref{legendlabel}, always write \tikzexternaldisable\ref{legendlabel}\tikzexternalenable.
    – Hendrik
    Commented Dec 7, 2011 at 17:09
  • 5
    Second possibility: Use a preamble \pgfplotsset{invoke before crossref tikzpicture={\tikzexternaldisable},invoke after crossref tikzpicture={\tikzexternalenable}} and then reference legends normally with \cref{legendlabel}. It might be possible that this second possibility also prevents other pictures from being externalized, I have not tested this out. Source of this answer: [Forum][1]. [1]: pgfplots-features.706524.n3.nabble.com/…
    – Hendrik
    Commented Dec 7, 2011 at 17:09
  • 1
    See also the related question tex.stackexchange.com/questions/36786/… Commented Dec 10, 2011 at 8:57

2 Answers 2


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.
  • I am not sure how current this answer is, and how current it is supposed to be, but I noticed something that I think is worth putting in a comment here. When you have externalized plot graphics and externalized \ref-icons of plot references into those graphics, list and make does not put the icon jobs into the list after the first run of Xlatex. One has to run Xlatex; make; Xlatex; make; ... to get all of these. (Is this intentional? I'm using pgfplots 1.12.1 with TikZ 3.0.1)
    – Raphael
    Commented Sep 7, 2015 at 14:52

The quick and easy solution is to temporarily disable the externalization with \tikzexternaldisable and \tikzexternalenable. Therefore one should write:


instead of simply


Where named is the label corresponding to

legend to name = named,

inside the options of the axis environment, as specified in the section 4.9.7 Legends Outside Of an Axis of the pgfplots manual.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .