[Tex/LaTex] tikzexternalize and align environment – errors

aligntikz-external

I am experiencing a problem when using externalize and the align environment. In some weird cases the pdf of tikz figures are not generated properly and they have to be re-generated every time that the tex file is compiled.

Here is a MWE:

The main file, called test.tex

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize % activate with the default mode
\usepackage[cmex10]{amsmath}

\begin{document}

\tikzsetexternalprefix{figs/tikz2pdf/}

\begin{figure}
  \tikzpicturedependsonfile{figs/fig.tikz}
  \tikzsetnextfilename{fig}
  \input{figs/fig.tikz}
\end{figure}

\begin{align}
  A &= 1 \label{A} \nonumber \\ 
  B &= 2 \label{B} 
\end{align}

\end{document}

The content of figs/fig.tikz is:

\begin{tikzpicture}
\draw (0,0) --(1,2);
\end{tikzpicture}

The directory figs/tikz2pdf exists prior to the compilation. I compile the code using "pdflatex -interaction=nonstopmode -shell-escape test.tex". To my understanding this should create figs/tikz2pdf/fig.pdf, but it does not. It successfuly creates test.pdf, but reports the following two errors:

  1. ! Package tikz Error: Sorry, the system call 'pdflatex -shell-escape -halt-on-e
    rror -interaction=batchmode -jobname "figs/tikz2pdf/fig" "\def\tikzexternalreal
    job{test}\input{test}"' did NOT result in a usable output file 'figs/tikz2pdf/f
    ig' (expected one of .pdf:.jpg:.jpeg:.png:). Please verify that you have enable
    d system calls. For pdflatex, this is 'pdflatex -shell-escape'. Sometimes it is
    also named 'write 18' or something like that. Or maybe the command simply fail
    ed? Error messages can be found in 'figs/tikz2pdf/fig.log'. If you continue now
    , I'll try to typeset the picture.

  2. ! Package amsmath Error: Multiple \label's: label 'A' will be lost.

Interestingly:

  1. if I remove either the "\nonumber" or "\label{A}" from the align environment, figs/tikz2pdf/fig.pdf is created.

  2. Once that figs/tikz2pdf/fig.pdf is created once, it is used in following compilations of test.tex, not needing to re-compile figs/fig.tikz

I know that labelling a line in align and then using \nonumber is somehow incongruent. What puzzles me is that the error in the align environment prevents externalize to work properly. And despite all, a proper test.pdf is produced. The overall visible effect is a large compilation time in documents containing many tikz figures.

A final note: I came up with the provided MWE stripping down a long complex document. After that I removed all (accidental) occurrences of simultaneous labelling and usage of \nonumber, after which externalize still does not produce the pdf from tikz figures. I therefore suspect that the previous MWE could be one way to reproduce a more intricate issue.

Any light on this is appreciated. Am I doing something wrong? Am I missing something?

EDIT: It seems that I forgot an occurrence of simultaneous labelling and usage of \nonumber. After its removal externalize does produce the pdf from tikz figures, so this seems to be the key issue.

Best Answer

The process of generating separate pdf figures involves a complete scan over the input document. This scan is optimized in the sense that a lot of expensive macros are disabled unless they are part of the target figure. But it is still a complete scan.

If there is a single error somewhere during that scan, the export will fail. And it seems as if that happened. You will see the root cause explained in figs/tikz2pdf/fig.log.

The fact that you receive a complete test.pdf is caused by -interaction nonstopmode: it reports the error, but continues to process the document. In that case, the external library falls repeats the process as if it would not have attempted the externalization.

To conclude: please repair any errors in your main document, otherwise the external lib will fail as well.

Related Question