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

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:

enter image description here

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

enter image description here


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:

enter image description here

\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:

enter image description here