[Tex/LaTex] How to auto replace tikzpicture by includegraphics using TikZ external

tikz-pgf

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}

Best Answer

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.