4

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.

  • 2
    Welcome to TeX.SE. I think you want \renewenvironment{figure}{}{}. Alternatively you should consider using the draft option for the graphicx package: \usepackage[draft]{graphicx}. – Peter Grill Apr 6 '15 at 3:25
  • Don't use \begin{center} within a figure unless you specifically want additional vertical space. Try \centering instead. – cfr Apr 6 '15 at 3:48
3

The comment environment looks ahead for the end of the environment so it does not play well when used inside other environments.

The easiest way that I know to comment out a particular environment is to use the environ package:

\documentclass[12pt]{article}
\usepackage{mwe}
\usepackage{graphicx}
\usepackage{environ}
\RenewEnviron{figure}{Nothing here}
\begin{document}
  \begin{figure}
    \begin{center}
      \includegraphics{example-image.png}
    \end{center}
  \end{figure}
\end{document}

Using environ allows you to play with the \BODY of the environment directly. Above I have made the figure environment replace whatever is inside its body with Nothing here. Of course you could make the figure environment do nothing:

\RenewEnviron{figure}{}

Either way, the contents of the body of the figure environment don't seem to be processed so this should be fast.

  • @Andrew How would I apply this to remove equations defined by \[ ... \] ? – drake7707 May 16 '17 at 8:06
  • 1
    @drake7707 As \[...\] is not an environment you can't use the trick above for it. However, \def\[#1\]{ *snip* } will kill off an equation -- replace {*snip*} with whatever you want to appear, or with {} if you want nothing. – Andrew May 16 '17 at 10:49
2

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.

  • Thanks! The draft option was precisely what I was looking for. – Prahar Apr 6 '15 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.