# Tikz externalize: use external PDF or render by tikz, do not create external PDF

I have texlive 2012 and tikz. Is it possible to get the following behavior?

With external library to tikz, use external image, if it is available. However, if it is not available, render the picture by tikz. Do NOT create external PDF.

I have some files where externalization fails, probably because tikz code is nested in several more complicated complicated macros (which include writing code into auxiliary TeX file and so on).

We are speaking about approx 500 files which are complied in a bash a script if the input for these files changes (fixing typos, improving the template, adding new type of interactivity in PDF). I think, I can check if the pdf files with corresponding names exist and turn the externalization on/off from command line, but I wonder is there is some cleaner solution in TeX only.

Typical example of the PDF file is http://user.mendelu.cz/marik/temp/000040.pdf

Update

The following file does not compile with \tikzexternalize

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize
\begin{document}

\def\mypic#1{
\begin{tikzpicture}\clip (-1,-1) rectangle (1,2);
\draw[red] plot (\x,{#1});
\end{tikzpicture}
}

\mypic{(\x)^2}
\begin{tikzpicture}\clip (-1,-1) rectangle (1,2);
\draw[red] plot (\x,{\x*\x*\x});
\end{tikzpicture}

\end{document}

The reason why we do need externalize is the fact, that we will create TeX files on a specific web site on the fly and compile. We would like to save the computer load and we also want not to edit the TeX files again. The idea was

• We compile on a fast machine, most of the tikz pictures will be externalized, some of them not
• We move the PDF files to the server.
• If something has to be compiled on the server, and PDF file is available, use this PDF file. If not, compile as with regular tikz. We want to have a functionality similar to the answer of benwilfut but without modifying TeX files.
-
Please post an MWE. You posted a PDF as a 'typical example of the TeX file'... Also, could you explain why you need the external library here? What you want to achieve isn't really externalization of TikZ graphics IMO. –  benwilfut Mar 11 '13 at 12:56
Thanks, I updated the question. –  robert.marik.cz Mar 11 '13 at 13:22
Hm... compiling your example with pdflatex --shell-escape yourfile.tex gives me no errors and produces yourfile.pdf, yourfile-figure0.pdf, and yourfile-figure1.pdf as expected. Could you perhaps post a log file detailing the errors you're encountering? Of course, your example would also create externalized PDFs if not already present, which doesn't seem to be what you want... –  benwilfut Mar 11 '13 at 13:37
Hm... compiles for me also just now. Perhaps I did a typo in pdflatex call. Thanks. –  robert.marik.cz Mar 11 '13 at 14:04

### edit:

I believe the TikZ key external/mode=graphics if exists does exactly what you want. (See the TikZ/PGF Manual v2.10 Section 32.4.4, page 349.) Try the following for your example:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize
\tikzset{external/mode=graphics if exists}
\begin{document}

\def\mypic#1{
\begin{tikzpicture}\clip (-1,-1) rectangle (1,2);
\draw[red] plot (\x,{#1});
\end{tikzpicture}
}

\mypic{(\x)^2}
\begin{tikzpicture}\clip (-1,-1) rectangle (1,2);
\draw[red] plot (\x,{\x*\x*\x});
\end{tikzpicture}

\end{document}

If you know that the external graphics has a particular extension (as seems to be given in your case) you can use \IfFileExists{<file>}{}{}, e.g.

\documentclass{article}

\usepackage{tikz}

\begin{document}

\IfFileExists{./logo.pdf}{%
\includegraphics{logo}
}{%
\begin{tikzpicture}
\draw (0,0) rectangle (1,1);
\end{tikzpicture}
}

\end{document}
-
Thanks, this is exactly what we want, but automatically, without using the \IfFileExists command. I updated the question. thanks. –  robert.marik.cz Mar 11 '13 at 13:23
Thanks, thats it –  robert.marik.cz Mar 11 '13 at 14:03