[Tex/LaTex] Speed up the compilation of a document with a lot of tikzpictures made with pgfplots

compilingpgfplots

I have a document with a lot of tikzpicture made with pgfplots, most of them take data from files so the compilation may be very long. In order to have only a draft I would like to have blank images with same dimension of the full ones.

Until now I used a trick of having a tikzpicture with an empty axis environment of the same dimensions of the original one and with some if statements I included the blank or the full image like this:

\newcommand{\img}{Y} % immages Y(es) or N(o)
...
\begin{figure}
  \centering
  \ifthenelse{\equal{\img}{Y}}{%
    \input{Immagini/full.tex}}{%
    \input{Immagini/empty.tex}}
  \caption{caption}
\end{figure}

file full.tex

\begin{tikzpicture}
  \begin{axis}[height=9cm, width=.8\textwidth]
    \addplot table [x index=0, y index=2] {dati.dat};
  \end{axis}
\end{tikzpicture}

file empty.tex

\begin{tikzpicture}
  \begin{axis}[height=9cm, width=.8\textwidth]
  \end{axis}
\end{tikzpicture}

But today I read this question How I can speed up the compilation of a document with multiple images? and now I would like to know if there is a similar way to the draft option of the package graphicx that does the same thing with tikzpicture.

Best Answer

A part of the total solution is to use the external library as suggested and then put the option draft to \documentclass[draft]{whatever}. But doing so the image is replaced by a box of fixed dimensions, not equal to those of the real image.

\documentclass[a4paper,draft]{article}
\usepackage{pgfplots}
\usepackage{lipsum}
\usepackage{tikz}
\usetikzlibrary{external}

\tikzexternalize

\begin{document}

\begin{figure}[h]
  \centering
  \input{anderlini_10.tex}
  \caption{caption of this beautiful image}
\end{figure}

\lipsum[1-2]

\end{document}

enter image description here

This happens because the external image created is not imported with \includegraphics, to correct this it is sufficient to add this line (\pgfkeys{/pgf/images/include external/.code=\includegraphics{#1}}) before \tikzexternalize. In this way we obtain the desired result:

enter image description here