2

In my document, the comment environment works fine, but \comment and \endcomment stop processing of what comes next in the document:

\documentclass{article}
\usepackage{verbatim}
\begin{document}
X

\begin{comment}
1
\end{comment}

Y

\comment
2
\endcomment

Z
\end{document}

Why is that? Is that similar to End \verbatim command?

  • 1
    'Yes': this is verbatim-like – Joseph Wright Jun 20 '16 at 15:22
  • 3
    Never use the \foo...\endfoo commands in the body of a document. Ever. Is that clear? ;-) You immediately get into troubles if you do, not only for this kind of verbatim-like environments. – egreg Jun 20 '16 at 15:29
4

The comment environment is defined by the verbatim package as a variant of verbatim: instead of printing each line, it simply throws it away.

The package defines its verbatim-like environments by doing some steps.

First all special characters (well, almost all) become non special; then TeX is instructed to absorb one line at a time, checking whether it contains the string \end (with a non-special backslash). If it does, a further check is done, to see whether it is followed by the string {foo}, where foo is the name the current environment. If this string is not found, the line is treated like the ones not containing \end. Otherwise, the line is thrown away and the verbatim-like environment is finished up, executing the macro \endfoo and closing the group started by \begin{foo}.

Thus, when seeing \endcomment, the line is recognized to contain \end, but no {comment} string follows, so TeX continues its work processing line by line until finding \end{document}, which does satisfy the requirements, because the current environment is indeed document.


As a general rule, never use \foo...\endfoo in the document body. You'll get into troubles. Just for an easy example, try

\documentclass{article}
\begin{document}

\quote
Some text that should wrap around, let's type something long
enough. Hope this will suffice.
\endquote

\quote
Some text that should wrap around, let's type something long
enough. Hope this will suffice.
\endquote

\end{document}

Here's the (perhaps surprising) output:

enter image description here

Using \foo and \endfoo when defining environments is OK and, in some cases, mandatory. But use it with care.

| improve this answer | |

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.