[Tex/LaTex] Problem/solution environment

packagesprobsoln

I'm writing a textbook and an accompanying solution manual. In the book, I have a series of exercises at the end of each chapter. As the number of chapters and exercises grows, I'm getting to the point where it's easy to get the ordering of the exercises and solutions mixed up. So what I'm looking for is a simple but effective way to always have them in a consistent order.

What I thought about, but don't know to implement (yet), is that each exercise and the corresponding solution would be located in one tex file. I could then \input that file in the book and the manual in the appropriate location, and (through an as of yet undetermined mechanism), only the exercise or the solution would be input. I also thought that I could create a separate "master" tex file for each chapter that in turn inputs the exercise/solution tex files. Then I simply input that "master" tex file in the book and the manual. In this way, it would be impossible to get the ordering mixed up.

In UNIX, I guess one way of accomplishing this would be true environment variables, but I don't know whether this is possible in LaTeX and whether this would be sensible.

Thanks in advance for criticisms, ideas, feedback.

Best Answer

Here is a possibility (and I really don't like the fact that this - again - seems like an advertisement): I have a package exsheets which I haven't uploadad to CTAN yet as it still requires some testing (feedback highly welcome). With it the task at hand seems fairly easy.

You create a separate tex file for each chapter that contains both exercises and solutions. In these files a solution is written directly after the exercise it belongs to.

These files are then either input at the appropriate points in each chapter of the main book or in the document providing the solutions. The choice if exercises or solutions are printed is made through a package option.

The main file would look for example like this:

\documentclass{book}
\usepackage{exsheets}

\SetupExSheets{
  counter-format = ch.qu ,
  counter-within = chapter
}

\begin{document}
\chapter{One}
\section{Exercises}
\input{\jobname-exercises-one}

\chapter{Two}
\section{Exercises}
\input{\jobname-exercises-two}
\end{document}

enter image description here

The files for the exercises look like this:

% this is \jobname-exercises-one.tex
\begin{question}
 The first exercise in chapter one.
\end{question}
\begin{solution}
 The answer to the first exercise in chapter one.
\end{solution}
\begin{question}
 The second exercise in chapter one.
\end{question}
\begin{solution}
 The answer to the second exercise in chapter one.
\end{solution}

The second file:

% this is \jobname-exercises-two.tex
\begin{question}
 The first exercise in chapter two.
\end{question}
\begin{solution}
 The answer to the first exercise in chapter two.
\end{solution}
\begin{question}
 The second exercise in chapter two.
\end{question}
\begin{solution}
 The answer to the second exercise in chapter two.
\end{solution}

A suiting solutions document now could look as follows:

\documentclass{article}
\usepackage{exsheets}

\SetupExSheets{
  question/print = false ,
  solution/print = true ,
  counter-format = se.qu ,
  counter-within = section
}

\makeatletter
\@addtoreset{question}{section}
\makeatother

\begin{document}
\section{Solutions to chapter one}
\input{\jobname-exercises-one}

\section{Solutions to chapter one}
\input{\jobname-exercises-two}

\end{document}

enter image description here