I'm writing a simple package to be used for homework exercises written by me and my colleagues. Depending on the package option, the document should compile with or without the answers. I do this by (re)defining an environment called answer. As the answer often contains listings, I use the comment feature of the package verbatim as was advised here in SE and as shown below. Everything works fine unless the answer begins with some other environment. I have found a simple workaround: the MWE can be compiled by placing an empty string {} in the beginning of the answer environment, but I do not understand, why the MWE cannot be compiled without this empty string? The error I've got is
! File ended while scanning use of \verbatim@.
MWE:
\documentclass{article}
\usepackage{verbatim}
\newenvironment{answer}[1]{\comment}{\endcomment}
\begin{document}
Some text
\begin{answer} %{}
\begin{itemize}
\item a
\end{itemize}
\end{answer}
\end{document}
\expandaftertokens serve no purpose. You should consider thecommentpackage, though. – egreg Dec 15 '16 at 16:45