%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%    University of Oxford, Mathematical Institute LaTeX exam class
%    Created by K A Gillow, 21 December 2010
%    Last updated 27 February 2012
%
% this latex package is designed for use with the standard exam.cls
% exam.cls is standard in texlive and miktex
% exam.cls also available at 
%         http://www.ctan.org/tex-archive/macros/latex/contrib/exam/
%         http://math.mit.edu/~psh/#LaTeX
% exam.cls has numerous options, see the 90 page doc examdoc.pdf
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{oxmathexam}[2012/2/27 v1.3 Oxford Maths exam class]

%load in the main exam class with appropriate options
\LoadClass[11pt,a4paper]{exam}

%load in some needed packages 
\RequirePackage{ifthen,amsmath,amssymb,amsthm,bm,graphicx}

%turn on marks appearing at left in brackets and define total marks format
\addpoints
\marksnotpoints
\bracketedpoints
\totalformat{\textbf{Total marks for question \thequestion: \totalpoints}}
\newcommand*{\showtotalmarks}{\droptotalpoints}

%define a topic number prefix
\newcommand{\topicprefix}[1]{\def\thetopicprefix{#1}}
\topicprefix{}

%make the main question numbers bold
%+ add some space between questions in a way that will work with topic headings
%+ include topicprefix letter in question number
%+ adjust space for question number to allow for a topicprefix letter
%\renewcommand{\questionlabel}{\textbf{\thetopicprefix\thequestion.}}
%\renewcommand{\questionshook}{\settowidth{\leftmargin}{M10.\hskip\labelsep}%
%\labelwidth\leftmargin\advance\labelwidth-\labelsep}

%trick class into starting subpart on a new line even when the part is empty
%+ make subpart labels left justified
%+ print subparts in brackets
%\renewcommand{\subpartshook}{\hspace*{1mm}\def\makelabel##1{\rlap{##1}}}
\renewcommand{\subpartlabel}{(\thesubpart)}

% define newcommand for user set paper/exam codes
\newcommand*{\papercodes}[1]{\def\thepapercodes{#1}}
\papercodes{}

%increase gap between page header and page text and allow for 4 line header
\setlength{\headsep}{0ex}
\addtolength{\headheight}{4ex}
\addtolength{\footskip}{-4ex}
%\addtolength{\textheight}{-8ex}

%increase the printed page area width
\extrawidth{0.5cm}

%put papercodes in header of cover page
\coverlhead{\textsf{\thepapercodes}}

%put papercodes in footer of other pages
\lfoot{\textsf{\parbox[t]{5cm}{\thepapercodes}}}

%put page number on cover page
\covercfoot{Page 1 of \numpages}

%put page number at centre of other page footers
\cfoot{Page \thepage{} of \numpages}

% put turn over/end at right of other page footers 
% but nudged into the margin to align with the marks
\rfoot{\rlap{\hskip\rightmargin\hskip\@rightmargin\hskip-\rightpointsmargin
        \llap{\textbf{%
          \oddeven{\iflastpage{End of Last Page}{Turn Over}}%
                  {\iflastpage{End of Last Page}{}}%
        }}
}}

%define layout of main cover page information and then adjust margins
\newcommand{\makecoverpage}{
\begin{coverpages}
\vspace*{2cm}
\begin{center}
\textbf{
\theexam \\[0.5cm]
\theschool \\[1cm]
\hrule\vspace*{1cm}
\LARGE \thetitle \\[1cm]
\hrule\vspace*{1cm}
\normalsize \thedate \\[1cm]
}
\vspace{0.5cm}
\textit{\themessage}
\vfill
\textbf{Do not turn this page until you are told that you may do so}
\end{center}
\end{coverpages}
\addtocounter{page}{1}
}

% define newcommands for user set cover page details
\newcommand*{\exam}[1]{\def\theexam{#1}}
\newcommand*{\school}[1]{\def\theschool{#1}}
\renewcommand*{\title}[1]{\def\thetitle{#1}}
\renewcommand*{\date}[1]{\def\thedate{#1}}
\renewcommand{\message}[1]{\def\themessage{#1}}
\exam{}\school{}\title{}\date{}\message{}

%define a handy line spacing command for cover page items
\newcommand*{\coverskip}{\\[1ex]}

%define topicheaders format
\newcommand*{\topicheader}[1]{\vspace{4ex}\fullwidth{\hspace{-3ex}\Large \textbf{#1}}}


