2

I am using the externalize feature of tikz/pgf to save space for my thesis. I am using TeXShop on a Mac. I am running into trouble referencing something in the main document from the externalized tikzpicture. A MWE for the main file:

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepackage{tikz}
\usepgfplotslibrary{external}
\tikzexternalize[prefix=pdfimages/]

\newcommand{\includetikz}[1]{%
    \tikzsetnextfilename{#1}%
    \input{#1.tex}%
}

\begin{document}

\begin{equation} \label{eq:equation}
y = x + x^2
\end{equation}

\includetikz{image}

\end{document}

A MWE for"image.tex":

\begin{tikzpicture}
    \begin{axis}
        \addplot [color=black,solid,line width=1.0pt,domain=0:1,samples=11]{x+x^2};
        \addlegendentry{Eq. \ref{eq:equation}};
    \end{axis}
\end{tikzpicture}

Produces: Can't reference!

The pgfplots manual (Operations Mode in sec 7.1) suggests using mode=list and make to solve this issue, but I am having trouble understanding how to run the makefile that is generated. What would be a robust way of implementing this with TeXShop? Thanks for your help.

EDIT: Using make from the command line is the only way of doing this so far that has worked for me. I added mode=list and make to \tikzexternalize and then compiled it from TeXShop. Prompted by the compiler, running make -f main.makefile (main is the filename) generated the images. Compiled the document a few more times and the reference from within the graphic worked as promised. This works but wasn't what I originally intended. If someone knows a way to this without the command line from within TeXShop, I would be much obliged.

2 Answers2

2

As of pgfplots 1.13, this works without any modifications, i.e. simply running pdflatex -shell-escape file.tex twice results in the desired output.

Here is a reduced minimal without the extra image.tex (but otherwise identical), compiled with pgfplots 1.13 and pgf 3.0.1:

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepackage{tikz}
\usepgfplotslibrary{external}
\tikzexternalize

\begin{document}

\begin{equation} \label{eq:equation}
y = x + x^2
\end{equation}

\begin{tikzpicture}
    \begin{axis}
        \addplot [color=black,solid,line width=1.0pt,domain=0:1,samples=11]{x+x^2};
        \addlegendentry{Eq. \ref{eq:equation}};
    \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

A solution for you might be to update your TeX distribution.

1

Did you try to rerun Latex? For the first run I get the warnings LaTeX Warning: External picture 'pdfimages/Latex1' contains undefined references on input line 6. and LaTeX Warning: There were undefined references. After a second Latex run the warnings clear and I get:

enter image description here

JMP
  • 3,238
  • Unfortunately that did not work. Would have been a nice fix. – Private Mandella Apr 24 '16 at 19:20
  • This is strange. Please try to run the MWE without the externalisation, i.e. just using \input{image.tex} and removing \tikzexternalize[prefix=pdfimages/] and your declaration of \includetikz. Probably this is an issue with the set-up of your Latex compiler. – JMP Apr 24 '16 at 19:24
  • I checked just make sure, and it did work without the externalization. I'm surprised yours worked without doing anything extra as the manual does mention this problem for external graphics. – Private Mandella Apr 24 '16 at 19:30
  • Then I think the problem is the Latex compiler. The externalization just works, if there are some additional commands passed to the compiler. Does the externalization work in general for you? What compiler do you use? – JMP Apr 24 '16 at 19:36
  • The externalization has worked so far for me. The only issue I can remember is the current problem. I'm not sure which compiler I'm using, but I downloaded MacTeX and use TeXShop as the editor if that helps. – Private Mandella Apr 24 '16 at 19:41
  • Copied comment from Exporting TikZ to EPS using MacTex: Are you sure you have --shell-escape enabled in your pdflatex command in TeXShop? In the TeXShop preferences, under the Engines tab, the command for pdflatex should be pdflatex --file-line-error --synctex=1 --shell-escape. – JMP Apr 24 '16 at 19:49
  • I do, here is the command: pdflatex --file-line-error --synctex=1 -shell-escape – Private Mandella Apr 24 '16 at 19:52
  • 1
    Replace -shell-escape by --shell-escape – JMP Apr 24 '16 at 19:53
  • That did not fix the problem either. Here is the warning from the console: LaTeX Warning: Reference 'eq:equation' in external picture 'pdfimages/image' could not be resolved on input line 6. This is because the main.aux file is not accessable in this context, you will need to issue the externalize command pdflatex -shell-escape -halt-on-error -interaction=batchmode -jobname "pdfimages/image" "\def \tikzexternalrealjob {main}\input {main}" manually. – Private Mandella Apr 24 '16 at 20:01
  • It does not compile with that command in the preamble. – Private Mandella Apr 24 '16 at 20:14
  • The pgf manual states: A \ref inside of an externalized graphics works only if you issue the required system call manually or by make. The initial configuration mode=convert with system call does not support \ref. But you can copy–paste the system call generated by mode=convert with system call and issue it manually. The reason is that \ref information is stored in the main .aux file – but this auxiliary file is not completely written when mode=convert with system call is invoked (there is a race condition). Hmm what do we learn from that? – JMP Apr 24 '16 at 20:15
  • I'd rather not manually set it for all the images in the document. As mentioned in the original question, the manual suggests using mode=list and make and using the makefile generated from this option instead of manually doing it. – Private Mandella Apr 24 '16 at 20:20
  • Did you consider to exclude this specific graph from externalization? Or is it a larger set of graphs, where you're using \ref? – JMP Apr 24 '16 at 20:36
  • I considered not having \ref in the legend as well as not externalizing it. If I can't figure out how to get it to work, I'll either not have to use \ref or turn off the externalize feature. Neither of these is optimal because of the amount of figures in my thesis that refer to equations in the main body of the text. – Private Mandella Apr 24 '16 at 20:39