[Tex/LaTex] Bad interaction between comment package and enumerate environment

#enumerateerrors

I'm trying to use the comment package inside an enumerate environment, and it produces a compilation error. Minimal example:

\documentclass{article}
\usepackage{comment}

\specialcomment{com}{}{}
\excludecomment{com}

\begin{document}
\begin{enumerate}
\item Outside.
  \begin{com}
    Inside.
  \end{com}
\end{enumerate}
\end{document}

This produces the following error:

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

For reference, everything works fine without enumerate, as in the following example:

\documentclass{article}
\usepackage{comment}

\specialcomment{com}{}{}
\excludecomment{com}

\begin{document}
Outside.
\begin{com}
  Inside.
\end{com}
\end{document}

Is this a known problem? I'm having trouble finding anything about it ("comment" isn't exactly a Google-friendly term to search for…).

Best Answer

The comment package has an important limitation; quoting from the documentation

The opening and closing commands should appear on a line of their own. No starting spaces, nothing after it.

So there's not much more to say about it: don't indent the environment that you want to "comment out".

Here's a different implementation; I've left the same syntax for \specialcomment with three arguments, although nothing is done with the second and third (it can be improved for using them).

\documentclass{article}
\usepackage{environ,etoolbox}

\newcommand\specialcomment[3]{%
  \newtoggle{#1}\toggletrue{#1}
  \NewEnviron{#1}{\iftoggle{#1}{\BODY}{}}
}
\newcommand{\excludecomment}[1]{\togglefalse{#1}}
\newcommand{\includecomment}[1]{\toggletrue{#1}}

\specialcomment{com}{}{}
\excludecomment{com} 

\begin{document}
\begin{enumerate}
\item Outside.
  \begin{com}
    Inside.
  \end{com}
\item Outside. Inside.
\end{enumerate}
\end{document}
Related Question