Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
}

###

\assignment{question}{answer}

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

2 Answers 2

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:

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



\assignment{question}{answer}

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

 
discard

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.