# Problem with the answers package.

I am using the answers package to build a series of exercises. The problem i have encountered is when going from

\item $5x^2-x \geq 0$
\begin{sol}
$x \geq 1, x \leq 0$
\end{sol}


to

\uppg{$x<-2, x>2$}{$-9 < x < 9$}


Where uppg looks like

\newcommand{\uppg}[2]
{
\item  #1
\begin{sol}
\#2
\end{sol}
}


I keep getting errors because it reads the \begin{sol} in uppg but ignores the \end{sol} and just keeps reading in the file until it realizes it's misstakes and throws an error. How can i resolve this?

-
Welcome to TeX.sx! You don't have to sign with your name since it automatically appears in the lower right corner of your post. – przemoc Jul 14 '11 at 8:59

The answers package uses a verbatim-like approach to collect up material. As a result, you cannot hide the \end{sol} part inside a macro: it has to be in the source as written.

-

If Joseph is right and it is verbatim-like approach then that is the problem

How \begin{verbatim} works. briefly and roughly.

1. \begin{verbatim} is expanded to \verbatim.
2. Then \verbatim sets category code of each special characters to 12. Now all chars is like digits or puncts.
3. Then \verbatim sets font, parindent and calls \@xverbatim.
4. \@xverbatim catches the end of verbatim using the following trick:

\def\@xverbatim#1\end{#1\end}

5. Then \end{verbatim} finishes work.

How newcommand{\uppg}[1]{\begin{verbatim}#1\end{verbatim}} works.

1. \uppg{$x$} expands to \begin{verbatim}$x$\end{verbatim}}.
2. Then \begin{verbatim} expands to \varbatim. \varbatim changes all categories and font.
3. Then \verbatim calls \@xverbatim.
4. \@xverbatim tries to catch your argument using the following trick:

\def\@xverbatim#1\end{#1\end}


but it is impossible because of \@xverbatim tries to catch \end where all letters (\,e,n,d) have the category 12 and 11. But in fact there are only \ec exsits.

5. \@xverbatim is trying and trying to find \end where backslash (\) has category 12 but.... File ended while scanning use of \@xverbatim
-
Is there any was around this, to avoid having to repetedly add four lines of code for each assignment, instead of the above \newcommand approach? – Mats Jul 14 '11 at 11:46