summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/mathexam/mathexam.dtx
blob: 2a70cb88697f9da01b3d98d6b89eb1236ed4636e (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
% \def\EmailAddress{jhlavace@svsu.edu}
% \iffalse meta-comment
% 
% Program mathexam  
% Copyright (C) 2007 Jan Hlavacek
%
% This program may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.2
% of this license or (at your option) any later version.
% The latest version of this license is in
%    http://www.latex-project.org/lppl.txt
% and version 1.2 or later is part of all distributions of LaTeX 
% version 1999/12/01 or later.
%
% The Program's files are mathexam.dtx, mathexam.ins and mathexam.sty.
%
% Run latex with the input file mathexam.ins to generate the mathexam.sty 
% package file. Run latex with the input file mathexam.dtx to generate the 
% package documentation.
%
% \fi
%
% \CheckSum{179}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
% \iffalse
%<*driver>
\ProvidesFile{mathexam.dtx}
%</driver>
%<package,ls>\NeedsTeXFormat{LaTeX2e}
%<package>\ProvidesPackage{mathexam}
%<*package>
        [2007/07/30 v1.00 Package for typesetting exams]
%</package>
%<*driver>
\documentclass{ltxdoc}
\usepackage[nohdr]{mathexam}[2007/07/30]
\usepackage{hyperref}
\EnableCrossrefs         
\CodelineIndex
\RecordChanges
\begin{document}
\DocInput{mathexam.dtx}
\PrintChanges
\PrintIndex
\end{document}
%</driver>
% \fi
%
% \GetFileInfo{mathexam.dtx}
%
% \DoNotIndex{\newcommand,\newenvironment}
%
% \changes{1.00}{2007/07/30}{Rewrote as a .dtx file for distribution}
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% \title{\bf The \textsf{mathexam} Package\thanks{This document
%        describes the version number \fileversion, last
%        revised \filedate.}}
%
% \author{Jan Hlavacek\\ 
% \small Email: \texttt{\EmailAddress}}
% \date{30 July 2007}
% \maketitle
%
% \begin{abstract}
% This package can help you typeset exams (mostly in mathematics and related
% disciplines where students are required to show their calculations followed by
% one or more short answers). It provides commands for inclusion of space for
% calculations, as well as commands for automatic creation of ``answer spaces''.
% In addition, the package will automatically create page headers and footers,
% and will let you include instructions and space for students to put their
% name.  
% \end{abstract}
%  \thispagestyle{empty}
%  \tableofcontents
%  \section{Introduction}
%
% There are several classes and packages for typesetting exams in \LaTeX\
% available. However, I found out that none of them satisfy my needs.  Some of
% the classes and packages are very sophisticated, providing commands and
% environments for things like fill in the blank, true/false and multiple choice
% questions. In contrast, nearly all exams in my lower level undergraduate math
% classes (including exams I took myself as an undergraduate) have basically the
% same format: a list of questions, each followed by a vertical space for
% students to do their calculations, each optionally followed by one or more
% reserved spaces for students to write their final answers. 
%
% Some of my colleagues use various word-processing softwares to type their
% exams.  Because of limitations of these programs, they usually end up typing
% each questions, followed by bunch of blank lines, followed by something like 
% ``Answer underscore underscore underscore \ldots'', carefully inserting just
% the right number of blank lines so that the each question will be on the same
% page as its corresponding ``Answer:\ldots'', and the last ``Answer:\ldots'' on
% each page will be somewhat close to the bottom of the page.  This works fine
% as long as you don't change the questions, the font, margins, and as long as
% you always use the same printer to print the exam.  Every time you do anything
% that results in a change in pagination, you have to go back and insert or
% delete blank lines in order to have everything align correctly again. 
%
% \TeX\, with its stretchable vertical glue, can easily solve this
% problem.  This package provides a way for easy inclusion of vertical space
% after questions, as well as single or multiple ``answer spaces''. 
% It does not take care of things like numbering of questions (I prefer to use
% standard \LaTeX\ list making environments), tracking the number of points,
% etc.  
%
%  \section{Installation}
%
% To install this package, simply run \LaTeX\ with the input file
% \texttt{mathexam.ins} like this:
%  \begin{verbatim}
% $ latex mathexam.ins
%  \end{verbatim}
% That will create the file \texttt{mathexam.sty}. You need to move this file to
% a place where \LaTeX\ can find it. 
%
% To generate documentation for this package, run \LaTeX\ with the input file
% \texttt{mathexam.dtx} instead, like
%  \begin{verbatim}
% $ latex mathexam.dtx
%  \end{verbatim}
% to generate the documentation in the \texttt{.dvi} format, or
%  \begin{verbatim}
% $ pdflatex mathexam.dtx
%  \end{verbatim}
% to create a \texttt{pdf} file.
%
%  \section{Usage} \label{usage}
%
% To use the package, all you have to do is include |\usepackage{mathexam}| in
% the preamble of your document:
%  \begin{verbatim}
%  \documentclass[11pt]{article}
%  \usepackage{mathexam}
%  \end{verbatim}
%
% \DescribeMacro{nohdr}
% Normally, the \texttt{mathexam} package automatically generates headers and
% footers for each page, containing information about the exam. In order to do
% that, the package uses several other packages, namely \texttt{fancyhdr},
% \texttt{lastpage} and \texttt{ifthen}.  These packages have to be installed on
% your computer if you want the \texttt{mathexam} package generate headers.  If
% you don't have all of these packages, or if for some reason you don't want the
% \texttt{mathexam} package to generate headers (you have your own way to
% include headers, for example), you can call the \texttt{mathexam} package with
% \texttt{nohdr} option:
%  \begin{verbatim}
%  \documentclass[11pt]{article}
%  \usepackage[nohdr]{mathexam}
%  \end{verbatim}
%
% \subsection{Main Commands}
%
% \DescribeMacro{\answer} 
%  The command |\answer| inserts a stretchable vertical space followed by a
%  generic ``answer space'':
%  \begin{verbatim}
%   What is $1+1$? \answer
%  \end{verbatim}
% produces:
%
% What is $1+1$? \answer
%
% \DescribeMacro{\addanswer} 
%  The command |\addanswer| works just like |\answer| except that it does not
%  insert any extra vertical space.  It can be used for example in situation where we
%  need two ``answer spaces'' immediately following each other:
%  \begin{verbatim}
%   Product of two numbers equals 24, while their sum is 10.  What are the
%   numbers? \answer\addanswer
%  \end{verbatim}
% produces:
%
%   Product of two numbers equals 24, while their sum is 10.  What are the
%   numbers? \answer\addanswer
%
% \subsection{Optional Arguments}
%
% In the examples above, there is a stretchable vertical glue between the text
% of each problem and the ``answer space''. You cannot really see it in this
% document, since the problems are surrounded by other text and we let \LaTeX\
% decide where to break the page.  Normally, you would insert |\newpage| after
% several problems, which would make the problems nicely distributed on the
% page. 
% 
% Sometimes you want to make sure that certain problem has enough space for
% students to write down their work.  You can specify an exact amount of space between
% the text of the problem and the ``answer space'' using an optional argument
% with the |\answer| or |\addanswer| commands: 
%  \begin{verbatim}
%   What is $1+1$? \answer[1in]
%  \end{verbatim}
% produces:
%
% What is $1+1$? \answer[1in]
%
% Here, the space between the text of the problem and the ``answer space'' will
% be 1 inch.  In this aspect,  |\addanswer[1in]| will work the exact same way. 
% The argument can be any glue, for example if you want to include at least one inch,
% which can possibly stretch further, you can do |\answer[1in plus 1fill]|
%
% \subsection{Changing Labels for ``Answer Spaces''}
%
% Often you want to use different text instead of the default ``Answer:'' label
% for an ``answer space''.  This can easily be done with the ``stared'' version
% of the commands.  The commands |\answer*| and |\addanswer*| take one mandatory
% argument (in addition to the optional argument described above) with the text
% you want to use for the label.  For example
% \begin{verbatim}
% Find the first two derivatives of the function $f(x) = x^2\cos(x)$.  Simplify
% your answers as much as possible.  Show all your work.
% \answer*[1in]{$f'(x)=$}\answer*[1in]{$f''(x)=$}
% \end{verbatim}
% produces
%
% Find the first two derivatives of the function $f(x) = x^2\cos(x)$.  Simplify
% your answers as much as possible.  Show all your work.
% \answer*[1in]{$f'(x)=$}\answer*[1in]{$f''(x)=$}
%
% Notice that here, vertical spaces before both of the ``answer spaces'' will be
% 1 inch long.
% In the following example, the answer spaces for $x$ and $y$ will be right
% above each other:
% \begin{verbatim}
% If $x + y = 10$ and $2x - y = 8$, find $x$ and $y$.
% \answer*[1in]{$x=$}\addanswer*{$y=$}
% \end{verbatim}
% produces
%
% If $x + y = 10$ and $2x - y = 8$, find $x$ and $y$.
% \answer*[1in]{$x=$}\addanswer*{$y=$}
%
% \subsection{No ``Answer Space''}
%
% Sometimes you will have problems where the work is the answer, or the answer
% is too long to fit into a short ``answer space''. For that purpose, the
% package defines the |\noanswer| command.
%
% \DescribeMacro{\noanswer}
% This command will simply include a stretchable vertical space after the
% problem. Again, as with |\answer| and |\addanswer|, the command takes one
% optional argument, which is an optional length of the vertical space. 
%
% \subsection{Other Commands}
% 
% The package provides several other commands for things like identifying the
% exam, giving instructions to students, including space for student's name etc.
%
% \DescribeMacro{\ExamName}\DescribeMacro{\ExamClass}\DescribeMacro{\ExamHead}
% The commands |\ExamName|, |\ExamClass| and |\ExamHead| are used for
% identifying the exam.  They will determine how will the headers of the exam
% pages look like.  For example, in the preamble of your document you could
% specify
% \begin{verbatim}
% \ExamName{Final Exam}
% \ExamClass{Calculus III}
% \ExamHead{\today}
% \end{verbatim}
% The \texttt{mathexam} package will use the \texttt{fancyhdr} package to
% include this information in the page headers.
%
% \DescribeMacro{\ExamNameLine}
% The |\ExamNameLine| command can be used to include a line on which students
% can write their name:
% \begin{verbatim}
% \ExamNameLine
% \end{verbatim}
% produces
% 
% \ExamNameLine
% 
% \DescribeMacro{\ExamInstrBox}
% The command |\ExamInstrBox| lets you include some basic instructions to
% students taking the exam.  Example:
% \begin{verbatim}
% \ExamInstrBox{Please show all your work! Answers without supporting work will
% not be given credit. Write answers in spaces provided.  You have 1 hour and 50
% minutes to complete this exam.}
% \end{verbatim}
% produces
% 
% \ExamInstrBox{Please show all your work! Answers without supporting work will
% not be given credit. Write answers in spaces provided.  You have 1 hour and 50
% minutes to complete this exam.}
%
% \section{Notes}
% 
% \subsection{TODO}
% 
% There are several things I plan to improve in the future:
% \begin{itemize}
%    \item Some basic internationalization (right now everything is in English)
%    \item Add some code for printing point value of problems.
% \end{itemize}
% If you have any other suggestions, please contact me.
%
% \subsection{Bugs, Suggestions, Comments\ldots}
% 
% If you find any bugs, or have any suggestions, comments, patches etc.~please
% let me know at \texttt{\EmailAddress}.
% \StopEventually{}
%
% \section{Implementation}
%
% First we will process options:
%    \begin{macrocode}
\newif\ifExamHdr
\ExamHdrtrue
\DeclareOption{nohdr}{\ExamHdrfalse}
\ProcessOptions
%    \end{macrocode}
%
% If ExamHdr is true, we load some packages we need
%    \begin{macrocode}
\ifExamHdr
\RequirePackage{fancyhdr}
\RequirePackage{lastpage}
\RequirePackage{ifthen}
\fi
%    \end{macrocode}
%
% \begin{macro}{\ExamName}
% \begin{macro}{\ExamClass}
% \begin{macro}{\ExamHead}
% Here we will set up macros that handle exam headers and footers.  First we
% will define the three commands that provide a user interface:
%    \begin{macrocode}
\newcommand{\ExamName}[1]{\def\@xamname{#1}}
\newcommand{\ExamClass}[1]{\def\@xamclass{#1}}
\newcommand{\ExamHead}[1]{\def\@xamrighthdr{#1}}
%    \end{macrocode}
% Then we will initialize the internal macros to some default values:
%    \begin{macrocode}
\def\@xamname{\relax}
\def\@xamclass{\relax}
\def\@xamrighthdr{\relax}
%    \end{macrocode}
% If ExamHdr is true, set up the fancy headers:
%    \begin{macrocode}
\ifExamHdr
\pagestyle{fancy}

\lhead{\@xamclass}
\chead{\@xamname}
\rhead{Page \thepage\ of \pageref{LastPage}}

\rfoot{\ifthenelse{\value{page}=\pageref{LastPage}}{The End.}{Cont.}}
\cfoot{}
%    \end{macrocode}
% Handle the first page differently:
%    \begin{macrocode}
\AtBeginDocument{
\begin{center}
   \large\scshape \@xamclass \hfill \@xamname \hfill \@xamrighthdr
\end{center}}

\thispagestyle{empty}
\fi
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% 
% \begin{macro}{\answer}
% \begin{macro}{\addanswer}
% Prepare auxiliary commands for |\answer| and |\addanswer|.  First the regular
% (non-stared) version:
%    \begin{macrocode}
\newcommand{\answ@r}[1][\fill]{%
   \nopagebreak\vspace{#1}\par\nopagebreak\hfill\hbox to
   .5\columnwidth{Answer:\hrulefill}\vspace{\baselineskip}}

\newcommand{\addansw@r}[1][\baselineskip]{%
   \nopagebreak\vspace{#1}\par\nopagebreak\hfill\hbox to
   .5\columnwidth{Answer:\hrulefill}\vspace{\baselineskip}}
%    \end{macrocode}
%
% Then the stared version:
%    \begin{macrocode}
\newcommand{\answ@rstar}[2][\fill]{%
   \nopagebreak\vspace{#1}\par\nopagebreak\hfill\hbox to
   .5\columnwidth{#2\hrulefill}\vspace{\baselineskip}}

\newcommand{\addansw@rstar}[2][\baselineskip]{%
   \nopagebreak\vspace{#1}\par\nopagebreak\hfill\hbox to
   .5\columnwidth{#2\hrulefill}\vspace{\baselineskip}}
%    \end{macrocode}
%
% Now we will pot them together.  Look ahead to see if there is a star.  If
% there is, use the stared version: 
%    \begin{macrocode}
\def\answer{%
   \def\e@t*{}%
   \def\n@xt{\if\noexpand\myn@@xt*%
      \expandafter\expandafter\expandafter\answ@rstar\expandafter\e@t\else%
      \expandafter\answ@r\fi}%
   \futurelet\myn@@xt\n@xt}

\def\addanswer{%
   \def\e@t*{}%
   \def\n@xt{\if\noexpand\myn@@xt*%
      \expandafter\expandafter\expandafter\addansw@rstar\expandafter\e@t\else%
      \expandafter\addansw@r\fi}%
   \futurelet\myn@@xt\n@xt}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% 
% \begin{macro}{\noanswer}
%    \begin{macrocode}
\newcommand{\noanswer}[1][\fill]{\nopagebreak\vspace{#1}\par}
%    \end{macrocode}
% \end{macro}
%
% Finally, couple of very simple macros.  They could probably be made more
% interesting and flexible.
% \begin{macro}{\ExamNameLine}
%    \begin{macrocode}
\newcommand{\ExamNameLine}{%
\par
\vspace{\baselineskip}
Name:\hrulefill\relax
\par}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ExamInstrBox}
%    \begin{macrocode}
\newcommand{\ExamInstrBox}[1]{\begin{center}\vspace{\baselineskip}%
   \fbox{\fbox{\parbox{.8\hsize}{#1}}}\end{center}}
%    \end{macrocode}
% \end{macro}
%
% \Finale

\endinput