joulev
Currently, I want to have a pair of command, say \commentfoo and \uncommentfoo which work like this

tex
\documentclass{article}
\usepackage{enumitem}
\newlist{foo}{enumerate}{1}
% Do something with \commentfoo and \uncommentfoo
\begin{document}
% This should be shown
\begin{foo}
\item a
\item b
\end{foo}
\commentfoo
% This should be hidden, i.e. comment out
\begin{foo}
\item a
\item b
\end{foo}
\uncommentfoo
% This should be shown again
\begin{foo}
\item a
\item b
\end{foo}
\end{document}


So far I have used comment's commands, and it works quite well for "normal" environments:

tex
\documentclass{article}
\usepackage{comment}
\newenvironment{foo}{}{}
\def\commentfoo{\excludecomment{foo}}
\def\uncommentfoo{\includecomment{foo}}
\begin{document}
\begin{foo}
This should be shown 1
\end{foo}
\commentfoo
\begin{foo}
This should be hidden
\end{foo}
\uncommentfoo
\begin{foo}
This should be shown 2
\end{foo}
\end{document}


But for list environments, it fails

tex
\documentclass{article}
\usepackage{enumitem}
\usepackage{comment}
\newlist{foo}{enumerate}{2}
\setlist[foo]{label=\arabic*.}
\def\commentfoo{\excludecomment{foo}}
\def\uncommentfoo{\includecomment{foo}}
\begin{document}
\begin{foo}
\item This should be shown 1
\end{foo}
% works until this point
\commentfoo
\begin{foo}
\item This should be hidden
\end{foo}
\uncommentfoo
\begin{foo}
\item This should be shown 2
\end{foo}
\end{document}


How to implement \commentfoo and \uncommentfoo that works for list environments?
Skillmon
The problem is that the comment package doesn't seem to alter the \endfoo macro. The following does \let the \endfoo to \empty so that it doesn't call the enumitem internals which lead to the error. Also we have to restore \foo and \endfoo in \uncommentfoo, for this we again just use \let and don't let the comment package do this job (it will not restore the list behaviour, and then we'd get some Lonely \item errors).

tex
\documentclass{article}

\usepackage{enumitem}
\usepackage{comment}
\newlist{foo}{enumerate}{2}
\setlist[foo]{label=\arabic*.}

% save the definitions of \foo as a list
\let\fooList\foo
\let\endfooList\endfoo

% clear \endfoo and set up \foo to start the comment behaviour
\def\commentfoo{\let\endfoo\empty\excludecomment{foo}}
% restore both \foo and \endfoo
\def\uncommentfoo{\let\foo\fooList\let\endfoo\endfooList}

\begin{document}
\begin{foo}
\item This should be shown 1
\end{foo}
% works until this point and even works beyond
\commentfoo
\begin{foo}
\item This should be hidden
\end{foo}
\uncommentfoo
\begin{foo}
\item This should be shown 2
\end{foo}
\end{document}

Skillmon
Since the original answer has the problem that comment requires the environment's \end{foo} to be completely on its own in a line without anything preceding it and anything following it (including spaces), the following is a new answer, that should work (almost) arbitrarily.

It utilizes the parser module of pgf to gobble everything regardless of braces. It even tries to locally change the category code of % to ignore comments, but this will only work if it's not in an argument of another macro (so that the category change does apply). As long as you don't use %\end{foo} to end the environment this limitation doesn't really matter, though.


\documentclass{article}

\usepackage{enumitem}
\newlist{foo}{enumerate}{2}
\setlist[foo]{label=\arabic*.}

\usepackage{pgf}
\usepgfmodule{parser}

% save the definitions of \foo as a list
\let\fooList\foo
\let\endfooList\endfoo

% clear \endfoo and set up \foo to start the comment behaviour
\def\commentfoo
{%
\let\endfoo\empty
\def\foo
{%
% set up a parser rule for \end, which grabs an argument and compares
% that argument to the string 'foo', if they match, end the parser
\pgfparserdef{commentfoo}{initial}\end[m]
{%
\def\tmpA{####1}\def\tmpB{foo}%
\ifx\tmpA\tmpB
\pgfparserswitch{final}%
\fi
}%
% need to formally \end the environment, so do this after the parser is
% done
\pgfparserdeffinal{commentfoo}{\end{foo}}%
% ignore input for which no parser rule is defined
\pgfparserset{commentfoo/silent=true}%
\catcode\%=12
\pgfparserparse{commentfoo}%
}%
}
% restore both \foo and \endfoo
\def\uncommentfoo{\let\foo\fooList\let\endfoo\endfooList}

\begin{document}
\begin{foo}
\item This should be shown 1
\end{foo}
% works until this point and even works beyond.
\commentfoo
\begin{foo}\item This should be hidden\end{foo}This should be shown because it's
not inside of \texttt{foo}.
\uncommentfoo
\begin{foo}
\item This should be shown 2
\end{foo}
\commentfoo
\begin{foo}THis}is an{ unbalanced token list that's completely #gobbled\end{foo}
\end{document}

CrazyHorse
Use \iffalse  ... \fi:


\documentclass{article}
\usepackage{enumitem}
\newlist{foo}{enumerate}{2}
\setlist[foo]{label=\arabic*.}
\let\commentfoo\iffalse
%\let\commentfoo\iftrue % for printing
\let\uncommentfoo\fi
\begin{document}
\begin{foo}
\item a
\item b
\end{foo}
\commentfoo
% This should be hidden, i.e. comment out
\begin{foo}
\item a
\item b
\end{foo}
\uncommentfoo
\begin{foo}
\item a
\item b
\end{foo}
\end{document}


However, instaed of defining the two macros you can simply use


\iffalse
% This is hidden, i.e. comment out
% With \iftrue it is active
\begin{foo}
\item a
\item b
\end{foo}
\fi
`

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.