%
% csexam.sty
%
% LaTeX package for producing University of Manchester, School of Computer Science exams.
% Example of usage is provided in the accompanying file csexamex.tex
%
%
% Version release date: 4/4/19
\newcommand{\@versiondate}{4/4/19}

\typeout{   ******************************************} 
\typeout{   *** csexam.sty: Version \@versiondate. ***} 
\typeout{ *** Latest version located at
 http://studentnet.cs.manchester.ac.uk/resources/latex/CSExam/  ***}
\typeout{ *** Please check for updates ***}
\typeout{   **************************************} 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
% Author:  
% Graham Gough, School of Computer Science <Graham.Gough@manchester.ac.uk>
% using some ideas from mathexam.sty, created by Chris Paul, School of Mathematics
% 
%  6/10/10
%  First release
%
%  18/10/10
%  Changed \mks to use Knuth TeX book version to fix end of line problem
%
%  27/10/10
%  Added missing unit code header
%  +  tweaked footers
%  + added \school command  to enable change of School
%  + added \noindent to \mks to exit vertical mode (bug report by PJJ)
%  + reinstated mkscheme environment for more complicated marking schemes
%
%  28/10/10
%  Added mkscheme* environment for interpolated solutions
%
%  29/10/10
%  Fixed bug in page footers for documents longer than 10 pages!
%
%  1/11/10
%  Changed footers so that PTO only appears on odd pages
%  + added \clearpage to \maketitle 
%  + added date and version header to ms version
%  
%  2/11/10
%  Added boxes to distinguish marking schemes and removed italics
%
%  3/11/10
%  Added commands \msonly and \examonly for version specific
%  material (eg \clearpage)
%
%  5/11/10
%  Added options environment for MCQs, see examples in example file
%  + Restructured example into two master files with common body as
%      model for probable use
%
%  31/1/11
%  Added feedback environment and fb option to include examiner's feedback
%
%  7/2/11
%  Added some extra space between questions, question parts and between
%  paras within parts.
%
%  15/2/11
%  Changed definition of sectionheader to use centering, to allow multiline headers
%
%  16/2/11
%  Added \raggedright to marking scheme macros and reduced space after
%  \answer headers.
%
%  18/2/11
%  Fixed missing rubric in mark scheme version bug!
%
%  23/10/12
%  Added question and part number to mkscheme* 
%
%  16/06/13
%  Changed PTO instruction
%
%  26/03/14
%  Disabled hyphenation
%
%  10/03/16
%  Fixed bug in [ size in Next page macro
%
%  1/12/17
%  Added mkscheme** which allows another level of counter to
%  mkscheme* (ACS request)
%  Changed example mechanism for large font paper.
%
%  17/2/19
%  Added comment about non-ascii characters to example.
%
%  4/4/19
% Enclosed marks in mbox to prevent line break. Mainly issue in large font version.
% Added largefont option
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage[margin=30mm]{geometry}
\usepackage{pslatex}
\usepackage{enumerate}
\usepackage{graphicx}
\usepackage[none]{hyphenat}

\usepackage{ifthen}
\newboolean{@ms}
\setboolean{@ms}{false}
\newboolean{@fb}
\setboolean{@fb}{false}
\newboolean{@lf}
\setboolean{@lf}{false}

\usepackage{lastpage}

\usepackage{boxedminipage}
\setlength{\fboxsep}{3mm}

%% Headers and footers
\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\headrulewidth}{0pt}
\setlength{\headheight}{15pt}
\fancyfoot[c]{\normalsize\textbf{Page \thepage\ of \pageref{LastPage}}\ifthenelse{\thepage=\pageref{LastPage}}{\\ \textbf{END OF EXAMINATION}\\}{}}
\fancyfoot[ro]{\ifthenelse{\thepage=\pageref{LastPage}}{}{\large\textbf 
      [Next page $\rightarrow$]}}
\fancyhead[l]{\ifthenelse{\boolean{@ms} \AND {\thepage=1}}{\footnotesize \today: (csexam.sty ver. \@versiondate)}{}}
\fancyhead[r]{\normalsize\textbf{\@coursecode}}


% === Macros relating to front page.

\def\coursecode#1{\gdef\@coursecode{#1}}
\coursecode{Coursecode not specified} % Default

\def\duration#1{\gdef\@duration{#1}}
\duration{Duration not specified} % Default

\def\information#1{\gdef\@information{#1}}
\information{\ } % Default

\def\@@coursename#1{\gdef\@coursename{{#1}}}
\def\coursename#1{\@@coursename{#1}}
\coursename{Coursename not specified} % Default

\def\examinationdate#1{\gdef\@examinationdate{#1}}
\examinationdate{Examination date not specified} % Default

\def\examinationtime#1{\gdef\@examinationtime{#1}}
\examinationtime{Examination time not specified} % Default

\def\rubric#1{\gdef\@rubric{#1}}
\rubric{Rubric not specified}

\def\calculators#1{\gdef\@calculators{#1}}
\calculators{The use of electronic calculators is permitted provided they
are not programmable and do not store text.}

\def\@@school#1{\gdef\@school{{#1}}}
\def\school#1{\@@school{#1}}
\school{COMPUTER SCIENCE} % Default

% Redefine \maketitle, so that use of this style file is
% similar to use of article.

\def\maketitle{
   \mbox{}\vspace{-3\baselineskip}%
   \begin{center}%
       \underline{\bfseries \@duration}\\[2\baselineskip]%
       \textit{\@information}\\[3\baselineskip]%
      \textbf{ UNIVERSITY OF MANCHESTER\\SCHOOL OF \@school}\\[3\baselineskip]%
      {\large\@coursename}\\[0.6\baselineskip]%
      \textbf{\@examinationdate}\\[0.6\baselineskip]
     \textbf{Time: \@examinationtime}\\[1.5\baselineskip]
    \ifthenelse{\boolean{@fb}}{%
     \begin{boxedminipage}[t]{0.7\textwidth}%
        \center \textbf{\Large Marker's feedback version}%
        \ifthenelse{\boolean{@ms}}{\\[2ex]\Large \textbf{Marking Scheme Included}}{}%
      \end{boxedminipage}\\[4ex]%
 %   }%
  }{% else
     \ifthenelse{\boolean{@ms}}{%
     \begin{boxedminipage}[t]{0.7\textwidth}%
          \center \bfseries \Large Marking Scheme Included\\[2ex]%
           Do not publish 
       \end{boxedminipage}
       \\[4ex]
     }{}}%
   \@rubric
   \end{center}
   \vspace{2.5\baselineskip}
   \hrule
  \begin{center}{\@calculators}\end{center}
   \vspace{6mm}
   \hrule
   \clearpage
}% end of \maketitle

\ifnum\@ptsize<2 \@latexerr
{Should be using 12pt option, %
 e.g.: \string\documentclass[12pt]{article}}\fi

%%%%%%%

% Redefine the \section command so that sections start on new page
% Reset question number counter so that we get B1, B2, ....
\renewcommand\section{\addtocounter{section}{1}
                      \newpage
                      \mbox{}
                      \\[-2.5\baselineskip]
                      \centerline{\underline{\large \textbf{Section \@Alph\c@section}}}\vspace{\baselineskip}}


% Define command for adding extra commentary immediately at start of sections
% Do NOT remove the \\[0mm] commands - they do actually do something!
\def\sectionheader#1{~\\[0mm]\noindent{\centering{\large #1}\\}}

% Argument text is made bold and uppercase, and then underlined.
\def\jazzy#1{\underline{\bfseries \uppercase{#1}}}

% mks command: used to indicate mark breakdown
% cf TeX book p 102
% do not put a par break between the end of the question and the \mks command. 
%
\def\mks #1 {{\noindent\unskip\nobreak\hfil\penalty50\hskip2em\hbox{}\nobreak
  \hfil \mbox{(#1 \ifnum#1<2  mark\else marks\fi)}\parfillskip=0pt \par}}


% questions and parts list environments
\newenvironment{questions}{\begin{enumerate}[1.]\setlength{\itemsep}{5ex}}{\end{enumerate}}
\newenvironment{parts}{\begin{enumerate}[a)]\setlength{\itemsep}{3ex}\setlength{\parskip}{2ex}
  \setlength{\parsep}{0pt}}{\end{enumerate}}
\newenvironment{subparts}{\begin{enumerate}[i)]}{\end{enumerate}}

\newcommand{\msonly}[1]{\ifthenelse{\boolean{@ms}}{#1}{}}
\newcommand{\examonly}[1]{\ifthenelse{\boolean{@ms}}{}{#1}}

\newcommand{\answer}[2]{%
\msonly{%
~\hspace{0.06\textwidth}\begin{boxedminipage}[t]{0.85\textwidth}%
\textbf{Model Answer: }\raggedright #1%
\\\textbf{Distribution of Marks: }#2%
\end{boxedminipage}\\[1ex]}}

%% Simple MCQ support, environment options and command \correct
%%
\newcounter{optioncount}
\newcounter{correctoption}

% Use this to flag the correct answer
\newcommand{\correct}{\global\setcounter{correctoption}{\value{optioncount}}}

\newcommand{\@optionbox}[1]{\raisebox{-.2ex}{
                          \mbox{\unitlength=2.2ex
                          \begin{picture}(2,2)
                          \linethickness{0.06ex}
                          \put(0,0){\line(1,0){2}}
                          \put(0,2){\line(1,0){2}}
                          \put(0,0){\line(0,1){2}}
                          \put(2,0){\line(0,1){2}}
                          \put(0.75,0.75){\msonly{#1}}
                         \end{picture}}}
                     \,}

\newenvironment{options}{\begin{list}{\Alph{optioncount}.}%
    {\usecounter{optioncount}}%
    \setcounter{correctoption}{0}}%
  {\end{list}\noindent\@optionbox{\Alph{correctoption}}}

%%% Set up mechanism for  optional inclusion of marking schemes
\usepackage{comment}

\excludecomment{mkscheme}
\excludecomment{mkscheme*}
\excludecomment{mkscheme**}
\excludecomment{feedback}

% Use option fb to include feedback
\DeclareOption{fb}{%
\setboolean{@fb}{true}%
\specialcomment{feedback}{\hspace{0.06\textwidth}%
 \begin{boxedminipage}[t]{0.85\textwidth}%
   \textbf{Marker's feedback}\\}{\par\end{boxedminipage}}%
}

% Use option ms to include mark scheme
\DeclareOption{ms}{%
\setboolean{@ms}{true}%
\specialcomment{mkscheme}{\hspace{0.06\textwidth}%
   \begin{boxedminipage}[t]{0.85\textwidth}\raggedright%
   \textbf{Model answer and marking scheme for Q\theenumi}\\[2ex]}%
   {\par\end{boxedminipage}}%
\specialcomment{mkscheme*}{\hspace{0.06\textwidth}%
 \begin{boxedminipage}[t]{0.85\textwidth}\raggedright%
 \textbf{Model answer and marking scheme for Q\theenumi\theenumii}\\}{\end{boxedminipage}}%
\specialcomment{mkscheme**}{\hspace{0.06\textwidth}%
 \begin{boxedminipage}[t]{\textwidth}\raggedright%
 \textbf{Model answer and marking scheme for Q\theenumi\theenumii\theenumiii}\\}{\end{boxedminipage}}%
}

\DeclareOption{largefont}{%
\setboolean{@lf}{true}%
}

\ProcessOptions

\ifthenelse{\boolean{@lf}}{
\RequirePackage{helvet}%
\RequirePackage[fontsize=24pt]{scrextend}%
\renewcommand{\familydefault}{\sfdefault}%
% Make a bit of elbow room 
\geometry{margin=20mm}%
% Change default font to san serif 
\renewcommand{\rmdefault}{phv}%
%% When using fonts as large as this it probably makes sense to use 
%% ragged right edges 
\raggedright%
\fancyfoot[ro]{\ifthenelse{\thepage=\pageref{LastPage}}{}{\normalsize\textbf  [Next page $\rightarrow$]}}
}{}


%%% end of csexam.sty
