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
    Commented Jun 25, 2014 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
    Commented Jun 25, 2014 at 20:19

2 Answers 2


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.








  \forloop{loopcounter}{1}{\number\value{loopcounter} < #1}{%



First exercise
First solution.
Second exercise
Second solution.

\chapter{Problems 2}
Third exercise
Third solution.
Fourth exercise
Fourth solution.




enter image description here


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:



{\textbf{Exercise #1}}

% in a macro called at the start of each chapter:

In main.tex:


%% \include various chapters


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?

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

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

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. 



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
    Commented Jun 25, 2014 at 16:02

You must log in to answer this question.

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