[Tex/LaTex] Draft mode in Tikz externalize hides graphics, but not without externalize

drafttikz-externaltikz-pgf

Maybe this is expected behaviour, but it's not documented in the manual, and it certainly surprised me.

When you use the draft class option and use the tikz external library, all Tikz pictures are replaced by a box with the name of the externalized graphic.

MWE:

\documentclass[draft]{article}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize
\begin{document}
\begin{tikzpicture}
  \draw (0,0) circle [radius=1];
\end{tikzpicture}
\end{document}

result with draft

The fact that it displays the correct filename leads me to think it's the external library doing this, because whilst I know that pgf itself does a similar thing with the draft option, I wouldn't expect it to know the filename (and anyway, I haven't loaded pgf directly, and I think it only obeys draft when called directly.)

However, commenting out the \tikzexternalize line gives the picture.

Result without draft

I'm not saying that this behaviour wouldn't often be useful! However, it's not what I'd prefer in what I'm working on, and as it isn't documented I don't know what I can do about it.

I can try and work around it, and not use draft but it's in a big document that already does several different things depending on whether draft is set.

What are my options?

Best Answer

Documentclass [draft] option:

Using the optional parameter [draft] to the \documentclass has two main uses:

  • indicate overfull hbox by a vertical bar in the margin
  • graphics that are included from external sources do not get displayed (only an outline of the box is displayed). A similar effect can be achieved using \usepackage[demo]{graphicx}.

This has no effect on the tikzpicture environment within a document as that is not considered an external graphic. Its content is typeset and displayed in the output, just as the content of any other environment would be.

TikZ Externalization Library:

The main purpose of \usetikzlibrary[external] and the associated \tikzexternalize is to speed up compile times by converting each tikzpicture into a separate external graphic which is then imported with \includegraphics. Hence, the behavior you are seeing.

This can be disabled on a per use basis by using \tikzexternaldisable within a group:

{\tikzexternaldisable% disable within a group
\begin{tikzpicture}
  ...
\end{tikzpicture}
}

or disabling externalization before the tikzpicture and re-enabling it following the picture:

\tikzexternaldisable% disable outside of a group
\begin{tikzpicture}
  ...
\end{tikzpicture}
\tikzexternalenable% should re-enable at end.

Disabling will probably be required for cases where there are nested tikzpicture environments.

The MWE example below illustrates these and yields:

enter image description here

Code:

\documentclass[draft]{article}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize
\begin{document}

{\tikzexternaldisable% disable within a group
\begin{tikzpicture}
  \draw [blue, ultra thick, fill=red!10] (0,0) circle [radius=1] node {1};
\end{tikzpicture}
}

\tikzexternaldisable% disbale outside of a group
\begin{tikzpicture}
  \draw [red, ultra thick, fill=blue!10] (0,0) circle [radius=1] node {2};
\end{tikzpicture}
\tikzexternalenable% should renable at end.

\begin{tikzpicture}
  \draw [black, ultra thick,fill=yellow!20] (0,0) circle [radius=1] node {3};
\end{tikzpicture}
\end{document}