1

I know we can comment out a block of text in Latex using \begin{comment} and \end{comment} commands. However, when I call this recursively (or in a nested fashion) as shown below, it fails:

\begin{comment} % block 1
some text
\begin{comment} % block 2
some text
\end{comment} % block 2
some text
\end{comment} % block 1

The above code should comment entire text but gives error. I need the above code for example, when I decide later to remove block 1 comment, but still want to keep block 2 commented.

This is just a simple example. We can as well have another block 3 within block 1 and then decide to keep block 3 commented but remove blocks 1 and 2.

5
  • 3
    You could simply make comment not see the \end{comment} of block 2 by changing it to something different of \end{comment} (\ end{comment}, for example). In fact, adding a comment at the end of the line, like in your example, is enough to make the \end{comment} unseen. I think this is much easier than trying to balance the \begins and \ends. In fact, balancing verbatim environments (such as comment) is usually a problem... Aug 27, 2019 at 23:27
  • Thank you @PhelypeOleinik, this information is also very useful!
    – omsrisagar
    Aug 28, 2019 at 17:37
  • @PhelypeOleinik I have just now tried your solution. Adding a comment at the end of \end{comment} does not seem to work. Also, changing to \ end{comment} gives error as there is no corresponding \end{comment} for \begin{comment}
    – omsrisagar
    Aug 28, 2019 at 17:46
  • The comment package (for any other package this may not apply) checks for a line containing exactly \end{comment}. Anything else will not be "seen" and will be considered part of the comment. Taking the example from your question: if you compile it, it will throw an error, will it not? Because neither \end{comment} is seen because of the comments at the end of the lines. If you remove the % block 1 comment from the \end, everything will be considered as a comment. Also, changing the block 2 \end to \ end will hide it and only the second \end will be seen. It should work... Aug 28, 2019 at 23:03

1 Answer 1

3

Here is a simple version of a comment environment that supports nesting:

\documentclass{article}

\makeatletter

\newcount\comment@nesting

\newenvironment{comment}{%
    \begingroup
    \global\comment@nesting=0
    \def\do##1{\catcode`##1=12\relax}%
    \dospecials
    \catcode`\^^M=\active
    \comment@processline
}{%
    \endgroup
}

\begingroup
\escapechar=-1
\xdef\comment@begincomment{\string\\begin\string\{comment\string\}}
\xdef\comment@endcomment{\string\\end\string\{comment\string\}}
\endgroup

\begingroup
\catcode`\^^M=\active
\def\@temp{\endgroup\def\comment@processline##1^^M}%
\@temp{%
    \def\comment@curline{#1}%
    \let\@next=\comment@processline
    \ifx\comment@curline\comment@endcomment
        \ifnum\comment@nesting=0
            \def\@next{\end{comment}}%
        \else
            \global\advance\comment@nesting by -1
        \fi
    \else
        \ifx\comment@curline\comment@begincomment
            \global\advance\comment@nesting by 1
        \fi
    \fi
    \@next
}

\makeatother

\parindent=0pt
\begin{document}

Some
\begin{comment}
simple comment
\end{comment}
text.

\begin{enumerate}
\item First
\item Second
\begin{comment}
\item Commented out
\end{comment}
\item Third
\end{enumerate}

More
\begin{comment}
simple comment
\begin{comment}
nested comment
\begin{comment}
even deeper nested comment
\end{comment}
\begin{comment}
another deeply nested comment
\end{comment}
some odd characters: _^$%&#
unbalanced braces: }}}}{{{
ignored end of comment:
  \end{comment}
\end{comment}
simple comment again
\end{comment}
text.
\end{document}

outputs

enter image description here

\begin{comment} starts a verbatim environment that processes the following code line by line. Each line contents is tested against the sequences \begin{comment} and \end{comment} to keep track of nesting in a global counter. All other lines are ignored completely.

2
  • Thank you @siracusa, this is what I was looking for!
    – omsrisagar
    Aug 28, 2019 at 17:36
  • May I suggest that you log a bugfix suggestion so that \usepackage{comment} can be updated.
    – skvery
    Jun 2, 2021 at 12:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .