[Tex/LaTex] Convert tikz to \includegraphics

tikz-pgf

I have a paper that I wrote with a lot of figures generated with tikz, but I've been asked to supply a version that doesn't use any "nonstandard tricks", i.e. with the images included with \includegraphics instead.

Of course I can go through and generate a separate pdf of each tikz image, then include them in my LaTeX document and fiddle around getting the layout the same – but I was wondering if there is a way to automate some or all of this process.

So is there an existing way to automatically go through every tikz picture in a LaTeX document and create a pdf image, with the appropriate dimensions so that it will be the same size when inserted with \includegraphics?

Best Answer

You can use the externalization library. I added an automatic regeneration feature which fits better for me than the one which is implemented in the pgf-package.

in your header:

\usepackage{filemod}             % needed for tikz externalization automation
\usepackage{pgf}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usetikzlibrary{external}
\tikzexternalize
\tikzset{external/figure list=true}
\tikzset{external/up to date check=simple} % manual up to date check with filemod
%%% tikz include command
\newcommand{\tikzcustomremake}[2]{%
  \tikzset{external/remake next}%
  \message{figure #2 was automatically rebuilt}%
}
\newcommand{\includetikz}[2]{%
  \tikzsetnextfilename{#1tikz_external/#2}%
  \filemodCmp{#1#2.tikz}{#1tikz_external/#2.pdf}%
    {\tikzcustomremake{#1}{#2}}% tikz is newer => remake
    {}%
  \input{#1#2.tikz}%
}

in your document:

%\tikzset{external/export=false}
\begin{figure}[htbp]
  \centering
  \small\includetikz{fig/matlab/}{plot5}
  \caption{title}
  \label{fig:tikz5}
\end{figure}