% oxmathex.sty
% Style for formatting exam papers.  Provides automatic numbering of questions
% and figures, a simplified figure interface, roman and alpha itemized lists,
% a "standard" form for the title at the top of the page, automatic generation
% of "Turn Over" commands at the foot of odd pages, etc.

% This version provides one question per page when desired by using the 
% option `qnbyqn'.



% Simon Turner, March and June 1989 (based on article.sty)
% Modified and tidied up 19-22 January 1990
% Tiny standardisation 2 February 1990
% Paper number added 3 April 1991; modified 17 April 1991; remodified 14 Feb 94
% Blank rule removed from \tidy command, 17 April 1991
% Taken over and modified as a LaTeX2e package by David Handscomb, October 1995
% Minor correction, March 1996
% Further correction - no questions on front page, November 1996
% School code added July 1997 by Brian Stewart.
% Varied by Brian Stewart to provide one question per page 
% for drafting purposes using the option `qnbyqn', July 1997
%
% Varied to get School codes in format now prescribed March 1998
% Varied to match current Proctors' Requirements on placing of page nos 
% and margins March 1998

\ProvidesPackage{oxmathex}[1997/07/28 - W B Stewart %
- goes with Document Class `article']


%%These provide the standard size of  Oxford paper 1997

%\setlength{\textwidth}{6.2in}\setlength{\textheight}{8.8in}
%\setlength{\topmargin}{0pt}
%\setlength{\oddsidemargin}{0pt}\setlength{\evensidemargin}{0pt}

%%These provide the standard size of  Oxford paper 1998

\setlength{\textwidth}{6.2in}
\setlength{\textheight}{9.0in}
\setlength{\topmargin}{0in}
\setlength{\headsep}{0in}
\setlength{\oddsidemargin}{0pt}
\setlength{\evensidemargin}{0pt}

% The following are needed to provide the option. 
% singles is usually false, but true in the option.

\RequirePackage{ifthen}
\newboolean{singles}
\DeclareOption{qnbyqn}{\setboolean{singles}{true}}
\ProcessOptions




% Page style exam -- page number centred in the head, with bold "Turn Over"
% at the right end of the foot for odd pages.  Note that we *don't* want a
% "Turn Over" if there are no more pages -- the command \tidy after the last
% question in the document ensures this.

% It now also makes sure the last page is captioned that way so as to 
% avoid embarassments when the Schools lose a page...

% Paper number (an number which identifies this version of the paper) goes
% at the bottom left corner of all pages except the first; the first page has
% the paper number, and the base numbers of any other schools which sit this
% paper, in a vertical list at the top left corner.
%
%specimen \papernumber{2632}




% School code
%(four upper case letters used by the Exam Schools to identify
% the  Honour Schools of each paper...) plus the paper number
% These must appear on page 1, 
% not on the succeeding pages, 
% where the papernumber of the lead school suffices. 
%
%specimen  \schoolcode{DMAU 2632}


% Extra numbers
% Only for first page, and should be four upper case letters 
% plus the paper no for each extra Honour School with line dividers
%
% specimen \extranumbers{DMAS 2632\\DMAW 2632}


\gdef\pto{\textbf{Turn Over}}
\newlength{\wpto}\settowidth{\wpto}{\textbf{Turn Over}}
\gdef\bpto{\protect\makebox[\wpto]{}}

\def\schoolcode#1{\gdef\@schoolcode{#1}}      % sets the school code
\gdef\@schoolcode{}
\def\papernumber#1{\gdef\@papernumber{#1}}      % sets the paper number
\gdef\@papernumber{}
\def\extranumbers#1{\gdef\@extranumbers{#1}}    % sets any extra numbers
\gdef\@extranumbers{}

\def\ps@exam{\let\@mkboth\@gobbletwo
 \def\@paperno{\makebox[0pt][l]{\small\sf \@papernumber}}
% \def\@oddhead{\rm\hfil\thepage\hfil}
 \def\@oddhead{}
 \if@twoside
  \def\@evenfoot{\@paperno\hfil{\rm\thepage}\hfil}
 \else
  \def\@evenfoot{\@paperno\hfil{\rm\thepage}\hfil\pto}
 \fi
 \let\@evenhead\@oddhead
 \def\@oddfoot{\@paperno\hfil{\rm\thepage}\hfil\pto}}

% Page style firstexam -- as exam, 
% %%%%but no page numbers, 
% there is now a page number as prescribed Mar 98
% and exam numbers at
% the top left corner.  The paper numbers are put in a tabular environment --
% if there are any extra numbers, this will generate an "Overfull \vbox"
% warning because the tabular box is higher than \headheight.  However, the
% effect is exactly right -- the box extends below the header without
% affecting the spacing of the text below, so we just ignore the warning.

% The school code must appear in the same environment.

% -- Also a different `Turn over' command; there will be no questions on this
% page.


% We do not want footers of any sort on the qnbyqn option.
% but we need evenheaders as every page is `firstpage' in qnbyqn option.


\def\ps@firstexam{\let\@mkboth\@gobbletwo
 \def\@oddhead{\textsf{\small\begin{tabular}[t]{l}\@schoolcode %\\ \@papernumber
  \ifx\@empty\@extranumbers \else \\ \@extranumbers \fi
  \end{tabular}\hfil}}
  \ifthenelse{\boolean{singles}}{\def\@oddfoot{}}{%
                                 \def\@oddfoot{\hfil{\rm\thepage}\hfil%
\raisebox{1cm}[0cm][0cm]{
\makebox[0pt][r]{%
\textbf{Do not turn this page until you are told that you may do so}%
}}%end boxes
}}%
 \def\@evenhead{\textsf{\small\begin{tabular}[t]{l}\@schoolcode %\\ \@papernumber
  \ifx\@empty\@extranumbers \else \\ \@extranumbers \fi
  \end{tabular}\hfil}}
 \def\@evenfoot{}}

% Last page style to allow for Proggie instrn to specify last page


\def\ps@lastexam{\let\@mkboth\@gobbletwo
 \def\@paperno{\makebox[0pt][l]{\small\sf \@papernumber}}
% \def\@oddhead{\rm\hfil\thepage\hfil}
 \def\@oddhead{}
 \if@twoside
  \def\@evenfoot{\@paperno\hfil{\rm\thepage}\hfil%
\makebox[0pt][r]{\textbf{End of Last Page}}}
 \else
  \def\@evenfoot{\@paperno\hfil{\rm\thepage}\hfil%
\makebox[0pt][r]{\textbf{End of Last Page}\pto}}
 \fi
 \let\@evenhead\@oddhead
 \def\@oddfoot{\@paperno\hfil{\rm\thepage}\hfil\textbf{End of Last Page}\pto}}












\def\tidy{%
\gdef\pto{}%
\gdef\bpto{\mbox{}}%
\thispagestyle{lastexam}%
}

% Default set up for document

\pagestyle{exam} \pagenumbering{arabic} \onecolumn \raggedbottom
\setlength{\parindent}{0pt}

% Question environments.  The user uses "question"; this is interpreted in
% terms of "qblock".  Note that the argument to "question" is unused (and is,
% therefore, optional) -- it is merely a way for the user to tell which
% question is which! 

\newcounter{quests}
\setcounter{quests}{0}

\newenvironment{qblock}[2]%
{\vspace{#2}
 \begin{minipage}{\textwidth}
 \begin{itemize}\item[\textbf{#1}]}%
{\end{itemize}\end{minipage}}


% In the option each question begins on a new page, is dated 
% at least by TeXprocessing, and has a full paper title

\newenvironment{question}[1]%
{\addtocounter{quests}{1}
 \ifthenelse{\boolean{singles}}%
 {\newpage\message{This version \TeX{}'ed on \today}\ourmaketitle}{}%
 \begin{qblock}{\topicmark \arabic{quests}.}{\baselineskip}}%
{\end{qblock}\vspace{\baselineskip}}

% Topic marks.  \topic{}{} starts a new topic, complete with a mark which is
% prepended to the question number in future (and also resets the question
% counter).  \topicheader{} just puts up a heading for the new topic.

%% For qnbyqn option ensure these do not generate text between the qns
\ifthenelse{\boolean{singles}}{%First the option
\def\topicmark{}

\newcommand{\topic}[2]{
 \def\topicmark{#1}
 \setcounter{quests}{0}}

\newcommand{\topicheader}[1]{
 \def\topicmark{}}

}{%Now the standard case

\def\topicmark{}

\newcommand{\topic}[2]{
 \def\topicmark{#1}
 \setcounter{quests}{0}
 \par
 \vspace{\baselineskip}
 \textbf{\topicmark\@. {#2}}
 \par
 \nopagebreak}

\newcommand{\topicheader}[1]{
 \def\topicmark{}
 \par
 \vspace{\baselineskip}
 \textbf{#1}
 \par
 \nopagebreak}



}





% Figures.  \newfig{height} produces a new figure;  \nextfig and \lastfig
% produce text references to the next figure to be defined and the last one,
% respectively (e.g. \nextfig --> "Figure 3" while \lastfig --> "Figure 2").

\newcounter{figs}
\newcounter{tfctr}
\setcounter{figs}{1}

\newcommand{\pfig}[2]{\vspace{#1}\begin{center}#2\end{center}}
\newcommand{\newfig}[1]{\pfig{#1}{\nextfig}\addtocounter{figs}{1}}
\newcommand{\nextfig}{Figure {\arabic{figs}}}
\newcommand{\lastfig}{Figure\setcounter{tfctr}{\value{figs}}
 \addtocounter{tfctr}{-1}{\arabic{tfctr}}}

% Sure-fire roman [(i), (ii) etc.] and alpha [(a), (b)] itemized lists.  The
% ordinary LaTeX commands don't guarantee the type.  Use \itm instead of \item
% for each new item in the list.

\newcommand{\itm}{}

\newcounter{ritmctr}\renewcommand{\theritmctr}{\roman{ritmctr}}
\newenvironment{ritem}{\begin{itemize}\setcounter{ritmctr}{0}%
 \renewcommand{\itm}{\refstepcounter{ritmctr}\item[(\theritmctr)]}}%
{\end{itemize}}

\newcounter{aitmctr}\renewcommand{\theaitmctr}{\alph{aitmctr}}
\newenvironment{aitem}{\begin{itemize}\setcounter{aitmctr}{0}%
 \renewcommand{\itm}{\refstepcounter{aitmctr}\item[(\theaitmctr)]}}%
{\end{itemize}}

% A new environment to handle lists with variable labels (which may have
% variable length) sensibly. The argument to \begin{varitem} should be the
% the longest label present in the list, in the exact form in which it will be
% used. Each new item should start with \item[<text>] where <text> is the label
% for this item. <text> should end with \hfill if you want the labels to line
% up down the left rather than down the right (which is the default).

\newenvironment{varitem}[1]%
{\begin{list}{#1}{
 \settowidth{\labelwidth}{#1}
 \setlength{\leftmargin}{\labelwidth}
 \addtolength{\leftmargin}{\labelsep}
 \addtolength{\leftmargin}{2em}}}%
{\end{list}}

% Some commands used for laying out the title part on the first page -- they
% may, of course, also be used elsewhere in the document.

\newcommand{\blank}{\rule{\textwidth}{0cm}}     % Not used by title-setting
\newcommand{\erule}{\hrule\vspace{0.25in}}
\newcommand{\step}{\\[0.2in]}
\newcommand{\sstep}{\\[0.1in]}

% Define commands for actually laying out the title page.  The user specifies
% the parts of the exam page with \exam{...} etc., then uses \ourmaketitle (which
% is normally a command in article.sty, but not here) to actually generate the
% title block for the paper.  Note that we re-use the LaTeX command \title for
% our own devious purposes (but since we have our own \ourmaketitle anyway, this
% seems perfectly reasonable).

\def\exam#1{\gdef\xexam{#1}}
\def\school#1{\gdef\xschool{#1}}
\def\title#1{\gdef\xtitle{#1}}
\def\time#1{\gdef\xtime{#1}}
\def\message#1{\gdef\xmessage{#1}}
\def\topics#1{\gdef\xtopics{#1}}

\let\with=\sstep              % For separating schools in \schools.

% Make the default values for \xexam etc. all null

\gdef\xexam{}\gdef\xschool{}\gdef\xtitle{}\gdef\xtime{}\gdef\xmessage{}\gdef\xtopics{}

% Define the actual title-generating command.  Note that we have to be
% careful about null entries -- in fact, if \exam, \school, \title and \time
% are *all* null (which they are by default) we mustn't even do a
% \begin{center} ... \end{center}, 'cos LaTeX will whinge about having nothing
% in it.  Hence the need for \@dotitle.  Yuk.

\def\ourmaketitle{\par
 \begingroup
 \thispagestyle{firstexam}
 \ifx\@empty\xexam \ifx\@empty\xschool \ifx\@empty\xtitle \ifx\@empty\xtime
  \let\@dotitle=\relax \fi \fi \fi \fi
 \@dotitle
 \par
 \vspace{0.1in}
 \erule
 \ifx\@empty\xmessage {} \else \begin{center} \emph{ \xmessage} 
                               \end{center} \fi
%reinserted from section c style
\par
 \vspace{0.8in}
 \ifx\@empty\xtopics {} \else \begin{center} \textbf
 { \begin{tabular}{ll} \xtopics \end{tabular}} \end{center} \fi
%to here
 \endgroup
 \ifthenelse{\boolean{singles}}{}{\let\ourmaketitle=\relax\clearpage}
% \gdef\xexam{}\gdef\xschool{}\gdef\xtitle{}\gdef\xtime{}\gdef\xmessage{}
% (no real need to reset these values to null)
 }

\def\@dotitle{\begin{center}
 \bf
 \ifx\@empty\xexam {} \else \xexam \step \fi
 \ifx\@empty\xschool {} \else \xschool \step \fi
 \ifx\@empty\xtitle {} \else \xtitle \step \fi
 \ifx\@empty\xtime {} \else \xtime \step \fi
 \end{center}}


% Provide longer A4 pages for qnbyqn option

\ifthenelse{\boolean{singles}}{\setlength{\textheight}{10in}}{}


% Lastly a line to ensure that \maketitle in the shell acts correctly
% even in the option and does not generate an unwanted title page.

\ifthenelse{\boolean{singles}}{\let\maketitle=\relax}{\let\maketitle=\ourmaketitle}
