Production of solution sheets in L^{A}TEX 2ε

Copyright (C) 1994,1995 by Mike Piff

October 1, 1996

Contents

1 Introduction

This package is a modification of the author’s previous style option answers, which has been in use for a few years, and was based upon the TEXbook idea of binding solutions to exercises. I have taken the opportunity with this revision to alter the format of the solutions, so that they are now presented as L^{A}TEX environments rather than being started with a command and ended with the end of the surrounding environment, a wholly un-L^{A}TEXy way of doing things!

The other main change is that several file handles are allowed to be active at once. This allows some solutions in a book (for instance) to go to the appendices, and some to go to a separate file, to be printed and handed to the students as the course progresses. Moreover, the actual physical files opened with each file handle can now be varied in the same job, allowing many different files to be created according to the same format. Thus, for instance, each chapter of a book could create its own solution file, allowing the user to use \include on both chapters and solutions.

Finally, any number of solution-types may now be bound to any file, not just the two old ones, solution and hint. The format of each solution type is under the complete control of the user.

2 The documentation driver file

This is the driver file that produces this documentation. We use the document class provided by the L^{A}TEX 2ε distribution for producing the documentation.

1 ⟨∗driver⟩

 2 \documentclass{ltxdoc}
 3 \RecordChanges
 4 \begin{document}
 5 \DocInput{answers.dtx}
 6 \PrintIndex
 7 \PrintChanges
 8 \end{document}

9 ⟨/driver⟩

3 User interface

The package needs to be included with the command

             \usepackage[nosolutionfiles]{answers}

If the optional argument is given, solutions appear at that point in the text, rather than being written to external files. This allows a demonstrator’s version to be produced.

\Newassociation After that, there should be several declarations of the form

        \Newassociation{xxx}{yyy}{zzz}

where xxx is an environment in the document, and yyy is an environment which will surround the contents of xxx when it is written to symbolic file handle zzz. The names xxx, yyy and zzz should consist of letters only, not numbers, punctuation or spaces.

\solutionextension By default, output will go to zzz.tex if zzz is open. The command \solutionextension can be redefined to change tex to some other extension. Alternatively, the output filename can be changed as an optional parameter to

\Opensolutionfile, and each \Opensolutionfile on the same handle can use

a different physical file.

\Opensolutionfile At some point the user types

\Closesolutionfile
        \Opensolutionfile{zzz}
        ...
        \Closesolutionfile{zzz}

to create a file of solutions written by environments xxx to environments yyy. If this construction is used several times, then several files of solutions will be created. The user may wish these files to have different names. If the form

\Opensolutionfile{zzz}[www], then www.tex is used as actual file output name

rather than zzz.tex. This allows file handle zzz to create many files www.tex, say one for each chapter of a book, or one for each problem sheet. These could then be processed using \include commands. The same value of \solutionextension is used for the optional argument as for the main argument. The name www should follow the usual file naming conventions.

\Writetofile In addition, material can be written directly to a file by means of \Writetofile. Its first argument is the file handle zzz and its second is the line of text to be written. It is most important to remember that any control words in the line to be written should be preceded by \protect, otherwise the primitive TEX \write command will expand them. Also, as the argument is read in TEX’ usual way before being written, any trailing spaces after a control word will disappear unless precautions are taken. Thus, to write \xx yyy to the file, the user can type

\protect\xx\space yyy.

Filesave Alternatively, a block of text can be saved to file handle zzz by means of

        \begin{Filesave}{zzz}
           ....
        \end{Filesave}

around it once, zzz has been opened. The restrictions that apply to \Writetofile above do not apply to this environment.

\Readsolutionfile One of the generated files can be read using

        \Readsolutionfile{zzz}

provided the file has not been closed and re-opened. Alternatively, simply \input or \include it if preferred.

None of the file operations should have any effect if the file handle zzz has not been opened, or if nosolutionfiles is specified.

4 A simple example

Here is a straightforward example to illustrate how these macros are used.

10 ⟨∗ex1⟩

11 \documentclass[12pt,a4paper]{article}
12 \usepackage{answers}
13 \Newassociation{sol}{Solution}{ans}
14 \newtheorem{ex}{Exercise}
15 \begin{document}
16 \Opensolutionfile{ans}[ans1]
17 \section{Problems}
18 \begin{ex}
19 First exercise
20 \begin{sol}
21  First solution.
22 \end{sol}
23 \end{ex}
24 \begin{ex}
25 Second exercise
26 \begin{sol}
27  Second solution.
28 \end{sol}
29 \end{ex}
30 \Closesolutionfile{ans}
31 \section{Solutions}
32 \input{ans1}
33 \end{document}

34 ⟨/ex1⟩

5 A complicated example

The following is an (over-complicated) example of the use of package answers. It uses some of the refinements described later.

35 ⟨∗ex2⟩

36 \documentclass[12pt,a4paper]{article}
37 \usepackage{answers}%\usepackage[nosolutionfiles]{answers}

First an environment which contains problems and numbers them. This is based on a L^{A}TEX theorem, but with a roman body rather than italic.

38 \newtheorem{Exc}{Exercise}
39 \newenvironment{Ex}{\begin{Exc}\normalfont}{\end{Exc}}

Three sorts of solution are written to two different files. File handle test will contain the solutions and hints that the students will see; testtwo contains the solutions to the problems which they will probably hand in, and so these must be formatted separately.

40 \Newassociation{solution}{Soln}{test}
41 \Newassociation{hint}{Hint}{test}
42 \Newassociation{Solution}{sSol}{testtwo}

Because we want to mark different types of problem in the master file of problems, we define the following.

43 \newcommand{\prehint}{~[Hint]}
44 \newcommand{\presolution}{~[Solution]}
45 \newcommand{\preSolution}{~[Homework]}

We provide an extra parameter when we open file handle test; this is because we want to write a \section command to the solution file. This is merely an illustration here, but would be more relevant if the solution file were \included.

46 \newcommand{\Opentesthook}[2]%
47 {\Writetofile{#1}{\protect\section{#1: #2}}}

The default text produced when L^{A}TEXmeets the solution environments is here modified.

48 \renewcommand{\Solnlabel}[1]{\emph{Solution #1}}
49 \renewcommand{\Hintlabel}[1]{\emph{Hint #1}}
50 \renewcommand{\sSollabel}[1]{\emph{Solution to #1}}

51

52 \begin{document}

We open handle test as actual file test1.tex,

53 \Opensolutionfile{test}[ans2]{Solutions}

and write some text on it.

54 \Writetofile{test}{\protect\subsection{Some Solutions}}
Handle testtwo is opened as testtwo.tex.
55 \Opensolutionfile{testtwo}[ans2x]
56 \Writetofile{testtwo}{%
57  \protect\subsection{Extra Solutions}}

Now the problems.

58 \section{Exercises}
59 \begin{Ex}
60  An exercise with a solution.
61  \begin{solution}
62  This is a solution.
63  \relax{}
64  \end{solution}
65 \end{Ex}
66 \begin{Ex}
67  An exercise with a hint and a secret solution.
68  \begin{hint}
69  This is a hint.
70  \end{hint}
71  \begin{Solution}
72  This is a secret solution.
73  \end{Solution}

74 \end{Ex}
75 \begin{Ex}
76  An exercise with a hint.
77  \begin{hint}
78  This is a hint.
79  \end{hint}
80 \end{Ex}

We close the two solution files and immediately input their contents. We could have used \include here.

81 \Closesolutionfile{test}
82 \Readsolutionfile{test}
83 \clearpage
84 \Closesolutionfile{testtwo}
85 \Readsolutionfile{testtwo}
86 \end{document}

87 ⟨/ex2⟩

6 A further example

Here is an example suggested to the author by Martin Osborne to a situation not originally envisaged. Here, the exercises and solutions are not numbered; they are described.

88 ⟨∗ex3⟩

89 \documentclass[12pt,a4paper]{article}
90 \usepackage{answers}
91 \newenvironment{Ex}[1]{\begin{trivlist}\item \emph{#1} %
92 \renewcommand{\Currentlabel}{#1}}{\end{trivlist}}
93 \Newassociation{solution}{Soln}{solutions}

94

95 \renewenvironment{Soln}[1]{\begin{trivlist}\item
96 Solution to \emph{#1} }{\end{trivlist}}

97

98 \begin{document}
99 \section*{Problems}
100 \Opensolutionfile{solutions}[ans3]
101 \begin{Ex}{First exercise}
102  An exercise with a solution.
103  \begin{solution}
104  This is a solution.
105  \relax{}
106  \end{solution}
107 \end{Ex}
108 \begin{Ex}{Second exercise}
109  A second exercise with a solution.
110  \begin{solution}
111  This is another solution.
112  \end{solution}
113 \end{Ex}
114 \Closesolutionfile{solutions}
115 \section*{Solutions}
116 \Readsolutionfile{solutions}
117 \end{document}

118 ⟨/ex3⟩

7 Identification

This package can only be used with L^{A}TEX 2ε, so an appropriate message is displayed when another format is used.

119 ⟨∗answers⟩

120 \NeedsTeXFormat{LaTeX2e}[1996/06/01]

Announce the package name and its version:

121 \ProvidesPackage{answers}[1996/07/10]

And display it on the terminal (and the log file):

122 \typeout{Package `answers' <\filedate>.}
123 \typeout{\Copyright}

8 Options

There is a single option nosolutionfiles that switches output off to files and produces the solutions here-and-now.

124 \newif\ifanswerfiles \answerfilestrue
125 \DeclareOption{nosolutionfiles}{\answerfilesfalse
126 \typeout{No answer files being produced}}%
127 \ProcessOptions

128

As this package now relies heavily on the verbatim package, we ensure that that is loaded.

129 \RequirePackage{verbatim}

9 File handling

\solutionextension The default extension for solution files is defined here.

130 \newcommand{\solutionextension}{tex}

It may be changed with \renewcommand.

Filesave We define an environment Filesave with one parameter, the file handle. It is similar to the example of Schöpf in the description of verbatim.

131 \newenvironment{Filesave}[1]{%
132 \@bsphack
133 \def\verbatim@processline{}%
134 \Iffileundefined{#1}{}{%
135  \Ifopen{#1}{%
136  \def\verbatim@processline{%
137  \Ifanswerfiles{%
138  \immediate\write\@nameuse{#1@file}%
139  {\the\verbatim@line}%
140  }{}%

141 }%

142  }{}%

143 }%

144 \let\do\@makeother\dospecials
145 \catcode`\^^M\active \catcode`\^^I=12\relax
146 \verbatim@start
147 }{\@esphack}

\Writetofile It is also useful to have a command to write material to the fil