| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
% Originally developed by Mike Piff |
| 4 |
% Copyright (C) 1990,1994-1996 by Mike Piff |
| 5 |
% Copyright (C) 2009-2010,2014 by |
| 6 |
% Joseph Wright <[email protected]> |
| 7 |
% |
| 8 |
% This file may be distributed and/or modified under the conditions of |
| 9 |
% the LaTeX Project Public License (LPPL), either version 1.3c of |
| 10 |
% this license or (at your option) any later version. The latest |
| 11 |
% version of this license is in the file: |
| 12 |
% |
| 13 |
% http://www.latex-project.org/lppl.txt |
| 14 |
% |
| 15 |
% This work is "maintained" (as per LPPL maintenance status) by |
| 16 |
% Joseph Wright. |
| 17 |
% |
| 18 |
% \fi |
| 19 |
% |
| 20 |
% \CheckSum{267} |
| 21 |
% |
| 22 |
% \changes{2.0}{1994/03/25}{First version for LaTeX2e} |
| 23 |
% \changes{2.01}{1994/03/30}{Whoops! |ProvidesPackage{answers}|, |
| 24 |
% not |ProvidesClass{Answers}|!!} |
| 25 |
% \changes{2.02}{1995/08/31}{Removed .drv file from installation} |
| 26 |
% \changes{2.03}{1995/08/31}{Allowed a file handle to open |
| 27 |
% many files successively, using an optional parameter.} |
| 28 |
% \changes{2.04}{1995/09/08}{Option nosolutionfiles added to allow |
| 29 |
% solutions to optionally follow problems for tutor's use. |
| 30 |
% Several minor changes to aid flexibility} |
| 31 |
% \changes{2.05}{1995/09/11}{Better documentation of features added} |
| 32 |
% \changes{2.06}{1996/01/05}{More documentation of features added, |
| 33 |
% as suggested by David Epstein.} |
| 34 |
% \changes{2.07}{1996/01/08}{Conditionals to check if files were open |
| 35 |
% were not changed globally. (Spotted by David Epstein.)} |
| 36 |
% \changes{2.08}{1996/01/11}{More examples added.} |
| 37 |
% \changes{2.09}{1996/07/10}{Bug spotted by David Epstein; |
| 38 |
% I had missed out the definition of the boolean that |
| 39 |
% checks if a file is open! Ah well!} |
| 40 |
% \changes{2.12}{2009/09/16}{License change to LPPL} |
| 41 |
% \changes{2.12}{2009/09/16}{New maintainer} |
| 42 |
% \changes{2.13}{2010/10/11}{Make hyperlinks to solutions work correctly} |
| 43 |
% \changes{2.14}{2014/08/08}{Use a protected write for creating output files} |
| 44 |
% \changes{2.15}{2014/08/14}{Fix bug in last update} |
| 45 |
% \changes{2.16}{2014/08/24}{Fix bug in last update} |
| 46 |
% |
| 47 |
% |
| 48 |
% \title{Production of solution sheets in \LaTeXe} |
| 49 |
% \author{Mike Piff \and Joseph Wright\thanks{[email protected]}} |
| 50 |
% |
| 51 |
% \maketitle |
| 52 |
% \tableofcontents |
| 53 |
% |
| 54 |
% \section{Introduction} |
| 55 |
% |
| 56 |
% This package is a modification of the author's previous style option |
| 57 |
% |answers|, which has been in use for a few years, |
| 58 |
% and was based upon the |
| 59 |
% \TeX book idea of binding solutions to exercises. I have taken the |
| 60 |
% opportunity with this revision to alter the format of the solutions, |
| 61 |
% so that they are now presented as \LaTeX\ environments rather than |
| 62 |
% being started with a command and ended with the end of the surrounding |
| 63 |
% environment, a wholly un-\LaTeX y way of doing things! |
| 64 |
% |
| 65 |
% The other main change is that several file |
| 66 |
% handles are allowed to be active |
| 67 |
% at once. This allows some solutions in a book (for instance) to go |
| 68 |
% to the appendices, and some to go to a separate file, to be printed and |
| 69 |
% handed to the students as the course progresses. |
| 70 |
% Moreover, the actual physical files opened with each file handle |
| 71 |
% can now be varied in the same job, allowing many different files |
| 72 |
% to be created according to the same format. Thus, for instance, |
| 73 |
% each chapter of a book could create its own solution file, allowing |
| 74 |
% the user to use |\include| on both chapters and solutions. |
| 75 |
% |
| 76 |
% Finally, any number of solution-types may now be bound to any file, not |
| 77 |
% just the two old ones, solution and hint. The format of each solution |
| 78 |
% type is under the complete control of the user. |
| 79 |
% |
| 80 |
% \section{The documentation driver file} |
| 81 |
% |
| 82 |
% This is the driver file that produces this documentation. |
| 83 |
% We use the document class provided by the \LaTeXe\ distribution |
| 84 |
% for producing the documentation. |
| 85 |
% \begin{macrocode} |
| 86 |
%<*driver> |
| 87 |
\documentclass{ltxdoc} |
| 88 |
\RecordChanges |
| 89 |
\begin{document} |
| 90 |
\DocInput{answers.dtx} |
| 91 |
\PrintIndex |
| 92 |
\PrintChanges |
| 93 |
\end{document} |
| 94 |
%</driver> |
| 95 |
% \end{macrocode} |
| 96 |
% |
| 97 |
% \section{User interface} |
| 98 |
% |
| 99 |
% The package needs to be included with the command |
| 100 |
% \begin{verbatim} |
| 101 |
% \usepackage[nosolutionfiles]{answers} |
| 102 |
% \end{verbatim} |
| 103 |
% If the optional argument is given, solutions appear at that point |
| 104 |
% in the text, rather than being written to external files. |
| 105 |
% This allows a demonstrator's version to be produced. |
| 106 |
% |
| 107 |
% \DescribeMacro{\Newassociation} |
| 108 |
% After that, there should be several declarations of the form |
| 109 |
% \begin{verbatim} |
| 110 |
% \Newassociation{xxx}{yyy}{zzz} |
| 111 |
% \end{verbatim} |
| 112 |
% where |xxx| is an environment in the document, and |yyy| is an |
| 113 |
% environment which will surround the contents of |xxx| when it is |
| 114 |
% written to symbolic file handle |zzz|. |
| 115 |
% The names |xxx|, |yyy| and |zzz| should consist of letters |
| 116 |
% only, not numbers, punctuation or spaces. |
| 117 |
% |
| 118 |
% \DescribeMacro{\solutionextension} |
| 119 |
% By default, output will go to |zzz.tex| if |zzz| is open. |
| 120 |
% The command |\solutionextension| can be redefined to change |tex| to |
| 121 |
% some other extension. Alternatively, the output filename can be |
| 122 |
% changed as an optional parameter to |\Opensolutionfile|, and each |
| 123 |
% |\Opensolutionfile| on the same handle can use a different physical |
| 124 |
% file. By default, |\solutionpoint| is added before |\solutionextension|. |
| 125 |
% Redefine it to remove it. (It has the obvious default value of a |
| 126 |
% period. |
| 127 |
% |
| 128 |
% \DescribeMacro{\Opensolutionfile} |
| 129 |
% \DescribeMacro{\Closesolutionfile} |
| 130 |
% At some point the user types |
| 131 |
% \begin{verbatim} |
| 132 |
% \Opensolutionfile{zzz} |
| 133 |
% ... |
| 134 |
% \Closesolutionfile{zzz} |
| 135 |
% \end{verbatim} |
| 136 |
% to create a file of solutions written by environments |xxx| to |
| 137 |
% environments |yyy|. |
| 138 |
% If this construction is used several times, then several files |
| 139 |
% of solutions will be created. |
| 140 |
% The user may wish these files to have different names. |
| 141 |
% If the form |\Opensolutionfile{zzz}[www]|, then |www.tex| is used as |
| 142 |
% actual file output name rather than |zzz.tex|. This allows file |
| 143 |
% handle |zzz| to create many files |www.tex|, say one for each |
| 144 |
% chapter of a book, or one for each problem sheet. These could then |
| 145 |
% be processed using |\include| commands. The same value of |
| 146 |
% |\solutionextension| is used for the optional argument as for the |
| 147 |
% main argument. |
| 148 |
% The name |www| should follow the usual file naming conventions. |
| 149 |
% |
| 150 |
% \DescribeMacro{\Writetofile} |
| 151 |
% In addition, material can be written directly to a file by means of |
| 152 |
% |\Writetofile|. |
| 153 |
% Its first argument is the file handle |zzz| and its second is |
| 154 |
% the line of text to be written. |
| 155 |
% It is most important to remember that any control words |
| 156 |
% in the line to be written should be preceded by |\protect|, |
| 157 |
% otherwise the primitive \TeX\ |\write| command will expand them. |
| 158 |
% Also, as the argument is read in \TeX' usual way before being |
| 159 |
% written, any trailing spaces after a control word will disappear |
| 160 |
% unless precautions are taken. Thus, to write |\xx yyy| to the file, |
| 161 |
% the user can type |\protect\xx\space yyy|. |
| 162 |
% |
| 163 |
% \DescribeEnv{Filesave} |
| 164 |
% Alternatively, a block of text can be saved to file handle |
| 165 |
% |zzz| by means of |
| 166 |
% \begin{verbatim} |
| 167 |
% \begin{Filesave}{zzz} |
| 168 |
% .... |
| 169 |
% \end{Filesave} |
| 170 |
% \end{verbatim} |
| 171 |
% around it once, |zzz| has been opened. |
| 172 |
% The restrictions that apply to |\Writetofile| above do not apply |
| 173 |
% to this environment. |
| 174 |
% |
| 175 |
% \DescribeMacro{\Readsolutionfile} |
| 176 |
% One of the generated files can be read using |
| 177 |
% \begin{verbatim} |
| 178 |
% \Readsolutionfile{zzz} |
| 179 |
% \end{verbatim} |
| 180 |
% provided the file has not been closed and re-opened. |
| 181 |
% Alternatively, simply |\input| or |\include| it if preferred. |
| 182 |
% |
| 183 |
% None of the file operations should have any effect if the file |
| 184 |
% handle |zzz| has not been opened, or if |nosolutionfiles| is specified. |
| 185 |
% |
| 186 |
% \section{A simple example} |
| 187 |
% |
| 188 |
% Here is a straightforward example to illustrate how these macros |
| 189 |
% are used. |
| 190 |
% \begin{macrocode} |
| 191 |
%<*ex1> |
| 192 |
\documentclass[12pt,a4paper]{article} |
| 193 |
\usepackage{answers} |
| 194 |
\Newassociation{sol}{Solution}{ans} |
| 195 |
\newtheorem{ex}{Exercise} |
| 196 |
\begin{document} |
| 197 |
\Opensolutionfile{ans}[ans1] |
| 198 |
\section{Problems} |
| 199 |
\begin{ex} |
| 200 |
First exercise |
| 201 |
\begin{sol} |
| 202 |
First solution. |
| 203 |
\end{sol} |
| 204 |
\end{ex} |
| 205 |
\begin{ex} |
| 206 |
Second exercise |
| 207 |
\begin{sol} |
| 208 |
Second solution. |
| 209 |
\end{sol} |
| 210 |
\end{ex} |
| 211 |
\Closesolutionfile{ans} |
| 212 |
\section{Solutions} |
| 213 |
\input{ans1} |
| 214 |
\end{document} |
| 215 |
%</ex1> |
| 216 |
% \end{macrocode} |
| 217 |
% |
| 218 |
% \section{A complicated example} |
| 219 |
% |
| 220 |
% The following is an (over-complicated) example of the use |
| 221 |
% of package |answers|. |
| 222 |
% It uses some of the refinements described later. |
| 223 |
% \begin{macrocode} |
| 224 |
%<*ex2> |
| 225 |
\documentclass[12pt,a4paper]{article} |
| 226 |
\usepackage{answers}%\usepackage[nosolutionfiles]{answers} |
| 227 |
% \end{macrocode} |
| 228 |
% First an environment which contains problems and numbers them. |
| 229 |
% This is based on a \LaTeX\ theorem, but with a roman body |
| 230 |
% rather than italic. |
| 231 |
% \begin{macrocode} |
| 232 |
\newtheorem{Exc}{Exercise} |
| 233 |
\newenvironment{Ex}{\begin{Exc}\normalfont}{\end{Exc}} |
| 234 |
% \end{macrocode} |
| 235 |
% Three sorts of solution are written to two different files. |
| 236 |
% File handle |test| will contain the solutions and hints that the |
| 237 |
% students will see; |testtwo| contains the solutions to the |
| 238 |
% problems which they will probably hand in, and so these must be |
| 239 |
% formatted separately. |
| 240 |
% \begin{macrocode} |
| 241 |
\Newassociation{solution}{Soln}{test} |
| 242 |
\Newassociation{hint}{Hint}{test} |
| 243 |
\Newassociation{Solution}{sSol}{testtwo} |
| 244 |
% \end{macrocode} |
| 245 |
% Because we want to mark different types of problem in the master file of |
| 246 |
% problems, we define the following. |
| 247 |
% \begin{macrocode} |
| 248 |
\newcommand{\prehint}{~[Hint]} |
| 249 |
\newcommand{\presolution}{~[Solution]} |
| 250 |
\newcommand{\preSolution}{~[Homework]} |
| 251 |
% \end{macrocode} |
| 252 |
% We provide an extra parameter when we open file handle |test|; |
| 253 |
% this is because we want to write a |\section| command to the |
| 254 |
% solution file. This is merely an illustration here, but would be |
| 255 |
% more relevant if the solution file were |\include|d. |
| 256 |
% \begin{macrocode} |
| 257 |
\newcommand{\Opentesthook}[2]% |
| 258 |
{\Writetofile{#1}{\protect\section{#1: #2}}} |
| 259 |
% \end{macrocode} |
| 260 |
% The default text produced when \LaTeX meets the solution |
| 261 |
% environments is here modified. |
| 262 |
% \begin{macrocode} |
| 263 |
\renewcommand{\Solnlabel}[1]{\emph{Solution #1}} |
| 264 |
\renewcommand{\Hintlabel}[1]{\emph{Hint #1}} |
| 265 |
\renewcommand{\sSollabel}[1]{\emph{Solution to #1}} |
| 266 |
|
| 267 |
\begin{document} |
| 268 |
% \end{macrocode} |
| 269 |
% We open handle |test| as actual file |test1.tex|, |
| 270 |
% \begin{macrocode} |
| 271 |
\Opensolutionfile{test}[ans2]{Solutions} |
| 272 |
% \end{macrocode} |
| 273 |
% and write some text on it. |
| 274 |
% \begin{macrocode} |
| 275 |
\Writetofile{test}{\protect\subsection{Some Solutions}} |
| 276 |
% \end{macrocode} |
| 277 |
% Handle |testtwo| is opened as |testtwo.tex|. |
| 278 |
% \begin{macrocode} |
| 279 |
\Opensolutionfile{testtwo}[ans2x] |
| 280 |
\Writetofile{testtwo}{% |
| 281 |
\protect\subsection{Extra Solutions}} |
| 282 |
% \end{macrocode} |
| 283 |
% Now the problems. |
| 284 |
% \begin{macrocode} |
| 285 |
\section{Exercises} |
| 286 |
\begin{Ex} |
| 287 |
An exercise with a solution. |
| 288 |
\begin{solution} |
| 289 |
This is a solution. |
| 290 |
\relax{} |
| 291 |
\end{solution} |
| 292 |
\end{Ex} |
| 293 |
\begin{Ex} |
| 294 |
An exercise with a hint and a secret solution. |
| 295 |
\begin{hint} |
| 296 |
This is a hint. |
| 297 |
\end{hint} |
| 298 |
\begin{Solution} |
| 299 |
This is a secret solution. |
| 300 |
\end{Solution} |
| 301 |
\end{Ex} |
| 302 |
\begin{Ex} |
| 303 |
An exercise with a hint. |
| 304 |
\begin{hint} |
| 305 |
This is a hint. |
| 306 |
\end{hint} |
| 307 |
\end{Ex} |
| 308 |
% \end{macrocode} |
| 309 |
% We close the two solution files and immediately input their |
| 310 |
% contents. We could have used |\include| here. |
| 311 |
% \begin{macrocode} |
| 312 |
\Closesolutionfile{test} |
| 313 |
\Readsolutionfile{test} |
| 314 |
\clearpage |
| 315 |
\Closesolutionfile{testtwo} |
| 316 |
\Readsolutionfile{testtwo} |
| 317 |
\end{document} |
| 318 |
%</ex2> |
| 319 |
% \end{macrocode} |
| 320 |
% |
| 321 |
% \section{A further example} |
| 322 |
% |
| 323 |
% Here is an application to a situation not originally envisaged, |
| 324 |
% suggested to the author by Martin Osborne. Here, the exercises and |
| 325 |
% solutions are not numbered; they are \emph{described}. |
| 326 |
% \begin{macrocode} |
| 327 |
%<*ex3> |
| 328 |
\documentclass[12pt,a4paper]{article} |
| 329 |
\usepackage{answers} |
| 330 |
\newenvironment{Ex}[1]{\begin{trivlist}\item \emph{#1} % |
| 331 |
\renewcommand{\Currentlabel}{#1}}{\end{trivlist}} |
| 332 |
\Newassociation{solution}{Soln}{solutions} |
| 333 |
|
| 334 |
\renewenvironment{Soln}[1]{\begin{trivlist}\item |
| 335 |
Solution to \emph{#1} }{\end{trivlist}} |
| 336 |
|
| 337 |
\begin{document} |
| 338 |
\section*{Problems} |
| 339 |
\Opensolutionfile{solutions}[ans3] |
| 340 |
\begin{Ex}{First exercise} |
| 341 |
An exercise with a solution. |
| 342 |
\begin{solution} |
| 343 |
This is a solution. |
| 344 |
\relax{} |
| 345 |
\end{solution} |
| 346 |
\end{Ex} |
| 347 |
\begin{Ex}{Second exercise} |
| 348 |
A second exercise with a solution. |
| 349 |
\begin{solution} |
| 350 |
This is another solution. |
| 351 |
\end{solution} |
| 352 |
\end{Ex} |
| 353 |
\Closesolutionfile{solutions} |
| 354 |
\section*{Solutions} |
| 355 |
\Readsolutionfile{solutions} |
| 356 |
\end{document} |
| 357 |
%</ex3> |
| 358 |
% \end{macrocode} |
| 359 |
% |
| 360 |
% \StopEventually{} |
| 361 |
% |
| 362 |
% |
| 363 |
% \section{Identification} |
| 364 |
% |
| 365 |
% This package can only be used with \LaTeXe, so |
| 366 |
% an appropriate message is displayed when another |
| 367 |
% format is used. |
| 368 |
% \begin{macrocode} |
| 369 |
%<*answers> |
| 370 |
\NeedsTeXFormat{LaTeX2e} |
| 371 |
% \end{macrocode} |
| 372 |
% |
| 373 |
% |
| 374 |
% Announce the package name and its version: |
| 375 |
% \begin{macrocode} |
| 376 |
\ProvidesPackage{answers} |
| 377 |
[2014/08/24 v2.16 Production of solution sheets in LaTeX2e] |
| 378 |
% \end{macrocode} |
| 379 |
% |
| 380 |
% \section{Options} |
| 381 |
% |
| 382 |
% There is a single option |nosolutionfiles| that |
| 383 |
% switches output off to files |
| 384 |
% and produces the solutions here-and-now. |
| 385 |
% |
| 386 |
% \begin{macrocode} |
| 387 |
\newif\ifanswerfiles \answerfilestrue |
| 388 |
\DeclareOption{nosolutionfiles}{\answerfilesfalse |
| 389 |
\typeout{No answer files being produced}}% |
| 390 |
\ProcessOptions |
| 391 |
|
| 392 |
% \end{macrocode} |
| 393 |
% |
| 394 |
% |
| 395 |
% As this package now relies heavily on the |verbatim| package, we |
| 396 |
% ensure that that is loaded. |
| 397 |
% \begin{macrocode} |
| 398 |
\RequirePackage{verbatim} |
| 399 |
% \end{macrocode} |
| 400 |
% |
| 401 |
% \begin{macro}{\protected@iwrite} |
| 402 |
% An immediate version of \cs{protected@write}: not available in the |
| 403 |
% kernel but needed for safety. |
| 404 |
% \begin{macrocode} |
| 405 |
\long\def\protected@iwrite#1#2#3{% |
| 406 |
\begingroup |
| 407 |
\let\thepage\relax |
| 408 |
#2% |
| 409 |
\let\protect\@unexpandable@protect |
| 410 |
\edef \reserved@a{\immediate\write#1{#3}}% |
| 411 |
\reserved@a |
| 412 |
\endgroup |
| 413 |
\if@nobreak |
| 414 |
\ifvmode |
| 415 |
\nobreak |
| 416 |
\fi |
| 417 |
\fi |
| 418 |
} |
| 419 |
% \end{macrocode} |
| 420 |
% \end{macro} |
| 421 |
% |
| 422 |
% \section{File handling} |
| 423 |
% |
| 424 |
% \begin{macro}{\solutionextension} |
| 425 |
% The default extension for solution files is defined here. |
| 426 |
% \begin{macrocode} |
| 427 |
\newcommand{\solutionpoint}{.} |
| 428 |
\newcommand{\solutionextension}{tex} |
| 429 |
% \end{macrocode} |
| 430 |
% It may be changed with |\renewcommand|. |
| 431 |
% \end{macro} |
| 432 |
% |
| 433 |
% \begin{environment}{Filesave} |
| 434 |
% We define an environment |Filesave| with one parameter, the file handle. |
| 435 |
% It is similar to the example of Sch\"opf in the |
| 436 |
% description of |verbatim|. |
| 437 |
% \begin{macrocode} |
| 438 |
\newenvironment{Filesave}[1]{% |
| 439 |
\@bsphack |
| 440 |
\def\verbatim@processline{}% |
| 441 |
\Iffileundefined{#1}{}{% |
| 442 |
\Ifopen{#1}{% |
| 443 |
\def\verbatim@processline{% |
| 444 |
\Ifanswerfiles{% |
| 445 |
\immediate\write\@nameuse{#1@file}% |
| 446 |
{\the\verbatim@line}% |
| 447 |
}{}% |
| 448 |
}% |
| 449 |
}{}% |
| 450 |
}% |
| 451 |
\let\do\@makeother\dospecials |
| 452 |
\catcode`\^^M\active \catcode`\^^I=12\relax |
| 453 |
\verbatim@start |
| 454 |
}{\@esphack} |
| 455 |
% \end{macrocode} |
| 456 |
% \end{environment} |
| 457 |
% |
| 458 |
% \begin{macro}{\Writetofile} |
| 459 |
% It is also useful to have a command to write material to the file. |
| 460 |
% In this, you need to put |\protect| before any control words in the |
| 461 |
% argument that might expand prematurely and create havoc. |
| 462 |
% \begin{macrocode} |
| 463 |
\newcommand{\Writetofile}[2]{% |
| 464 |
\@bsphack |
| 465 |
\Iffileundefined{#1}{}{% |
| 466 |
\Ifopen{#1}{% |
| 467 |
{% |
| 468 |
\let\protect\string |
| 469 |
\Ifanswerfiles{% |
| 470 |
\protected@iwrite{\@nameuse{#1@file}}{}{#2}% |
| 471 |
}{}% |
| 472 |
}% |
| 473 |
}{}% |
| 474 |
}% |
| 475 |
\@esphack |
| 476 |
} |
| 477 |
% \end{macrocode} |
| 478 |
% \end{macro} |
| 479 |
% |
| 480 |
% \begin{macro}{\Ifopen} |
| 481 |
% We need to check whether or not a file is already open. |
| 482 |
% \begin{macrocode} |
| 483 |
\newcommand{\Ifopen}[3]{% |
| 484 |
\csname if#1open\endcsname#2\else#3\fi}% |
| 485 |
% \end{macrocode} |
| 486 |
% \end{macro} |
| 487 |
% |
| 488 |
% \begin{macro}{\Iffileundefined} |
| 489 |
% We also need to check whether a file variable has already been defined |
| 490 |
% for a given file handle. |
| 491 |
% \begin{macrocode} |
| 492 |
\newcommand{\Iffileundefined}[3]{% |
| 493 |
\csname ifx\expandafter\endcsname |
| 494 |
\csname #1@file\endcsname\relax |
| 495 |
#2\else#3\fi} |
| 496 |
% \end{macrocode} |
| 497 |
% \end{macro} |
| 498 |
% Finally, we need a check as to whether we are outputting answers to a |
| 499 |
% file or not |
| 500 |
% \begin{macrocode} |
| 501 |
\newcommand{\Ifanswerfiles}[2]{% |
| 502 |
\ifanswerfiles #1\else #2\fi} |
| 503 |
% \end{macrocode} |
| 504 |
% |
| 505 |
% |
| 506 |
% |
| 507 |
% \section{The file interface} |
| 508 |
% |
| 509 |
% \begin{macro}{\Opensolutionfile} |
| 510 |
% Before we can write solutions, we must open the solution file(s). |
| 511 |
% The command to do this takes a single parameter, which should usually be |
| 512 |
% a file name without extension. Thus it should probably be |
| 513 |
% restricted to a string of at most 8 letters for portability. |
| 514 |
% This operation will not truncate any existing open file. |
| 515 |
% However, if the second optional parameter is specified, this |
| 516 |
% determines the actual filename, and the first parameter is then |
| 517 |
% an arbitrary symbolic file handle name. |
| 518 |
% \begin{macrocode} |
| 519 |
\def\Opensolutionfile#1{% |
| 520 |
\@ifnextchar[{\define@filename{#1}}% |
| 521 |
{\define@filename{#1}[#1]}}% |
| 522 |
\def\define@filename#1[#2]{% |
| 523 |
\global\@namedef{#1@filename}{#2\solutionpoint\solutionextension}% |
| 524 |
\Ifanswerfiles{% |
| 525 |
\typeout{Output from handle #1 going |
| 526 |
to #2.\solutionextension}% |
| 527 |
}{}% |
| 528 |
\Iffileundefined{#1}{% |
| 529 |
\expandafter\newwrite\csname #1@file\endcsname |
| 530 |
\csname newif\expandafter\endcsname |
| 531 |
\csname if#1open\endcsname |
| 532 |
\global\csname #1openfalse\endcsname |
| 533 |
\expandafter\ifx\csname Open#1hook\endcsname\relax |
| 534 |
\global\@namedef{Open#1hook}##1{}% |
| 535 |
\fi |
| 536 |
\expandafter\ifx\csname Close#1hook\endcsname\relax |
| 537 |
\global\@namedef{Close#1hook}##1{}% |
| 538 |
\fi |
| 539 |
}{}% |
| 540 |
\let\Tmp\relax |
| 541 |
\Ifopen{#1}{\typeout{File #1 already open}}{% |
| 542 |
\Ifanswerfiles{% |
| 543 |
\immediate\openout\@nameuse{#1@file}=% |
| 544 |
\@nameuse{#1@filename}% |
| 545 |
}{}% |
| 546 |
\global\csname#1opentrue\endcsname |
| 547 |
\def\Tmp{\@nameuse{Open#1hook}{#1}}% |
| 548 |
}% |
| 549 |
\Tmp |
| 550 |
} |
| 551 |
% \end{macrocode} |
| 552 |
% \begin{macro}{\Closesolutionfile} |
| 553 |
% We also have a command to close an already open file. |
| 554 |
% \begin{macrocode} |
| 555 |
\def\Closesolutionfile#1{% |
| 556 |
\let\Tmp\relax |
| 557 |
\Iffileundefined{#1}{}{% |
| 558 |
\Ifopen{#1}{% |
| 559 |
\Ifanswerfiles{% |
| 560 |
\immediate\closeout\@nameuse{#1@file}% |
| 561 |
}{}% |
| 562 |
\global\csname #1openfalse\endcsname |
| 563 |
\def\Tmp{\@nameuse{Close#1hook}{#1}}% |
| 564 |
}{}% |
| 565 |
}% |
| 566 |
\Tmp |
| 567 |
} |
| 568 |
% \end{macrocode} |
| 569 |
% |
| 570 |
% Note that the two file commands each provide a hook which allows them to |
| 571 |
% perform extra tasks. |
| 572 |
% For instance, the opening operation could be made to |
| 573 |
% write extra information to the file by redefining the appropriate hook. |
| 574 |
% The closing operation could if required do an immediate |\input| of the |
| 575 |
% solution file contents. For example, |
| 576 |
% \begin{verbatim} |
| 577 |
% \newcommand{\Openxxxhook}[2]{% |
| 578 |
% \Writetofile{#1}{\protect\section{#2}}% |
| 579 |
% }% |
| 580 |
% \newcommand{\Closexxxhook}[1]{% |
| 581 |
% \Readsolutionfile{#1}% |
| 582 |
% } |
| 583 |
% \end{verbatim} |
| 584 |
% and then |
| 585 |
% \begin{verbatim} |
| 586 |
% \Opensolutionfile{xxx}{Answers to selected problems} |
| 587 |
% ... |
| 588 |
% \Closesolutionfile{xxx} |
| 589 |
% \end{verbatim} |
| 590 |
% The default behaviour is to ignore the one parameter. Note that if you |
| 591 |
% redefine their behaviour, you must remember that the first parameter is |
| 592 |
% always the file handle. |
| 593 |
% \end{macro} |
| 594 |
% \end{macro} |
| 595 |
% |
| 596 |
% \begin{macro}{\Readsolutionfile} |
| 597 |
% The operation of reading the file of solutions can be done with the |
| 598 |
% following command. |
| 599 |
% \begin{macrocode} |
| 600 |
\def\Readsolutionfile#1{% |
| 601 |
\Ifanswerfiles{% |
| 602 |
\Iffileundefined{#1}{}{% |
| 603 |
\Ifopen{#1}{% |
| 604 |
\typeout{WARNING: attempt to read open file #1}% |
| 605 |
}{% |
| 606 |
\edef\Tmp{% |
| 607 |
\noexpand\InputIfFileExists |
| 608 |
{\@nameuse{#1@filename}}{}% |
| 609 |
{\noexpand\message{File |
| 610 |
\@nameuse{#1@filename}% |
| 611 |
\space not found}}% |
| 612 |
}% |
| 613 |
\Tmp |
| 614 |
}% |
| 615 |
}% |
| 616 |
}{}% |
| 617 |
} |
| 618 |
|
| 619 |
% \end{macrocode} |
| 620 |
% \end{macro} |
| 621 |
% |
| 622 |
% |
| 623 |
% \section{The solution interface} |
| 624 |
% |
| 625 |
% \begin{macro}{\Newassociation} |
| 626 |
% Several solution file handles may have been defined. |
| 627 |
% You are limited only by the |
| 628 |
% number that \TeX\ will make available to you. Each solution environment |
| 629 |
% that is to write to one of these handles |
| 630 |
% must know which handle to write to, |
| 631 |
% and also what extra information to write there, apart from its contents. |
| 632 |
% This is done by setting up an association |
| 633 |
% between the source environment, |
| 634 |
% the destination environment and the file handle. |
| 635 |
% \begin{macrocode} |
| 636 |
\newcommand{\Newassociation}[3]{% |
| 637 |
\newsolution{#2}% |
| 638 |
\expandafter\ifx\csname #3opentrue\endcsname\relax |
| 639 |
\expandafter\newif\csname if#3open\endcsname |
| 640 |
\fi |
| 641 |
\newenvironment{#1}{% |
| 642 |
\Ifanswerfiles{% |
| 643 |
\let\Tmp\relax |
| 644 |
\Iffileundefined{#3}{}{% |
| 645 |
\Ifopen{#3}{% |
| 646 |
\immediate\write\@nameuse{#3@file}% |
| 647 |
{\string\begin{#2}\@nameuse{#2params}}% |
| 648 |
\def\Tmp{\Filesave{#3}}% |
| 649 |
}{}% |
| 650 |
}% |
| 651 |
}{% |
| 652 |
\edef\Tmp{\noexpand\begin{#2}\@nameuse{#2params}}% |
| 653 |
}% |
| 654 |
\csname pre#1\endcsname |
| 655 |
\Tmp |
| 656 |
}% |
| 657 |
{% |
| 658 |
\Ifanswerfiles{% |
| 659 |
\Iffileundefined{#3}{}{% |
| 660 |
\Ifopen{#3}{% |
| 661 |
\endFilesave% |
| 662 |
\immediate\write\@nameuse{#3@file}% |
| 663 |
{\string\end{#2}}% |
| 664 |
\csname post#1\endcsname |
| 665 |
}{}% |
| 666 |
}% |
| 667 |
}{% |
| 668 |
\end{#2}% |
| 669 |
}% |
| 670 |
}% |
| 671 |
} |
| 672 |
% \end{macrocode} |
| 673 |
% \end{macro} |
| 674 |
% |
| 675 |
% \begin{macro}{\newsolution} |
| 676 |
% The default destination environment in the solution file is defined to |
| 677 |
% take a single parameter, a reference number inherited from the source |
| 678 |
% environment. This is set with style |\solutionstyle|, which defaults |
| 679 |
% to |\textbf|. In addition, solution type |yyy| can have markup added |
| 680 |
% before and after it by defining |\preyyy| and |\postyyy| suitably, |
| 681 |
% eg, a rule across the width of the page or a square. |
| 682 |
% If anything more sophisticated is intended, it is probably better to |
| 683 |
% |\renewenvironment{yyy}| to achieve it. |
| 684 |
% \begin{macro}{\Currentlabel} |
| 685 |
% \begin{macrocode} |
| 686 |
\newcommand{\newsolution}[1]{% |
| 687 |
\@ifundefined{#1}{% |
| 688 |
\global\@definecounter{#1}% |
| 689 |
\global\@namedef{#1params}{{\Currentlabel}}% |
| 690 |
\newenvironment{#1}[1]% |
| 691 |
{% |
| 692 |
\refstepcounter{#1}% |
| 693 |
\csname pre#1\endcsname |
| 694 |
\trivlist |
| 695 |
\item[\hskip\itemsep{\@nameuse{#1label}{##1}}]}% |
| 696 |
{\csname post#1\endcsname\endtrivlist}% |
| 697 |
\global\@namedef{#1label}##1{\solutionstyle{##1}}% |
| 698 |
}{\typeout{WARNING: environment #1 already in use}}% |
| 699 |
} |
| 700 |
\newcommand{\solutionstyle}[1]{\textbf{#1}} |
| 701 |
\newcommand{\Currentlabel}{\@currentlabel} |
| 702 |
% \end{macrocode} |
| 703 |
% The format of the label for solution environment |xxx| is governed |
| 704 |
% by the command |\xxxlabel|, which takes one argument by default. |
| 705 |
% The argument is passed to it by the command |\xxxparams|, |
| 706 |
% which expands to |{\Currentlabel}|, a synonym for |{\@currentlabel}|, |
| 707 |
% and this argument is written automatically by the source environment. |
| 708 |
% The label appears in boldface by default. |
| 709 |
% We could easily change the behaviour of this environment by changing |
| 710 |
% these two commands. For example |
| 711 |
% \begin{verbatim} |
| 712 |
% \renewcommand{\xxxlabel}[1]{\emph{Solution to #1}} |
| 713 |
% \renewcommand{\xxxparams}{{\Currentlabel(p.\thepage)}} |
| 714 |
% \end{verbatim} |
| 715 |
% would provide a number and page reference in italic. |
| 716 |
% |
| 717 |
% More complicated behaviour could be produced by redefining the |xxx| |
| 718 |
% environment itself to take a different number of parameters. Note |
| 719 |
% however that |\xxxparams| must be redefined to provide those parameters. |
| 720 |
% \end{macro} |
| 721 |
% \end{macro} |
| 722 |
% |
| 723 |
% Which brings us to the end of the |answers| package. |
| 724 |
% \begin{macrocode} |
| 725 |
%</answers> |
| 726 |
% \end{macrocode} |
| 727 |
% |
| 728 |
% |
| 729 |
% |
| 730 |
% \Finale |
| 731 |
% |
| 732 |
\endinput |
| 733 |
% |