% \iffalse
% ^^A Version and date information needed in doc- and cls-files:
\def\fileversion{3.25}
\def\filedate{1995/12/17}
%
%% File: exam.dtx Copyright (C) 1993-1994 Hans van der Meer
%
%<*driver>
\documentclass{ltxdoc}
\IfFileExists{exam.cls}{\def\noexamples{}}%
  {\ClassWarningNoLine{ltxdoc}{Install exam.cls first}%
  \def\noexamples{\fbox{\huge Install exam.cls first}\end{document}}}
  \makeatletter
  \let\old@maketitle=\maketitle
  \let\LoadClass=\@gobble
  \input{exam.cls}%
  \let\maketitle=\old@maketitle
  \makeatother
%
%  ^^A  We do want an index
%
%\OnlyDescription\nofiles
\CodelineIndex
\EnableCrossrefs\RecordChanges
\DisableCrossrefs
%
\begin{document}
\title{The \textsf{exam} package}
\author{Hans van der Meer\\hansm@fwi.uva.nl}
\date{vs.\fileversion\space-\ \filedate}
\maketitle
%
% \begin{abstract}
% This article describes the use and the implementation of the 
% \emph{exam class}.
% Its purpose is the typesetting of exams.
% Exam questions can be multiple choice or free long\slash short
% answer questions.
% Options are the typesetting of the exam itself, an exam
% showing all the answers and a collection of questions and answers.
% Questions can be parametrized.
% Use of a random generator provides for automatic shuffling
% of multiple choice items.
% \end{abstract}
%
\noexamples
\title{The \textup{exam} package}
\DocInput{exam.dtx}
\end{document}
%</driver>
% \fi
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% \changes{3.00}{1994/02/13}{First version for LaTeX2E and docstrip}
% \changes{3.01}{1994/03/24}{added mbox{} to Copyright (missing item error)}
% \changes{3.10}{1994/10/19}{updated several features}
% \changes{3.11}{1994/10/21}{added dumpitemno and ignorespace in SRset}
% \changes{3.12}{1994/10/25}{changed pagenumbering index}
% \changes{3.13}{1994/11/10}{help shows class options}
% \changes{3.14}{1994/11/24}{empty default for mainfolder, etc.}
% \changes{3.15}{1994/12/10}{default language initialization added}
% \changes{3.16}{1995/01/19}{require latest latex because of box trouble}
% \changes{3.17}{1995/01/26}{maketitle redefinition better in exam.cfg}
% \changes{3.18}{1995/02/02}{options, documentation, checksquare->boxtimes}
% \changes{3.19}{1995/03/10}{pagestyle tuned, added options}
% \changes{3.20}{1995/07/12}{table of contents problem solved}
% \changes{3.21}{1995/08/07}{writing toc-entry in question displaced}
% \changes{3.22}{1995/08/09}{folders default set to @currdir}
% \changes{3.23}{1995/10/26}{help standard, textbo removed, small changes}
% \changes{3.24}{1995/10/30}{ignorespaces added to longanswer start}
% \changes{3.25}{1995/12/17}{error in altanswer repaired}
%
% \CheckSum{930}
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%% \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         \~}
%
%  ^^A  We don't want everything to appear in the index
%
% \DoNotIndex{\#}
% \DoNotIndex{\@@input}
% \DoNotIndex{\@dblarg}
% \DoNotIndex{\@depth}
% \DoNotIndex{\@empty}
% \DoNotIndex{\@firstoftwo}
% \DoNotIndex{\@height}
% \DoNotIndex{\@ifstar}
% \DoNotIndex{\@ifundefined}
% \DoNotIndex{\@m}
% \DoNotIndex{\@makeother}
% \DoNotIndex{\@namedef}
% \DoNotIndex{\@ne}
% \DoNotIndex{\@sanitize}
% \DoNotIndex{\@secondoftwo}
% \DoNotIndex{\@tempdima}
% \DoNotIndex{\@tempdimb}
% \DoNotIndex{\@title}
% \DoNotIndex{\@warning}
% \DoNotIndex{\@whilenum}
% \DoNotIndex{\@width}
% \DoNotIndex{\\}
% \DoNotIndex{\{}
% \DoNotIndex{\}}
% \DoNotIndex{\^}
% \DoNotIndex{\ }
% \DoNotIndex{\addtolength}
% \DoNotIndex{\addvspace}
% \DoNotIndex{\advance}
% \DoNotIndex{\begin}
% \DoNotIndex{\begingroup}
% \DoNotIndex{\bgroup}
% \DoNotIndex{\bigskip}
% \DoNotIndex{\bigskipamount}
% \DoNotIndex{\box}
% \DoNotIndex{\catcode}
% \DoNotIndex{\count@}
% \DoNotIndex{\csname}
% \DoNotIndex{\def}
% \DoNotIndex{\dimen@}
% \DoNotIndex{\divide}
% \DoNotIndex{\do}
% \DoNotIndex{\dotfill}
% \DoNotIndex{\dp}
% \DoNotIndex{\edef}
% \DoNotIndex{\egroup}
% \DoNotIndex{\else}
% \DoNotIndex{\emph}
% \DoNotIndex{\end}
% \DoNotIndex{\endcsname}
% \DoNotIndex{\endgroup}
% \DoNotIndex{\endlist}
% \DoNotIndex{\enskip}
% \DoNotIndex{\enspace}
% \DoNotIndex{\ensuremath}
% \DoNotIndex{\expandafter}
% \DoNotIndex{\f@baselineskip}
% \DoNotIndex{\fbox}
% \DoNotIndex{\fi}
% \DoNotIndex{\gdef}
% \DoNotIndex{\global}
% \DoNotIndex{\hbox}
% \DoNotIndex{\hfil}
% \DoNotIndex{\hfill}
% \DoNotIndex{\hrule}
% \DoNotIndex{\hskip}
% \DoNotIndex{\hspace}
% \DoNotIndex{\hss}
% \DoNotIndex{\ht}
% \DoNotIndex{\if}
% \DoNotIndex{\ifcase}
% \DoNotIndex{\ifdim}
% \DoNotIndex{\ifnum}
% \DoNotIndex{\ifodd}
% \DoNotIndex{\ifx}
% \DoNotIndex{\ignorespaces}
% \DoNotIndex{\item}
% \DoNotIndex{\itemsep}
% \DoNotIndex{\leavevmode}
% \DoNotIndex{\let}
% \DoNotIndex{\list}
% \DoNotIndex{\llap}
% \DoNotIndex{\m@ne}
% \DoNotIndex{\makebox}
% \DoNotIndex{\mbox}
% \DoNotIndex{\medskip}
% \DoNotIndex{\medskipamount}
% \DoNotIndex{\multiply}
% \DoNotIndex{\newcommand}
% \DoNotIndex{\newcount}
% \DoNotIndex{\newcounter}
% \DoNotIndex{\newenvironment}
% \DoNotIndex{\newif}
% \DoNotIndex{\newlength}
% \DoNotIndex{\newpage}
% \DoNotIndex{\newsavebox}
% \DoNotIndex{\newtoks}
% \DoNotIndex{\next}
% \DoNotIndex{\noexpand}
% \DoNotIndex{\noindent}
% \DoNotIndex{\null}
% \DoNotIndex{\number}
% \DoNotIndex{\or}
% \DoNotIndex{\p@}
% \DoNotIndex{\par}
% \DoNotIndex{\parbox}
% \DoNotIndex{\parsep}
% \DoNotIndex{\partopsep}
% \DoNotIndex{\phantom}
% \DoNotIndex{\protect}
% \DoNotIndex{\providecommand}
% \DoNotIndex{\raggedright}
% \DoNotIndex{\relax}
% \DoNotIndex{\renewcommand}
% \DoNotIndex{\rlap}
% \DoNotIndex{\rmfamily}
% \DoNotIndex{\romannumeral}
% \DoNotIndex{\selectfont}
% \DoNotIndex{\setbox}
% \DoNotIndex{\setcounter}
% \DoNotIndex{\setlength}
% \DoNotIndex{\settowidth}
% \DoNotIndex{\sffamily}
% \DoNotIndex{\sloppy}
% \DoNotIndex{\small}
% \DoNotIndex{\smallskip}
% \DoNotIndex{\smallskipamount}
% \DoNotIndex{\space}
% \DoNotIndex{\stepcounter}
% \DoNotIndex{\string}
% \DoNotIndex{\strut}
% \DoNotIndex{\test}
% \DoNotIndex{\textbf}
% \DoNotIndex{\textemdash}
% \DoNotIndex{\textsl}
% \DoNotIndex{\texttt}
% \DoNotIndex{\the}
% \DoNotIndex{\thinspace}
% \DoNotIndex{\tw@}
% \DoNotIndex{\topsep}
% \DoNotIndex{\typeout}
% \DoNotIndex{\undefined}
% \DoNotIndex{\underbar}
% \DoNotIndex{\uppercase}
% \DoNotIndex{\upshape}
% \DoNotIndex{\vadjust}
% \DoNotIndex{\value}
% \DoNotIndex{\vbox}
% \DoNotIndex{\vfil}
% \DoNotIndex{\vfill}
% \DoNotIndex{\vskip}
% \DoNotIndex{\vspace}
% \DoNotIndex{\vss}
% \DoNotIndex{\wd}
% \DoNotIndex{\xdef}
% \DoNotIndex{\z@}
% \DoNotIndex{\z@skip}
%
%  ^^A  Start up the body of the document
%
% \tableofcontents
%
% \section{Usage}
%
% \subsection{Exam production}
%
% An exam can be built from the following template.
% For special issues as the use of default names for
% various directories, language selection, etc. see
% the implementation section.
% You may customize the typesetting by providing
% a file \emph{exam.cfg} in the search path; this file
% is read just before typesetting begins.
%
% \begin{verbatim}
% \documentclass[options]{exam}
%
% \title{title of exam}
% \author{the examinator}
% \target{the students}
% \begin{document}
% \begin{exam}[startvalue random generator]{date of exam}
% \question{directory}{file}
% \question[parameter value]{directory}{file} % parametrized
% ....
% \end{exam}
% ... % possibly other exams
% \end{document}
% \end{verbatim}
%
% \subsection{Format of a problem}
%
% A problem is built by environment |problem|.
% In it several elements can be placed. These are:
% \begin{enumerate}
% \item |\parameterproblem{text}|: used to communicate to the
% maintainer of the problems the possibilities offered
% by the transfer of macro |\parameter| on posing
% the question; an example of this will follow.
% Can also find a place before the problem declaration.
% \item |\begin{problem}[#1]|; the optional parameter
% can have the value |\compact| (no pagebreak within problem, default)
% or the value |\split| (pagebreak may occur in problem).
% \item |\problemdate{date}|: a macro to remember on which
% day the problem was born;
% \item |\score{value}|: use this macro for the number of
% points the answer is worth; it is possible to include
% several score items in one problem for partial rewards.
% The score value is not shown when an exam is typeset,
% the student must earn these points!
% \item text of the question.
% \item the answer or multiple choice list;
% see the description below.
% \item |\remark[h]{b}|: a boxed remark with heading h and body b.
% \item |\end{problem}|.
% \end{enumerate}
%
% \medskip 
% \begin{center}\small
% \fbox{problem --- coding ---}
% \par\smallskip
% \begin{verbatim}
% \begin{problem}
% \problemdate{\today}
% What is the question?
% \score{2}
% \shortanswer{To be or not to be.}
% \end{problem}
% \end{verbatim}
%
% \medskip
% \fbox{problem --- result --- 
% left without \emph{answers}, right with \emph{answers, series}}
% \par\smallskip
% \parbox[t]{.33\linewidth}{\answersfalse
% \begin{problem}
% \problemdate{\today}
% What is the question?
% \score{2}
% \shortanswer{To be or not to be.}
% \end{problem}
% }\hspace{2cm}\parbox[t]{.33\linewidth}{\answerstrue\seriestrue
% \begin{problem}
% \problemdate{\today}
% What is the question?
% \score{2}
% \shortanswer{To be or not to be.}
% \end{problem}
% }
% \end{center}
%
% The next example shows the use of a parameter for the selection
% of alternate questions.
%
% \medskip 
% \begin{center}\small
% \fbox{parameterproblem --- coding ---}
% \par\smallskip
% \begin{verbatim}
% \parameterproblem{1= to be\\2= not to be}
% \begin{problem}
% \problemdate{\today}
% \score{2}
% \def\parameter{2} % normally comes from \question[2]...
% What is\ifnum\parameter=1\relax\else n't\fi\ the question?
% \shortanswer{\ifnum\parameter=1\relax To be or n\else N\fi ot to be.}
% \end{problem}
% \end{verbatim}
%
% \medskip
% \fbox{parameterproblem --- result --- 
% left parameter = 1, right parameter = 2}
% \par\smallskip
% \parbox[t]{.45\linewidth}{\answerstrue\seriestrue
% \parameterproblem{1= to be\\2= not to be}
% \begin{problem}
% \problemdate{\today}
% \score{2}
% \def\parameter{1}
% What is\ifnum\parameter=1\relax\else n't\fi\ the question?
% \unskip\shortanswer{\ifnum\parameter=1\relax 
% To be or n\else N\fi ot to be.}
% \end{problem}
% }\hspace{10mm}\parbox[t]{.45\linewidth}{\answerstrue\seriestrue
% \parameterproblem{1= to be\\2= not to be}
% \begin{problem}
% \problemdate{\today}
% \score{2}
% \def\parameter{2}
% What is\ifnum\parameter=1\relax\else n't\fi\ the question?
% \unskip\shortanswer{\ifnum\parameter=1\relax 
% To be or n\else N\fi ot to be.}
% \end{problem}
% }
% \end{center}
%
% Another method for the selection of alternatives is via the
% mechanism implemented in macros |\loaditem|
% and friends (See sections~\ref{ref:dump1} and \ref{ref:dump2}.
% Pieces text can be loaded (in this implementation at most 5) and
% selectively dumped into the typeset input stream.
%
% Macro |\loaditem| can be used to load from one to five
% items in a data store. This data store can be shuffled
% by a call to |\shuffle|. Popping items from the store
% is effected by macros |\dumpitem| (pop one item) and
% |\dumpitems| (all items). Clearing of the store
% is done by |\resetloadcounter|.
% With |\SRtest{1}{2}| one can make a random choice between two
% alternatives.
%
% \medskip 
% \begin{center}\small
% \fbox{load and dump --- coding ---}
% \par\smallskip
% \begin{verbatim}
% \SRset{349}              % startup random generator
% \resetloadcounter        % initialize load stack
% \loaditem{\fbox{item 1}\space} % load 4 items of text
% \loaditem{\fbox{item 2}\space}
% \loaditem{\fbox{item 3}\space}
% \loaditem{\fbox{item 4}\space}
% Here comes nr~2: \dumpitemno{2}\par
% \shuffle                 % randomize
% Here comes nr~2 again after randomization: \dumpitemno{2}\par
% Dump the whole lot: \dumpitems
% \end{verbatim}
%
% \medskip
% \fbox{load and dump --- result ---}
% \par\smallskip
% \parbox[t]{.8\linewidth}{%
% \SRset{349}
% \resetloadcounter
% \loaditem{\fbox{item 1}\space}
% \loaditem{\fbox{item 2}\space}
% \loaditem{\fbox{item 3}\space}
% \loaditem{\fbox{item 4}\space}
% Here comes nr~2: \dumpitemno{2}\par
% \shuffle
% Nr~2 again after randomization: \dumpitemno{2}\par
% Dump the whole lot: \dumpitems
% }
% \end{center}
%
%
% \subsection{Answers}
% The basic macros for showing and suppressing answers are
% |\answer| that shows its argument when the \emph{answers}
% option is chosen, and |\altanswer| that alternates its
% two arguments. Both macros have a first, optional argument
% for specifying the width of the box wherein the text
% is placed.
%
% \medskip 
% \begin{center}\small
% \fbox{answer/altanswer --- coding ---}
% \par\smallskip
% \begin{verbatim}
% \answer[.3\linewidth]{answer}\\
% \altanswer[.3\linewidth]{answers NO}{answers YES}
% \end{verbatim}
%
% \medskip
% \fbox{answer/altanswer --- result --- 
% left without \emph{answers}, right with \emph{answers}}
% \par\smallskip
% \parbox[t]{.4\linewidth}{\answersfalse
% \answer[.3\linewidth]{answer}\\
% \altanswer[.3\linewidth]{answers NO}{answers YES}
% }\qquad\parbox[t]{.4\linewidth}{\answerstrue
% \answer[.3\linewidth]{answer}\\
% \altanswer[.3\linewidth]{answers NO}{answers YES}
% }
% \end{center}
%
% Some questions can be answered by a few words, a short sentence.
% The command |\shortanswer| serves this purpose;
% its first argument is optional and used for the width of
% the answerbox, its second argument is the answer itself.
%
% \medskip 
% \begin{center}\small
% \fbox{short answer --- coding ---}
% \par\smallskip
% \begin{verbatim}
% Answer the next question:
% \shortanswer[.35\linewidth]{The answer.}
% \end{verbatim}
%
% \medskip
% \fbox{short answer --- result --- 
% left without \emph{answers}, right with \emph{answers}}
% \par\smallskip
% \parbox[t]{.4\linewidth}{\answersfalse
% Answer the next question:
% \shortanswer[.35\linewidth]{The answer.}}
% \qquad\parbox[t]{.4\linewidth}{\answerstrue
% Answer the next question:
% \shortanswer[.35\linewidth]{The answer.}}
% \end{center}
%
% When however more space is needed by the student, the
% environment |longanswer| can be used. 
% This environment has one optional parameter, meant
% for specifying the amount of white space to be reserved
% for the students answer. 
%
% \medskip 
% \begin{center}\small
% \fbox{long answer --- coding ---}
% \par\smallskip
% \begin{verbatim}
% Answer the next question:
% \begin{longanswer}[5mm]
% The answer.
% \end{longanswer}
% \end{verbatim}
%
% \medskip
% \fbox{long answer --- result --- 
% left without \emph{answers}, right with \emph{answers}}
% \par\smallskip
% \parbox[t]{.4\linewidth}{\answersfalse
% Answer the next question:
% \begin{longanswer}[5mm]
% The answer.
% \end{longanswer}}
% \qquad\parbox[t]{.4\linewidth}{\answerstrue
% Answer the next question:
% \begin{longanswer}[5mm]
% The answer.
% \end{longanswer}}
% \end{center}
%
% The answer is headed by a call to |\answerstart|; redefine
% to your taste.
%
% The default of white space reserved for the depth of the short answer
% can be changed by redefinition of |\shortwhite|.
% The default for the white space of the long answer
% can be changed by redefinition of |\longwhite|.
%
% \subsection{Multiple Choice}
%
% Multiple choice is provided for by environment |choice|.
% Within this environment a itemized list of alternatives is given.
% However instead of |\item| one codes |\baditem{text}| for wrong answers
% and |\gooditem{text}| for the correct one; the answer being put
% into the argument of these two macros.
% The optional parameter of this environment can be |\ordered| for
% production of the alternatives in the order specified, or
% |\random| for randomization; randomize is the default, unless
% the \emph{series} option is specified in the |\documentclass| call.
%
% \medskip 
% \begin{center}\small
% \fbox{multiple choice example --- coding ---}
% \par\smallskip
% \begin{verbatim}
% Choose the most appropriate alternative:
% \begin{choice}[\ordered]
% \baditem{first wrong answer}
% \gooditem{the right answer}
% \baditem{second wrong answer}
% \end{choice}
% \end{verbatim}
%
% \medskip
% \fbox{multiple choice --- result --- 
% left without \emph{answers}, right with \emph{answers}}
% \par\smallskip
% \parbox[t]{.4\linewidth}{\answersfalse
% Choose appropriate alternative:
% \begin{choice}[\ordered]
% \baditem{first wrong answer}
% \gooditem{the right answer}
% \baditem{second wrong answer}
% \end{choice}}
% \qquad\parbox[t]{.4\linewidth}{\answerstrue
% Choose appropriate alternative:
% \begin{choice}[\ordered]
% \baditem{first wrong answer}
% \gooditem{the right answer}
% \baditem{second wrong answer}
% \end{choice}}
% \end{center}
%
% The marks for the multiple choice items are produced
% by the macros |\badmark| and |\goodmark|. For their
% redefinition see the implementation section of this
% document.
%
% \StopEventually
%
% \newpage
%    \begin{macrocode}
%<*package>
%    \end{macrocode}
%
% \section{Identification}
%
% This document class can only be used with \LaTeXe, so we make
% sure that an appropriate message is displayed when another \TeX{}
% format is used. We require the latest version that has no known
% troubles with this class.
%    \begin{macrocode}
\NeedsTeXFormat{LaTeX2e}[1995/06/01]
%    \end{macrocode}
%
% Announce the Class name and its version.
%    \begin{macrocode}
\ProvidesClass{exam}[\filedate\space vs\fileversion\space Exam package]
%    \end{macrocode}
%
% Define |\examerror| and |\examwarning| to issue a proper 
% |\ClassError| or |\ClassWarning| in case of errors.
% Note that the error macro provides for a help text in its 
% second argument.
% \begin{macro}{\examerror}
% \begin{macro}{\examwarning}
%    \begin{macrocode}
\newcommand\examerror[2]{\ClassError{exam}{!!!! #1}{#2}}
\newcommand\examwarning[1]{\ClassWarning{exam}{!!!! #1}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \section{Declaration of Class Options}
%
% In this part we define the options for this class that are additional
% to those of its parent class.
%
% \subsection{Switching answers on and off}
%
% The flag |\ifanswers| governs the production of answers in the
% typesetting of problems. With the |answers| options in the
% optional argument of the document class this option is turned on.
% Then also we show the score values.
%
% \begin{macro}{\ifanswers}
%    \begin{macrocode}
\newif\ifanswers
\DeclareOption{answers}{\answerstrue\scorestrue}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Each problem on separate page}
%
% The flag |\ifpages| governs the typesetting of problems 
% on separate pages, or their collection of more than one to a page.
% If separate pages are chosen, a separator between problems
% is unnecessary.
%
% \begin{macro}{\ifproblempages}
%    \begin{macrocode}
\newif\ifproblempages
\DeclareOption{pages}{\problempagestrue\problemsepfalse}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Suppress prompt for answer}
%
% The flag |\ifreservespace| governs the typesetting of space 
% for answers. If true, all reservation of answer space is
% suppressed. It is set by option |questiononly|;
% this option has no effect when the |answers| option is on.
%
% \begin{macro}{\ifreservespace}
%    \begin{macrocode}
\newif\ifreservespace
\reservespacetrue
\DeclareOption{questiononly}{\reservespacefalse}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Visible separation between problems}
%
% The value of flag |\ifproblemsep| determines the appearance 
% of a visible separation between successive problems.
%
% \begin{macro}{\ifproblemsep}
%    \begin{macrocode}
\newif\ifproblemsep
\problemseptrue
\DeclareOption{nosep}{\problemsepfalse}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Showing score values}
%
% The flag |\ifshowscores| determines when score values are printed.
%
% \begin{macro}{\ifscores}
%    \begin{macrocode}
\newif\ifscores
\DeclareOption{scores}{\scorestrue}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Typeset a Catalogue of Problems}
%
% The flag |\ifseries| initiates the production of a problem catalogue.
% In order to show answers and score values, the respective flags are set.
%
% \begin{macro}{\ifseries}
%    \begin{macrocode}
\newif\ifseries
\DeclareOption{series}{\seriestrue\answerstrue\scorestrue}
%    \end{macrocode}
% \end{macro}
%
% \section{Loading of Parent Class}
%
% Since the \emph{exam class} is implemented as a modification
% of an existing document class, we must load the parent class.
% In order to make changes in parent class easy, the
% name of this class is parametrized in macro |\parentclass|.
% Obvious candidates are \emph{article} and \emph{report}.
% In order to provide some flexibility, we allow for the case
% that the user has already defined |\parentclass| (before
% the call to |\documentclass|. In that case we refrain
% from redefinition.
%
% \begin{macro}{\parentclass}
%    \begin{macrocode}
\providecommand\parentclass{article}
%    \end{macrocode}
% \end{macro}
%
% The options of the |\documentclass| call which are not specific for the
% \emph{exam class} must be passed to the parent class.
% We take the opportunity to select the production of a titlepage 
% (not automatically added if the parent class is \emph{article}.
% After this we process the local options.
%
%    \begin{macrocode}
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{\parentclass}}
\PassOptionsToClass{titlepage}{\parentclass}
\ProcessOptions
%    \end{macrocode}
%
% Show options to the user
%
%    \begin{macrocode}
  \ClassWarningNoLine{exam}{%
    documentclass `exam' version \fileversion, date \filedate.
      \MessageBreak
    available options:
      \MessageBreak
    answers:\space show questions with answers;
      \MessageBreak
    nosep:\space no separators between problems;
      \MessageBreak
    pages:\space each problem on a page;
      \MessageBreak
    questiononly:\space suppress answer space in exams;
      \MessageBreak
    scores:\space typeset score values always;
      \MessageBreak
    series:\space\space typeset catalogue of problems}
%    \end{macrocode}
%
% Then we load the parent class.
%
%    \begin{macrocode}
\LoadClass{\parentclass}
%    \end{macrocode}
%
% At the end of the document we produce a table of contents
% when appropriate.
%
%    \begin{macrocode}
\ifanswers\AtEndDocument{\cleardoublepage\tableofcontents}\fi
%    \end{macrocode}
%
% \section{Produce an Exam}
%
% First we need a counter for exams, since in one run more than
% one exam can be produced.
% By stepping this counter we will effect the automatic reset of
% the counter that numbers the problems and 
% the counter that remembers the score value.
%
% \begin{macro}{\examnum}
%    \begin{macrocode}
\newcounter{examnum}
%    \end{macrocode}
% \end{macro}
%
% Exams are produced within the |exam| environment. This environment takes
% 2 parameters. The first one is optional and provides the initial value
% of the random generator.\footnote{Not used when a series
% is run.} The default is 0, which effectively shuts off randomness.
% The second parameter must be present, but can be empty.
% It fixes the date for which the exam is planned; an empty argument
% fills in the current date.
%
% \begin{environment}{exam}
%    \begin{macrocode}
\newenvironment{exam}[2][0]{%
  \stepcounter{examnum}%
  \@ifemptyarg{#2}{}{\date{#2}}%
%    \end{macrocode}
%
% When answers are requested we start with a titlepage\footnote{%
% If not inhibited by the |notitlepage| option.} and
% a table of contents.
% In the case of exam production, typesetting of the titlepage 
% is deferred to the end of the exam,
% so that we may print on it the number of problems.
% We write a few messages to the table of contents (date and initial 
% value of the random generator) when an exam with answers 
% is in production.
%
%    \begin{macrocode}
  \ifanswers
    \pagenumbering{roman}%
    \maketitle\newpage\mbox{}\newpage
  \fi
  \pagenumbering{arabic}%
  \ifseries\SRset{0}\else
    \SRset{#1}%
    \addtocontents{toc}{\protect\contentsline{section}%
      {\Exam~\theexamnum~\textemdash~\@date~%
      \textemdash~random start #1}{}}%
  \fi
%    \end{macrocode}
%
% In each separate exam the first page gets the number one.
%
%    \begin{macrocode}
  \setcounter{page}{1}}%
%    \end{macrocode}
%
% At the end of the exam produced for the students
% a titlepage is produced. If answers are given for an exam
% we provide the total value of the scores.
%
%    \begin{macrocode}
  {\ifseries\else
    \typeout{Total value scores = \thetotalscore}%
    \ifanswers
      \addtocontents{toc}{\protect\contentsline{section}%
        {Total value scores = \thetotalscore}{}}%
    \else\maketitle
    \fi\fi}
%    \end{macrocode}
% \end{environment}
%
% \section{Choice of Problems}
%
% We start with a counter |\problemnum| with which the problems
% of the exam are neatly numbered. This counter is automatically
% reset each time a new |exam| environment is entered.
% A textual identification of the current problem is collected
% in token register |\problemid|.
%
% \begin{macro}{\problemnum}
% \begin{macro}{\problemid}
%    \begin{macrocode}
\newcounter{problemnum}[examnum]
\newtoks\problemid
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% Each question resides in its own file, which is called up
% by macro |\question|. Of its three parameters the first is
% optional and provides a means of communication with the
% problem itself. To achieve this the first 
% argument of |\question| is cached 
% in macro |\parameter|.\footnote{As most uses of this mechanism
% boil down to a choice between several alternatives, the
% number~1 is provided by macro {\ttfamily\protect\bslash parameterproblem}
% as a convenient default value. See also the discussion
% under the heading ``Parametrized Problems''.}
% The default behaviour here is not touching the
% definition of |\parameter| in case of an empty argument;
% in many cases a forgotten argument will then lead to
% a ``missing something'' error. The benefit of not
% touching |\parameter| in case of an empty argument
% is that this macro now also can be initialized by
% other means, e.g. by definition earlier in the problem coding.
%
% The second parameter of |\question| is the name of the (sub)directory
% where the file named in the third parameter can be found.
% This second parameter doubles up as section name in the
% series production.\footnote{It is silently assumed
% that all problems of a given category reside in a common
% directory.}
%
% \begin{macro}{\question}
%    \begin{macrocode}
\newcommand\question[3][]{%
  \@ifemptyarg{#1}{}{\renewcommand\parameter{#1}}%
%    \end{macrocode}
%
% When a series is run we look for the start of a new section and
% perform the appropriate actions if indeed a new section is found.
% I.e.\ eject the page and then reset the section name 
% and the problem counter.
% Note the use of uppercase in order to smooth out differences in typing.
% The identification of the problem is set to its file name and,
% in the case of a series, is mentioned in the output.
% Then the problem number is incremented. 
%
%    \begin{macrocode}
  \ifseries
    \uppercase{\def\@namesection{#2}}%
    \ifx\namesection\@namesection
    \else
      \newpage
      \global\let\namesection=\@namesection
      \addcontentsline{toc}{subsection}{\namesection}%
      \setcounter{problemnum}{0}%
    \fi   
  \fi
  \problemid={\uppercase{#3}}%
  \ifseries
    \noindent\underbar{\emph{File\,:}~\texttt{\the\problemid}}\par
    \nopagebreak\medskip\nopagebreak
  \fi
  \stepcounter{problemnum}%
%    \end{macrocode}
%
% If appropriate a summary of this problem is written to the table of contents.
%
%    \begin{macrocode}
  \ifanswers
    \addcontentsline{toc}{subsection}%
      {\hbox to1cm{\theproblemnum:\hss}#3}%
  \fi
%    \end{macrocode}
%
% Reading of the problem itself is surrounded by calculations
% on the score that this question will bring.
% Scores are mentioned on the console except when a series is run.
% In a problem all contributions from the various parts of the
% problem are collected in counter |scorecounter|.
% At the end of the problem |totalscore| is 
% updated with this value.\footnote{%
% Note the resets for |totalscore| with |examnum|
% and |scorecounter| with |problemnum| in their declaration.}
% The code guards against typing errors in the name of the file.
%
%    \begin{macrocode}
  \IfFileExists{\mainfolder#2:#3}{\@@input \mainfolder#2:#3}%
    {\examwarning{File \mainfolder#2:#3 not found}}%
  \ifseries\else
    \addtocounter{totalscore}{\value{scorecounter}}%
    \typeout{\Problem\space\theproblemnum: score=\thescorecounter}%
  \fi
  }
%    \end{macrocode}
% \end{macro}
%
% Macro |\namesection| gets its initial value here:
% \begin{macro}{namesection}
%    \begin{macrocode}
\newcommand\namesection{\Collection}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Parametrized Problems}
%
% A parametrized problem gets its parameter from the first
% argument of macro |\question|, as already have been mentioned.
% This is effected by definition of macro |\parameter| to
% the value of that argument. 
% We add code here to (re)initialize this macro.
%
% \begin{macro}{\reset@parameter}
%    \begin{macrocode}
\newcommand\reset@parameter{\gdef\parameter{}}
\reset@parameter
%    \end{macrocode}
% \end{macro}
%
% The first argument is optional and serves as default value
% for |\parameter|. The default value for this is the number~1.
% It is recommended that the macro |\parameterproblem| is
% placed in the problem before the use of |\parameter|.
% Then this macro provides a check on the proper definition of 
% |\parameter| when in exam production; if it is
% not yet defined this macro will receive the default value of 
% |\parameterproblem| and issue a warning.
% The macro will typeset its second argument in a framed box.
% Usually it tells the reader which options are available; however,
% only in the case a series is run, otherwise `silence' is the word.
%
% The description is placed in a framed |\parbox|, hanging with an indent
% from the intro text by macro |\remark|.
% 
% \begin{macro}{\parameterproblem}
%    \begin{macrocode}
\newcommand\parameterproblem[2][1 ]{%
  \ifx\parameter\@empty
    \ifseries\else\examwarning{\string\parameter\space undefined,
    value `#1' taken}\fi
    \renewcommand\parameter{#1}%
  \fi
  \remark[Parameter \Problem]{#2}}
%    \end{macrocode}
% \end{macro}

%
% \section{Typesetting a Problem}
%
% Each problem must be enclosed in an environment |problem|.
% Within this environment a default setup exists.
% By supplying code in token register |\everyproblem| one
% can influence the typesetting of each problem.
%
% \begin{macro}{\everyproblem}
%    \begin{macrocode}
\newtoks\everyproblem
%    \end{macrocode}
%
% The |problem| environment also has one optional parameter
% for specific adjustments of the options setting.
% Execution of options occurs in the order:
% default setup, possible modification by |\everyproblem| and
% final customization through the optional parameter.
% This mechanism provides for maximum flexibility.
% \end{macro}
%
% \begin{environment}{problem}
%    \begin{macrocode}
\newenvironment{problem}[1][]{%
%    \end{macrocode}
%
% Calling of options is guarded by a mechanism that makes them
% active when necessary. Only when the switch |\if@tagsforbid|
% is set false has calling of such an option any effect.
% The relevant macros are described in section~\ref{ref:tags}.
%
% Choose by default for keeping the whole problem on a page,
% execute any code in the token register and honor the
% option calls from the user. Finally shut off option processing.
%
%    \begin{macrocode}
  \@tagsforbidfalse\compact\the\everyproblem#1\@tagsforbidtrue
%    \end{macrocode}
%
% In order to keep everything on page we will enclose
% the problem in a |\vbox|, coded in
% macro |\@boxing|. Otherwise |\@boxing| is a noop and
% \TeX's pagebuilder can choose its breakpoint freely.
% For the declaration of |\@boxing| see section~\ref{ref:boxing}.
%
% The problem is typeset with a standard opening
% programmed in |\problemstart|, completing the
% opening manoeuvres of the environment.
%
%    \begin{macrocode}
  \@boxing\bgroup\noindent\problemstart\ignorespaces}%
%    \end{macrocode}
%
% After processing the body of the problem some postprocessing follows
% and the possible |\vbox| is closed by an |\egroup|.
%
% In particular a visual separation from the next problem is added,
% if not suppressed.
% In the case of series production the origin date
% of the problem is added too.\footnote{Only if it has been
% provided to it by the proper macro call, of course.}
%
%    \begin{macrocode}
  {\par\ifproblemsep
    \nopagebreak\smallskip\nopagebreak
    \hbox to\linewidth{\hrulefill
      \ifseries
        \emph{\footnotesize\thinspace\the\@problemdate}%
      \fi}\fi
  \egroup\par
%    \end{macrocode}
%
% Start a new page or separate the problem from the next one by a skip.
%
%    \begin{macrocode}
  \ifproblempages\newpage\else\bigskip\fi
%    \end{macrocode}
%  
% The origin date and the communicated value
% in macro |\parameter| are cleared for the next problem.
%
%    \begin{macrocode}
  \reset@problemdate\reset@parameter}
%    \end{macrocode}
% \end{environment}
% 
% \subsection{Code for Options to Problem}
% \label{ref:boxing}
%
% The options to |problem| are |\compact| or |\split|. 
% These options govern the possibility for the problem 
% to be split between successive pages or the necessity 
% to keep everything on page; the last one being the 
% favoured behaviour in this implementation.
% Note the |\noident| before the |\vbox| that prevents
% an unwanted shift to the right.
% 
% \begin{macro}{\compact}
% \begin{macro}{\split}
%    \begin{macrocode}
\newcommand\compact{\@chktag\compact\def\@boxing{\noindent\vbox}\fi}
\newcommand\split{\@chktag\split\def\@boxing{\@empty}\fi}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \label{ref:tags}The |\@chktag| macro is provided.
% Note the open ended |\if...\else| which will be closed
% by a |\fi| in the option definition.
%
% \begin{macro}{\if@tagsforbid}
% \begin{macro}{\@chktag}
%    \begin{macrocode}
\newif\if@tagsforbid \@tagsforbidtrue
\providecommand\@chktag[1]{%
  \if@tagsforbid\@warning{tag \string#1\space ignored here}\else}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Numbering the Problem}
%
% A problem gets a standard opening clause, coded in
% macro |\@problemstart|. The opening code is used to
% format the first paragraph with a nice indentation.\footnote{%
% This indentation is also used in the left margin in multiple
% choice listings in order to limit the variation in margins.}
%
% \begin{macro}{\@problemstart}
% \begin{macro}{\problemstart}
%    \begin{macrocode}
\newcommand\@problemstart{%
  \textbf{\Problem\,\ifnum\value{problemnum}<10 \phantom{0}\fi
  \theproblemnum}.\enskip}
\newcommand\problemstart{%
  \hangafter-2\settowidth\hangindent{\@problemstart}%
  \noindent\llap{\@problemstart}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Date of Origin}
%
% The user may specify an original date or date of last change
% for the problem to be printed when a series is produced.
% The global assignments are here just in case things happen in
% a deeper nested level.
%
% \begin{macro}{\@problemdate}
% \begin{macro}{\problemdate}
% \begin{macro}{\@resetproblemdate}
%    \begin{macrocode}
\newtoks\@problemdate
\newcommand\problemdate[1]{\global\@problemdate={#1}\ignorespaces}
\newcommand\reset@problemdate{\global\@problemdate={}}
\reset@problemdate
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsection{Score Values}
%
% Associated with each problem are of course the benefits the
% student receives for a good answer to (part of) the problem.
% The |\score| macro exists for this purpose.
% If answers are not included, just an empty square is printed
% into which the teacher can express his satisfaction with
% the answer given. When answers are included in the printout
% the each call |\score{value}| shows up in the right margin
% of the document.\footnote{At the end of each problem a summary
% of its total score plus a grand total are presented
% on the console.}
%
% These counters collect the values. Note that |\totalscore|
% is reset for each new exam and |\scorecounter| for each problem.
%
% \begin{macro}{\totalscore}
% \begin{macro}{\scorecounter}
%    \begin{macrocode}
\newcounter{totalscore}[examnum]
\newcounter{scorecounter}[problemnum]
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% The next commands are used for the production of the box
% for the score value.
%
% \begin{macro}{\scoreboxsize}
% \begin{macro}{\scorebox}
%    \begin{macrocode}
\newcommand\scoreboxsize{6mm}
\newcommand\scorebox[1]{%
  \fbox{\vbox to\scoreboxsize{\vss\hbox to
    \scoreboxsize{\hss#1\hss}\vss}}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% Finally the next code puts the score box on paper.
% It takes the value of the score as its argument and adds
% it to the running sum for this problem.
%
% \begin{macro}{\score}
%    \begin{macrocode}
\newcommand\score[1]{%
  \addtocounter{scorecounter}{#1}%
  \rightnote{\scorebox{\ifscores#1\fi}}%
  \ignorespaces}
%    \end{macrocode}
% \end{macro}
%
% We do not use |\marginpar| for the placement of the score values,
% because we do not want these items wandering around, as
% |\marginpar|'s sometimes do.
% The optional argument to the |..note| macros specifies
% a vertical amount to be skipped in the placement of the note.
% We use |\providecommand| because we may have them
% incorporated already in other packages.
% Note the |\noindent| before the |\vadjust| that also
% leaves vmode for the benefit of the |\vadjust|.
%
% \begin{macro}{\leftnote}
% \begin{macro}{\rightnote}
% \begin{macro}{\@rlnote}
%    \begin{macrocode}
\providecommand\leftnote[1][-\baselineskip]{\@rlnote l{#1}}
\providecommand\rightnote[1][-\baselineskip]{\@rlnote r{#1}}
\providecommand\@rlnote[3]{%
  \noindent\vadjust{\vbox to\z@{%
    \vskip#2\leftskip\z@skip\rightskip\z@skip\noindent
    \if#1l\llap{#3\hskip\marginparsep}%
    \else\hfill\rlap{\hskip\marginparsep#3}\fi
    \vss\vskip\z@skip}}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsection{Adding remarks}
%
% In making a catalogue of problems (option \emph{series} selected) 
% it is useful when remarks can be added that stand out against the rest
% of the text. Macro |\remark| provides such a mechanism.
% Its first (optional) argument is set emphasized, its second argument
% hangs on the first. The complete remark is placed
% in a |\parbox| and then boxed and centered.
%
% \begin{macro}{\remark}
%    \begin{macrocode}
\newcommand\remark[2][]{%
  \ifseries
    \begin{center}%
    \fbox{\parbox{.9\linewidth}{%
    \sloppy\hangafter\@ne
    \setbox\z@=\hbox{\emph{#1}\@ifemptyarg{#1}{}{:~}}%
    \hangindent=\wd\z@
    \strut\box\z@ #2}}%
    \end{center}\nopagebreak
    \addvspace{5mm}\nopagebreak
  \fi}
%    \end{macrocode}
% \end{macro}
%
% \section{Answers}
%
% In this section various ways of typesetting answers are provided.
%
% We start with two definitions for long and short stretches of white
% space. These are meant for leaving room for the students answer.
%
% \begin{macro}{\longwhite}
% \begin{macro}{\shortwhite}
%    \begin{macrocode}
\newcommand\longwhite{25mm}
\newcommand\shortwhite{8mm}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% Just as with the typesetting of the problem, we provide
% a macro to start an answer. Note that the text is
% parametrized in order to keep switching to other
% languages simple.
%
% \begin{macro}{\answerstart}
%    \begin{macrocode}
\newcommand\answerstart{\noindent\emph{\Answer}:\enspace}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Switching Answer On and Off}
% 
% Macro call |\answer| holds the answer and shows it when
% answers are requested. The optional first argument specifies
% a width for the box into which the typesetting takes places.
% The answer is centered by default; change it with |\hfil|'s.
% Implementation of |\answer| is by the next macro |\altanswer|. 

% \begin{macro}{\answer}
%    \begin{macrocode}
\newcommand\answer[2][]{\altanswer[#1]{}{#2}}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Alternating Some Stuff and Answer}
% 
% With |\altanswer| the text alternates between two possibilities:
% the first one is typeset when answers are suppressed, the second
% one for the opposite case. Optional width argument and placement
% are the same as for |\answer|.
%
% \begin{macro}{\altanswer}
%    \begin{macrocode}
\newcommand\altanswer[3][]{%
  \@ifemptyarg{#1}%
    {\mbox{\ifanswers#3\else#2\fi}}%
    {\makebox[#1]{\ifanswers#3\else#2\fi}}%
  }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Problem with a Short Answer}
% 
% A question ``Give a short answer to \ldots'' is formatted
% in |\shortanswer|. Usually the answer will fit on one line.
% In the exam a row of dots is produced, otherwise the answer will show.
% The optional argument provides the width of the box into which
% the data are typeset.
%
% \begin{macro}{\shortanswer}
%    \begin{macrocode}
\newcommand\shortanswer[2][.75\linewidth]{\par
  \ifanswers
    \addvspace{\smallskipamount}%
    \answerstart\parbox[t]{#1}{\noindent #2}%
    \par\medskip
  \else\ifreservespace
    \addvspace{\shortwhite}%
    \answerstart\makebox[#1]{\dotfill}%
    \par\medskip
   \fi\fi
  }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Problem with a Long Answer}
% 
% For elaborate questions, problems, etc.\ an environment is available.
% The |longanswer| environment takes as optional argument the length
% of white to be reserved for the student.
%
% Code for opening of the environment. 
% It opens a box in order to let the answer disappear
% and places a rule in order to guarantee sufficient
% white space.
%
% \begin{environment}{longanswer}
%    \begin{macrocode}
\newenvironment{longanswer}[1][\longwhite]{
  \par
  \ifanswers
    \addvspace{\medskipamount}\answerstart
    \nopagebreak\par\noindent
  \else\ifreservespace
      \addvspace{\medskipamount}\answerstart
      \nopagebreak\par\noindent
      \hrule\@height#1\@width\z@\par
    \fi
    \setbox\z@\vbox\bgroup\leavevmode
   \fi\ignorespaces}
%    \end{macrocode}
%
% Aftermath of |longanswer|. If necessary close the box
% and empty it to get rid of the answer.
%    \begin{macrocode}
  {\ifanswers\par\medskip\else\egroup\setbox\z@\hbox{}\fi}
%    \end{macrocode}
% \end{environment}
%
% \section{Multiple Choice Questions}
%
% Multiple choice problems must be placed
% in an |choice| environment, a modification
% of |itemize|.
%
% \label{ref:dump1}We will make it possible to 
% shuffle the items of a multiple
% choice problem randomly. These items are held in a series
% of token registers declared below.
%
% We need a counter into which to keep the number of items
% at any time loaded into the token registers declared above.
% Also we provide for resetting, incrementing and decrementing
% of this register. Note the global assignments.
%
% \begin{macro}{\loadcounter}
% \begin{macro}{\resetloadcounter}
% \begin{macro}{\incloadcounter}
% \begin{macro}{\decloadcounter}
%    \begin{macrocode}
\newcount\loadcounter
\newcommand\resetloadcounter{\global\loadcounter\z@}
\newcommand\incloadcounter{\global\advance\loadcounter\@ne}
\newcommand\decloadcounter{\global\advance\loadcounter\m@ne}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% We want a specific behaviour when the list of items is typeset.
% However, we cannot be sure at which listlevel this will occur.
% Therefore we predeclare a replacement for |\@listi|, |\@listii|,
% or whatsoever, and swap the |\@list..| at the right time.
% Note the choice for the leftside margin, derived from the
% width of the text with which the problem starts. This choice
% diminishes the number of different margins. It is easily adapted
% to your own taste.
%
% \begin{macro}{\@listk}
%    \begin{macrocode}
\newcommand\@listk{%
  \settowidth{\leftmargin}{\@problemstart}%
  \topsep\medskipamount
  \partopsep\z@
  \itemsep\smallskipamount
  \parsep\z@}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Typesetting Multiple Choice}
%
% The multiple choice environment |choice| takes one argument,
% the modifier options to the environment typesetting.
% Here the options are |\random| and |\ordered|; the names
% speak for themselves. Note that random permutation is not
% executed if a series is run. Furthermore the counter
% for the number of items loaded is reset.
%
% \begin{environment}{choice}
%    \begin{macrocode}
\newenvironment{choice}[1][]{%
  \@tagsforbidfalse\ifseries\ordered\else\random\fi#1\@tagsforbidtrue
  \resetloadcounter
%    \end{macrocode}
%
% The following code is taken from \LaTeX's |itemize|.
% I did not find a more elegant way to bend this environment
% to my whims.
%
%    \begin{macrocode}
  \ifnum\@itemdepth>3 \@toodeep \else
  \advance\@itemdepth\@ne
  \expandafter\let
    \csname @list\romannumeral\the\@itemdepth\endcsname=\@listk
  \list{\badmark}{\def\makelabel##1{\hss\llap{##1}}}%
  \fi}%
%    \end{macrocode}
%
% At the end of |choice| we dump all the items that may have been
% collected inbetween and finish the |list|.
%
%    \begin{macrocode}
  {\@dumpitems\endlist}
%    \end{macrocode}
% \end{environment}
% 
% \subsection{Code for Options to Choice}
%
% The option |\random| codes macros |\@loaditem| and
% |\@dumpitems| so that the items are actually loaded,
% then shuffled and dumped afterwards.
% The |\ordered| option makes them noops and thus the
% items will be typeset on the fly.
%
% \begin{macro}{\random}
% \begin{macro}{\ordered}
%    \begin{macrocode}
\newcommand\random{\@chktag\random
    \def\@loaditem{\loaditem}%
    \def\@dumpitems{\shuffle\dumpitems}%
  \fi}
\newcommand\ordered{\@chktag\ordered
    \def\@loaditem{}%
    \def\@dumpitems{}%
  \fi}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% 
% \subsection{Formatting the Item Mark}
%
% We require two marks: one for the bad guys and one
% for the good guy. We use the two symbols |\square| and |\boxtimes|,
% but provide replacements (later on, after giving exam.cfg a chance
% to define them) in case these are undefined.
% Typeset these marks in fixed size (unchanged baselineskip) 
% provided by macro |\marksize|.
%
% \begin{macro}{\marksize}
% \begin{macro}{\badmark}
% \begin{macro}{\goodmark}
%    \begin{macrocode}
\newcommand{\marksize}{\fontsize{12}{\f@baselineskip}\selectfont}
\newcommand{\badmark}{{\marksize\ensuremath{\square}}}
\newcommand{\goodmark}%
  {\ifanswers{\marksize\ensuremath{\boxtimes}}\else\badmark\fi}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% Each item can either be right or wrong. We take the precaution
% to suppress the difference when typesetting the actual exam.
% Enclose each item in your list in the argument to
% |\gooditem| and |\baditem|. They
% will load the item in memory prior to (possible) random shuffling.
%
% \begin{macro}{\baditem}
% \begin{macro}{\gooditem}
%    \begin{macrocode}
\newcommand{\baditem}[1]{\@loaditem{\item[\badmark]#1}}
\newcommand{\gooditem}[1]{\@loaditem{\item[\goodmark]#1}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Loading and Dumping Items}
% \label{ref:dump2}
%
% This series of token registers
% can hold five alternatives. The mechanism that loads the
% items is sufficiently general to use it for other purposes too.
% Use your imagination!
% That there are five of them is remembered in a definition
% because we will need this number to prevent overfilling the store.
%
% \begin{macro}{\@itemA}
% \begin{macro}{\@itemB}
% \begin{macro}{\@itemC}
% \begin{macro}{\@itemD}
% \begin{macro}{\@itemE}
% \begin{macro}{\@itemstore}
%    \begin{macrocode}
\newtoks\@itemA
\newtoks\@itemB
\newtoks\@itemC
\newtoks\@itemD
\newtoks\@itemE
\newcommand\@itemstore{5}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% According to the value of |loadcounter| the token registers
% |\@itemA|, etc.\ are filled. Argument to macro |\loaditem|
% is the contents of the item.
%
% \begin{macro}{\loaditem}
%    \begin{macrocode}
\newcommand\loaditem[1]{%
  \ifcase\loadcounter
    \@itemA={#1}%
    \or\@itemB={#1}%
    \or\@itemC={#1}%
    \or\@itemD={#1}%
    \or\@itemE={#1}%
  \fi
  \ifnum\loadcounter<\@itemstore \incloadcounter
  \else\examwarning{\string\loaditem\space ignored, too many}\fi}
%    \end{macrocode}
% \end{macro}
%
% Produce items that were loaded.
%
% \begin{macro}{\dumpitemno}
%    \begin{macrocode}
\newcommand\dumpitemno[1]{%
  \ifnum#1>\loadcounter
    \examwarning{\string\dumpitemno[#1] ignored, out range}%
  \else\ifcase#1\relax
    \or\the\@itemA
    \or\the\@itemB
    \or\the\@itemC
    \or\the\@itemD
    \or\the\@itemE
  \fi\fi}
%    \end{macrocode}
% \end{macro}
%
% With |\dumpitem| the last one comes out and is chopped off
% from the stack, with |\dumpitems| the whole lot is dumped.
% By means of |\dumpitemno| one can peek inside the stack:
% its parameter gives the position to be produced, the item itself
% remains on the stack.
% \begin{macro}{\dumpitem}
% \begin{macro}{\dumpitems}
%    \begin{macrocode}
\newcommand\dumpitem{\dumpitemno{\loadcounter}\decloadcounter}
\newcommand\dumpitems{\@whilenum\loadcounter>\z@\do{\dumpitem}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsubsection{Shuffling Items}
%
% This macro permutes |loadcounter| items in the
% token registers |\@itemA|, etc. Undoubtedly it
% can be done better, but who's perfect?
%
% \begin{macro}{\shuffle}
%    \begin{macrocode}
\newcommand\shuffle{%
  \ifcase\loadcounter
    \or
    \or\shuffle@ii
    \or\shuffle@\@itemA\@itemC \shuffle@ii \shuffle@\@itemB\@itemC
    \or\shuffle@iv
    \or\shuffle@\@itemD\@itemE \shuffle@iv \shuffle@\@itemD\@itemE
    \fi
  }
%    \end{macrocode}
% \end{macro}
%
% Random interchange of two and four items.
%
% \begin{macro}{\@item@}
% \begin{macro}{\shuffle@}
% \begin{macro}{\shuffle@ii}
% \begin{macro}{\shuffle@iv}
%    \begin{macrocode}
\newtoks\@item@
\newcommand\shuffle@[2]{\SRtest{}{\@item@=#1 #1=#2 #2=\@item@}}
\newcommand\shuffle@ii{\shuffle@\@itemA\@itemB}
\newcommand\shuffle@iv{%
  \SRtest{\shuffle@\@itemA\@itemB}{\shuffle@\@itemC\@itemD}%
  \SRtest{\shuffle@\@itemA\@itemC}{\shuffle@\@itemB\@itemD}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsubsection{Random Generator Implementation}
%
% Not much commentary with these macros. They are
% described in Tugboat~1994, vol.~15.1, p.~57--58.
%
% \begin{macro}{\@SR}
% \begin{macro}{\@SRconst}
% \begin{macro}{\SRset}
% \begin{macro}{\@SRadvance}
% \begin{macro}{\SRbit}
% \begin{macro}{\SRtest}
% \begin{macro}{\SRvalue}
%    \begin{macrocode}
\ifx\@SR\undefined\newcount\@SR\fi
\providecommand\@SRconst{2097152}
\providecommand\SRset[1]{\global\@SR#1 \ignorespaces}
\providecommand\@SRadvance{%
  \begingroup
  \ifnum\@SR<\@SRconst\relax\count@\z@\else\count@\@ne\fi
  \ifodd\@SR\advance\count@\@ne\fi
  \global\divide\@SR\tw@
  \ifodd\count@\global\advance\@SR\@SRconst\relax\fi
  \endgroup}
\providecommand\SRbit{\@SRadvance\ifodd\@SR1\else0\fi}
\providecommand\SRtest[2]{\@SRadvance
  \ifodd\@SR#2\else#1\fi\ignorespaces}
\providecommand\SRvalue{\number\@SR }
\SRset{0}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \section{Page Style}
%
% For a page style |examheadings| is offered.
% Choose it by supplying to |\pagestyle|.
%
% \begin{macro}{\thehead}
% \begin{macro}{\ps@examheadings}
%    \begin{macrocode}
\newcommand\thehead{%
  \textsl{\@title\enspace:\enspace
  \ifseries\namesection\else\@date\fi}}
\newcommand\ps@examheadings{%
  \let\@oddfoot\@empty
  \let\@evenfoot\@empty
  \renewcommand\@oddhead{%
    \vbox{%
    \hbox to\textwidth{\headerfont\thehead\hfil\upshape\thepage}%
    \vskip1.5\p@
    \hrule\@height.5\p@\@width\textwidth
    }}%
  \let\@evenhead\@oddhead}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \section{Titlepage}
%
% With target we denote the group of students for whom
% the exam is meant. Define with |\target| and call up
% with |\@target|.
%
% \begin{macro}{\target}
%    \begin{macrocode}
\newcommand\target[1]{\gdef\@target{#1}}\def\@target{}
%    \end{macrocode}
% \end{macro}
%
% The titlepage is best set by a redefined |\maketitle|. Of course it 
% needs to be suppressed if the \texttt{notitlepage} option is given
% on the |\documentclass| call. Provide two versions, one
% for a real exam and one for collections and/or answers.
%
% \section{Miscellaneous}
%
% \subsection{Language Adjustments}
%
% Predefine all language specific macros, default is the English language.
%
% \begin{macro}{\Exam}
% \begin{macro}{\Collection}
% \begin{macro}{\Answers}
% \begin{macro}{\Answer}
% \begin{macro}{\Problem}
%    \begin{macrocode}
\newcommand\Exam{EXAM}
\newcommand\Collection{COLLECTION OF EXAMS}
\newcommand\Answers{ANSWERS}
\newcommand\Answer{Answer}
\newcommand\Problem{Problem}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}

% A small problem arose with the Babel system.
% If a language is not defined with |\addlanguage| an
% |\errmessage| interfers with the smooth processing.
% We avoid this problem with the following macro.
%
% \begin{macro}{\onlanguage}
%    \begin{macrocode}
\newcommand\onlanguage[4]{% {language}{current}{known}{unknown}
  \expandafter\ifx\csname l@#1\endcsname\relax
  #4\else\ifnum\csname l@#1\endcsname=\language #2\else#3\fi\fi}
%    \end{macrocode}
% \end{macro}
%
% Below follow macros that on the presence of the Babel system
% automatically redefine the language specific expressions.
%
% \subsubsection{English}
%
% Put here all english equivalents.
% \begin{macro}{\englishExamNames}
%    \begin{macrocode}
\newcommand\englishExamNames{%
  \renewcommand\Answers{ANSWERS}%
  \renewcommand\Answer{Answer}%
  \renewcommand\Exam{EXAM}%
  \renewcommand\Collection{COLLECTION OF EXAMS}%
  \renewcommand\Problem{Problem}%
  }
%    \end{macrocode}
% \end{macro}
%
% If appropriate set one of these languages at the start of the document.
%
%    \begin{macrocode}
\AtBeginDocument{\onlanguage{english}{\englishExamNames}{}{}}
\AtBeginDocument{\onlanguage{american}{\englishExamNames}{}{}}
%    \end{macrocode}
%
% \subsubsection{Dutch}
%
% Put here all dutch equivalents.
%
% \begin{macro}{\dutchExamNames}
%    \begin{macrocode}
\newcommand\dutchExamNames{%
  \renewcommand\Answers{ANTWOORDEN}%
  \renewcommand\Answer{Antwoord}%
  \renewcommand\Exam{TENTAMEN}%
  \renewcommand\Collection{TENTAMENBUNDEL}%
  \renewcommand\Problem{Opgave}%
  }
\AtBeginDocument{\onlanguage{dutch}{\dutchExamNames}{}{}}
%    \end{macrocode}
% \end{macro}
%
% \section{Initializations}
%
% \subsection{Fonts}
%
% Fonts for pageheader, body of the text and on the titlepage.
%
% \begin{macro}{\headerfont}
% \begin{macro}{\bodyfont}
% \begin{macro}{\titlefont}
%    \begin{macrocode}
\newcommand\headerfont{\rmfamily\small}
\newcommand\bodyfont{\sffamily}
\newcommand\titlefont{\rmfamily\upshape}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% And initialize to |\bodyfont|.
%
%    \begin{macrocode}
\bodyfont
%    \end{macrocode}
%
% \subsection{Directory Localization}
%
% Specify folders (directories) for the main source,
% a common folder for related material and a folder
% for e.g.\ EPSF figures etc.
%
% \begin{macro}{\Mainfolder}
% \begin{macro}{\Commonfolder}
% \begin{macro}{\Figuresfolder}
%    \begin{macrocode}
\newcommand\Mainfolder[1]{\def\mainfolder{#1}}
\newcommand\Commonfolder[1]{\def\commonfolder{#1}}
\newcommand\Figuresfolder[1]{\def\figuresfolder{#1}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% Initialize them
%
%    \begin{macrocode}
\Mainfolder{\@currdir}
\Commonfolder{\@currdir}
\Figuresfolder{\@currdir}
%    \end{macrocode}
%
% \subsection{Configuration File}
%
% Last, but not least, see if there is a configuration
% file \texttt{exam.cfg} and read it for the final adjustments.
%
%    \begin{macrocode}
\InputIfFileExists{exam.cfg}{}{}
%    \end{macrocode}
%
% \subsection{Macros Needed but Possibly Missing}
%
% Testing for the presence or absence of a parameter.
%
% \begin{macro}{\@ifemptyarg}
%    \begin{macrocode}
\providecommand\@ifemptyarg[1]{% {absence}{presence}
  \ifx\@empty#1\@empty
  \expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi}
%    \end{macrocode}
% \end{macro}
%
% Possibly these macros are still undefined:
% \begin{macro}{\square}
% \begin{macro}{\boxtimes}
%    \begin{macrocode}
\providecommand\square{\bigcirc}
\providecommand\boxtimes{\surd}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%    \begin{macrocode}
%</package>
%    \end{macrocode}
% \PrintIndex\PrintChanges
% \Finale
%
