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!

1

3 Answers 3

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
}
2
  • 1. 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, 2013 at 6: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, 2013 at 10:07
3

This is very similar to Bordaigorl's answer, but definitions are in a more readable form.

I used \@namedef instead of \expandafter\def\csname ...\endcsname and \@nameuse instead of \csname ...\endcsname

\documentclass{article}

\makeatletter
\newcommand{\problem}[2]{%
    \@namedef{#1}{#2}%
    #2%
}
\newcommand{\solution}[1]{%
    \@nameuse{#1}%
}
\makeatother

\begin{document}

\section*{Part I}

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

\section*{Part II}

\solution{prob33}

\end{document} 

enter image description here

2
  • 1. Do \@namedef and \@nameuse allow for 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 faster than exsheet package?
    – Pygmalion
    Sep 20, 2013 at 7:53
  • 1. \@namedef expands to \expandafter\def\csname ...\endcsname and \@nameuse expands to \csname ...\endcsname, so the answer is the same as the one given by Bordaigorl, that is: yes. 2. I have no idea, I'm sorry. Sep 20, 2013 at 10:47
1

This alternative utilizes prob environment associated with section counter. The problem statement is repeated in the solution via \@namedef and \@nameuse

 \documentclass[a4paper]{article}
 \usepackage[margin=1in]{geometry}

 \newtheorem{prob}{Problem}[section]

 \makeatletter
 \newcommand{\problem}[1]{%
 \begin{prob}
 \par #1 \par
 \end{prob}
 \@namedef{\theprob}{#1}
 }

 \newcommand{\solution}[2]{%
 \vspace*{1cm}   % adjust for distance between solutions
 \noindent\textbf{Problem #1:} \@nameuse{#1} \par
 \noindent\texttt{\bf Solution #1:} #2  
 }
 \makeatother

 \begin{document}

 \section*{Part A}

 \section{Section I}

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

 \problem{This is the second problem to find the speed of the car...}

 \section{Section II}

 \problem{This is the thrid problem in section II.}

 \section*{Part B}

 \solution{1.1}{This is the solution for airplane. }

 \solution{1.2}{This is the solution for the second problem. }

 \solution{2.1}{This is the solution for the third problem.}
 \end{document} 

enter image description here

You must log in to answer this question.

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