Why does starting a %-type comment after "\end{comment}" cause problems?

Assuming the tex file is called main.tex, the input lines

...
\end{comment}  %comment begins
...
\end{document}

generate the following, somewhat incomprehensible prompt:

Runaway argument?
! File ended while scanning use of \next.
<inserted text> 
                \par 
<*> main.tex
            
?

What has happened is that LaTeX has gotten to the end of the file without ever "seeing" \end{comment} and , by extension, \end{document}. What's going on?

By design, a comment environment is allowed to contain pretty much any arbitrary material, including code that could change the meaning, i.e., the so-called category code, of \ (backslash), %, (whitespace), {, }, and all other characters, whether or not these characters are "TeX-special" by default. Thus, in order to determine when and where the comment material ends, one mustn't rely on TeX's usual input processing subsystem, which does assume that \, {, }, %, etc have certain meanings. Instead, some fairly stringent formal requirements have to be imposed on the literal contents of the input string. The author of the comment package has decided that the termination marker must consist of the literal string

\end{comment}

and nothing else; i.e., nothing else is allowed to be on that input line, either before or after \end{comment}.

I suppose that one could point out that this requirement represents a sufficient condition for deciding where the comment material ends, and that a necessary condition could be formulated in a less stringent way, e.g., to allow a %-type comment to occur on the same line as \end{comment}. It's quite likely that one could come up with such a weaker necessary condition. Clearly, though, the author of the comment package decided that this wasn't necessary (pun intended).

Tags:

Comments