7

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.

BrenBarn
  • 212
  • 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
  • What does a nested comment mean? Isn't the inner text already a comment? – Steven B. Segletes Mar 21 '14 at 20:12
  • 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
  • By comment, do you just mean "to go away from the input" or "to go away from the output"? Also, must it rely on the comment package, or are other solutions viable? – Steven B. Segletes Mar 21 '14 at 20:18
  • 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
  • @StevenB.Segletes: Just to go away from the output. It doesn't need to depend on the comment package, although I would prefer a drop-in solution (i.e., another package) rather than a bit of code that I have to copy and paste around myself. – BrenBarn Mar 21 '14 at 20:33
  • @egreg: Yes, I figured that out. The question is, what can I do instead of that to get nested comments? – BrenBarn Mar 21 '14 at 20:34

2 Answers2

5

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]{% }%

\mycomment{%

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. –  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 03 '21 at 13:23
0

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.

\documentclass[a4paper]{article}
\begin{document}
a
\ifx\relax
b
\ifx\relax
c
\fi
d
\fi
e
\end{document}
Malipivo
  • 13,287