[Tex/LaTex] Commenting out specific environments

commentsfloats

I'm working on a large document which contains several figures. Due to the figures it takes a long time to compile.

I would like to comment out all the figures while I'm working and then put them back in at the end. To do this, I added something like

\renewenvironment{figure}{\begin{comment}}{\end{comment}}

which I could then comment out when needed. However, this just doesn't work and gives all sorts of problems. Specifically, I tried to compile this –

\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{comment}
%\renewenvironment{figure}{\begin{comment}}{\end{comment}}
\begin{document}
\begin{figure}
\begin{center}
\includegraphics{image.png}
\end{center}
\end{figure}
\end{document}

With the % everything compiled perfectly. If I remove the %, I get an error

Excluding 'comment' comment.)
Runaway argument?
! File ended while scanning use of \next.

Best Answer

If you only include stand-alone images, then you can easily "turn of" the \includegraphics command by loading the document class (or graphicx) with the draft option:

enter image description here

\documentclass[draft]{article}
\usepackage{graphicx}
\begin{document}
Image A: \includegraphics[height=2\baselineskip]{example-image-a}

Image B: \includegraphics[height=2\baselineskip]{example-image-b}

Image C: \includegraphics[height=2\baselineskip]{example-image-c}
\end{document}

A similar approach would be to redefine \includegraphics to do whatever you want:

enter image description here

\documentclass{article}
\usepackage{graphicx}
\renewcommand{\includegraphics}[2][]{\rule{50pt}{20pt}}
\begin{document}
Image A: \includegraphics[height=2\baselineskip]{example-image-a}

Image B: \includegraphics[height=2\baselineskip]{example-image-b}

Image C: \includegraphics[height=2\baselineskip]{example-image-c}
\end{document}

However, if you create your graphics on-the-fly (using something like a tikzpicture or pspicture), then you need to be more creative. One way to achieve this would be to capture the entire environment and gobble its contents:

enter image description here

\documentclass{article}
\usepackage{tikz,environ}
\RenewEnviron{tikzpicture}[1][]{}
\begin{document}
Image A: \begin{tikzpicture}[options-a] A beautiful image \end{tikzpicture}

Image B: \begin{tikzpicture}[options-b] A beautiful image \end{tikzpicture}

Image C: \begin{tikzpicture}[options-c] A beautiful image \end{tikzpicture}
\end{document}

The same could be done using the figure (float) environment, but then you'd lose all references to \caption (however, you could consider it).

The environment-gobble mechanism could be made more advanced by actually writing the picture environment inside \phantom, thereby creating the required size/shape of the output. However, this would incur the same amount of compilation overhead/time.