19

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.

6
  • 1
    You might be interested in this: tex.stackexchange.com/questions/22269/… (or use ConTeXt;)).
    – mbork
    Sep 7, 2012 at 17:46
  • Thanks, I'm aware of them. As far as I can tell, they do not address the issue I'm trying to resolve. Sep 7, 2012 at 18:34
  • A have a vague memory that the answers package does store solutions in separate files…
    – cgnieder
    Sep 7, 2012 at 18:44
  • I think this a job for the answers package- have a look at the questions with the answers tag. With that in mind, I'd be tempted to call this a duplicate
    – cmhughes
    Sep 7, 2012 at 21:44
  • I recall doing exactly this a long time ago (in plain TeX), i.e., putting each exercise in a separate file, and I recall one thing about it: it was a bad idea (in terms of compilation speed). (Though hardware 10 years ago was a bit different, I admit.)
    – mbork
    Sep 8, 2012 at 6:54

2 Answers 2

12

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

3
  • @user1362373 you're welcome. And as I said in the answer: feedback on the package is highly welcome. So please don't hesitate me (via email) if you encounter any problems (or have suggestions…)
    – cgnieder
    Sep 8, 2012 at 14:27
  • Thank you! Your package works even if I define the exercises and sections in macros, unlike the other packages I tried. Bonus point for the simplicity of using built in sections, and the easy possibility of changing the environment names.
    – Rolf
    Mar 31 at 9:59
  • 1
    @Rolf exsheets is no longer developed, though. It has been superseded by xsim.
    – cgnieder
    Mar 31 at 10:10
3

You may want to look at toggle commands from the etoolbox package. For example you can set up something like this

\documentclass{article}
\usepackage{etoolbox}

\providetoggle{problem}
\toggletrue{problem}

\providetoggle{solution}
\togglefalse{solution}

\begin{document}

\input{problem_file}

\end{document}

The problem_file will contain something like

\iftoggle{problem}{Problem statement}{}
\iftoggle{solution}{Solution}{}
2
  • The comment package makes that even easier. Sep 7, 2012 at 20:31
  • Thanks, I'll take a look at the etoolbox package as well as the comment package. Sep 8, 2012 at 14:21

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.