11

I've been reading and gathering material from multiple sources online about what steps are required to externalize a document that contains TikZ pictures which store a reference to their legends using legend to name=<name> and use \ref{<name>} to include the legends at the bottom of, for example, groupplots.

Options include using list and make instead of convert with system call, or using a trick/hack which does not externalize the reference by using the following in the preamble:

\pgfplotsset{
  invoke before crossref tikzpicture={\tikzexternaldisable},
  invoke after crossref tikzpicture={\tikzexternalenable},
}

Since my document includes 30+ figures and I have not found a way to make list and make not re-produce the figures every time (two passes are needed as well!), my only option is to try the "hacky" way. Below is a MWE of what I have.

MWE:

\documentclass{article}

\usepackage{tikz,pgfplots}
\usepgfplotslibrary{groupplots}
\usepgfplotslibrary{external}
\pgfplotsset{
  compat=newest,
  invoke before crossref tikzpicture={\tikzexternaldisable},
  invoke after crossref tikzpicture={\tikzexternalenable},
}
% \tikzexternalize

\begin{document}
  \begin{figure}[]
    \centering
    \begin{tikzpicture}
      \begin{groupplot}[
        group style={
          group size=2 by 1,},]
        \nextgroupplot[scale only axis,width=0.35\textwidth,height=0.35\textwidth,legend to name=grouplegend,]
        \addplot
          coordinates {(1,1) (2,2) (3,3)};
        \addlegendentry{Legend 1}
        \addplot
          coordinates {(1,3) (2,2) (3,1)};
        \addlegendentry{Legend 2}
        \nextgroupplot[scale only axis,width=0.35\textwidth,height=0.35\textwidth]
        \addplot
          coordinates {(1,1) (2,2) (3,3)};
        \addplot
          coordinates {(1,3) (2,2) (3,1)};
      \end{groupplot}
      \node (legend) at ($(group c1r1.south)!0.5!(group c2r1.south)$)
        [below, yshift=-3\pgfkeysvalueof{/pgfplots/every axis title shift}]
        {\ref{grouplegend}};
    \end{tikzpicture}%
    \caption{The caption.}
  \end{figure}
\end{document}

If I compile this with two passes of pdflatex -shell-escape <file>, the reference is resolved and the legend shows up correctly at the bottom of my two figures. Similarly, when list and make is used, running pdflatex -shell-escape <file> and make -B -f <makefile> twice also resolves the reference. When \tikzexternalize is used however, I can't find the right sequence of commands which resolves the reference. No matter how many times I run pdflatex -shell-escape <file>, the reference remains unresolved and the legend shows "??".

Does anyone know why the trick in the preamble has no effect? Am I doing something wrong?

5
  • My bad, I was sure it was enabled in your MWE. No, I can confirm that this also fails here with a pretty recent combination of pgfplots and tikz on MiKTeX 2.9. I came across the same issue and used the list and make option as a not so nice workaround.
    – Alexander
    Jul 26, 2013 at 16:39
  • The problem with using list and make is that the -B flag is needed for two passes to take place, and this causes all 30+ figures to be re-produced. If you know an easy way to fix this, I'll gladly use it.
    – sudosensei
    Jul 26, 2013 at 16:55
  • I use a batch file that deletes only the figures that contain references and leaves the others untouched. This is all neither nice nor elegant. Maybe one of the developers will come up with a better solution at some point.
    – Alexander
    Jul 26, 2013 at 16:59
  • Can I remove the -B flag, run make -f <makefile> once, delete the externalized pdf of the figure that includes a reference and run make -f <makefile> again, or would that break something?
    – sudosensei
    Jul 26, 2013 at 17:13
  • Exactly that works here nicely.
    – Alexander
    Jul 26, 2013 at 17:27

1 Answer 1

10

The fact that mode=list and make does not handle the case of \ref inside of the externalized image is a bug in the external library.

I have fixed it in PGF CVS (right now).


Potential work-around:

After running make -f P.makefile, the next run of pdflatex will result in the log message

LaTeX Warning: External picture `P-figure0' contains undefined references on input line 36.

I guess this is your only chance: you can generate some simple script which checks for this message. The script can then delete the affected pdf(s) and rerun make.

2
  • I can confirm that the update fixes this. Thank you! For those wondering, I followed the instructions from here to create a TDS archive of the latest version pgf and put it in my ~/texmf tree. I then created a bash script which calls pdflatex <file> and make -f <file>.makefile two times in a row and the reference is resolved. This seemed to only work with \usetikzlibrary{external}, however. @christian-feuersanger, it would be great if you could edit your answer slightly to state that "this" refers to the make and list method.
    – sudosensei
    Jul 27, 2013 at 9:23
  • 1
    Thanks for the test. I have edited my answer. And: yes, there was an issue in \usepgfplotslibrary{external}: it is supposed to prefer the tikz version if available. This "if available" check is broken in pgfplots 1.8; will be fixed with the next pgfplots version. Jul 27, 2013 at 17:53

You must log in to answer this question.

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