Macros Comments – Using \excludecomment and \newcommand

commentsmacros

I have a problem with \excludecomment from comment package and a \newcommand.

The following codes work good, which means, it can generate the desired pdf output.

\documentclass{article}

\usepackage{comment}
\excludecomment{dontshow}

\begin{document}
This is a test.

\begin{dontshow}
test test test
\end{dontshow}

\end{document}

However, if I add the following:

\newcommand{\noshow}[2]{#1=#2 \begin{dontshow}#2=#1\end{dontshow} #1=#2}
\noshow{abc}{def}

It generates an error:

(c:/texlive/2010/texmf-dist/tex/latex/comment/comment.sty
Excluding comment 'comment') Excluding comment 'dontshow' (./test.aux))
Runaway argument?
! File ended while scanning use of \next.
<inserted text> 
                \par 
<*> test.tex

? 

How can I use 'comment' in a \newcommand?

Best Answer

The environments of the comment package need to be on their own:

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

AFAIK they don't work in macros like yours because they skip everything verbatim. There you could use if-switches for most content (except for unbalanced if-switches).

\documentclass{article}

\newif\ifshow
%\showtrue
\showfalse

\usepackage{comment}
\ifshow
\includecomment{dontshow}
\else
\excludecomment{dontshow}
\fi

\newcommand{\noshow}[2]{#1=#2 \ifshow #2=#1 \fi #1=#2}

\begin{document}
This is a test.

\begin{dontshow}
test test test
\end{dontshow}

\noshow{abc}{def}

\end{document}