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
    The two \expandafter tokens serve no purpose. You should consider the comment package, though. – egreg Dec 15 '16 at 16:45
  • @GuilhermeZanotelli, thank you very much, that solved the problem! Please convert your comment into an answer so I could accept it. – Oleg Soloviev Dec 15 '16 at 16:51
  • @egreg Yes, you're right. I put them here following this post. I remove them from my question as not relevant. – Oleg Soloviev Dec 16 '16 at 9:20
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.