[Tex/LaTex] Tikz externalize: use external PDF or render by tikz, do not create external PDF

graphicstikz-externaltikz-pgf

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.

Best Answer

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}

original answer:

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}
Related Question