I cannot explain the error. Do not give me the bounty. (see next section)
Assuming your MWE, \scalebox
works. Let me know if you intend to achieve something more complicated. By the way, whatever your goal is, the logic is that external
is superior to \IfFileExists
.
\documentclass{article}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tikz,xparse,hyperref}
\usetikzlibrary{external}\tikzexternalize
\NewDocumentCommand{\mypic}{O{red}O{1}}{
\tikzsetnextfilename{mypic_#1}
\scalebox{#2}{
\begin{tikzpicture}
\draw[#1] (1,0)--(0,1);
\end{tikzpicture}%
}
}
\begin{document}
\mypic[Cerulean] \mypic[Cyan] \mypic[red] \mypic[YellowGreen]
\mypic[Cerulean][1] \mypic[Cyan][2] \mypic[red][3] \mypic[YellowGreen][4]
\end{document}
What causes the error?
Before I can answer the question, I want to talk about the library external
.
Question: How does external
work?
Wrong answer:
It extracts the code between \begin{tikzpicture}
and \end{tikzpicture}
. And then runs that piece of code.
You can prove that this is wrong by adding some global setting outside:
\tikzset{
every picture/.style={ ... }
}
Correct answer:
It calls pdftex
to run the original file again, but this time:
- the output routine is suppressed, unless it meets the
tikzpicture
;
- it passes
-jobname "name_of_that_picture"
as an option, so this run would end up with name_of_that_picture.pdf
.
This mechanism is explained in the manual, currently part IX The Basic Layer section 107 Externalizing Graphics.
So ... what exactly happens normally?
Image that you call pdftex
:
pdftex
runs your_article.tex
;
pdftex
reaches \mypic{Cerulean}
;
pdftex
tries \IfFileExists{mypic_Cerulean.pdf}
, but in vain;
pdftex
decides to generated mypic_Cerulean.pdf
;
pdftex
calls another pdftex -jobname "mypic_Cerulean"
;
pdftex
2 runs your_article.tex
;
pdftex
2 reaches \mypic{Cerulean}
;
pdftex
2 tries \IfFileExists{mypic_Cerulean.pdf}
, but in vain;
pdftex
2 decides to typeset the tikzpicture
;
pdftex
2 finishes, leaves mypic_Cerulean.pdf
on the disk;
pdftex
is happy to see the result, mypic_Cerulean.pdf
included;
pdftex
reaches \mypic{Cyan}
;
pdftex
tries \IfFileExists{mypic_ Cyan.pdf}
, but in vain;
pdftex
decides to generated mypic_ Cyan.pdf
;
pdftex
calls another pdftex -jobname "mypic_ Cyan"
;
pdftex
3 runs your_article.tex
;
and so on...
So ... why hyperref
causes the error?
pdftex
runs your_article.tex
;
pdftex
includes hyperref
, your_article.pdf
created;
pdftex
reaches \mypic{Cerulean}
;
pdftex
tries \IfFileExists{mypic_Cerulean.pdf}
, but in vain;
pdftex
decides to generated mypic_Cerulean.pdf
;
pdftex
calls another pdftex -jobname "mypic_Cerulean"
;
pdftex
2 runs your_article.tex
;
pdftex
2 includes hyperref
, mypic_Cerulean.pdf
created;
pdftex
2 reaches \mypic{Cerulean}
;
pdftex
2 tries \IfFileExists{mypic_Cerulean.pdf}
, successes;
pdftex
2 decides to include mypic_Cerulean.pdf
, but in vain;
pdftex
2 terminates with Fatal error
;
pdftex
is unhappy to see no mypic_Cerulean.pdf
;
pdftex
pauses and throws the error you saw;
Other workarounds
Try not to test the file by yourself. If you really want to do so, mimic the way PGF testing files. If you just want to control \includegraphics
, PGF provides /pgf/images/include external
.
hyperref
temporarily?