TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

\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?

share|improve this question
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
up vote 4 down vote accepted

You can try

\newcommand{\assignment}[2]{\item #1\scantokens{\begin{sol}#2\end{sol}}}
share|improve this answer
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:

   \item #1


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.)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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