# Evaluating macros in answers package

I am using the answers package to create a list of exercises in a set of lecture notes and solutions to those exercises as files separate from the notes. I would like for the solution files to have the text of the corresponding question. I tried storing the text of the exercise and then presenting it in the solution, but it doesn't get filled in. For example,

\begin{exercise}
\def\problemtext{
Prove Proposition~\ref{prop:solnspan}.
}
\problemtext
\begin{solution}
\problemtext \\

\textbf{Solution}\\
(Solution goes here...)
\end{solution}
\end{exercise}


The corresponding .tex file for the solutions though literally contains \problemtext, not filled in with the corresponding text. This creates a bit of a problem since I build this TeX file separately from the notes.

• How about a complete, minimal example that replicates your setup, rather than some code snippet? – Werner Jun 21 '16 at 23:47
• Using the \Opensolutionfile and \Closesolutionfile macros this will work if \gdef\problemtext is used, but this is no good strategy, since it requires a new macroname for each problem text – user31729 Jun 22 '16 at 6:31

A solution with \@namedef and \@nameuse -- since the counters are not the same, there must be two macros for retrieving the problemtext (or a more or less sophisticated \let... statement depending on which environment is active at the moment!)

\documentclass{article}

\Newassociation{sol}{Solution}{ans}
\newtheorem{exercise}{Exercise}

\makeatletter
\newcommand{\storeproblemtext}[1]{%
\global\@namedef{problemtext\theexercise}{#1}%
}
\newcommand{\retrieveproblemtext}{%
\@nameuse{problemtext\theexercise}%
}

\newcommand{\solutionproblemtext}{%
\@nameuse{problemtext\theSolution}% Counter is named Solution
}
\makeatother

\begin{document}
\Opensolutionfile{ans}[ans1]

\begin{exercise}\label{prop:solnspan}
\storeproblemtext{%
Prove Proposition~\ref{prop:solnspan}.%
}
\retrieveproblemtext{1}%
\begin{sol}
Some text just for testing here:

\solutionproblemtext%

\textbf{Solution}\\
(Solution goes here...)
\end{sol}
\end{exercise}

\begin{exercise}\label{einstein}
\storeproblemtext{%
Prove $E=mc^2$.%
}

\retrieveproblemtext%
\begin{sol}
Some text just for testing here:

\solutionproblemtext%

\textbf{Solution}\\
(Solution goes here...)
\end{sol}
\end{exercise}

\Closesolutionfile{ans}

\input{ans1}
\end{document}


Update (Thanks to the suggestion of touhami!)

\documentclass{article}

\Newassociation{sol}{Solution}{ans}
\newtheorem{exercise}{Exercise}

\makeatletter
\newcommand{\storeproblemtext}[1]{%
\global\@namedef{problemtext\@currentlabel}{#1}%
}
\newcommand{\retrieveproblemtext}{%
\@nameuse{problemtext\@currentlabel}%
}

\makeatother

\begin{document}
\Opensolutionfile{ans}[ans1]

\begin{exercise}\label{prop:solnspan}
\storeproblemtext{%
Prove Proposition~\ref{prop:solnspan}.%
}
\retrieveproblemtext{1}%
\begin{sol}
Some text just for testing here:

\retrieveproblemtext%

\textbf{Solution}\\
(Solution goes here...)
\end{sol}
\end{exercise}

\begin{exercise}\label{einstein}
\storeproblemtext{%
Prove $E=mc^2$.%
}

\retrieveproblemtext%
\begin{sol}
Some text just for testing here:

\retrieveproblemtext%

\textbf{Solution}\\
(Solution goes here...)
\end{sol}
\end{exercise}

\Closesolutionfile{ans}

\input{ans1}
\end{document}

• if you really want to use one command you can use \Currentlabel or the internal \@currentlabel – touhami Jun 22 '16 at 7:33
• @touhami:I thought, this would not work because the Solution counter is only stepped and not refstepped, but this was an error! Thanks for the suggestion! – user31729 Jun 22 '16 at 8:20
• This is still doing the same thing as before. The TeX file that's generated for the solutions just says "\retrieveproblemtext" instead of the text of the problem. – cjohnson Jun 23 '16 at 14:32
• @cjohnson: Yes, but the situation is still better than before, you have at least now the text in the solution which was not the case in your -- er -- non - document ;-) I've not looked into answers.sty, but I assume that the solution stuff is just written verbatim to file, so hoping for expansion of macros is useless, in my point of view – user31729 Jun 23 '16 at 15:34