# How to auto replace tikzpicture by includegraphics using TikZ external?

Is there a way using e.g. the external TikZ library to produce, from a master LaTeX document, another LaTeX source where each tikzpicture environment is replaced by the appropriate includegraphics command?

This can be painful to do by hand in a document which contains many such environments.

Example master document x.tex:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{...}
\usetikzlibrary{external}
\tikzexternalize

\begin{document}
...
\begin{figure}
\begin{tikzpicture}
...
\end{tikzpicture}
\caption{A}
\end{figure}

\begin{figure}
\begin{tikzpicture}
...
\end{tikzpicture}
\caption{B}
\end{figure}
...
\end{document}


desired output:

\documentclass{article}

\usepackage{graphics}

\begin{document}
...
\begin{figure}
\includegraphics{x-figure0}
\caption{A}
\end{figure}

\begin{figure}
\includegraphics{x-figure1}
\caption{B}
\end{figure}
...
\end{document}

• This is more or less what external library does. When the figure doesn't exist, it creates a pdf file which is used in later compilations. – Ignasi Oct 6 '15 at 15:03
• Then how can I get the .tex that external generates? I need to replace pictures in TikZ and pgfplots by the generated PDF images in the .tex, i.e. I would like to get automatically a LaTeX document with no PGF/TikZ code. This is for a submission to arxiv, which does not seem to support correctly pgfplots. – user43513 Oct 6 '15 at 15:26

All about externalization is explained is section "50 Externalization Library" and how to send a tex file with tikzpictures without using tikz is explained in section "50.5 Using External Graphics Without pgf Installed". So, please read these pages. What follows is just a short recipe without to many details. The code has been borrowed from pgfmanual.

The original file contains tikzpictures which are externalizated. It load tikz and external library. With command \tikzexternalize, all tikzpictures are extracted and processed. For a main.tex file several main-figureX.pdf (and some .dpth).

\documentclass{article}
% main document, called main.tex
\usepackage{tikz}
%\usepackage{graphicx}
%\usepackage{tikzexternal}
\usetikzlibrary{external}
\tikzexternalize
\begin{document}
\begin{tikzpicture}
\node {root}
child {node {left}}
child {node {right}
child {node {child}}
child {node {child}}
};
\end{tikzpicture}
A simple image is \tikz \fill (0,0) circle(5pt);.
Furthermore, we might want to draw \tikz[baseline]\draw (0,-1) rectangle (1,1);
\end{document}


Once all figures are correct. Preamble is changed to:

\documentclass{article}
% main document, called main.tex
%\usepackage{tikz}
\usepackage{graphicx}
\usepackage{tikzexternal}
%\usetikzlibrary{external}
\tikzexternalize


tikz package and external library are not used any more and are replaced by graphicx and tikzexternal packages (tikzexternal.sty is in folder latex/pgf/utilities/tikzexternal.sty and you must copy it to your working folder).

Next run will load .pdf figures instead of generating them.