# Disable/Enable Comment Block With Macro?

Is there a way to make a macro within a LaTeX document, which would enable or disable a comment block? I do this sort of thing in C and C++, but am not sure about LaTeX.

For example, near the top of the document I would like some sort of line(s) which have a 0 or a 1. I would change between the 0 and 1 at my discretion. If 0, then my later portion would look like:

\begin{comment}
\begin{figure}[ht]
\centering
\includegraphics[width=0.5\textwidth]{BLARG.jpg}
\caption{\label{fig:}HONK.}
\end{figure}
\FloatBarrier
\end{comment}


But if the macro number were 1, it would disable the comment function, so it would effectively leave the document with:

\begin{figure}[ht]
\centering
\includegraphics[width=0.5\textwidth]{BLARG.jpg}
\caption{\label{fig:}HONK.}
\end{figure}
\FloatBarrier


Is this possible?

• Hi, welcome to TeX.SE! I've taken the liberty to edit the formatting a little. In the future, you can simply highlight your code and press Ctrl+K so that the entire code appears in the grey box. – Troy Feb 15 '17 at 1:12
• You should consider using the comment package. – jon Feb 15 '17 at 1:56
• @DavidRobie: Your original question was closed as a duplicate of Commenting out large sections. Do you think your question is similar to that one and we should close your question as such? Alternatively or in addition to, do you have any feedback regarding my answer? – Werner Feb 15 '17 at 23:43
• @Werner: I think that the questions are essentially different. Your answer was fantastic, and works exactly how I hoped it would. Thank you very much! – David Robie Feb 16 '17 at 3:49

Yes... if you write your own comment environment using environ:

\documentclass{article}

\usepackage{environ,graphicx}

\NewEnviron{comment}{%
\ifnum\keepcomment=1
\else
\BODY
\fi
}

%\newcommand{\keepcomment}{1}% Keep comment
\newcommand{\keepcomment}{0}% Remove comment

\begin{document}

Some text before the figure.

\begin{comment}
\begin{figure}[ht]
\centering
\includegraphics[width=0.5\textwidth]{example-image}
\caption{\label{fig:example}Example image.}
\end{figure}
\end{comment}

Some text after the figure.

\end{document}


If you're using the comment package you can do something similar:

\usepackage{comment}

%\newcommand{\keepcomment}{1}% Keep comment
\newcommand{\keepcomment}{0}% Remove comment

\AtBeginDocument{\ifnum\keepcomment=1
\excludecomment{comment}
\else
\includecomment{comment}
\fi}

• Please. We already have tex.stackexchange.com/q/17816/5763 - this question is clearly a duplicate. – Reinstate Monica - M. Schröder Feb 15 '17 at 2:31
• @MartinSchröder: I disagree. This is not about commenting out sections in general, but about providing a flag (in macro form) that can be used as a switch. – Werner Feb 15 '17 at 5:40
• @MartinSchröder: As the person who posed the question, I would also disagree. The one you refer to is asking for how to use \begin{comment}, but I would like to have a control sequence at the top of the document which enables/disables all instances of \begin{comment} so that I can switch all commented sections on/off, while retaining the two lines to make the comment block work, and without manually changing each one to enable/disable it. Werner: Thanks for the answer friend! This looks like the sort of thing I wanted! – David Robie Feb 16 '17 at 3:42