Is there any way to get nestable block comments in LaTeX? I am using the comment package to get block comments, but nesting one comment inside another with \begin{comment}...\begin{comment}...\end{comment}...\end{comment} doesn't seem to work. (I get an error "\begin{document} ended by \end{comment}".)

When writing and revising papers, it would be useful to me to be able to comment out arbitrary blocks of text, even if they already have commented-out blocks within them.

I'm aware that there are editor-based solutions. What I'm asking is if there is a way to do this within the source of my LaTeX document.

  • 1
    I usually simply use CTRL+T in Texmaker and you can do nested comments with that.
    – azetina
    Mar 21 '14 at 19:45
  • Go to Edit menu. I'm almost sure that your editor has the capability of insert comments on the selected block of text.
    – Sigur
    Mar 21 '14 at 19:55
  • I'm aware that there are editor-based solutions (although I don't use Texmaker). What I'm asking is whether there is a way to do this from within the document source.
    – BrenBarn
    Mar 21 '14 at 20:03
  • 1
    @StevenB.Segletes: Yes. I edited my question to clarify. The point is that if I have Blah \begin{comment}blah blah\end{comment} more blah, I want to be able to wrap the entire thing in another comment, without worrying about whether it already has a comment inside it.
    – BrenBarn
    Mar 21 '14 at 20:15
  • 1
    comment is similar to verbatim: it ends as soon as the string \end{comment} is scanned. You can't nest comment environments.
    – egreg
    Mar 21 '14 at 20:24

I suggest a wrapper command, that uses the block which should be commented as an argument and 'drops it into the trash bin'.

\newcommand{\mycomment}[1]{% }%


Other stuff...

This works as long as the commented block follows correct LaTeX syntax, i.e. no unclosed right } or something similar, however, in such cases, compilation will fail eventually without the comment anyway.

  • 2
    Thanks, this seems to work. I wrapped it in an environment because I like to see the \end{mycomment} at the end of a long commented-out block so it reminds me that what is ending is a comment and not some other command.
    – BrenBarn
    Mar 22 '14 at 18:06
  • Using an environment as you stated was once also my way of doing comments, but it was tedious to write the end statement, so I switched to the command version.
    – user31729
    Mar 22 '14 at 22:03
  • +1 as nesting \comment{ \comment{ }} now works like a charm. I now stopped using \usepackage{comment} because it does not nest properly!
    – skvery
    Jun 3 at 13:23

I use \ifx ended by \fi. It searches for a token, that's what I don't want, therefore I use \ifx\relax. A tiny example illustrates it, it typesets a e.


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.