# Problem putting answers in a macro

I want to put the \begin{sol} and \end{sol} from the answers package in a macro like

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

###



It's just that answers use a verbatim environment that makes it miss the \end{sol} if it isn't written in the 'main' function. Is there any way to use the \expandafter,\csname or such commands to let it just print the \begin{sol} #2 \end{sol} and then expand it after it has been printed in the main document?

-
Welcome to TeX.sx! A tip: If you indent lines by 4 spaces, then they are marked as a code sample. You can also highlight the code and click the "code" button (with "{}" on it). You can also use backticks  to mark your inline code as I did in my edit. –  egreg Jul 29 '11 at 9:10

You can try

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

-
Thanks, Worked like a charm! –  Mats Jul 29 '11 at 9:55

You can't use verbatim in macro arguments. I would make assignment an environment with one argument for the item instead. Alternatively you can use the following trick to place \end{sol} automatically after the } behind the answer:

\newcommand{\assignment}[1]{%
\item #1
\begin{sol}
\def\endassignment{\end{sol}}%
\bgroup
\aftergroup\endassignment
\let\dummy=%
}


Here the { in front of the answer is removed, so the } will match the internal \bgroup. Then \aftergroup is used to insert the \end{sol} afterwards.
(Using \sol and \endsol` instead would be simpler, but I don't know if this environment relies on the extra group. Having to add it manually wouldn't make the code shorter.)