summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/answers/answers.dtx
blob: 9d5e9d9e30e1186c4e98b31fccddbff682637660 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
%    \iffalse meta-comment
%
% Originally developed by Mike Piff 
% Copyright (C) 1990,1994-1996 by Mike Piff   
% Copyright (C) 2009-2010,2014 by
%   Joseph Wright <joseph.wright@morningstar2.co.uk>
%
% This file may be distributed and/or modified under the conditions of
% the LaTeX Project Public License (LPPL), either version 1.3c of
% this license or (at your option) any later version.  The latest
% version of this license is in the file:
%
%   http://www.latex-project.org/lppl.txt
%
% This work is "maintained" (as per LPPL maintenance status) by
%   Joseph Wright.
%
%    \fi
%
%    \CheckSum{267}
%
% \changes{2.0}{1994/03/25}{First version for LaTeX2e}
% \changes{2.01}{1994/03/30}{Whoops! |ProvidesPackage{answers}|,
%           not |ProvidesClass{Answers}|!!}
% \changes{2.02}{1995/08/31}{Removed .drv file from installation}
% \changes{2.03}{1995/08/31}{Allowed a file handle to open
%           many files successively, using an optional parameter.}
% \changes{2.04}{1995/09/08}{Option nosolutionfiles added to allow
%           solutions to optionally follow problems for tutor's use.
%           Several minor changes to aid flexibility}
% \changes{2.05}{1995/09/11}{Better documentation of features added}
% \changes{2.06}{1996/01/05}{More documentation of features added,
%           as suggested by David Epstein.}
% \changes{2.07}{1996/01/08}{Conditionals to check if files were open
%           were not changed globally. (Spotted by David Epstein.)}
% \changes{2.08}{1996/01/11}{More examples added.}
% \changes{2.09}{1996/07/10}{Bug spotted by David Epstein;
%           I had missed out the definition of the boolean that
%           checks if a file is open!  Ah well!}
% \changes{2.12}{2009/09/16}{License change to LPPL}
% \changes{2.12}{2009/09/16}{New maintainer}
% \changes{2.13}{2010/10/11}{Make hyperlinks to solutions work correctly}
% \changes{2.14}{2014/08/08}{Use a protected write for creating output files}
% \changes{2.15}{2014/08/14}{Fix bug in last update}
% \changes{2.16}{2014/08/24}{Fix bug in last update}
%
%
%    \title{Production of solution sheets in \LaTeXe}
%    \author{Mike Piff \and Joseph Wright\thanks{joseph.wright@morningstar2.co.uk}}
%
%    \maketitle
%    \tableofcontents
%
%    \section{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
%    \TeX book 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 \LaTeX\ environments rather than
%    being started with a command and ended with the end of the surrounding
%    environment, a wholly un-\LaTeX y 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.
%
%    \section{The documentation driver file}
%
%    This is the driver file that produces this documentation.
%    We use the document class provided by the \LaTeXe\ distribution
%    for producing the documentation.
%    \begin{macrocode}
%<*driver>
\documentclass{ltxdoc}
\RecordChanges
\begin{document}
  \DocInput{answers.dtx}
  \PrintIndex
  \PrintChanges
\end{document}
%</driver>
%    \end{macrocode}
%    
%    \section{User interface}
%
%    The package needs to be included with the command
%    \begin{verbatim}
%             \usepackage[nosolutionfiles]{answers}
%    \end{verbatim}
%    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.
%
%    \DescribeMacro{\Newassociation}
%    After that, there should be several declarations of the form
%    \begin{verbatim}
%        \Newassociation{xxx}{yyy}{zzz}
%    \end{verbatim}
%    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.
%
%    \DescribeMacro{\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.  By default, |\solutionpoint| is added before |\solutionextension|.
%	  Redefine it to remove it. (It has the obvious default value of a
%    period.
%
%    \DescribeMacro{\Opensolutionfile}
%    \DescribeMacro{\Closesolutionfile}
%    At some point the user types
%    \begin{verbatim}
%        \Opensolutionfile{zzz}
%        ...
%        \Closesolutionfile{zzz}
%    \end{verbatim}
%    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.
%
%    \DescribeMacro{\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|.
%
%    \DescribeEnv{Filesave}
%    Alternatively, a block of text can be saved to file handle
%    |zzz| by means of
%    \begin{verbatim}
%        \begin{Filesave}{zzz}
%           ....
%        \end{Filesave}
%    \end{verbatim}
%    around it once, |zzz| has been opened.
%    The restrictions that apply to |\Writetofile| above do not apply
%    to this environment.
%
%    \DescribeMacro{\Readsolutionfile}
%    One of the generated files can be read using
%    \begin{verbatim}
%        \Readsolutionfile{zzz}
%    \end{verbatim}
%    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.
%
%    \section{A simple example}
%
%    Here is a straightforward example to illustrate how these macros
%    are used.
%    \begin{macrocode}
%<*ex1>
\documentclass[12pt,a4paper]{article}
\usepackage{answers}
\Newassociation{sol}{Solution}{ans}
\newtheorem{ex}{Exercise}
\begin{document}
\Opensolutionfile{ans}[ans1]
\section{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}
\Closesolutionfile{ans}
\section{Solutions}
\input{ans1}
\end{document}
%</ex1>
%    \end{macrocode}
%
%    \section{A complicated example}
%
%    The following is an (over-complicated) example of the use
%    of package |answers|.
%    It uses some of the refinements described later.
%    \begin{macrocode}
%<*ex2>
\documentclass[12pt,a4paper]{article}
\usepackage{answers}%\usepackage[nosolutionfiles]{answers}
%    \end{macrocode}
%    First an environment which contains problems and numbers them.
%    This is based on a \LaTeX\ theorem, but with a roman body
%    rather than italic.
%    \begin{macrocode}
\newtheorem{Exc}{Exercise}
\newenvironment{Ex}{\begin{Exc}\normalfont}{\end{Exc}}
%    \end{macrocode}
%    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.
%    \begin{macrocode}
\Newassociation{solution}{Soln}{test}
\Newassociation{hint}{Hint}{test}
\Newassociation{Solution}{sSol}{testtwo}
%    \end{macrocode}
%    Because we want to mark different types of problem in the master file of
%    problems, we define the following.
%    \begin{macrocode}
\newcommand{\prehint}{~[Hint]}
\newcommand{\presolution}{~[Solution]}
\newcommand{\preSolution}{~[Homework]}
%    \end{macrocode}
%    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 |\include|d.
%    \begin{macrocode}
\newcommand{\Opentesthook}[2]%
   {\Writetofile{#1}{\protect\section{#1: #2}}}
%    \end{macrocode}
%    The default text produced when \LaTeX meets the solution
%    environments is here modified.
%    \begin{macrocode}
\renewcommand{\Solnlabel}[1]{\emph{Solution #1}}
\renewcommand{\Hintlabel}[1]{\emph{Hint #1}}
\renewcommand{\sSollabel}[1]{\emph{Solution to #1}}

\begin{document}
%    \end{macrocode}
%    We open handle |test| as actual file |test1.tex|,
%    \begin{macrocode}
   \Opensolutionfile{test}[ans2]{Solutions}
%    \end{macrocode}
%    and write some text on it.
%    \begin{macrocode}
   \Writetofile{test}{\protect\subsection{Some Solutions}}
%    \end{macrocode}
%    Handle |testtwo| is opened as |testtwo.tex|.
%    \begin{macrocode}
   \Opensolutionfile{testtwo}[ans2x]
   \Writetofile{testtwo}{%
      \protect\subsection{Extra Solutions}}
%    \end{macrocode}
%    Now the problems.
%    \begin{macrocode}
   \section{Exercises}
   \begin{Ex}
      An exercise with a solution.
      \begin{solution}
         This is a solution.
         \relax{}
      \end{solution}
   \end{Ex}
   \begin{Ex}
      An exercise with a hint and a secret solution.
      \begin{hint}
         This is a hint.
      \end{hint}
      \begin{Solution}
         This is a secret solution.
      \end{Solution}
   \end{Ex}
   \begin{Ex}
      An exercise with a hint.
      \begin{hint}
         This is a hint.
      \end{hint}
   \end{Ex}
%    \end{macrocode}
%    We close the two solution files and immediately input their
%    contents.  We could have used |\include| here.
%    \begin{macrocode}
   \Closesolutionfile{test}
   \Readsolutionfile{test}
   \clearpage
   \Closesolutionfile{testtwo}
   \Readsolutionfile{testtwo}
\end{document}
%</ex2>
%    \end{macrocode}
%
%    \section{A further example}
%
%    Here is an application to a situation not originally envisaged,
%    suggested to the author by Martin Osborne.  Here, the exercises and
%    solutions are not numbered; they are \emph{described}.
%    \begin{macrocode}
%<*ex3>
\documentclass[12pt,a4paper]{article}
\usepackage{answers}
\newenvironment{Ex}[1]{\begin{trivlist}\item \emph{#1} %
   \renewcommand{\Currentlabel}{#1}}{\end{trivlist}}
\Newassociation{solution}{Soln}{solutions}

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

\begin{document}
\section*{Problems}
   \Opensolutionfile{solutions}[ans3]
   \begin{Ex}{First exercise}
      An exercise with a solution.
      \begin{solution}
         This is a solution.
         \relax{}
      \end{solution}
   \end{Ex}
   \begin{Ex}{Second exercise}
      A second exercise with a solution.
      \begin{solution}
         This is another solution.
      \end{solution}
   \end{Ex}
   \Closesolutionfile{solutions}
\section*{Solutions}
   \Readsolutionfile{solutions}
\end{document}
%</ex3>
%    \end{macrocode}
%
%    \StopEventually{}
%
%
%    \section{Identification}
%
%    This package can only be used with \LaTeXe, so
%    an appropriate message is displayed when another
%    format is used.
%    \begin{macrocode}
%<*answers>
\NeedsTeXFormat{LaTeX2e}
%    \end{macrocode}
%
%
%    Announce the package name and its version:
%    \begin{macrocode}
\ProvidesPackage{answers}
    [2014/08/24 v2.16 Production of solution sheets in LaTeX2e]
%    \end{macrocode}
%
%  \section{Options}
%
%  There is a single option |nosolutionfiles| that
%  switches output off to files
%  and produces the solutions here-and-now.
%
%    \begin{macrocode}
\newif\ifanswerfiles \answerfilestrue
\DeclareOption{nosolutionfiles}{\answerfilesfalse
   \typeout{No answer files being produced}}%
\ProcessOptions

%    \end{macrocode}
%
%
%    As this package now relies heavily on the |verbatim| package, we
%    ensure that that is loaded.
%    \begin{macrocode}
\RequirePackage{verbatim}
%    \end{macrocode}
%
% \begin{macro}{\protected@iwrite}
%   An immediate version of \cs{protected@write}: not available in the
%   kernel but needed for safety.
%    \begin{macrocode}
\long\def\protected@iwrite#1#2#3{%
  \begingroup
    \let\thepage\relax
    #2%
    \let\protect\@unexpandable@protect
    \edef \reserved@a{\immediate\write#1{#3}}%
    \reserved@a
  \endgroup
  \if@nobreak
    \ifvmode
      \nobreak
    \fi
  \fi
}
%    \end{macrocode}
% \end{macro}
%
%    \section{File handling}
%
%    \begin{macro}{\solutionextension}
%    The default extension for solution files is defined here.
%    \begin{macrocode}
\newcommand{\solutionpoint}{.}
\newcommand{\solutionextension}{tex}
%    \end{macrocode}
%    It may be changed with |\renewcommand|.
%    \end{macro}
%
%    \begin{environment}{Filesave}
%    We define an environment |Filesave| with one parameter, the file handle.
%    It is similar to the example of Sch\"opf in the
%    description of |verbatim|.
%    \begin{macrocode}
\newenvironment{Filesave}[1]{%
   \@bsphack
   \def\verbatim@processline{}%
   \Iffileundefined{#1}{}{%
      \Ifopen{#1}{%
         \def\verbatim@processline{%
            \Ifanswerfiles{%
               \immediate\write\@nameuse{#1@file}%
                  {\the\verbatim@line}%
            }{}%
         }%
      }{}%
   }%
   \let\do\@makeother\dospecials
   \catcode`\^^M\active \catcode`\^^I=12\relax
   \verbatim@start
}{\@esphack}
%    \end{macrocode}
%    \end{environment}
%
%    \begin{macro}{\Writetofile}
%    It is also useful to have a command to write material to the file.
%    In this, you need to put |\protect| before any control words in the
%    argument that might expand prematurely and create havoc.
%    \begin{macrocode}
\newcommand{\Writetofile}[2]{%
   \@bsphack
   \Iffileundefined{#1}{}{%
      \Ifopen{#1}{%
         {%
            \let\protect\string
            \Ifanswerfiles{%
               \protected@iwrite{\@nameuse{#1@file}}{}{#2}%
            }{}%
         }%
      }{}%
   }%
   \@esphack
}
%    \end{macrocode}
%    \end{macro}
%
%    \begin{macro}{\Ifopen}
%    We need to check whether or not a file is already open.
%    \begin{macrocode}
\newcommand{\Ifopen}[3]{%
   \csname if#1open\endcsname#2\else#3\fi}%
%    \end{macrocode}
%    \end{macro}
%
%    \begin{macro}{\Iffileundefined}
%    We also need to check whether a file variable has already been defined
%    for a given file handle.
%    \begin{macrocode}
\newcommand{\Iffileundefined}[3]{%
   \csname ifx\expandafter\endcsname
      \csname #1@file\endcsname\relax
      #2\else#3\fi}
%    \end{macrocode}
%    \end{macro}
%    Finally, we need a check as to whether we are outputting answers to a
%    file or not
%    \begin{macrocode}
\newcommand{\Ifanswerfiles}[2]{%
   \ifanswerfiles #1\else #2\fi}
%    \end{macrocode}
%
%
%
%    \section{The file interface}
%
%    \begin{macro}{\Opensolutionfile}
%    Before we can write solutions, we must open the solution file(s).
%    The command to do this takes a single parameter, which should usually be
%    a file name without extension. Thus it should probably be
%    restricted to a string of at most 8 letters for portability.
%    This operation will not truncate any existing open file.
%    However, if the second optional parameter is specified, this
%    determines the actual filename, and the first parameter is then
%    an arbitrary symbolic file handle name.
%    \begin{macrocode}
\def\Opensolutionfile#1{%
   \@ifnextchar[{\define@filename{#1}}%
      {\define@filename{#1}[#1]}}%
\def\define@filename#1[#2]{%
   \global\@namedef{#1@filename}{#2\solutionpoint\solutionextension}%
   \Ifanswerfiles{%
      \typeout{Output from handle #1 going
         to #2.\solutionextension}%
   }{}%
   \Iffileundefined{#1}{%
      \expandafter\newwrite\csname #1@file\endcsname
      \csname newif\expandafter\endcsname
         \csname if#1open\endcsname
      \global\csname #1openfalse\endcsname
      \expandafter\ifx\csname Open#1hook\endcsname\relax
         \global\@namedef{Open#1hook}##1{}%
      \fi
      \expandafter\ifx\csname Close#1hook\endcsname\relax
         \global\@namedef{Close#1hook}##1{}%
      \fi
   }{}%
   \let\Tmp\relax
   \Ifopen{#1}{\typeout{File #1 already open}}{%
      \Ifanswerfiles{%
         \immediate\openout\@nameuse{#1@file}=%
         \@nameuse{#1@filename}%
      }{}%
      \global\csname#1opentrue\endcsname
      \def\Tmp{\@nameuse{Open#1hook}{#1}}%
   }%
   \Tmp
}
%    \end{macrocode}
%    \begin{macro}{\Closesolutionfile}
%    We also have a command to close an already open file.
%    \begin{macrocode}
\def\Closesolutionfile#1{%
   \let\Tmp\relax
   \Iffileundefined{#1}{}{%
      \Ifopen{#1}{%
         \Ifanswerfiles{%
            \immediate\closeout\@nameuse{#1@file}%
         }{}%
         \global\csname #1openfalse\endcsname
         \def\Tmp{\@nameuse{Close#1hook}{#1}}%
      }{}%
   }%
   \Tmp
}
%    \end{macrocode}
%
%    Note that the two file commands each provide a hook which allows them to
%    perform extra tasks.
%    For instance, the opening operation could be made to
%    write extra information to the file by redefining the appropriate hook.
%    The closing operation could if required do an immediate |\input| of the
%    solution file contents. For example,
%    \begin{verbatim}
%        \newcommand{\Openxxxhook}[2]{%
%           \Writetofile{#1}{\protect\section{#2}}%
%        }%
%        \newcommand{\Closexxxhook}[1]{%
%           \Readsolutionfile{#1}%
%        }
%    \end{verbatim}
%    and then
%    \begin{verbatim}
%        \Opensolutionfile{xxx}{Answers to selected problems}
%        ...
%        \Closesolutionfile{xxx}
%    \end{verbatim}
%    The default behaviour is to ignore the one parameter. Note that if you
%    redefine their behaviour, you must remember that the first parameter is
%    always the file handle.
%    \end{macro}
%    \end{macro}
%
%    \begin{macro}{\Readsolutionfile}
%    The operation of reading the file of solutions can be done with the
%    following command.
%    \begin{macrocode}
\def\Readsolutionfile#1{%
   \Ifanswerfiles{%
      \Iffileundefined{#1}{}{%
         \Ifopen{#1}{%
            \typeout{WARNING: attempt to read open file #1}%
         }{%
            \edef\Tmp{%
               \noexpand\InputIfFileExists
                  {\@nameuse{#1@filename}}{}%
               {\noexpand\message{File
                  \@nameuse{#1@filename}%
                     \space not found}}%
            }%
            \Tmp
         }%
      }%
   }{}%
}

%    \end{macrocode}
%    \end{macro}
%
%
%    \section{The solution interface}
%
%    \begin{macro}{\Newassociation}
%    Several solution file handles may have been defined.
%    You are limited only by the
%    number that \TeX\ will make available to you. Each solution environment
%    that is to write to one of these handles
%    must know which handle to write to,
%    and also what extra information to write there, apart from its contents.
%    This is done by setting up an association
%    between the source environment,
%    the destination environment and the file handle.
%    \begin{macrocode}
\newcommand{\Newassociation}[3]{%
   \newsolution{#2}%
   \expandafter\ifx\csname #3opentrue\endcsname\relax
      \expandafter\newif\csname if#3open\endcsname
   \fi
   \newenvironment{#1}{%
      \Ifanswerfiles{%
         \let\Tmp\relax
         \Iffileundefined{#3}{}{%
            \Ifopen{#3}{%
               \immediate\write\@nameuse{#3@file}%
                  {\string\begin{#2}\@nameuse{#2params}}%
            \def\Tmp{\Filesave{#3}}%
            }{}%
         }%
      }{%
         \edef\Tmp{\noexpand\begin{#2}\@nameuse{#2params}}%
      }%
      \csname pre#1\endcsname
      \Tmp
   }%
   {%
      \Ifanswerfiles{%
         \Iffileundefined{#3}{}{%
            \Ifopen{#3}{%
               \endFilesave%
               \immediate\write\@nameuse{#3@file}%
                  {\string\end{#2}}%
               \csname post#1\endcsname
            }{}%
         }%
      }{%
         \end{#2}%
      }%
   }%
}
%    \end{macrocode}
%    \end{macro}
%
%    \begin{macro}{\newsolution}
%    The default destination environment in the solution file is defined to
%    take a single parameter, a reference number inherited from the source
%    environment.  This is set with style |\solutionstyle|, which defaults
%    to |\textbf|.  In addition, solution type |yyy| can have markup added
%    before and after it by defining |\preyyy| and |\postyyy| suitably,
%    eg, a rule across the width of the page or a square.
%    If anything more sophisticated is intended, it is probably better to
%    |\renewenvironment{yyy}| to achieve it.
%    \begin{macro}{\Currentlabel}
%    \begin{macrocode}
\newcommand{\newsolution}[1]{%
   \@ifundefined{#1}{%
      \global\@definecounter{#1}%
      \global\@namedef{#1params}{{\Currentlabel}}%
      \newenvironment{#1}[1]%
      {%
         \refstepcounter{#1}%
         \csname pre#1\endcsname
         \trivlist
         \item[\hskip\itemsep{\@nameuse{#1label}{##1}}]}%
      {\csname post#1\endcsname\endtrivlist}%
      \global\@namedef{#1label}##1{\solutionstyle{##1}}%
   }{\typeout{WARNING: environment #1 already in use}}%
}
\newcommand{\solutionstyle}[1]{\textbf{#1}}
\newcommand{\Currentlabel}{\@currentlabel}
%    \end{macrocode}
%    The format of the label for solution environment |xxx| is governed
%    by the command |\xxxlabel|, which takes one argument by default.
%    The argument is passed to it by the command |\xxxparams|,
%    which expands to |{\Currentlabel}|, a synonym for |{\@currentlabel}|,
%    and this argument is written automatically by the source environment.
%    The label appears in boldface by default.
%    We could easily change the behaviour of this environment by changing
%    these two commands. For example
%    \begin{verbatim}
%        \renewcommand{\xxxlabel}[1]{\emph{Solution to #1}}
%        \renewcommand{\xxxparams}{{\Currentlabel(p.\thepage)}}
%    \end{verbatim}
%    would provide a number and page reference in italic.
%
%    More complicated behaviour could be produced by redefining the |xxx|
%    environment itself to take a different number of parameters. Note
%    however that |\xxxparams| must be redefined to provide those parameters.
%    \end{macro}
%    \end{macro}
%
%    Which brings us to the end of the |answers| package.
%    \begin{macrocode}
%</answers>
%    \end{macrocode}
%
%
%
% \Finale
%
\endinput
%