# How to turn off an environment (e.g. tikzpicture) for faster draft compiling

I'm currently writing my thesis and have been looking for ways to speed up compile time. I can shave off a lot of time by using draft mode so that images aren't processed, but I have several TikZ diagrams which also take some time to compile. Is there a way for me to put a switch in my header that can ignore processing the TikZ diagrams when I'm just checking my writing?

For example, I'd just like to 'turn off' the tikzpicture environment when compiling drafts.

• I recommend the TikZ library external: tikz-external – Qrrbrbirlbel Apr 30 '13 at 14:47
• Maybe this answer is of help: tex.stackexchange.com/a/37945/9517 – T. Verron Apr 30 '13 at 15:22
• You might also consider putting your TikZ pictures in separate TeX files with the standalone document class, and then including the pdf files as graphics. That way, draft mode will automatically omit them. – Charles Staats Apr 30 '13 at 15:26
• This question is essentially answered here: tex.stackexchange.com/a/60474/484 – Charles Staats Apr 30 '13 at 16:08
• @T.Verron: First of all, the question is not an exact duplicate; it just so happens that the answer there applies here. Second, if you eliminate the lines about drawing a box in the answer there, it should be adaptable to "turn off" any environment. – Charles Staats Apr 30 '13 at 16:27

Here is a solution that redefines the tikzpicture environment by conditionally wrapping a comment environment (from the verbatim package) around it. I've only tested it with tikzpicture but I think it could be easily adapted to any other environment.

Set the \newif called \showtikz to true (using \showtikztrue) or false (using \showtikzfalse) at the beginning of your document in order to show or hide all the tikzpicture environments in your document, respectively.

This solution by cmhughes to the question How to make LaTeX ignore the contents of an environment? was useful.

EDIT: I changed my \newif, originally \hidetikz, to new \showtikz; double negatives such as \hidetikzfalse are difficult to parse.

\documentclass{article}
\usepackage{tikz}
\usepackage{verbatim}   % for the comment environment
\usepackage{lipsum}

\newif\ifshowtikz
\showtikztrue
%\showtikzfalse   % <---- comment/uncomment that line

\let\oldtikzpicture\tikzpicture
\let\oldendtikzpicture\endtikzpicture

\renewenvironment{tikzpicture}{%
\ifshowtikz\expandafter\oldtikzpicture%
\else\comment%
\fi
}{%
\ifshowtikz\oldendtikzpicture%
\else\endcomment%
\fi
}

\begin{document}
\begin{tikzpicture}[scale=1]
\path[draw=red,fill=red!20] (0,0) rectangle (4,4);
\end{tikzpicture}

\lipsum[1]
\end{document}

If \showtikz is set to true, the output is:

If \showtikz is set to false, the output is:

• If you want this to work when options are given to \begin{tikzpicture} (e.g. [scale=2]), you need to put an \expandafter between \else and \oldtikzpicture in your redefinition of {tikzpicture}. – Philippe Goutet Apr 30 '13 at 17:03
• What if the OP wants the rest of the formatting (paragraphing, pagination) to be independent of whether the picture is present/absent? Probably not what he needs, but makes the question harder. Then you'd need to know the size of the picture. – Ethan Bolker Apr 30 '13 at 17:48
• @EthanBolker Well, that's not what I understand the OP wants, so I didn't consider that possibility at all. I don't see how that could be easily done, though. The size of the picture depends on the tikz commands inside it, so you'd probably have to somehow draw the complete picture anyway in order to get the correct size. That would not speed up the compilation, as required by the OP. – jub0bs Apr 30 '13 at 17:53
• Thank you, everyone! Re: Ethan's question, indeed, this isn't relevant for my current workflow, but it's an interesting question as well. Thanks to all for the very helpful discussion, solution, and references. – Flip Apr 30 '13 at 19:04

Similar to my answer in Simple way to switch inline asymptote figures on and off (with a slight modification), you can gobble the content of an environment using environ, and output whatever you want instead:

\documentclass{article}

\usepackage{tikz,environ}
\usepackage{lipsum}

%\newcounter{tikzfigcntr}
%\RenewEnviron{tikzpicture}[1][]{%
%  \par% New paragraph
%  \stepcounter{tikzfigcntr}% Step tikzfigcntr counter
%  This is \texttt{tikzpicture}~\thetikzfigcntr% Place appropriate text
%  \par% New paragraph
%}

\begin{document}

\begin{tikzpicture}[scale=1]
\path[draw=red,fill=red!20] (0,0) rectangle (4,4);
\end{tikzpicture}

\lipsum[1]

\end{document}

If you uncomment the tikzpicture redefinition (and counter definition), the replacement text "This is tikzpicture <num>" is printed (where <num> is a counter representing the picture number). Replace this with whatever you want:

• Does providing \env@tikzpicture@save@env and \env@tikzpicture@process have any effect in this? – Andreas Storvik Strauman Jun 13 at 8:07
• @AndreasStorvikStrauman: There was a bug in environ which required this to exist in order to \RenewEnviron an environment that wasn't originally defined with \NewEnviron. This was fixed in v0.3 (2015/05/04). I've updated the answer since it's not necessary anymore. – Werner Jun 13 at 19:53