# Referencing externalized pgfplots with subfig and cleverref

I use pgfplots to produce a vast amount of images in my document. Due to limited LaTeX memory, I am forced to use \tikzexternalize. Moreover, I also use the subfig package to group several pictures into a single float and the cleverref package. So far, everything works perfect. However, when I add references to my externalized images, strange bugs occur.

In short, some plots which are based on data from files do not produce correct references. The exact same plots with fixed data do produce correct references. Apart from that, both methods results in correct images. My only problem are the missing references.

Due to the complexity of the project, it is hard to deliver a complete working sample code which produces such a bug. I hope the following explanations are enough to give you a rough idea of what might go wrong.

For all images, I use a shortcut command. Let us assume we have

\newcommand{\myfigureA}[1]{\tikzsetnextfilename{#1}\begin{tikzpicture}
\begin{axis}[width=4cm,height=4cm]
\addplot3[surf] file{#1.data};
\end{axis}\end{tikzpicture}\label{fig:#1}}


and

\newcommand{\myfigureB}[1]{\tikzsetnextfilename{#1}\begin{tikzpicture}
\begin{axis}[width=4cm,height=4cm]
\addplot3[surf] coordinates {
(0,0,0) (1,0,0) (2,0,0) (3,0,0)
(0,1,0) (1,1,0.6) (2,1,0.7) (3,1,0.5)
(0,2,0) (1,2,0.7) (2,2,0.8) (3,2,0.5)};
\end{axis}\end{tikzpicture}\label{fig:#1}}


When I use \myfigureB{name} to produce a certain image, all references \cref{fig:name} are working. On the other hand, when I use \myfigureA{name}, all references \cref{fig:name} are broken and I get "??" signs instead. However, both \myfigureA{name} and \myfigureB{name} result in the exact same image in the final PDF document (assuming the coordinates in the file correspond to the coordinates given here). Strangely enough, references to some images (always the same images) do in fact work. I have no idea what makes these images different from the others.

A usage of these commands to produce a series of four images might look like this:

\begin{figure}[h]
\centering
\subfloat[][]{\myfigureA{name1}}
\subfloat[][]{\myfigureA{name2}} \\
\subfloat[][]{\myfigureA{name3}}
\subfloat[][]{\myfigureA{name4}}
\caption{caption text}
\end{figure}


I use the latest versions of all mentioned packages. Compilation is performed by the usual four commands, namely pdflatex -interaction=nonstopmode -shell-escape mylatexfile, bibtex mybibliography, pdflatex -interaction=nonstopmode -shell-escape mylatexfile and pdflatex -interaction=nonstopmode -shell-escape mylatexfile.

In the .log file of the main document I only get the "Labels may have changed..." message for both \myfigureA{name} and \myfigureB{name}, but even compiling a few dozen times does not change this. I think this might be a result of the externalization process. The individual log files of the externalized images show a lot of missing references for both \myfigureA{name} and \myfigureB{name}, although in the former case they do work in the final PDF document. In the latter case, they do not, as already mentioned. The cleverref package does not seem to have anything to do with this bug.

Unfortunately, I have no more ideas where else to look for my bug.

-
So, the key seems to be the difference between file input and inline coordinate input, right? If I am not mistaken, the only difference between external images using file and coordinate input is when mode=list and make is used: pgfplots writes the input file name into the makefile dependencies. I am currently unaware of other differences. Did you try other image conversion modes (like mode=convert with system call)? You could also try to insert \relax before your \label commands to exclude string gobbling bugs in the external lib (although not very likely) – Christian Feuersänger Dec 30 '11 at 15:58
you could also try to set /pgf/images/aux in dpth=false which will deactivate references/labels inside of externalized images - this will free a further write register. – Christian Feuersänger Dec 30 '11 at 16:05
"the key seems to be the difference between file input and inline coordinate input" - Yes, apparently at least. Thanks for these hints, trying them out. – Werner Eims Dec 30 '11 at 16:28
If it does not help, it might be interesting to study the .aux files (and perhaps the .dpth files as well). They should contain the definitions of the \label commands in question. – Christian Feuersänger Dec 30 '11 at 16:38
Interestingly, all .dpth files are empty! There also seem to exist no .aux files for my individual externalized pictures. Do I have to enable these somewhere? – Werner Eims Dec 30 '11 at 17:17

## 1 Answer

It seems like I oversaw a deep-seated conflict between auto-pst-pdf and externalize. Disabling the former makes all my references available.

As an alternative, I can also use externalize and auto-pst-pdf with the [runs=1] option. An option [runs=2] or higher may lead to missing references though.

-