2

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.

2
  • 2
    How about a complete, minimal example that replicates your setup, rather than some code snippet?
    – Werner
    Commented Jun 21, 2016 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
    Commented Jun 22, 2016 at 6:31

1 Answer 1

2

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}

\usepackage{answers}

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

enter image description here

Update (Thanks to the suggestion of touhami!)

\documentclass{article}

\usepackage{answers}

\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}
4
  • 1
    if you really want to use one command you can use \Currentlabel or the internal \@currentlabel
    – touhami
    Commented Jun 22, 2016 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
    Commented Jun 22, 2016 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
    Commented Jun 23, 2016 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
    Commented Jun 23, 2016 at 15:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .