4

I am trying to use the package answers and found the answer to "Please explain \Newassociation and provide an overview of how the package answers works" very useful. However, when working with answers I ran into another problem. I am writing a book with chapters and at the end of each chapter I use the package answers to create problems and write solutions to a file. However, each chapter will have its own file with solutions and in the end of the book I have to input each of these in the correct order. So if I change the order of the chapters I have to change the order of the input commands. I would prefer to have all solutions in the same file, i.e. not use \Opensolutionfile, but instead an \Appendsolutionfile-command. Then I could just have one input-command in the solutions chapter. Is this possible?

  • If I understand you correctly, you want to preserve the order in which the solutions appear, coinciding with the chapters? – user31729 Jun 25 '14 at 11:54
  • Could you make a small example with a couple of chapters? If the solutions are to be input at the end of the document, there should be no problem in defining a final \inputsolutions macro that will input the solution files the right order. – egreg Jun 25 '14 at 20:19
5

This can be achieved by using chapter-specific solution files, here I used the name of the file, appended the chapter number, so each chapter writes to foo1.tex, foo2.tex etc. and afterwards, the command \CollectAllSolutions inputs all solution files in the order they have been written.

The writting is done automatically, by each chapter, in order to keep it simple for the user of such a feature (i.e. he/she does not have to specifiy the \Opensolutionfile/\Closesolutionfile each time a chapter starts. I have redefined the chapter start a little bit.

There are of course some things to make it more sophisticated, queries etc.

The 'example' exercises are taken and adapted from the answers documentation by J. Wright.

\documentclass[12pt,a4paper]{book}
\usepackage{answers}
\usepackage{forloop}
\usepackage{totcount}%
\Newassociation{sol}{Solution}{ans}
\newtheorem{ex}{Exercise}


\let\LaTeXStandardChapter\chapter
\makeatletter
\newcommand{\unstarredchapter@noopt}[1]{%
\unstarredchapter@opt{#1}%
}

\newcommand{\unstarredchapter@opt}[2][]{%
\LaTeXStandardChapter[#1]{#2}%
\Closesolutionfile{ans}
\Opensolutionfile{ans}[\jobname\number\value{chapter}]
}

\newcommand{\starredchapter}[1]{%
\LaTeXStandardChapter*[#1]%
}

\newcommand{\unstarredchapter}{%
\@ifnextchar[{\unstarredchapter@opt}{\unstarredchapter@noopt}
}%

\renewcommand{\chapter}{%
\@ifstar{\starredchapter}{\unstarredchapter}
}%
\makeatother

\newcounter{loopcounter}

\newcommand{\CollectAllSolutions}[1]{%
  \setcounter{loopcounter}{1}%
  \forloop{loopcounter}{1}{\number\value{loopcounter} < #1}{%
    \IfFileExists{\jobname\number\value{loopcounter}}{\input{\jobname\number\value{loopcounter}}}{}%
  }%

}%



\begin{document}

\newcounter{chaptercounter}%
\regtotcounter{chapter}%
\chapter{Problems}
\begin{ex}
First exercise
\begin{sol}
First solution.
\end{sol}
\end{ex}
\begin{ex}
Second exercise
\begin{sol}
Second solution.
\end{sol}
\end{ex}


\chapter{Problems 2}
\begin{ex}
Third exercise
\begin{sol}
Third solution.
\end{sol}
\end{ex}
\begin{ex}
Fourth exercise
\begin{sol}
Fourth solution.
\end{sol}
\end{ex}

\chapter{Solutions}

\CollectAllSolutions{\totvalue{chapter}}

\end{document}

enter image description here

1

Why use separate answer files for each chapter? The outline that follows is not a complete compilable MWE, but does work in my book project.

In the preamble:

\usepackage{answers}
\newcommand{\answerFileName}{allanswers}

\Newassociation{sol}{Solution}{\answerFileName}

\renewenvironment{Solution}[1]
{
\vspace{0.15in}\hrule\vspace{0.15in}
{\textbf{Exercise #1}}
\par 
}
{}


% in a macro called at the start of each chapter:
\Writetofile{\answerFileName}{%
\noexpand\chapter{\mychaptername}%
}
}

In main.tex:

\input{preamble}
\Opensolutionfile{\answerFileName}

%% \include various chapters

\Closesolutionfile{\answerFileName}

Then you can \include{\answerfilename} in your main document or (as I do) include it in a separate document for solutions. My solution document looks like this:

enter image description here

...

enter image description here

generated from chapter2.tex code

\begin{exx} Drive carefully!

Suppose you drive from Here to There at a speed of 30 miles/hour
and then drive back from There to Here at 60 miles/hour. 

What is your average speed?

\begin{hint}
\emph{Warning}: it's not 45 miles/hour.

The problem does not say how far it is from Here to There. You're free
to pick some convenient distance to work with if you like.
\end{hint}

\begin{sol}
Imagine that the trip from Here to There is 60 miles. Then it takes two
hours to get there and one hour to come back. That's three hours for
the 120 mile round trip, so the average speed is (120~miles)/(3~hours
40~miles/hour.

This conclusion surprises many people. You can see the idea even more
clearly if you imagine that it's possible to make the return trip at
infinite speed -- in no time at all. Then the average speed is
(120~miles)/(2~hours) = 60~miles/hour, which is \emph{not} the average
of 30 and infinity. 

\end{sol}

\end{exx}

The exx environment manages Exercise numbering. The hints appear at the end of the book, also done with the answers package.

  • This is also a good solution. I used another approach, which allows to omit some solutions (however, the code has still to be written) by specific chapters. – user31729 Jun 25 '14 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.