3

I am writing problems-solutions book. I have decided that problems and solutions shall be separated; first half of the book having problems and the second half solutions. But I need the text of the problem in the solution part too.

Eg.


PART I

Problem 3.3: Find the speed of the airplane...


PART II

Problem 3.3: Find the speed of the airplane...

Solution to the problem goes as...


I am thinking of a command of type

\problem{prob33}{Find the speed of the airplane...}

\solution{prob33}

So the idea is that the command \solution would be somehow connected to the command \problem and invoking \solution{prob33} would again print the text "Find the speed of the airplane...", which was defined by \problem.

Is there any good solution for my problem?

Thanks for your help!

Pygmalion
  • 6,387
  • 4
  • 34
  • 68

3 Answers3

4

For problems and solution there are two excellent packages as suggested by @Thorsten: exsheets (See Section 6 of manual) and probsoln.

However if you want to bake your own version or you are still curious about how this can be achieved here's one option.

You can define a macro that displays your problem while storing the text in a macro at the same time:

\newcommand{\problem}[2]{%
    \expandafter\def\csname #1\endcsname{#2}%
    #2 %
}

the command \problem{p1}{bla} will then call \expandafter\def\csname #1\endcsname{#2} which in turn rewrites to \def\p1{bla} so that you can call \p1 when you need to print that problem again. The magic is done by \csname<something>\endcsname that creates a token \<something> and allows you to call/define macro names computed on the fly. Note that when you create macros from parameters like this it's safer to use prefixes to avoid clashes with already defined macros (e.g. with \problem{emph}{bla}).

Now the \solution macro is easy to define:

\newcommand{\solution}[2]{
    Recall the problem:
    \csname #1\endcsname \par
    \textbf{Solution:} #2 \par
}

For a better solution you may also introduce counters for proper handling of cross-references:

\newcounter{problem}
\newcommand{\problem}[2]{%
    \refstepcounter{problem}
    \expandafter\def\csname problem#1\endcsname{#2}%
    \textbf{Problem \theproblem:} #2 \par
    \label{probl:#1}
}

\newcommand{\solution}[2]{
    Recall problem~\ref{probl:#1}:
    \csname problem#1\endcsname \par
    \textbf{Solution:} #2 \par
}
Bordaigorl
  • 15,135
  • So this \csname \endcsname enables to create macro names that include numbers and other special characters? 2. I generally prefer baking my own solutions instead of calling whole packages. But I am extremely concerned about speed of compilation due to 200+ pages document. Is that self-baked solution the fastest one?
  • – Pygmalion Sep 20 '13 at 06:59
  • yes this is compatible with problem names containing numbers and punctuation as in \problem{prob1.4-bis'}. It should be pretty fast as well. However if you are desperate for speed, maybe saving each problem (and solution) in a separate file and then using \input would achieve the maximum performance (no extra memory used). This can however get very cumbersome very soon. – Bordaigorl Sep 20 '13 at 10:07