2

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}

1 Answers1

4

Converting my comment to an answer, and improving it a little:

Environments are defined, somewhat, like commands: they can take arguments or not.

\newenvironment{env-name}[<num of args>][<opt arg>]{<begin code>}{<end code>}

If the [<num of args] is set, the environment will expect an argument (or more). In your definition states the the environment should receive one mandatory argument (no optionals), therefore when you compile without the "empty string" {} an error rises, because the environment was expecting an argument and none was given.

Furthermore it's worth to note that the environments arguments are only valid on the <begin code> part, e.g.

\newenvironment{env-name}[1]{...}{something with #1}

won't work.