% The following packages are hardcoded to ensure consistency between users
% This way, if the packages change, the exam style file should continue to work
% This is ENUMITEM.STY
  % Copyright (c) 2003-2011 by Javier Bezos.

  \def\enitkv@setkeys#1#2{%
    \def\enitkv@prefix{enitkv@#1@}%
    \let\@tempc\relax
    \enitkv@do#2,\relax,}

  \def\enitkv@do#1,{%
   \ifx\relax#1\empty\else
    \enitkv@split#1==\relax
    \expandafter\enitkv@do\fi}

  \def\enitkv@split#1=#2=#3\relax{%
    \enitkv@@sp@def\@tempa{#1}%
    \ifx\@tempa\@empty\else
      \expandafter\let\expandafter\@tempc
        \csname\enitkv@prefix\@tempa\endcsname
      \ifx\@tempc\relax
        \enitkv@errx{\@tempa\space undefined}%
      \else
        \ifx\@empty#3\@empty
          \enitkv@default
        \else
          \enitkv@@sp@def\@tempb{#2}%
          \expandafter\@tempc\expandafter{\@tempb}\relax
        \fi
      \fi
    \fi}

  \def\enitkv@default{%
    \expandafter\let\expandafter\@tempb
      \csname\enitkv@prefix\@tempa @default\endcsname
    \ifx\@tempb\relax
      \enitkv@err{No value specified for \@tempa}%
    \else
      \@tempb\relax
    \fi}

  \def\enitkv@errx#1{\enit@error{#1}\@ehc}

  \let\enitkv@err\enitkv@errx

  \def\@tempa#1{%
    \def\enitkv@@sp@def##1##2{%
      \futurelet\enitkv@tempa\enitkv@@sp@d##2\@nil\@nil#1\@nil\relax##1}%
    \def\enitkv@@sp@d{%
      \ifx\enitkv@tempa\@sptoken
        \expandafter\enitkv@@sp@b
      \else
        \expandafter\enitkv@@sp@b\expandafter#1%
      \fi}%
    \def\enitkv@@sp@b#1##1 \@nil{\enitkv@@sp@c##1}}

  \@tempa{ }

  \def\enitkv@@sp@c#1\@nil#2\relax#3{\enitkv@toks@{#1}\edef#3{\the\enitkv@toks@}}

  \@ifundefined{KV@toks@}
     {\newtoks\enitkv@toks@}
     {\let\enitkv@toks@\KV@toks@}

  \def\enitkv@key#1#2{%
    \@ifnextchar[{\enitkv@def{#1}{#2}}{\@namedef{enitkv@#1@#2}####1}}

  \def\enitkv@def#1#2[#3]{%
    \@namedef{enitkv@#1@#2@default\expandafter}\expandafter
      {\csname enitkv@#1@#2\endcsname{#3}}%
    \@namedef{enitkv@#1@#2}##1}

  % +=============================+
  % |        DEFINITIONS          |
  % +=============================+
  %
  % (1) The package uses a token register very often. To be on the
  %   safe side, instead of \toks@, etc., a new one is declared.
  % (2) \enit@inbox is the box storing the items in boxed inline
  %   lists.
  % (3) \enit@outerparindent is used to save the outer parindent 
  %   so that it can be used in the key parindent 
  % (4) \enit@type has three values: 0 = enum, 1 = item, 2 = desc.
  % (5) \enit@calc stores which dimen is to be computed:
  %   0=labelindent, 1=labelwidth, 2=labelsep, 3=leftmargin,
  %   4=itemindent
  % (6) \enit@resuming has four values: 0 = none, 1 = series,
  %   2 = resume* series (computed in group enumitem-resume),
  %   3 = resume* list (ie, with no value).

  \chardef  \enit@iv=4
  \newlength\labelindent
  \newdimen \enit@outerparindent
  \newtoks  \enit@toks
  \newbox   \enit@inbox

  \newif\ifenit@boxmode
  \newif\ifenit@sepfrommargin
  \newif\ifenit@lblfrommargin
  \newif\ifenit@calcwidest
  \newif\ifenit@nextline
  \newif\ifenit@boxdesc

  % An alias (calc-savvy):

  \let\c@enit@cnt\@tempcnta

  \def\enit@meaning{\expandafter\strip@prefix\meaning}
  \def\enit@noexcs#1{\expandafter\noexpand\csname#1\endcsname}

  % Miscelaneous errors
  % ===================

  \def\enit@error{\PackageError{enumitem}}
    
  \def\enit@checkerror#1#2{%
    \enit@error{Unknown value `#2' for key `#1'}%
        {See the manual for valid values}} 

  \def\enit@itemerror{%
    \enit@error{Misplaced \string\item}%
        {Either there is some text before the first\MessageBreak
         item or the last item has no text}} 

  \def\enit@noserieserror#1{%
    \enit@error{Series `#1' not started}%
        {You are trying to continue a series\MessageBreak
         which has not been started with series}} 

  \def\enit@checkseries#1{%
    \ifcase\enit@resuming
      \enit@error{Misplaced key `#1'}%
        {`series' and `resume*' must be used\MessageBreak
         in the optional argument of lists}%
    \fi}

  \def\enit@checkseries@m{%
    \ifcase\enit@resuming\else
      \enit@error{Uncompatible series settings}%
        {`series' and `resume*' must not be used\MessageBreak
         at the same time}%
    \fi}

  \let\enit@toodeep\@toodeep

  \def\@toodeep{%
    \ifnum\@listdepth>\enit@listdepth\relax
      \enit@toodeep
    \else
      \count@\@listdepth
      \global\advance\@listdepth\@ne
      \@ifundefined{@list\romannumeral\the\@listdepth}%
        {\expandafter\let
           \csname @list\romannumeral\the\@listdepth\expandafter\endcsname
           \csname @list\romannumeral\the\count@\endcsname}{}%
    \fi}


  % +=============================+
  % |            KEYS             |
  % +=============================+
  %
  % Including code executed by keys.
  %
  % There are 2 keyval groups: enumitem, and enumitem-delayed.
  % The latter is used to make sure a prioritary key is the
  % latest one; eg, ref, so that the ref format set by label
  % is overriden. So, when this key is found in enumitem,
  % nothing is done, except the key/value is moved to 
  % enumitem-delayed.
  %
  % A further group (enumitem-resume) catches resume* and
  % series in optional arguments in lists.
  %
  % Vertical spacing
  % ================

  \enitkv@key{enumitem}{topsep}{%
    \setlength\topsep{#1}}

  \enitkv@key{enumitem}{itemsep}{%
    \setlength\itemsep{#1}}

  \enitkv@key{enumitem}{parsep}{%
    \setlength\parsep{#1}}

  \enitkv@key{enumitem}{partopsep}{%
    \setlength\partopsep{#1}}

  % Horizontal spacing
  % ==================
  %
  % There are 3 cases: *, ! and a value. The latter also
  % cancels widest with the sequence key=* ... key=value
  % \string is used, just in case some package changes the
  % catcodes.

  \def\enit@calcset#1#2#3{%
    \if\string*\string#3%
      \enit@calcwidesttrue
      \let\enit@calc#2%
    \else\if\string!\string#3%
      \enit@calcwidestfalse
      \let\enit@calc#2%
    \else
      \ifnum\enit@calc=#2%
        \enit@calcwidestfalse
        \let\enit@calc\z@
      \fi
      \setlength#1{#3}%
    \fi\fi}

  \def\enitkv@enumitem@widest#1{%
    \expandafter\let\csname enit@cw@\@enumctr\endcsname\relax
    \@namedef{enit@widest@\@enumctr}##1{#1}}

  \def\enitkv@enumitem@widest@default{%
    \expandafter\let\csname enit@cw@\@enumctr\endcsname\relax
    \expandafter\let\csname enit@widest@\@enumctr\endcsname\relax}

  \enitkv@key{enumitem}{widest*}{%
    \setcounter{enit@cnt}{#1}%
    \expandafter\edef\csname enit@cw@\@enumctr\endcsname
      {\the\c@enit@cnt}%
    \expandafter\edef\csname enit@widest@\@enumctr\endcsname##1%
      {##1{\the\c@enit@cnt}}}

  \enitkv@key{enumitem}{labelindent*}{%
    \enit@lblfrommargintrue
    \ifnum\enit@calc=\z@
      \enit@calcwidestfalse
    \fi
    \setlength\labelindent{#1}%
    \advance\labelindent\leftmargin}

  \enitkv@key{enumitem}{labelindent}{%
    \enit@lblfrommarginfalse
    \enit@calcset\labelindent\z@{#1}}

  \enitkv@key{enumitem}{labelwidth}{%
    \enit@calcset\labelwidth\@ne{#1}}

  \enitkv@key{enumitem}{leftmargin}{%
    \edef\enit@c{\the\leftmargin}%
    \enit@calcset\leftmargin\thr@@{#1}%
    \ifenit@lblfrommargin
      \advance\labelindent-\enit@c\relax
      \advance\labelindent\leftmargin
    \fi}

  \enitkv@key{enumitem}{itemindent}{%
    \edef\enit@c{\the\itemindent}%
    \enit@calcset\itemindent\enit@iv{#1}%
    \ifenit@sepfrommargin
      \advance\labelsep-\enit@c\relax
      \advance\labelsep\itemindent
    \fi}

  \enitkv@key{enumitem}{listparindent}{%
    \setlength\listparindent{#1}}

  \enitkv@key{enumitem}{rightmargin}{%
    \setlength\rightmargin{#1}}

  % labelsep, from itemindent; labelsep*, from leftmargin

  \enitkv@key{enumitem}{labelsep*}{%
    \enit@sepfrommargintrue
    \ifnum\enit@calc=\tw@
      \enit@calcwidestfalse
      \let\enit@calc\z@
    \fi
    \setlength\labelsep{#1}%
    \advance\labelsep\itemindent}

  \enitkv@key{enumitem}{labelsep}{%
    \enit@sepfrommarginfalse
    \enit@calcset\labelsep\tw@{#1}}

  % Series, resume and start
  % ========================

  \enitkv@key{enumitem-resume}{series}{%
    \enit@checkseries@m
    \let\enit@resuming\@ne
    \@ifundefined{enitkv@enumitem@#1}{}%
      {\enit@error{Invalid series name `#1'}%
         {Do not name a series with an existing key}}%
    \def\enit@series{#1}}

  \enitkv@key{enumitem}{series}{%
    \enit@checkseries{series}}

  \def\enitkv@enumitem@resume#1{%
    \edef\enit@series{#1}%
    \@nameuse{enit@resume@series@#1}\relax}

  \def\enitkv@enumitem@resume@default{%
    \@nameuse{enit@resume@\@currenvir}\relax}
    
  \@namedef{enitkv@enumitem-resume@resume*}#1{%
    \enit@checkseries@m
    \let\enit@resuming\tw@
    \edef\enit@series{#1}%
    \@ifundefined{enit@resumekeys@series@#1}%
      {\enit@noserieserror{#1}}%
      {\expandafter\let\expandafter\enit@resumekeys
           \csname enit@resumekeys@series@#1\endcsname}}

  \@namedef{enitkv@enumitem-resume@resume*@default}{%
    \let\enit@resuming\thr@@
    \expandafter\let\expandafter\enit@resumekeys
      \csname enit@resumekeys@\@currenvir\endcsname
    \@nameuse{enit@resume@\@currenvir}\relax}

  \enitkv@key{enumitem}{resume*}[]{%
    \enit@checkseries{resume*}}

  \newcommand\restartlist[1]{%
    \@ifundefined{end#1}%
      {\enit@error{Undefined list `#1'}%
        {No list has been defined with that name.}}%
      {\expandafter\let
       \csname enit@resume@#1\endcsname\@empty}}

  \enitkv@key{enumitem}{start}[\@ne]{%
    \setcounter{\@listctr}{#1}%
    \advance\@nameuse{c@\@listctr}\m@ne}

  % Penalties
  % =========
    
  \enitkv@key{enumitem}{beginpenalty}{%
    \@beginparpenalty#1\relax}
    
  \enitkv@key{enumitem}{midpenalty}{%
    \@itempenalty#1\relax}

  \enitkv@key{enumitem}{endpenalty}{%
    \@endparpenalty#1\relax}
     
  % Font/Format
  % ===========

  \enitkv@key{enumitem}{format}{%
    \def\enit@format{#1}}

  \enitkv@key{enumitem}{font}{%
    \def\enit@format{#1}}

  % Description styles
  % ==================

  \enitkv@key{enumitem}{style}[normal]{%
    \@ifundefined{enit@style@#1}%
      {\enit@checkerror{style}{#1}}%
      {\enit@nextlinefalse
       \enit@boxdescfalse
       \@nameuse{enit@style@#1}%
       \edef\enit@descstyle{\enit@noexcs{enit@#1style}}}}

  \def\enit@style@standard{%
    \enit@boxdesctrue
    \enit@calcset\itemindent\enit@iv!}

  \let\enit@style@normal\enit@style@standard

  \def\enit@style@unboxed{%
    \enit@calcset\itemindent\enit@iv!}

  \def\enit@style@sameline{%
    \enit@calcset\labelwidth\@ne!}

  \def\enit@style@multiline{%
    \enit@align@parleft
    \enit@calcset\labelwidth\@ne!}

  \def\enit@style@nextline{%
    \enit@nextlinetrue
    \enit@calcset\labelwidth\@ne!}

  % Labels and refs
  % ===============

  % Aligment
  % --------

  \enitkv@key{enumitem}{align}{%
    \@ifundefined{enit@align@#1}%
      {\enit@checkerror{align}{#1}}%
      {\csname enit@align@#1\endcsname}}

  % \nobreak for unboxed label with color. See below.

  \newcommand\SetLabelAlign[2]{%
    \enit@toks{#2}%
    \expandafter\edef\csname enit@align@#1\endcsname
      {\def\noexpand\enit@align####1{\nobreak\the\enit@toks}}}

  \def\enit@align@right{%
    \def\enit@align##1{\nobreak\hss\llap{##1}}}

  \def\enit@align@left{%
    \def\enit@align##1{\nobreak##1\hfil}}

  \def\enit@align@parleft{%
    \def\enit@align##1{%
      \nobreak
      \strut\smash{\parbox[t]\labelwidth{\raggedright##1}}}}

  % \enit@ref has three possible definitions:
  % (1) \relax, if there is neither label nor ref (ie, use
  %   LaTeX settings).
  % (2) set ref to @itemlabel, if there is label but not ref
  % (3) set ref to ref, if there is ref (with or without label)

  \enitkv@key{enumitem}{label}{%
    \expandafter\def\@itemlabel{#1}%
    \def\enit@ref{\expandafter\enit@reflabel\@itemlabel\z@}}

  \enitkv@key{enumitem}{label*}{%
    \ifnum\enit@depth=\@ne
      \expandafter\def\@itemlabel{#1}%
    \else % no level 0
      \advance\enit@depth\m@ne
      \enit@toks{#1}%
      \expandafter\edef\@itemlabel{%
        \enit@noexcs{label\enit@prevlabel}%
        \the\enit@toks}%
      \advance\enit@depth\@ne  
    \fi
    \def\enit@ref{\expandafter\enit@reflabel\@itemlabel\z@}}

  % ref is set by label, except if there is an explicit ref
  % in the same hierarchy level. Explicit refs above the
  % current hierarchy level are overriden by label (besides ref),
  % too. Since an explicit ref has preference, it's delayed.
    
  \enitkv@key{enumitem}{ref}{%
    \g@addto@macro\enit@delayedkeys{,ref=#1}}

  \enitkv@key{enumitem-delayed}{ref}{%
    \def\enit@ref{\enit@reflabel{#1}\@ne}}

  % #2=0 don't "normalize" (ie, already normalized)
  %   =1 "normalize" (in key ref)
  % Used thru \enit@ref

  \def\enit@reflabel#1#2{%
    \ifnum\enit@depth=\@ne\else % no level 0
      \advance\enit@depth\@ne
      \@namedef{p@\@enumctr}{}% Don't accumulate labels
      \advance\enit@depth\m@ne
    \fi
    \ifcase#2%
      \@namedef{the\@enumctr}{{#1}}%
    \else
      \enit@normlabel{\csname the\@enumctr\endcsname}{#1}%
    \fi}

  % \xxx* in counters (refstar) and widest (calcdef)
  % ------------------------------------------------
  % \enit@labellist contains a list of
  % \enit@elt{widest}\count\@count\enit@sc@@count
  % \enit@elt is either \enit@getwidth or \enit@refstar, defined
  % below
  % The current implementation is sub-optimal -- labels are stored in 
  % labellist, counters defined again when processing labels, and
  % modifying it is almost impossible.

  \let\enit@labellist\@empty 

  \newcommand\AddEnumerateCounter{%
    \@ifstar\enit@addcounter@s\enit@addcounter}

  \def\enit@addcounter#1#2#3{%
    \enit@toks\expandafter{%
      \enit@labellist
      \enit@elt{#3}}%
    \edef\enit@labellist{%
      \the\enit@toks
      \enit@noexcs{\expandafter\@gobble\string#1}%
      \enit@noexcs{\expandafter\@gobble\string#2}%
      \enit@noexcs{enit@sc@\expandafter\@gobble\string#2}}}

  \def\enit@addcounter@s#1#2#3{%
    \enit@addcounter{#1}{#2}%
      {\@nameuse{enit@sc@\expandafter\@gobble\string#2}{#3}}}

  % The 5 basic counters:

  \AddEnumerateCounter\arabic\@arabic{0}
  \AddEnumerateCounter\alph\@alph{m}
  \AddEnumerateCounter\Alph\@Alph{M}
  \AddEnumerateCounter\roman\@roman{viii}
  \AddEnumerateCounter\Roman\@Roman{VIII}

  % Inline lists
  % ============
  %
  % Labels
  % ------

  \enitkv@key{enumitem}{itemjoin}{%
    \def\enit@itemjoin{#1}}

  \enitkv@key{enumitem}{itemjoin*}{%
    \def\enit@itemjoin@s{#1}}

  \enitkv@key{enumitem}{afterlabel}{%
    \def\enit@afterlabel{#1}}

  % Mode
  % ----

  \enitkv@key{enumitem}{mode}{%
    \@ifundefined{enit@mode#1}%
      {\enit@checkerror{mode}{#1}}%
      {\csname enit@mode#1\endcsname}}

  \let\enit@modeboxed\enit@boxmodetrue
  \let\enit@modeunboxed\enit@boxmodefalse

  % Short Labels
  % ============

  \let\enit@marklist\@empty

  % shorthand, expansion:

  \newcommand\SetEnumerateShortLabel[2]{%
    \let\enit@a\@empty
    \def\enit@elt##1##2{%
      \def\enit@b{#1}\def\enit@c{##1}%
      \ifx\enit@b\enit@c\else
        \expandafter\def\expandafter\enit@a\expandafter{%
          \enit@a
          \enit@elt{##1}{##2}}%
      \fi}%
    \enit@marklist
    \expandafter\def\expandafter\enit@a\expandafter{%
      \enit@a
      \enit@elt{#1}{#2}}%
    \let\enit@marklist\enit@a}

  \SetEnumerateShortLabel{a}{\alph*}
  \SetEnumerateShortLabel{A}{\Alph*}
  \SetEnumerateShortLabel{i}{\roman*}
  \SetEnumerateShortLabel{I}{\Roman*}
  \SetEnumerateShortLabel{1}{\arabic*}

  % This is called \enit@first one,two,three,\@nil\@@nil. If there
  % are just one element #2 is \@nil, otherwise we have to remove
  % the trailing ,\@nil with enit@first@x
  % Called with the keys in \enit@c
  % Returns enit@toks

  \def\enit@first#1,#2\@@nil{%
    \in@{=}{#1}% Quick test, if contains =, it's key=value
    \ifin@\else
      \enitkv@@sp@def\enit@a{#1}%
      \@ifundefined{enitkv@enumitem@\enit@meaning\enit@a}%
        {\ifnum\enit@type=\z@
           \def\enit@elt{\enit@replace\enit@a}%
           \enit@marklist % Returns \enit@toks
         \else
           \enit@toks{#1}%
         \fi
         \ifx\@nil#2%
           \ifx,#1,\else
             \edef\enit@c{label=\the\enit@toks}%
           \fi
         \else
           \@temptokena\expandafter{\enit@first@x#2}%
           \edef\enit@c{label=\the\enit@toks,\the\@temptokena}%
         \fi}%
       {}%
    \fi
    \enit@toks\expandafter{\enit@c}}

  \def\enit@first@x#1,\@nil{#1}

  \def\enit@replace#1#2#3{%
    \enit@toks{}%
    \def\enit@b##1#2##2\@@nil{%
      \ifx\@nil##2%
        \addto@hook\enit@toks{##1}%
      \else
        \edef\enit@a{\the\enit@toks}%
        \ifx\enit@a\@empty\else
          \enit@error{Extra short label ignored}%
             {There are more than one short label}%
        \fi
        \addto@hook\enit@toks{##1#3}%
        \enit@b##2\@@nil
      \fi}%
    \expandafter\enit@b#1#2\@nil\@@nil
    \edef#1{\the\enit@toks}}

  % Pre and post code
  % =================

  \enitkv@key{enumitem}{before}{%
    \def\enit@before{#1}}

  \enitkv@key{enumitem}{after}{%
    \def\enit@after{#1}}

  \enitkv@key{enumitem}{before*}{%
    \expandafter\def\expandafter\enit@before\expandafter
      {\enit@before#1}}

  \enitkv@key{enumitem}{after*}{%
    \expandafter\def\expandafter\enit@after\expandafter
      {\enit@after#1}}

  % Miscelaneous keys
  % ================
    
  \enitkv@key{enumitem}{nolistsep}[true]{%
    \partopsep=\z@skip
    \topsep=\z@ plus .1pt
    \itemsep=\z@skip
    \parsep=\z@skip}

  \enitkv@key{enumitem}{nosep}[true]{%
    \partopsep=\z@skip
    \topsep=\z@skip
    \itemsep=\z@skip
    \parsep=\z@skip}


  \enitkv@key{enumitem}{noitemsep}[true]{%
    \itemsep=\z@skip
    \parsep=\z@skip}

  \enitkv@key{enumitem}{wide}[\parindent]{%
    \enit@align@left
    \leftmargin\z@
    \labelwidth\z@
    \setlength\labelindent{#1}%
    \listparindent\labelindent
    \enit@calcset\itemindent\enit@iv!}

  % The following is deprecated in favour of wide:

  \enitkv@key{enumitem}{fullwidth}[true]{%
    \leftmargin\z@
    \labelwidth\z@
    \def\enit@align##1{\hskip\labelsep##1}}

  % "Abstract" layer
  % ================
  %
  % Named values
  % ------------

  \newcommand\SetEnumitemValue[2]{% Implicit #3
    \@ifundefined{enit@enitkv@#1}%
      {\@ifundefined{enitkv@enumitem@#1}%
         {\enit@error{Wrong key `#1' in \string\SetEnumitemValue}%
            {Perhaps you have misspelled it}}{}%
       \expandafter\let\csname enit@enitkv@#1\expandafter\endcsname
         \csname enitkv@enumitem@#1\endcsname}{}%
    \@namedef{enitkv@enumitem@#1}##1{%
      \def\enit@a{##1}%
      \@ifundefined{enit@enitkv@#1@\enit@meaning\enit@a}%
        {\@nameuse{enit@enitkv@#1}{##1}}%
        {\@nameuse{enit@enitkv@#1\expandafter\expandafter\expandafter}%
           \expandafter\expandafter\expandafter
           {\csname enit@enitkv@#1@##1\endcsname}}{}}%
    \@namedef{enit@enitkv@#1@#2}}

  % Defining keys
  % -------------

  \newcommand\SetEnumitemKey[2]{%
    \@ifundefined{enitkv@enumitem@#1}%
      {\enitkv@key{enumitem}{#1}[]{\enitkv@setkeys{enumitem}{#2}}}%
      {\enit@error{Duplicated key `#1' in \string\SetEnumitemKey}%
         {There already exists a key with that name}}}

  % +=============================+
  % |       PROCESSING KEYS       |
  % +=============================+
  %
  % Set keys
  % ========

  \def\enit@setkeys#1{%
    \@ifundefined{enit@@#1}{}%
      {\expandafter\expandafter\expandafter
       \enit@setkeys@i\csname enit@@#1\endcsname\@@}}

  % The following is used directly in resumeset:

  \def\enit@setkeys@i#1\@@{%
    \let\enit@delayedkeys\@empty
    \enit@shl{#1}% is or returns \enit@toks
    \expandafter\enit@setkeys@ii\the\enit@toks\@@}

  \def\enit@setkeys@ii#1\@@{%
    \enitkv@setkeys{enumitem}{#1}%
    \enit@toks\expandafter{\enit@delayedkeys}%
    \edef\enit@a{%
      \noexpand\enitkv@setkeys{enumitem-delayed}{\the\enit@toks}}%
    \enit@a}

  % Handling * and ! values
  % =======================
  %
  % \@gobbletwo removes \c from \c@counter.

  \def\enit@getwidth#1#2#3#4{%
    \let#4#3%
    \def#3##1{%
      \@ifundefined{enit@widest\expandafter\@gobbletwo\string##1}% if no widest=key
        {#1}%
        {\csname enit@widest\expandafter\@gobbletwo\string##1\endcsname{#4}}}}

  \def\enit@valueerror#1{\z@ % if after an assignment, but doesn't catch \ifnum
     \enit@error{No default \string\value\space for `#1'}%
       {You can provide one with widest*}}%

  \let\enit@values\@empty

  \def\enit@calcwidth{%
    \ifenit@calcwidest
      \ifnum\enit@type=\z@ % ie, enum
        \@ifundefined{enit@cw@\@enumctr}%
          {\@namedef{enit@cv@\@enumctr}{\enit@valueerror\@enumctr}}%
          {\edef\enit@values{%
             \enit@values
             \@nameuse{c@\@enumctr}\@nameuse{enit@cw@\@enumctr}\relax}%
           \expandafter
           \edef\csname enit@cv@\@enumctr\endcsname
             {\@nameuse{c@\@enumctr}}}%
      \fi
      \begingroup
        \enit@values
        \def\value##1{\csname enit@cv@##1\endcsname}%
        \let\enit@elt\enit@getwidth
        \enit@labellist
        \settowidth\labelwidth{\@itemlabel}%
        \xdef\enit@a{\labelwidth\the\labelwidth\relax}%
      \endgroup
      \enit@a
    \fi
    \advance\dimen@-\labelwidth}

  \def\enit@calcleft{%
    \dimen@\leftmargin
    \advance\dimen@\itemindent
    \advance\dimen@-\labelsep
    \advance\dimen@-\labelindent
    \ifcase\enit@calc % = 0 = labelindent
      \enit@calcwidth
      \advance\labelindent\dimen@
    \or % = 1 = labelwidth, so no \enit@calcwidth
      \labelwidth\dimen@
    \or % = 2 = labelsep
      \enit@calcwidth
      \advance\labelsep\dimen@
    \or % = 3 = leftmargin
      \enit@calcwidth
      \advance\leftmargin-\dimen@
    \or % = 4 =itemindent
      \enit@calcwidth
      \advance\itemindent-\dimen@
    \fi}

  % "Normalizing" labels
  % ====================
  %
  % Replaces \counter* by \counter{level} (those in \enit@labellist).
  %
  % #1 is either \csname...\endcsmame or the container \@itemlabel --
  % hence \expandafter

  \def\enit@refstar@i#1#2{%
    \if*#2\@empty
      \noexpand#1{\@enumctr}%
    \else
      \noexpand#1{#2}%
    \fi}%

  \def\enit@refstar#1#2#3#4{%
    \def#2{\enit@refstar@i#2}%
    \def#3{\enit@refstar@i#3}}

  \def\enit@normlabel#1#2{%
    \begingroup
      \def\value{\enit@refstar@i\value}%
      \let\enit@elt\enit@refstar
      \enit@labellist
      \protected@xdef\enit@a{{#2}}% Added braces as \ref is in the 
    \endgroup
    \expandafter\let#1\enit@a}                    % global scope. 

  % Preliminary settings and default values
  % =======================================

  \def\enit@prelist#1#2#3{%
    \let\enit@type#1%
    \def\enit@depth{#2}%
    \edef\enit@prevlabel{#3\romannumeral#2}%
    \advance#2\@ne}
       
  \def\enit@preset#1#2#3{%
     \enit@sepfrommarginfalse
     \enit@calcwidestfalse
     \let\enit@resuming\z@
     \let\enit@series\relax
     \enit@boxmodetrue
     \def\enit@itemjoin{ }%
     \let\enit@itemjoin@s\relax
     \let\enit@afterlabel\nobreakspace
     \let\enit@before\@empty
     \let\enit@after\@empty
     \let\enit@format\@firstofone % and NOT empty
     \let\enit@ref\relax
     \labelindent\z@skip
     \ifnum\@listdepth=\@ne
       \enit@outerparindent\parindent
     \else
       \parindent\enit@outerparindent
     \fi
     \enit@setkeys{list}%
     \enit@setkeys{list\romannumeral\@listdepth}%
     \enit@setkeys{#1}%
     \enit@setkeys{#1\romannumeral#2}%
     \enit@setresume{#3}}

  % keyval "error" in enumitem-resume: all undefined keys (ie, all
  % except resume*) are ignored, but <series> is treated like
  % resume*=<series>

  \def\enitkv@err@a#1{%
     \@ifundefined{enit@resumekeys@series@\@tempa}{}%
       {\@nameuse{enitkv@enumitem-resume@resume*\expandafter}%
          \expandafter{\@tempa}}}

  % keyval "error" in the optional argument: all undefined keys are
  % passed to the keyval error, but <series> is ignored (already
  % processed in enumitem-resume

  \def\enitkv@err@b#1{%
     \@ifundefined{enit@resumekeys@series@\@tempa}%
       {\enit@savekverr{#1}}%
       {}}

  % Process keys in optional argument:

  \def\enit@setresume#1{%
    \enit@shl{#1}% Returns enit@toks
    \edef\enit@savekeys{\the\enit@toks}%
    \let\enit@savekverr\enitkv@errx
    \let\enitkv@errx\enitkv@err@a
    \edef\enit@b{%
      \noexpand\enitkv@setkeys{enumitem-resume}{\the\enit@toks}}%
    \enit@b
    \let\enitkv@errx\enitkv@err@b
    \ifcase\enit@resuming\or\or % = 2
      \expandafter
      \enit@setkeys@i\enit@resumekeys,resume=\enit@series\@@
    \or % = 3
      \expandafter
      \enit@setkeys@i\enit@resumekeys,resume\@@
    \fi
    \expandafter\enit@setkeys@i\enit@savekeys\@@
    \let\enitkv@errx\enit@savekverr}

  % +=============================+
  % |         LIST TYPES          |
  % +=============================+
  %
  % Displayed lists
  % ===============
  % #1 #2 implicit

  \def\enit@dylist{%
    \enit@align@right
    \list}

  \def\enit@endlist{%
    \enit@after
    \endlist
    \ifx\enit@series\relax\else % discards resume*, too
      \ifnum\enit@resuming=\@ne % ie, series=
        \enit@setresumekeys{series@\enit@series}\global\global
      \else % ie, resume=, resume*= (save count, but not keys)
        \enit@setresumekeys{series@\enit@series}\@gobblefour\global
      \fi
      \enit@afterlist
    \fi
    \ifnum\enit@resuming=\thr@@ % ie, resume* list (save count only)
      \enit@setresumekeys\@currenvir\@gobblefour\global
    \else
      \enit@setresumekeys\@currenvir\@empty\@empty
    \fi
    \aftergroup\enit@afterlist}

  % #1 = either \@currenvir or series@<series>
  % #2(keys) #3(counter) are \global, \@gobblefour or \@empty

  \def\enit@setresumekeys#1#2#3{%
    \enit@toks\expandafter{\enit@savekeys}%
    \xdef\enit@afterlist{%
      #2\def\enit@noexcs{enit@resumekeys@#1}{\the\enit@toks}%
      \ifnum\enit@type=\z@ % ie, enum
        #3\def\enit@noexcs{enit@resume@#1}{%
          \csname c@\@listctr\endcsname
          \the\csname c@\@listctr\endcsname}%
      \fi}}

  % Inline lists
  % ============

  % Definition of \@trivlist inside inline lists.  So, when
  % \@trivlist is found in any displayed list (including quote,
  % center, verbatim...) the default \@item is restored.

  \def\enit@intrivlist{%
    \enit@changed@itemfalse
    \let\@item\enit@outer@item
    \let\par\@@par
    \let\@trivlist\enit@outer@triv
    \@trivlist}

  % Keep track of \@item and \item changes

  \newif\ifenit@changed@item
  \enit@changed@itemfalse

  \newif\ifenit@changeditem
  \enit@changeditemfalse

  % List
  % ----

  % Arguments, as before:
  % \enitdp@<name>, <name>, <max-depth>, <format>
  % About @newlist, see @initem.

  \def\enit@inlist#1#2{%
    \ifnum\@listdepth>\enit@listdepth\relax
      \@toodeep
    \else
      \global\advance\@listdepth\@ne
    \fi
    \let\enit@align\@firstofone
    \def\@itemlabel{#1}%
    \@nmbrlistfalse
    \ifenit@changed@item\else
      \enit@changed@itemtrue
      \let\enit@outer@triv\@trivlist
      \let\@trivlist\enit@intrivlist
      \@setpar\@empty
      \let\enit@outer@item\@item
    \fi
    #2\relax
    \global\@newlisttrue
    \ifenit@boxmode
      \ifenit@changeditem\else
        \enit@changeditemtrue
        \let\enit@outeritem\item
      \fi
      \let\@item\enit@boxitem
    \else
      \let\@item\enit@noboxitem
      \ifx\enit@itemjoin@s\relax\else
        \PackageWarning{enumitem}%
           {itemjoin* discarded in mode unboxed\MessageBreak}%
      \fi
    \fi
    \let\enit@calcleft\relax
    \let\enit@afteritem\relax
    \ifenit@boxmode
      \global\setbox\enit@inbox\hbox\bgroup\color@begingroup
        \let\item\enit@endinbox
    \fi
    \ignorespaces}

  \def\enit@endinlist{%
    \ifenit@boxmode
        \unskip
        \xdef\enit@afteritem{%
          \ifhmode\spacefactor\the\spacefactor\relax\fi}%
        \color@endgroup
      \egroup
      \ifdim\wd\enit@inbox=\z@
        \enit@itemerror
      \else
        \ifenit@noinitem\else
          \ifhmode\unskip\fi
          \@ifundefined{enit@itemjoin@s}%
            {\enit@itemjoin}%
            {\enit@itemjoin@s}%
        \fi
        \unhbox\@labels
        \enit@afterlabel
        \unhbox\enit@inbox
        \enit@afteritem
      \fi
    \else
      \unskip
      \if@newlist
        \enit@itemerror
      \fi
    \fi
    \enit@after
    \global\advance\@listdepth\m@ne
    \global\@inlabelfalse
    \if@newlist
      \global\@newlistfalse
      \@noitemerr
    \fi
    \ifx\enit@series\relax\else % discards resume* list, too
      \ifnum\enit@resuming=\@ne % ie, series
        \enit@setresumekeys{series@\enit@series}\global\global
      \else % ie, resume, resume* (save count, but not keys)
        \enit@setresumekeys{series@\enit@series}\@gobblefour\global
      \fi
      \enit@afterlist
    \fi
    \ifnum\enit@resuming=\thr@@ % ie, resume* list (save count only)
      \enit@setresumekeys\@currenvir\@gobblefour\global
    \else
      \enit@setresumekeys\@currenvir\@empty\@empty
    \fi
    \aftergroup\enit@afterlist}

  % \@item: unboxed
  % ---------------

  \def\enit@noboxitem[#1]{%
    \if@newlist
      \leavevmode % ships pending labels out
      \global\@newlistfalse
    \else
      \ifhmode
        \unskip
        \enit@itemjoin
      \else
        \noindent
      \fi
    \fi
    \if@noitemarg
      \@noitemargfalse
      \if@nmbrlist
        \refstepcounter{\@listctr}% after \unskip (hyperref)
      \fi
    \fi
    \mbox{\makelabel{#1}}%
    \enit@afterlabel
    \ignorespaces}

  % \@item: boxed
  % ------------
  %
  % We don't want \item to be executed locally, because it sets a flag
  % (and hyperref adds another flag, too).  So, we redefine it inside
  % the box to \enit@endinbox which ends the box and then use the actual
  % (outer) \item.  labels are stored in another box, to detect empty
  % boxes, ie, misplaced \item's.  Note the 2nd \item ends collecting
  % the 1st item and ships it out, while the 3rd \item ends collecting
  % the 2nd item, puts the itemjoin and then ships the 2nd item out.
  % The flag enit@noinitem keeps track of that.

  \newif\ifenit@noinitem

  \def\enit@endinbox{%
      \unskip
      \xdef\enit@afteritem{%
        \ifhmode\spacefactor\the\spacefactor\relax\fi}%
      \color@endgroup
    \egroup
    \enit@outeritem}

  \def\enit@boxitem[#1]{%
    \if@newlist
      \global\@newlistfalse
      \ifdim\wd\enit@inbox>\z@
         \enit@itemerror
      \fi
      \enit@noinitemtrue
      \leavevmode % ships pending labels out
    \else
      \ifdim\wd\enit@inbox=\z@
        \enit@itemerror
      \else
        \ifenit@noinitem
          \enit@noinitemfalse
        \else
          \ifhmode\unskip\fi
          \enit@itemjoin
        \fi
        \unhbox\@labels
        \enit@afterlabel
        \unhbox\enit@inbox
        \enit@afteritem
      \fi
    \fi
    \if@noitemarg
      \@noitemargfalse
      \if@nmbrlist
        \refstepcounter{\@listctr}%
      \fi
    \fi
    \sbox\@labels{\makelabel{#1}}%
    \let\enit@afteritem\relax
    \setbox\enit@inbox\hbox\bgroup\color@begingroup
      \let\item\enit@endinbox
      \hskip1sp % in case the first thing is \label
      \ignorespaces} 

  % Pause item
  % ----------
  %
  % To do.
  %
  % The three types
  % ===============
  %
  % enumerate and enumerate*
  % ------------------------
  %
  % The following has 4 arguments, which in enumerate are:
  % \@enumdepth, enum, \thr@@, <format>.
  % In user defined environments they are:
  % \enitdp@<name>, <name>, <max-depth>, <format>

  \def\enit@enumerate{%
    \let\enit@list\enit@dylist
    \enit@enumerate@i}

  \@namedef{enit@enumerate*}{%
    \let\enit@list\enit@inlist
    \enit@enumerate@i}

  \def\enit@enumerate@i#1#2#3#4{%
    \ifnum#1>#3\relax
      \enit@toodeep
    \else
      \enit@prelist\z@{#1}{#2}%
      \edef\@enumctr{#2\romannumeral#1}%
      \expandafter
      \enit@list
        \csname label\@enumctr\endcsname
        {\usecounter\@enumctr
         \let\enit@calc\z@
         \def\makelabel##1{\enit@align{\enit@format{##1}}}%
         \enit@preset{#2}{#1}{#4}%
         \enit@normlabel\@itemlabel\@itemlabel
         \enit@ref
         \enit@calcleft
         \enit@before}%
    \fi}

  \let\enit@endenumerate\enit@endlist
  \@namedef{enit@endenumerate*}{\enit@endinlist}

  % itemize and itemize*
  % --------------------
  %
  % The following has 4 arguments, which in itemize are:
  % \@itemdepth, item, \thr@@, <format>.
  % In user defined environments they are:
  % \enitdp@<name>, <name>, <max-depth>, <format>

  \def\enit@itemize{%
    \let\enit@list\enit@dylist
    \enit@itemize@i}

  \@namedef{enit@itemize*}{%
    \let\enit@list\enit@inlist
    \enit@itemize@i}
    
  \def\enit@itemize@i#1#2#3#4{%
    \ifnum#1>#3\relax
      \enit@toodeep
    \else
      \enit@prelist\@ne{#1}{#2}%
      \edef\@itemitem{label#2\romannumeral#1}%
      \expandafter
      \enit@list
        \csname\@itemitem\endcsname
         {\let\enit@calc\z@
          \def\makelabel##1{\enit@align{\enit@format{##1}}}%
          \enit@preset{#2}{#1}{#4}% 
          \enit@calcleft
          \enit@before}%
    \fi}

  \let\enit@enditemize\enit@endlist
  \@namedef{enit@enditemize*}{\enit@endinlist}

  % description and description*
  % ----------------------------
  %
  % Make sure \descriptionlabel exists:

  \providecommand*\descriptionlabel[1]{%
    \hspace\labelsep
    \normalfont\bfseries#1}

  \@namedef{enit@description*}{%
    \let\enit@list\enit@inlist
    \enit@description@i}

  \def\enit@description{%
    \let\enit@list\enit@dylist
    \enit@description@i}

  \def\enit@description@i#1#2#3#4{%
    \ifnum#1>#3\relax
      \enit@toodeep
    \else
      \enit@list{}%
        {\let\enit@type\tw@
         \advance#1\@ne
         \labelwidth\z@
         \enit@align@left
         \let\makelabel\descriptionlabel
         \enit@style@standard
         \enit@preset{#2}{#1}{#4}%
         \enit@calcleft
         \let\enit@svlabel\makelabel
         \def\makelabel##1{%
           \labelsep\z@
           \ifenit@boxdesc
             \enit@svlabel{\enit@align{\enit@format{##1}}}%
           \else
             \nobreak
             \enit@svlabel{\enit@format{##1}}%
             \aftergroup\enit@postlabel
           \fi}%
         \enit@before}%
    \fi}

  \let\enit@enddescription\enit@endlist
  \@namedef{enit@enddescription*}{\enit@endinlist}

  % trivlist
  % ========

  \def\enit@trivlist{%
    \let\enit@type\tw@
    \parsep\parskip
    \csname @list\romannumeral\the\@listdepth\endcsname
    \@nmbrlistfalse
    \enit@setkeys{trivlist}%
    \enit@setkeys{trivlist\romannumeral\@listdepth}%
    \@trivlist
    \labelwidth\z@
    \leftmargin\z@
    \itemindent\z@
    \let\@itemlabel\@empty
    \def\makelabel##1{##1}}

  % Description styles
  % ==================
  %
  % the next definition is somewhat tricky because labels are boxed.
  % That's fine when the label is just placed at the begining of a line
  % of text, but when the box is placed without horizontal material,
  % leading is killed.  So, we need change somehow \box to \unhbox, but
  % I don't want to modify \@item.  The code below presumes \@item has
  % not been changed and arguments gobble the part setting \@labels,
  % which is replaced by a new one.
  %
  % The default value in description is itemindent=!, but some styles
  % (those whose item text begin at a fixed place, ie, nextline,
  % multiline and sameline) change it to labelwidth=!.
  %
  % We must be careful with the group and the whatsit added by color to
  % boxes.  Alignment is applied here and some adjustments in skips are
  % necessary to get proper line breaks (including a \nobreak at the
  % beginning of \enit@align, ie, after the first whatsit, see above).
  % To "pass" the inner group added by color to the box, \enit@postlabel
  % ckecks if the following is }.  ie, \egroup -- if not, the box has
  % not reached yet its end.

  \def\enit@postlabel{%
    \@ifnextchar\egroup
      {\aftergroup\enit@postlabel}%
      {\enit@postlabel@i}}

  \def\enit@postlabel@i#1#2#3#4#5{%
    \def\enit@lblpenalty{\penalty\z@\hskip\skip@}%
    \ifenit@nextline
      \ifdim\wd\@tempboxa>\labelwidth
        \def\enit@lblpenalty{\newline\@nobreaktrue}%
      \fi
    \fi
    \everypar{%
      \@minipagefalse
      \global\@newlistfalse
      \if@inlabel
        \global\@inlabelfalse
        {\setbox\z@\lastbox
         \ifvoid\z@
           \kern-\itemindent
         \fi}%
        \unhbox\@labels
        \skip@\lastskip % Save last \labelsep
        \unskip % Remove it 
        \enit@lblpenalty % Restore it, after penalty
      \fi
      \if@nobreak
        \@nobreakfalse
        \clubpenalty\@M
      \else
        \clubpenalty\@clubpenalty
        \everypar{}%
      \fi}%
    \def\enit@a{#1#2#3#4}%
    \def\enit@b{\global\setbox\@labels\hbox}%
    \ifx\enit@a\enit@b\else
      \enit@error{Non standard \string\item}%
        {A class or a package has redefined \string\item\MessageBreak
         and I do not know how to continue}%
    \fi
    \global\setbox\@labels\hbox{%
      \unhbox\@labels
      \hskip\itemindent
      \hskip-\labelwidth
      \hskip-\labelsep
      \ifdim\wd\@tempboxa>\labelwidth
        \enit@align{\unhbox\@tempboxa}\unskip % Removes (typically) \hfil
      \else
        \leavevmode\hbox to\labelwidth{\enit@align{\unhbox\@tempboxa}}%
      \fi
      \hskip\labelsep}}

  % +=============================+
  % |     (RE)DEFINING LISTS      |
  % +=============================+
  %
  % Set keys/values
  % ===============
  % Remember \romannumeral0 expands to nothing.
  % #1 = list name, #2 = level, #3 = flag if star, #4 = keys/values

  \def\enit@saveset#1#2#3#4{%
    \setcounter{enit@cnt}{#2}%
    \ifcase#3%
      \expandafter
      \def\csname enit@@#1\romannumeral\c@enit@cnt\endcsname{#4}%
    \or
      \expandafter\let\expandafter\enit@b
        \csname enit@@#1\romannumeral\c@enit@cnt\endcsname
      \ifx\enit@b\relax
        \let\enit@b\@empty
      \fi
      \expandafter\def
        \csname enit@@#1\romannumeral\c@enit@cnt\expandafter\endcsname
        \expandafter{\enit@b,#4}%
    \fi}

  % To do: more robust tests (catch wrong names, but not easy)

  % Internally, LaTeX uses a short name for enumerate (enum)
  % and itemize (item). To be consistent with this convention,
  % a couple of macros provide a "translation". I'm not very
  % happy with the current implementation.

  \def\enit@shortenumerate{enum}
  \def\enit@shortitemize{item}

  \newcommand\setlist{%
    \@ifstar{\enit@setlist\@ne}{\enit@setlist\z@}}

  \def\enit@setlist#1{%
    \@ifnextchar[{\enit@setlist@x#1}{\enit@setlist@i#1\@empty}}

  % Let's accept \setlist[]*{}, too, because an error in <=3.5.1

  \def\enit@setlist@x#1[#2]{%
    \@ifstar{\enit@setlist@i\@ne{#2}}{\enit@setlist@i#1{#2}}}

  % #1 list names/levels, #2 keys/values

  % #1 star flag, #2 list names/levels, #3 keys/values

  \def\enit@setlist@i#1#2#3{%
    \let\enit@eltnames\relax
    \let\enit@b\@empty
    \let\enit@eltlevels\relax
    \let\enit@c\@empty
    \protected@edef\enit@a{#2}%
    \@for\enit@a:=\enit@a\do{% the 2nd enit@a is first expanded
      \@ifundefined{enitdp@\enit@meaning\enit@a}%
        {\edef\enit@c{\enit@c\enit@eltlevels{\enit@a}}}%
        {\@ifundefined{enit@short\enit@meaning\enit@a}%
           \@empty
           {\edef\enit@a{\@nameuse{enit@short\enit@a}}}%
         \edef\enit@b{\enit@b\enit@eltnames{\enit@a}}}}%
    \ifx\enit@b\@empty
       \def\enit@b{\enit@eltnames{list}}%
    \fi
    \ifx\enit@c\@empty
       \def\enit@c{\enit@eltlevels{0}}%
    \fi
    \def\enit@eltnames##1{%
      \def\enit@a{##1}%
      \enit@c}%
    \def\enit@eltlevels##1{%
      \enit@saveset\enit@a{##1}#1{#3}}%
    \enit@b}%

  % Deprecated:

  \newcommand\setdisplayed[1][0]{\setlist[trivlist,#1]}
  \let\enitdp@trivlist\@empty % dummy, let know it exists
  \newcommand\setenumerate[1][0]{\setlist[enumerate,#1]}
  \newcommand\setitemize[1][0]{\setlist[itemize,#1]}
  \newcommand\setdescription[1][0]{\setlist[description,#1]}

  % New lists
  % =========

  % When defining a list, \label... and counters must be defined
  % for each level, too:

  \def\enit@xset@itemize{%
    \@namedef{label\enit@c\romannumeral\count@}{%
      \enit@error{Undefined label}%
        {You have defined a list, but labels have
         not been setup.\MessageBreak
         You can set the label field with \string\setlist.}}}
  \@namedef{enit@xset@itemize*}{\enit@xset@itemize}

  \def\enit@xset@enumerate{%
    \enit@xset@itemize
    \@ifundefined{c@\enit@c\romannumeral\count@}%
      {\@definecounter{\enit@c\romannumeral\count@}}{}}
  \@namedef{enit@xset@enumerate*}{\enit@xset@enumerate}

  \let\enit@xset@description\@empty
  \@namedef{enit@xset@description*}{\enit@xset@description}

  \newcommand\newlist{\enit@newlist\newenvironment}
  \newcommand\renewlist{\enit@newlist\renewenvironment}

  % <new/renew>, <name>, <type>, <max-depth>

  \def\enit@newlist#1#2#3#4{%
    \@ifundefined{enit@xset@#3}%
      {\enit@error{Unknown list type `#3')}%
            {Valid types are:
             enumerate, itemize, description,\messageBreak
             enumerate*, itemize*, description*}}%
      {}%
    \setcounter{enit@cnt}{#4}%
    \count@\@ne
    \@ifundefined{enit@short#2}%
      {\def\enit@c{#2}}%
      {\edef\enit@c{\csname enit@short#2\endcsname}}%
    \loop
      \@nameuse{enit@xset@#3}% Uses \enit@c
      \ifnum\count@<\c@enit@cnt
      \advance\count@\@ne
    \repeat
    \@ifundefined{enitdp@#2}%
      {\expandafter\newcount\csname enitdp@#2\endcsname}{}%
    \csname enitdp@#2\endcsname\z@
    \advance\c@enit@cnt\m@ne
    \edef\enit@a{%
      \noexpand#1{#2}[1][]%
        {\enit@noexcs{enit@#3}%
         \enit@noexcs{enitdp@#2}%
         {\enit@c}%
         {\the\c@enit@cnt}%
         {####1}}%
        {\enit@noexcs{enit@end#3}}}%
    \enit@a}

  % Changing the default nesting limit
  % ----------------------------------

  \newcommand\setlistdepth{\def\enit@listdepth}
  \setlistdepth{5}

  % +=============================+
  % |       PACKAGE OPTIONS       |
  % +=============================+

  \newif\ifenit@loadonly

  \DeclareOption{ignoredisplayed}{\let\enit@trivlist\trivlist}
  \DeclareOption{loadonly}{\enit@loadonlytrue}
  \DeclareOption{shortlabels}
    {\def\enit@shl#1{%
       \ifnum\enit@type=\tw@
         \enit@toks{#1}%
       \else
         \def\enit@c{#1}%
         \enit@first#1,\@nil\@@nil % Returns enit@toks
      \fi}}
  \DeclareOption{inline}
    {\newenvironment{enumerate*}[1][]%
       {\@nameuse{enit@enumerate*}\enitdp@enumerate{enum}\thr@@{#1}}
       {\@nameuse{enit@endenumerate*}}
     \newenvironment{itemize*}[1][]%
       {\@nameuse{enit@itemize*}\enitdp@itemize{item}\thr@@{#1}}
       {\@nameuse{enit@enditemize*}}
     \newenvironment{description*}[1][]%
       {\@nameuse{enit@description*}\enitdp@description{description}\@M{#1}}
       {\@nameuse{enit@enddescription*}}}

  \let\enit@shl\enit@toks

  \ProcessOptions

  \let\trivlist\enit@trivlist

  % If there is no loadonly, redefine the basic lists:

  \ifenit@loadonly\else

  \let\enitdp@enumerate\@enumdepth
  \renewenvironment{enumerate}[1][]
    {\enit@enumerate\enitdp@enumerate{enum}\thr@@{#1}}
    {\enit@endenumerate}

  \let\enitdp@itemize\@itemdepth
  \renewenvironment{itemize}[1][]
    {\enit@itemize\enitdp@itemize{item}\thr@@{#1}}
    {\enit@enditemize}

  \newcount\enitdp@description
  \renewenvironment{description}[1][]
    {\enit@description\enitdp@description{description}\@M{#1}}
    {\enit@enddescription}

  \fi

% This is FULLPAGE.STY, version 2 
  % This is FULLPAGE.STY by H.Partl, Version 2 as of 15 Dec 1988.
  \topmargin 0pt
  \headheight 13.6pt  % necessary variation to avoid overfull vboxes
  \headsep 30pt       % necessary variation to provide nicer tops of pages
  \advance \topmargin by -\headheight
  \advance \topmargin by -\headsep
  \textheight 9.5in
  \oddsidemargin 0pt
  \evensidemargin \oddsidemargin
  \marginparwidth 0.5in
  \textwidth 6.5in
  %  end of FULLPAGE.STY

% This is FANCYHDR.STY
  % (hardcoded to ensure consistency between users)
  % fancyhdr.sty version 3.2
  % Fancy headers and footers for LaTeX.
  % Piet van Oostrum, 
  % Dept of Computer and Information Sciences, University of Utrecht,
  % Padualaan 14, P.O. Box 80.089, 3508 TB Utrecht, The Netherlands
  % Telephone: +31 30 2532180. Email: piet@cs.uu.nl
  % ========================================================================
  % LICENCE:
  % This file may be distributed under the terms of the LaTeX Project Public
  % License, as described in lppl.txt in the base LaTeX distribution.
  % Either version 1 or, at your option, any later version.
  % ========================================================================

  \def\ifancy@mpty#1{\def\temp@a{#1}\ifx\temp@a\@empty}

  \def\fancy@def#1#2{\ifancy@mpty{#2}\fancy@gbl\def#1{\leavevmode}\else
                                     \fancy@gbl\def#1{#2\strut}\fi}

  \let\fancy@gbl\global

  \def\@fancyerrmsg#1{%
          \ifx\PackageError\undefined
          \errmessage{#1}\else
          \PackageError{Fancyhdr}{#1}{}\fi}
  \def\@fancywarning#1{%
          \ifx\PackageWarning\undefined
          \errmessage{#1}\else
          \PackageWarning{Fancyhdr}{#1}{}\fi}

  % Usage: \@forc \var{charstring}{command to be executed for each char}
  % This is similar to LaTeX's \@tfor, but expands the charstring.

  \def\@forc#1#2#3{\expandafter\f@rc\expandafter#1\expandafter{#2}{#3}}
  \def\f@rc#1#2#3{\def\temp@ty{#2}\ifx\@empty\temp@ty\else
                                      \f@@rc#1#2\f@@rc{#3}\fi}
  \def\f@@rc#1#2#3\f@@rc#4{\def#1{#2}#4\f@rc#1{#3}{#4}}

  % Usage: \f@nfor\name:=list\do{body}
  % Like LaTeX's \@for but an empty list is treated as a list with an empty
  % element

  \newcommand{\f@nfor}[3]{\edef\@fortmp{#2}%
      \expandafter\@forloop#2,\@nil,\@nil\@@#1{#3}}

  % Usage: \def@ult \cs{defaults}{argument}
  % sets \cs to the characters from defaults appearing in argument
  % or defaults if it would be empty. All characters are lowercased.

  \newcommand\def@ult[3]{%
      \edef\temp@a{\lowercase{\edef\noexpand\temp@a{#3}}}\temp@a
      \def#1{}%
      \@forc\tmpf@ra{#2}%
          {\expandafter\if@in\tmpf@ra\temp@a{\edef#1{#1\tmpf@ra}}{}}%
      \ifx\@empty#1\def#1{#2}\fi}
  % 
  % \if@in <char><set><truecase><falsecase>
  %
  \newcommand{\if@in}[4]{%
      \edef\temp@a{#2}\def\temp@b##1#1##2\temp@b{\def\temp@b{##1}}%
      \expandafter\temp@b#2#1\temp@b\ifx\temp@a\temp@b #4\else #3\fi}

  \newcommand{\fancyhead}{\@ifnextchar[{\f@ncyhf\fancyhead h}%
                                       {\f@ncyhf\fancyhead h[]}}
  \newcommand{\fancyfoot}{\@ifnextchar[{\f@ncyhf\fancyfoot f}%
                                       {\f@ncyhf\fancyfoot f[]}}
  \newcommand{\fancyhf}{\@ifnextchar[{\f@ncyhf\fancyhf{}}%
                                     {\f@ncyhf\fancyhf{}[]}}

  % New commands for offsets added

  \newcommand{\fancyheadoffset}{\@ifnextchar[{\f@ncyhfoffs\fancyheadoffset h}%
                                             {\f@ncyhfoffs\fancyheadoffset h[]}}
  \newcommand{\fancyfootoffset}{\@ifnextchar[{\f@ncyhfoffs\fancyfootoffset f}%
                                             {\f@ncyhfoffs\fancyfootoffset f[]}}
  \newcommand{\fancyhfoffset}{\@ifnextchar[{\f@ncyhfoffs\fancyhfoffset{}}%
                                           {\f@ncyhfoffs\fancyhfoffset{}[]}}

  % The header and footer fields are stored in command sequences with
  % names of the form: \f@ncy<x><y><z> with <x> for [eo], <y> from [lcr]
  % and <z> from [hf].

  \def\f@ncyhf#1#2[#3]#4{%
      \def\temp@c{}%
      \@forc\tmpf@ra{#3}%
          {\expandafter\if@in\tmpf@ra{eolcrhf,EOLCRHF}%
              {}{\edef\temp@c{\temp@c\tmpf@ra}}}%
      \ifx\@empty\temp@c\else
          \@fancyerrmsg{Illegal char `\temp@c' in \string#1 argument:
            [#3]}%
      \fi
      \f@nfor\temp@c{#3}%
          {\def@ult\f@@@eo{eo}\temp@c
           \if@twoside\else
             \if\f@@@eo e\@fancywarning
               {\string#1's `E' option without twoside option is useless}\fi\fi
           \def@ult\f@@@lcr{lcr}\temp@c
           \def@ult\f@@@hf{hf}{#2\temp@c}%
           \@forc\f@@eo\f@@@eo
               {\@forc\f@@lcr\f@@@lcr
                   {\@forc\f@@hf\f@@@hf
                       {\expandafter\fancy@def\csname
                        f@ncy\f@@eo\f@@lcr\f@@hf\endcsname
                        {#4}}}}}}

  \def\f@ncyhfoffs#1#2[#3]#4{%
      \def\temp@c{}%
      \@forc\tmpf@ra{#3}%
          {\expandafter\if@in\tmpf@ra{eolrhf,EOLRHF}%
              {}{\edef\temp@c{\temp@c\tmpf@ra}}}%
      \ifx\@empty\temp@c\else
          \@fancyerrmsg{Illegal char `\temp@c' in \string#1 argument:
            [#3]}%
      \fi
      \f@nfor\temp@c{#3}%
          {\def@ult\f@@@eo{eo}\temp@c
           \if@twoside\else
             \if\f@@@eo e\@fancywarning
               {\string#1's `E' option without twoside option is useless}\fi\fi
           \def@ult\f@@@lcr{lr}\temp@c
           \def@ult\f@@@hf{hf}{#2\temp@c}%
           \@forc\f@@eo\f@@@eo
               {\@forc\f@@lcr\f@@@lcr
                   {\@forc\f@@hf\f@@@hf
                       {\expandafter\setlength\csname
                        f@ncyO@\f@@eo\f@@lcr\f@@hf\endcsname
                        {#4}}}}}%
       \fancy@setoffs}

  % Fancyheadings version 1 commands. These are more or less deprecated,
  % but they continue to work.

  \newcommand{\lhead}{\@ifnextchar[{\@xlhead}{\@ylhead}}
  \def\@xlhead[#1]#2{\fancy@def\f@ncyelh{#1}\fancy@def\f@ncyolh{#2}}
  \def\@ylhead#1{\fancy@def\f@ncyelh{#1}\fancy@def\f@ncyolh{#1}}

  \newcommand{\chead}{\@ifnextchar[{\@xchead}{\@ychead}}
  \def\@xchead[#1]#2{\fancy@def\f@ncyech{#1}\fancy@def\f@ncyoch{#2}}
  \def\@ychead#1{\fancy@def\f@ncyech{#1}\fancy@def\f@ncyoch{#1}}

  \newcommand{\rhead}{\@ifnextchar[{\@xrhead}{\@yrhead}}
  \def\@xrhead[#1]#2{\fancy@def\f@ncyerh{#1}\fancy@def\f@ncyorh{#2}}
  \def\@yrhead#1{\fancy@def\f@ncyerh{#1}\fancy@def\f@ncyorh{#1}}

  \newcommand{\lfoot}{\@ifnextchar[{\@xlfoot}{\@ylfoot}}
  \def\@xlfoot[#1]#2{\fancy@def\f@ncyelf{#1}\fancy@def\f@ncyolf{#2}}
  \def\@ylfoot#1{\fancy@def\f@ncyelf{#1}\fancy@def\f@ncyolf{#1}}

  \newcommand{\cfoot}{\@ifnextchar[{\@xcfoot}{\@ycfoot}}
  \def\@xcfoot[#1]#2{\fancy@def\f@ncyecf{#1}\fancy@def\f@ncyocf{#2}}
  \def\@ycfoot#1{\fancy@def\f@ncyecf{#1}\fancy@def\f@ncyocf{#1}}

  \newcommand{\rfoot}{\@ifnextchar[{\@xrfoot}{\@yrfoot}}
  \def\@xrfoot[#1]#2{\fancy@def\f@ncyerf{#1}\fancy@def\f@ncyorf{#2}}
  \def\@yrfoot#1{\fancy@def\f@ncyerf{#1}\fancy@def\f@ncyorf{#1}}

  \newlength{\fancy@headwidth}
  \let\headwidth\fancy@headwidth
  \newlength{\f@ncyO@elh}
  \newlength{\f@ncyO@erh}
  \newlength{\f@ncyO@olh}
  \newlength{\f@ncyO@orh}
  \newlength{\f@ncyO@elf}
  \newlength{\f@ncyO@erf}
  \newlength{\f@ncyO@olf}
  \newlength{\f@ncyO@orf}
  \newcommand{\headrulewidth}{0.4pt}
  \newcommand{\footrulewidth}{0pt}
  \newcommand{\footruleskip}{.3\normalbaselineskip}

  % Fancyplain stuff shouldn't be used anymore (rather
  % \fancypagestyle{plain} should be used), but it must be present for
  % compatibility reasons.

  \newcommand{\plainheadrulewidth}{0pt}
  \newcommand{\plainfootrulewidth}{0pt}
  \newif\if@fancyplain \@fancyplainfalse
  \def\fancyplain#1#2{\if@fancyplain#1\else#2\fi}

  \headwidth=-123456789sp %magic constant

  % Command to reset various things in the headers:
  % a.o.  single spacing (taken from setspace.sty)
  % and the catcode of ^^M (so that epsf files in the header work if a
  % verbatim crosses a page boundary)
  % It also defines a \nouppercase command that disables \uppercase and
  % \Makeuppercase. It can only be used in the headers and footers.
  \let\fnch@everypar\everypar% save real \everypar because of spanish.ldf
  \def\fancy@reset{\fnch@everypar{}\restorecr\endlinechar=13
   \def\baselinestretch{1}%
   \def\nouppercase##1{{\let\uppercase\relax\let\MakeUppercase\relax
       \expandafter\let\csname MakeUppercase \endcsname\relax##1}}%
   \ifx\undefined\@newbaseline% NFSS not present; 2.09 or 2e
     \ifx\@normalsize\undefined \normalsize % for ucthesis.cls
     \else \@normalsize \fi
   \else% NFSS (2.09) present
    \@newbaseline%
   \fi}

  % Initialization of the head and foot text.

  % The default values still contain \fancyplain for compatibility.
  \fancyhf{} % clear all
  % lefthead empty on ``plain'' pages, \rightmark on even, \leftmark on odd pages
  % evenhead empty on ``plain'' pages, \leftmark on even, \rightmark on odd pages
  \if@twoside
    \fancyhead[el,or]{\fancyplain{}{\sl\rightmark}}
    \fancyhead[er,ol]{\fancyplain{}{\sl\leftmark}}
  \else
    \fancyhead[l]{\fancyplain{}{\sl\rightmark}}
    \fancyhead[r]{\fancyplain{}{\sl\leftmark}}
  \fi
  \fancyfoot[c]{\rm\thepage} % page number

  % Use box 0 as a temp box and dimen 0 as temp dimen. 
  % This can be done, because this code will always
  % be used inside another box, and therefore the changes are local.

  \def\@fancyvbox#1#2{\setbox0\vbox{#2}\ifdim\ht0>#1\@fancywarning
    {\string#1 is too small (\the#1): ^^J Make it at least \the\ht0.^^J
      We now make it that large for the rest of the document.^^J
      This may cause the page layout to be inconsistent, however\@gobble}%
    \dimen0=#1\global\setlength{#1}{\ht0}\ht0=\dimen0\fi
    \box0}

  % Put together a header or footer given the left, center and
  % right text, fillers at left and right and a rule.
  % The \lap commands put the text into an hbox of zero size,
  % so overlapping text does not generate an errormessage.
  % These macros have 5 parameters:
  % 1. LEFTSIDE BEARING % This determines at which side the header will stick
  %    out. When \fancyhfoffset is used this calculates \headwidth, otherwise
  %    it is \hss or \relax (after expansion).
  % 2. \f@ncyolh, \f@ncyelh, \f@ncyolf or \f@ncyelf. This is the left component.
  % 3. \f@ncyoch, \f@ncyech, \f@ncyocf or \f@ncyecf. This is the middle comp.
  % 4. \f@ncyorh, \f@ncyerh, \f@ncyorf or \f@ncyerf. This is the right component.
  % 5. RIGHTSIDE BEARING. This is always \relax or \hss (after expansion).

  \def\@fancyhead#1#2#3#4#5{#1\hbox to\headwidth{\fancy@reset
    \@fancyvbox\headheight{\hbox
      {\rlap{\parbox[b]{\headwidth}{\raggedright#2}}\hfill
        \parbox[b]{\headwidth}{\centering#3}\hfill
        \llap{\parbox[b]{\headwidth}{\raggedleft#4}}}\headrule}}#5}

  \def\@fancyfoot#1#2#3#4#5{#1\hbox to\headwidth{\fancy@reset
      \@fancyvbox\footskip{\footrule
        \hbox{\rlap{\parbox[t]{\headwidth}{\raggedright#2}}\hfill
          \parbox[t]{\headwidth}{\centering#3}\hfill
          \llap{\parbox[t]{\headwidth}{\raggedleft#4}}}}}#5}

  \def\headrule{{\if@fancyplain\let\headrulewidth\plainheadrulewidth\fi
      \hrule\@height\headrulewidth\@width\headwidth \vskip-\headrulewidth}}

  \def\footrule{{\if@fancyplain\let\footrulewidth\plainfootrulewidth\fi
      \vskip-\footruleskip\vskip-\footrulewidth
      \hrule\@width\headwidth\@height\footrulewidth\vskip\footruleskip}}

  \def\ps@fancy{%
  \@ifundefined{@chapapp}{\let\@chapapp\chaptername}{}%for amsbook
  %
  % Define \MakeUppercase for old LaTeXen.
  % Note: we used \def rather than \let, so that \let\uppercase\relax (from
  % the version 1 documentation) will still work.
  %
  \@ifundefined{MakeUppercase}{\def\MakeUppercase{\uppercase}}{}%
  \@ifundefined{chapter}{\def\sectionmark##1{\markboth
  {\MakeUppercase{\ifnum \c@secnumdepth>\z@
   \thesection\hskip 1em\relax \fi ##1}}{}}%
  \def\subsectionmark##1{\markright {\ifnum \c@secnumdepth >\@ne
   \thesubsection\hskip 1em\relax \fi ##1}}}%
  {\def\chaptermark##1{\markboth {\MakeUppercase{\ifnum \c@secnumdepth>\m@ne
   \@chapapp\ \thechapter. \ \fi ##1}}{}}%
  \def\sectionmark##1{\markright{\MakeUppercase{\ifnum \c@secnumdepth >\z@
   \thesection. \ \fi ##1}}}}%
  %\csname ps@headings\endcsname % use \ps@headings defaults if they exist
  \ps@@fancy
  \gdef\ps@fancy{\@fancyplainfalse\ps@@fancy}%
  % Initialize \headwidth if the user didn't
  %
  \ifdim\headwidth<0sp
  %
  % This catches the case that \headwidth hasn't been initialized and the
  % case that the user added something to \headwidth in the expectation that
  % it was initialized to \textwidth. We compensate this now. This loses if
  % the user intended to multiply it by a factor. But that case is more
  % likely done by saying something like \headwidth=1.2\textwidth. 
  % The doc says you have to change \headwidth after the first call to
  % \pagestyle{fancy}. This code is just to catch the most common cases were
  % that requirement is violated.
  %
      \global\advance\headwidth123456789sp\global\advance\headwidth\textwidth
  \fi}
  \def\ps@fancyplain{\ps@fancy \let\ps@plain\ps@plain@fancy}
  \def\ps@plain@fancy{\@fancyplaintrue\ps@@fancy}
  \let\ps@@empty\ps@empty
  \def\ps@@fancy{%
  \ps@@empty % This is for amsbook/amsart, which do strange things with \topskip
  \def\@mkboth{\protect\markboth}%
  \def\@oddhead{\@fancyhead\fancy@Oolh\f@ncyolh\f@ncyoch\f@ncyorh\fancy@Oorh}%
  \def\@oddfoot{\@fancyfoot\fancy@Oolf\f@ncyolf\f@ncyocf\f@ncyorf\fancy@Oorf}%
  \def\@evenhead{\@fancyhead\fancy@Oelh\f@ncyelh\f@ncyech\f@ncyerh\fancy@Oerh}%
  \def\@evenfoot{\@fancyfoot\fancy@Oelf\f@ncyelf\f@ncyecf\f@ncyerf\fancy@Oerf}%
  }
  % Default definitions for compatibility mode:
  % These cause the header/footer to take the defined \headwidth as width
  % And to shift in the direction of the marginpar area

  \def\fancy@Oolh{\if@reversemargin\hss\else\relax\fi}
  \def\fancy@Oorh{\if@reversemargin\relax\else\hss\fi}
  \let\fancy@Oelh\fancy@Oorh
  \let\fancy@Oerh\fancy@Oolh

  \let\fancy@Oolf\fancy@Oolh
  \let\fancy@Oorf\fancy@Oorh
  \let\fancy@Oelf\fancy@Oelh
  \let\fancy@Oerf\fancy@Oerh

  % New definitions for the use of \fancyhfoffset
  % These calculate the \headwidth from \textwidth and the specified offsets.

  \def\fancy@offsolh{\headwidth=\textwidth\advance\headwidth\f@ncyO@olh
                     \advance\headwidth\f@ncyO@orh\hskip-\f@ncyO@olh}
  \def\fancy@offselh{\headwidth=\textwidth\advance\headwidth\f@ncyO@elh
                     \advance\headwidth\f@ncyO@erh\hskip-\f@ncyO@elh}

  \def\fancy@offsolf{\headwidth=\textwidth\advance\headwidth\f@ncyO@olf
                     \advance\headwidth\f@ncyO@orf\hskip-\f@ncyO@olf}
  \def\fancy@offself{\headwidth=\textwidth\advance\headwidth\f@ncyO@elf
                     \advance\headwidth\f@ncyO@erf\hskip-\f@ncyO@elf}

  \def\fancy@setoffs{%
  % Just in case \let\headwidth\textwidth was used
    \fancy@gbl\let\headwidth\fancy@headwidth
    \fancy@gbl\let\fancy@Oolh\fancy@offsolh
    \fancy@gbl\let\fancy@Oelh\fancy@offselh
    \fancy@gbl\let\fancy@Oorh\hss
    \fancy@gbl\let\fancy@Oerh\hss
    \fancy@gbl\let\fancy@Oolf\fancy@offsolf
    \fancy@gbl\let\fancy@Oelf\fancy@offself
    \fancy@gbl\let\fancy@Oorf\hss
    \fancy@gbl\let\fancy@Oerf\hss}

  \newif\iffootnote
  \let\latex@makecol\@makecol
  \def\@makecol{\ifvoid\footins\footnotetrue\else\footnotefalse\fi
  \let\topfloat\@toplist\let\botfloat\@botlist\latex@makecol}
  \def\iftopfloat#1#2{\ifx\topfloat\empty #2\else #1\fi}
  \def\ifbotfloat#1#2{\ifx\botfloat\empty #2\else #1\fi}
  \def\iffloatpage#1#2{\if@fcolmade #1\else #2\fi}

  \newcommand{\fancypagestyle}[2]{%
    \@namedef{ps@#1}{\let\fancy@gbl\relax#2\relax\ps@fancy}}
  %  end of FANCYHDR.STY

% This is SUBSTR.STY
  %%
  %% substr.sty
  %%
  %% Copyright 2000, 2005, 2009 Harald Harders
  %%
  %% This program can be redistributed and/or modified under the terms
  %% of the LaTeX Project Public License Distributed from CTAN
  %% archives in directory macros/latex/base/lppl.txt; either
  %% version 1 of the License, or any later version.
  %%
  %%
  %% 2009-10-20
  %% Harald Harders
  %% h.harders@tu-bs.de
  % expands the first and second argument with
  % \protected@edef and calls #3 with them:
  \newcommand\su@ExpandTwoArgs[3]{%
    \protected@edef\su@SubString{#1}%
    \protected@edef\su@String{#2}%
    \expandafter\expandafter\expandafter#3%
    \expandafter\expandafter\expandafter{%
      \expandafter\su@SubString\expandafter
    }\expandafter{\su@String}%
  }
  %%
  %% tests if #1 in #2. If yes execute #3, else #4
  \newcommand*\IfSubStringInString[2]{%
    \su@ExpandTwoArgs{#1}{#2}\su@IfSubStringInString
  }
  %%
  \newcommand*\su@IfSubStringInString[2]{%
    \def\su@compare##1#1##2\@nil{%
      \def\su@param{##2}%
      \ifx\su@param\@empty
        \expandafter\@secondoftwo
      \else
        \expandafter\@firstoftwo
      \fi
    }%
    \su@compare#2\@nnil#1\@nil
  }
  %%
  %% tests if #1 in #2. If yes execute #3, else #4
  \newcommand\IfCharInString{}
  \let\IfCharInString\IfSubStringInString
  %%
  %% returns the part of the string behind the found substring
  \newcommand*\BehindSubString[2]{%
    \su@ExpandTwoArgs{#1}{#2}\su@BehindSubString
  }
  \newcommand*\su@BehindSubString[2]{%
    \def\su@rest##1#1##2\@nil{##2}%
    \IfSubStringInString{#1}{#2}{\su@rest#2\@nil}{}%
  }
  %%
  %% returns the part of the string before the found substring
  \newcommand*\BeforeSubString[2]{%
    \su@ExpandTwoArgs{#1}{#2}\su@BeforeSubString
  }
  \newcommand*\su@BeforeSubString[2]{%
    \def\su@rest##1#1##2\@nil{##1}%
    \IfSubStringInString{#1}{#2}{\su@rest#2\@nil}{#2}%
  }
  %%
  %% calls #3 if part of string before substring is empty, otherwise calls #4.
  \newcommand*\IfBeforeSubStringEmpty[2]{%
    \su@ExpandTwoArgs{#1}{#2}\su@IfBeforeSubStringEmpty
  }
  %%
  \newcommand*\su@IfBeforeSubStringEmpty[4]{%
    \def\su@rest##1#1##2\@nil{##1}%
    \IfSubStringInString{#1}{#2}{%
      \edef\su@resta{\su@rest#2\@nil}%  
      \ifx\@empty\su@resta #3\else #4\fi
    }{#4}%
  }
  %%
  %% calls #3 if part of string after substring is empty, otherwise calls #4.
  \newcommand*\IfBehindSubStringEmpty[2]{%
    \su@ExpandTwoArgs{#1}{#2}\su@IfBehindSubStringEmpty
  }
  %%
  \newcommand*\su@IfBehindSubStringEmpty[4]{%
    \def\su@rest##1#1##2\@nil{##2}%
    \IfSubStringInString{#1}{#2}{%
      \edef\su@resta{\su@rest#2\@nil}%  
      \ifx\@empty\su@resta #3\else #4\fi
    }{#4}%
  }
  %%
  %% counter for counting appearances
  \newcounter{su@anzahl}
  %%
  % #1: String
  % #2: Substring
  % #3: Counter
  \newcommand*\su@StringSubstringCounter[3]{%
    \su@IfSubStringInString{#2}{#1}{%
      \stepcounter{#3}%
      \def\su@rest##1#2##2\@nil{##2}%
      \expandafter\su@StringSubstringCounter\expandafter
      {\su@rest#1\@nil}{#2}{#3}%
    }{}%
  }
  %%
  \newcommand*\CountSubStrings[2]{%
    \su@ExpandTwoArgs{#1}{#2}\su@CountSubStrings
  }
  \newcommand*\su@CountSubStrings[2]{%
    \setcounter{su@anzahl}{0}%
    \su@StringSubstringCounter{#2}{#1}{su@anzahl}%
    \thesu@anzahl
  }
  % #1: counter
  % #2: substring
  % #3: string
  \newcommand*\SubStringsToCounter[3]{%
    \su@ExpandTwoArgs{#2}{#3}\su@SubStringsToCounter{#1}%
  }
  % #1: substring
  % #2: string
  % #3: counter
  \newcommand*\su@SubStringsToCounter[3]{%
    \setcounter{#3}{0}%
    \su@StringSubstringCounter{#2}{#1}{#3}%
  }

%GWS: Pull in some packages. I try to keep this to a minimum.
\usepackage{pdftexcmds,ifthen,xcolor,xargs,expl3}

% Interate through the boxes that make up solutions and replace the boxes with empty boxes of the same height/width/depth.
% This should preserve page breaks better than my previous solution of inserting blank lines all of the same height.
% NOTE: This requires latex3 syntax and a modern latex compiler.
% https://tex.stackexchange.com/questions/43069/how-to-replace-a-large-block-of-text-by-an-empty-block-of-the-same-size
\ExplSyntaxOn

\box_new:N \l_mypkg_box
\int_new:N \l_mypkg_cleanup_int
\cs_new_protected:Npn \hideit #1#2
  {
    \tex_setbox:D \l_mypkg_box \tex_vbox:D
      {
        \let\oldvspacer\@vspacer
        \let\oldvspace\@vspace
        \renewcommand{\@vspacer}[1]{}%
        \renewcommand{\@vspace}[1]{}%
        #2\par
        \let\@vspacer\oldvspacer
        \let\@vspace\oldvspace
        \dim_zero:N \tex_baselineskip:D
        \dim_zero:N \tex_lineskip:D
        \dim_zero:N \tex_lineskiplimit:D
        \int_set:Nn \l_mypkg_cleanup_int {#1}
        \mypkg_dismantle_loop:
      }
    \tex_unvbox:D \l_mypkg_box
  }
\cs_new_protected:Npn \mypkg_dismantle_loop:
  {
    \prg_replicate:nn { \l_mypkg_cleanup_int }
      {
        \@ifundefined{str_if_eq:xxT}{
          \skip_if_eq:nnT { \tex_lastskip:D } { \c_zero_skip } { \tex_unskip:D }
        }{
          \str_if_eq:xxT { \tex_the:D \tex_lastskip:D } { \tex_the:D \c_zero_skip } { \tex_unskip:D }
        }
        \dim_compare:nT { \tex_lastkern:D = \c_zero_dim } { \tex_unkern:D }
        \int_compare:nT { \tex_lastpenalty:D = \c_zero } { \tex_unpenalty:D }
      }
    \@ifundefined{str_if_eq:xxTF}{
      \skip_if_eq:nnTF { \tex_lastskip:D } { \c_zero_skip }
      {
        \dim_compare:nTF { \tex_lastkern:D = \c_zero_dim }
          {
            \int_compare:nTF { \tex_lastpenalty:D = \c_zero }
              {
                \box_set_to_last:N \l_mypkg_box
                \box_if_empty:NF \l_mypkg_box
                  { \mypkg_dismantle_box: }
              }
              { \mypkg_dismantle_penalty: }
          }
          { \mypkg_dismantle_kern: }
      }
    }{
    \str_if_eq:xxTF { \tex_the:D \tex_lastskip:D } { \tex_the:D \c_zero_skip }
      {
        \dim_compare:nTF { \tex_lastkern:D = \c_zero_dim }
          {
            \int_compare:nTF { \tex_lastpenalty:D = \c_zero }
              {
                \box_set_to_last:N \l_mypkg_box
                \box_if_empty:NF \l_mypkg_box
                  { \mypkg_dismantle_box: }
              }
              { \mypkg_dismantle_penalty: }
          }
          { \mypkg_dismantle_kern: }
      }
      }

      { \mypkg_dismantle_skip: }
  }
\cs_new_protected:Npn \mypkg_dismantle_skip:
  { \mypkg_dismantle_aux:nN { \tex_vskip:D \skip_use:N \tex_lastskip:D } \tex_unskip:D }
\cs_new_protected:Npn \mypkg_dismantle_kern:
  { \mypkg_dismantle_aux:nN { \tex_kern:D \dim_use:N \tex_lastkern:D } \tex_unkern:D }
\cs_new_protected:Npn \mypkg_dismantle_penalty:
  { \mypkg_dismantle_aux:nN { \tex_penalty:D \int_use:N \tex_lastpenalty:D } \tex_unpenalty:D }
\cs_new_protected:Npn \mypkg_dismantle_box:
  { \mypkg_dismantle_aux:nN { \tex_vbox:D to \dim_eval:n { \box_ht:N \l_mypkg_box + \box_dp:N \l_mypkg_box } {} } \scan_stop: }
\cs_new_protected:Npn \mypkg_dismantle_aux:nN #1#2
  {
    \use:x
      {
        #2
        \mypkg_dismantle_loop:
        #1 \scan_stop:
      }
  }
\ExplSyntaxOff

\def\setvariant#1{\edef\@variant{#1}}
\def\setmoduleone#1{\edef\modulenoone{#1}}
\def\setmoduletwo#1{\edef\modulenotwo{#1}}
\def\setmodulethree#1{\edef\modulenothree{#1}}
\def\setmodulefour#1{\edef\modulenofour{#1}}
\def\modulename#1{\edef\@modulename{#1}}
\def\semester#1{\edef\@semester{#1}}
\def\setyear#1{\edef\@year{#1}}
\def\rubric#1{\edef\rubric{#1}}
\def\settime#1{\edef\@time{#1}}
\def\setguidetime#1{\edef\@guidetime{#1}}
\def\numquestions#1{\edef\numquestions{#1}}
\def\attempt#1{\edef\@attempt{#1}}
\def\numSectA#1{\edef\numSectA{#1}}
\def\numSectB#1{\edef\numSectB{#1}}
\def\extrainstructions#1{\edef\@extrainstructions{#1}}
\def\date#1{\gdef\@date{#1}}
\def\roughpages#1{\edef\@roughpages{#1}}
\def\setwriteonexam#1{\edef\@writeonexam{#1}}
\def\showinfocollection#1{\edef\@showinfocollection{#1}}
\def\showmarkboxes#1{\edef\@showmarkboxes{#1}}
\def\showIDcollection#1{\edef\@showinfocollection{#1}}
\def\setwriteonsecaonly#1{\edef\@writeonsecaonly{#1}}
\def\solutionshow#1{\edef\@solutionshow{#1}}
\def\settotalmarks#1{\edef\@settotalmarks{#1}}
\def\rubricNtotalmarks#1{\edef\@rubricNtotalmarks{#1}}
\def\setsolutioncolor#1{\edef\@solutioncolor{#1}}
\def\setcommentcolor#1{\edef\@commentcolor{#1}}
\def\setsolutionsize#1{\let\solutionsize #1}
\def\setquestionnsize#1{\let\questionsize #1}
\def\setquestionsize#1{\let\questionsize #1}
\def\newpagequestion#1{\edef\@newpagequestion{#1}}
\def\insertblankpages#1{\edef\@insertblankpages{#1}}
\def\questionpartlabelling#1{\edef\@questionpartlabelling{#1}}
\def\roughpagetext#1{\edef\@roughpagetext{#1}}
\def\showanswercontinued#1{\edef\@showanswercontinued{#1}}
\def\calculatorpermitted#1{\edef\@calculatorpermitted{#1}}
\def\forceroughpages#1{\edef\@forceroughpages{#1}}
\def\settakehome#1{\edef\@takehome{#1}}
\def\setdeadline#1#2{\edef\@deadlinetime{#1}\edef\@deadlinedate{#2}}

% Setup module choice for infocollection
\newcommand\moduleno[2][1]{
\ifthenelse{\equal{#1}{1}}{\setmoduleone{#2}}{}
\ifthenelse{\equal{#1}{2}}{\setmoduletwo{#2}}{}
\ifthenelse{\equal{#1}{3}}{\setmodulethree{#2}}{}
\ifthenelse{\equal{#1}{4}}{\setmodulefour{#2}}{}
}

\def\@infocollectionmodules{}%
\newcommandx{\infocollectionmodules}[4][1,2,3,4]{%
  \def\@infocollectionmodulesA{}%
  \def\@infocollectionmodulesB{}%
  \def\@infocollectionmodulesC{}%
  \ifthenelse{\equal{#2}{}}{}{%
    \def\@infocollectionmodulesA{Please indicate your module code: {\LARGE$\square$} #1, {\LARGE$\square$} #2}%
    \ifthenelse{\equal{#3}{}}{}{\def\@infocollectionmodulesB{, {\LARGE$\square$} #3}}%
    \ifthenelse{\equal{#4}{}}{}{\def\@infocollectionmodulesC{, {\LARGE$\square$} #4}}%
  }%
  \def\@infocollectionmodules{\@infocollectionmodulesA\@infocollectionmodulesB\@infocollectionmodulesC}%
}%

%gws: Global variables and set defaults
\let\oleftmargini\leftmargini
\newcount\qcount
\newcount\qcountA
\newcount\qcountB
\newcount\qsubcount
\newcount\qtotalcountA
\newcount\qtotalcountB
\newcount\insidequestion
\newcount\skipthisquestion
\newcount\firstQuestionInSect
\newsavebox\nullq
\newcounter{qcounter}
\date{\today}
\modulename{Module Title Not Set!}
\moduleno{MAS0000}
\moduleno[2]{Not_Set}
\moduleno[3]{Not_Set}
\moduleno[4]{Not_Set}
\setvariant{1}
\semester{1}
\roughpages{0}
\solutionshow{hidden}
\insertblankpages{false}
\setwriteonexam{false}
\showinfocollection{false}
\showmarkboxes{false}
\showIDcollection{false}
\setwriteonsecaonly{false}
\calculatorpermitted{true}
\forceroughpages{false}
\settakehome{false}
\setdeadline{2pm}{XX/XX/XXXX}
\setguidetime{X hours}
\settotalmarks{100}
\rubricNtotalmarks{false}
\setsolutionsize{\large}
\setquestionnsize{\large}
\setquestionsize{\large}
\newpagequestion{false}
\setsolutioncolor{red}
\setcommentcolor{blue}
\extrainstructions{}
\attempt{??}
\def\sectletA{??}
\def\sectletB{??}
\roughpagetext{Rough Work -- Work submitted for marking must be clearly indicated}
\showanswercontinued{false}
\questionpartlabelling{alph}
\qcount 0\relax
\qcountA 0\relax 
\qcountB 0\relax 
\qsubcount 0\relax
\qtotalcountA 0\relax
\qtotalcountB 0\relax
\insidequestion 0\relax
\skipthisquestion 0\relax
\firstQuestionInSect 1\relax

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Build front page                                %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\long\def\addto#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}
\def\makefront{
\parskip 0ex
\parindent 0pt
\edef\sectlet{ }
\setupEnumLabelling{}%
\questionsize
{
\bfseries
\makebox[6.0truein]{\ \hfill {\bf {%
\ifthenelse{\equal{\@variant}{1}}{\modulenoone}{}%
\ifthenelse{\equal{\@variant}{2}}{\modulenotwo}{}%
\ifthenelse{\equal{\@variant}{3}}{\modulenothree}{}%
\ifthenelse{\equal{\@variant}{4}}{\modulenofour}{}%
}}}
\vspace{\stretch{2}}
\begin{center}
NEWCASTLE UNIVERSITY\\
\vspace{\stretch{1}}
SCHOOL OF MATHEMATICS, STATISTICS \& PHYSICS\\
~\\\vspace{\stretch{1}}
\begin{tabular}{c}\hline\\
\ifthenelse{\equal{\@semester}{1}}{SEMESTER 1\ }{%
  \ifthenelse{\equal{\@semester}{2}}{SEMESTER 2\ }{%
    \@semester\ 
  }%
}%
{\@year} \\ \\\hline
\end{tabular}\\~\\\vspace{\stretch{1}}
{\ifthenelse{\equal{\@variant}{1}}{\modulenoone}{}%
\ifthenelse{\equal{\@variant}{2}}{\modulenotwo}{}%
\ifthenelse{\equal{\@variant}{3}}{\modulenothree}{}%
\ifthenelse{\equal{\@variant}{4}}{\modulenofour}{}}\\\vspace{\stretch{1}}
{\@modulename}\\\vspace{\stretch{1}}
\ifthenelse{\equal{\@solutionshow}{full}}{%
\textcolor{red}{\Large\ (This document contains solutions)}\\\vspace{\stretch{1}}}{}
Time allowed: {\@time}\\\vspace{\stretch{1}}
\ifthenelse{\equal{\@takehome}{true}}{%
Your completed work must be submitted by \@deadlinetime\ (UK~time,~GMT\texttt{+}1) on \@deadlinedate\\\vspace{\stretch{1}}}{}%
\checkTotalMarks{\@ifundefined{qmarkstotalA}{0}{\qmarkstotalA}}{\@ifundefined{qmarkstotalB}{0}{\qmarkstotalB}}{%
  \@ifundefined{qtotal}{??}{\ifnum\qBtotal=0 \qtotal\else\qBtotal\fi}%
}%
\end{center}
}%
\parskip\stretch{1}%
\parindent 0pt%

\ifthenelse{\equal{\@takehome}{true}}{%
You are not expected to work on this assessment for the full 24-hour period. The assessment corresponds in length to a \textbf{\@guidetime}\ exam under normal exam conditions.

You should submit a single PDF file containing your work via the Canvas 24 hour exam area, taking note of all instructions provided there.

When answering the exam you should observe normal exam conditions and must not communicate/collude with other students taking the paper. The work that you submit must be your own work.

Should you encounter IT difficulties that significantly affect your ability to complete this exam or when uploading your answer(s) you may submit a PEC detailing these.

}{}%
\makerubric
\vspace{\stretch{10}}
\ifthenelse{\equal{\@showinfocollection}{true}}{%
{\vbox{%
  \bfseries
  Name:~\rule{0.3\paperwidth}{0.4pt}\\[1.5\baselineskip]
  Student number:~\rule{0.3\paperwidth}{0.4pt}\\[0.33\baselineskip]
  }%
  \ifthenelse{\equal{\@infocollectionmodules}{}}{}{\@infocollectionmodules}%
}}{}%
\ifthenelse{\equal{\@insertblankpages}{true} \AND \equal{\@solutionshow}{hidden} \OR \equal{\@forceroughpages}{true}}{%
  \g@addto@macro\@outputpage{%
    \shipout \vbox{\raggedright%
    \begin{picture}(50,50)
      \put(90,80.8){\makebox[10cm]{\normalsize \bf \color{black} \@roughpagetext}}%
      \put(0,76.25){\textcolor{black}{\line(1,0){469.5}}}
    \end{picture}
    \addtocounter{page}{-1}%
    \stepcounter{page}
    }%
  }%
}{}%
\questionsize%
\newpage
\parskip 0ex%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Mark box macros                                 %%
%% These macros build a set of boxes for entering  %%
%% what marks the student got.                     %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\buildmarkbox{
  %construct table description
  \newcount\qct
  \@ifundefined{qtotal}{
  \def\qctdata{c|}
  }{
  \def\qctdata{} \qct=0
  \loop\ifnum\qct<\ifnum\qtotal<8 \qtotal\else8\fi 
    \advance \qct 1
    \expandafter\addto\expandafter\qctdata\expandafter{@{}p{0.1\linewidth}@{}|}
  \repeat
  }
  %construct table description > 8
  \@ifundefined{qtotal}{
  \def\qctextradata{c|}
  }{
  \def\qctextradata{}
  \loop\ifnum\qct<\qtotal 
    \advance \qct 1
    \expandafter\addto\expandafter\qctextradata\expandafter{@{}p{0.1\linewidth}@{}|}
  \repeat
  }
  %Dont show Section letters in infosafe if no sections defined.
  \def\infoBsect{B}
  \def\infoAsect{A}
  \@ifundefined{qtotal}{}{
    \ifthenelse{\equal{\qAtotal}{0}}{\gdef\qAtotal{\qtotal}\def\infoAsect{ }}{}
  }
  %construct table spacing
  \newcount\qcs
  \@ifundefined{qtotal}{
  \def\qcsdata{\qquad\qquad}
  }{
  \def\qcsdata{} \qcs=0
  \loop\ifnum\qcs<\ifnum\qtotal<8 \qtotal\else8\fi 
    \advance \qcs 1
    \ifnum\qcs=1
    \edef\qcsline{\hfill/$_{\noexpand\hspace*{-0.15em}\noexpand\text{\csname q\the\qcs marktotal\endcsname}}$\,}%
    \else
    \edef\qcsline{&\hfill/$_{\noexpand\hspace*{-0.15em}\noexpand\text{\csname q\the\qcs marktotal\endcsname}}$\,}%
    \fi
    \expandafter\addto\expandafter\qcsdata\expandafter{\qcsline}%
  \repeat
  }
  %construct table spacing > 8
  \@ifundefined{qtotal}{
  \def\qcsextradata{}
  }{
  \def\qcsextradata{}
  \loop\ifnum\qcs<\qtotal 
    \advance \qcs 1
    \ifnum\qcs=9
    \edef\qcsline{\hfill/$_{\noexpand\hspace*{-0.15em}\noexpand\text{\csname q\the\qcs marktotal\endcsname}}$\,}%
    \else
    \edef\qcsline{&\hfill/$_{\noexpand\hspace*{-0.15em}\noexpand\text{\csname q\the\qcs marktotal\endcsname}}$\,}%
    \fi
    \expandafter\addto\expandafter\qcsextradata\expandafter{\qcsline}
  \repeat
  }
  %A questions
  \newcount\qca
  \@ifundefined{qAtotal}{
  \def\qcadata{??}
  }{
  \def\qcadata{} \qca=0
  \loop\ifnum\qca<\ifnum\qAtotal<8 \qAtotal\else8\fi 
    \advance \qca 1
    \ifnum\qca=1
    \edef\qcaline{~~\,\infoAsect\the\qca}
    \else
    \edef\qcaline{&~~\,\infoAsect\the\qca}
    \fi
    \expandafter\addto\expandafter\qcadata\expandafter{\qcaline}
  \repeat
  }
  %B questions
  \@ifundefined{qBtotal}{
  \def\qcbdata{??}
  }{
  \def\qcbdata{}
  \loop\ifnum\qca<\ifnum\qtotal<8 \qtotal\else8\fi
    \advance \qca 1
    \edef\qcbline{&~~\,\infoBsect\the\qca}
    \expandafter\addto\expandafter\qcbdata\expandafter{\qcbline}
  \repeat
  }
  %A questions > 8
  \@ifundefined{qAtotal}{
  \def\qcaextradata{}
  }{
  \def\qcaextradata{}
  \loop\ifnum\qca<\qAtotal 
    \advance \qca 1
    \ifnum\qca=9
    \edef\qcaline{~~\,\infoAsect\the\qca}
    \else
    \edef\qcaline{&~\,\infoAsect\the\qca}
    \fi
    \expandafter\addto\expandafter\qcaextradata\expandafter{\qcaline}
  \repeat
  }
  %B questions > 8
  \@ifundefined{qBtotal}{
  \def\qcbextradata{}
  }{
  \def\qcbextradata{}
  \loop\ifnum\qca<\qtotal
    \advance \qca 1
    \ifnum\qca=9
    \edef\qcbline{~~\,\infoBsect\the\qca}
    \else
    \edef\qcbline{&~\,\infoBsect\the\qca}
    \fi
    \expandafter\addto\expandafter\qcbextradata\expandafter{\qcbline}
  \repeat
  }
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% setupEnumLabelling macro                    %%
%% gws: Allows users to decide to use alpha or %%
%%      roman counting for question parts.     %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\setupEnumLabelling#1{%
  \ifthenelse{\equal{\@questionpartlabelling}{roman}}{%
    \setlist[enumerate,1]{label={\bf #1\arabic{qcounter}}.\noexpand\label{qpagecheck\the\qcount},ref=#1\arabic{qcounter}}%
    \setlist[enumerate,2]{label=(\roman*),ref=#1\arabic{qcounter}(\roman*)}%
    \setlist[enumerate,3]{label=(\alph*),ref=\theenumii(\alph*)}%
  }{%
    \setlist[enumerate,1]{label={\bf #1\arabic{qcounter}}.\noexpand\label{qpagecheck\the\qcount},ref=#1\arabic{qcounter}}%
    \setlist[enumerate,2]{label=(\alph*),ref=#1\arabic{qcounter}(\alph*)}%
    \setlist[enumerate,3]{label=(\roman*),ref=\theenumii(\roman*)}%
  }%
}%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% QUESTION ENVIRONMENT                                                        %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\questionraw#1{
  \if\instring{t}{#1}% Check for old style Total: 50 args.
    {\textcolor{red}{\bf%
    \large Error reading number of marks for this question!\\You might be using the old  \{Total: N\} style...\\%
    If so, update the marks to \{N\}}} -- Skipping question.%
    \global\skipthisquestion 1\relax%
    \begin{lrbox}{\nullq}%
    \begin{minipage}{\linewidth}%
  \else%
    \if\instring{h}{#1}%  hidden
      \global\skipthisquestion 1\relax%
      \begin{lrbox}{\nullq}%
      \begin{minipage}{\linewidth}%
    \else%Okay, all's good lets go with the question!
      \ifthenelse{\equal{\@newpagequestion}{true} \AND \firstQuestionInSect=0}{\newpage}{}%
      \ifthenelse{\firstQuestionInSect=1}{\global\firstQuestionInSect0\relax}{}%
      \global\skipthisquestion0\relax%
      \global\advance\qcount by 1\relax%
      \ifthenelse{\equal{\sectlet}{A} \or \equal{\sectlet}{I}}{\global\advance\qcountA by 1\relax}{}%
      \ifthenelse{\equal{\sectlet}{B} \or \equal{\sectlet}{II}}{\global\advance\qcountB by 1\relax}{}%
      \ifthenelse{\equal{\@writeonexam}{true}}{%Indent Q for write on margins
        \ifnum\qcount>9%
          \setlength\leftmargini{\dimexpr\f@size pt-10pt+1.35\oleftmargini\relax}%Questions bigger than 10 need more indent
        \else%
          \setlength\leftmargini{\dimexpr\f@size pt-10pt+1.1\oleftmargini\relax}%
        \fi%
      }{\setlength\leftmargini{\oleftmargini}}%Otherwise normal indent
      \global\qsubcount 0\relax% Reset submark count at start of question
      \begin{enumerate}%
      \refstepcounter{qcounter}%
      \global\insidequestion 1\relax%Need to know this globally for foorers.
      \edef\qmarks{#1}%Store for after question mark display.
      \immediate\write\@auxout{\string\expandafter \string\gdef \noexpand\csname q\theqcounter marktotal\string\endcsname{#1}}%Store in .aux for front page
      \item%
    \fi%
  \fi%
}

\newcommand{\question}[2][qlabel]{%
\setquestionmarktext{#1}%
\ifthenelse{\equal{\@variant}{1}}{\questionraw{#2}}{}%
  \@ifnextchar\bgroup{\gobbleargtwo{#2}}{%
    \ifthenelse{\equal{\@variant}{2}}{\questionraw{#2}}{}%
    \ifthenelse{\equal{\@variant}{3}}{\questionraw{#2}}{}%
    \ifthenelse{\equal{\@variant}{4}}{\questionraw{#2}}{}%
  }%
}%

\newcommand{\setquestionmarktext}[1]{%
  \ifthenelse{\NOT \equal{#1}{qlabel}}{%
    \gdef\endquestionmarksmacro{#1}%
  }{%
    \gdef\endquestionmarksmacro{\printmarks{\qmarks}}%
  }%
}

\def\endquestion{%
  \ifthenelse{\skipthisquestion=1}{\end{minipage}\end{lrbox}}{%
    \ifthenelse{\qsubcount=0}{% No subparts
      \par\hfill[\bf\endquestionmarksmacro]\quad~%
    }{%Submarks exist in Q
      \ifthenelse{\qsubcount=\qmarks}{%
        \par\hfill[{\bf Total: \endquestionmarksmacro}]\quad~%
      }{%
        \par\hfill[{\bf Total: \endquestionmarksmacro}]\quad~%
        \par\centering\colorbox{red}{\strut\textcolor{white}{\bf\large ERROR: FOUND \the\qsubcount\ SUBMARKS - EXPECTED \qmarks !}}\\%
        {\Large\bf\textcolor{red}{Fix submarks and recompile exam.}}%
      }%
    }%
    \ifthenelse{\equal{\sectlet}{A} \or \equal{\sectlet}{I}}{\global\advance\qtotalcountA by \qmarks\relax}{
      \global\advance\qtotalcountB by \qmarks\relax%
    }%
    \end{enumerate}%
    \global\insidequestion 0\relax%
  }%
}

\newcommand{\gobbleargtwo}[2]{%
  \ifthenelse{\equal{\@variant}{2}}{\questionraw{#2}}{}%
  \@ifnextchar\bgroup{\gobbleargthree{#1}}{%
    \ifthenelse{\equal{\@variant}{3}}{\questionraw{#2}}{}%
    \ifthenelse{\equal{\@variant}{4}}{\questionraw{#2}}{}%
  }%
}%
\newcommand{\gobbleargthree}[2]{%
  \ifthenelse{\equal{\@variant}{3}}{\questionraw{#2}}{}%
  \@ifnextchar\bgroup{\gobbleargfour{#1}}{%
    \ifthenelse{\equal{\@variant}{4}}{\questionraw{#2}}{}%
  }%
}%
\newcommand{\gobbleargfour}[2]{%
\ifthenelse{\equal{\@variant}{4}}{\questionraw{#2}}{}%
}%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% submarks macros                                 %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\submarks}[1]{%
\ifthenelse{\equal{\@variant}{1}}{\submarksraw{#1}}{}%
  \@ifnextchar\bgroup{\submarksgobbleargtwo{#1}}{%
    \ifthenelse{\equal{\@variant}{2}}{\submarksraw{#1}}{}%
    \ifthenelse{\equal{\@variant}{3}}{\submarksraw{#1}}{}%
    \ifthenelse{\equal{\@variant}{4}}{\submarksraw{#1}}{}%
  }%
}%

\newcommand{\submarksgobbleargtwo}[2]{%
  \ifthenelse{\equal{\@variant}{2}}{\submarksraw{#2}}{}%
  \@ifnextchar\bgroup{\submarksgobbleargthree{#1}}{%
    \ifthenelse{\equal{\@variant}{3}}{\submarksraw{#2}}{}%
    \ifthenelse{\equal{\@variant}{4}}{\submarksraw{#2}}{}%
  }%
}%
\newcommand{\submarksgobbleargthree}[2]{%
  \ifthenelse{\equal{\@variant}{3}}{\submarksraw{#2}}{}%
  \@ifnextchar\bgroup{\submarksgobbleargfour{#1}}{%
    \ifthenelse{\equal{\@variant}{4}}{\submarksraw{#2}}{}%
  }%
}%
\newcommand{\submarksgobbleargfour}[2]{%
\ifthenelse{\equal{\@variant}{4}}{\submarksraw{#2}}{}%
}%

\newcommand{\submarksraw}[1]{%
  \ifthenelse{\skipthisquestion=1}{}{%
    \global\advance\qsubcount by #1\relax%
    \par\hfill[{\bf\printmarks{#1}}]\quad~%
  }
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Header and footer setup commands                %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\printfooter{\ifthenelse{\insidequestion=1 \AND\NOT \value{page}<\pageref{qpagecheck\the\qcount}}{
\itshape\ Question \ifthenelse{\equal{\sectlet}{ }}{}{\sectlet}\theqcounter\ continued on next page}{}}%

\newcommand\printheader{
  \ifthenelse{\equal{\@writeonexam}{true}}{{\marginline}\textcolor{MyGray}{Please leave margins blank}%
  \raisebox{-2em}[0pt][0pt]{\hspace*{-5cm}\printAnswerContinued}%
  }{
  \raisebox{-2em}[0pt][0pt]{\printAnswerContinued}%
  }%
}%

\newcommand\setheadfootexam{
\lhead{{}\noexpand\noexpand\printheader}
\rhead{{\bfseries
\ifthenelse{\equal{\@variant}{1}}{\modulenoone}{}%
\ifthenelse{\equal{\@variant}{2}}{\modulenotwo}{}%
\ifthenelse{\equal{\@variant}{3}}{\modulenothree}{}%
\ifthenelse{\equal{\@variant}{4}}{\modulenofour}{}}}
\lfoot{{}\noexpand\noexpand\printfooter}
\cfoot{}
\chead{}
\rfoot{\bfseries Page {\thepage} of \pageref{theend}\newpage}
\pagestyle{fancy}
}
\setheadfootexam
\thispagestyle{empty}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% ngrs: add macro for margins for write-on papers %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\definecolor{MyGray}{rgb}{0.7,0.7,0.7}
\newcommand\marginline{
\setlength{\unitlength}{1.0cm}
\begin{picture}(0,0)
  \put(0,2){\textcolor{MyGray}{\line(0,-1){40}}}
\end{picture}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Set current section A or B.                     %%
%%        Reset the header if the exam is write-on %%
%%        Section A only.                          %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\sect[2][]{\edef\sectlet{#2} \newpage\begin{center} {\bf SECTION \sectlet} \end{center}
  \setupEnumLabelling{#2#1}
  \global\firstQuestionInSect=1
  \ifthenelse{\equal{#2}{A} \or \equal{#2}{I}}{
    \def\sectletA{#2}%
  }{%
    \def\sectletB{#2}%
    \ifthenelse{\equal{\@writeonsecaonly}{true}}{%
      \setwriteonexam{false}%
      \fancyhead[L]{}%
    }{}%
  }%
}%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Macro to enable a write on exam                 %%
%% USAGE:                                          %%
%% \writeonexam{...} (Default: false)              %%
%%  true      - write on section A and B           %%
%%  secaonly  - write on section A only            %%
%%  false     - not a write on exam                %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\writeonexam}[1]{
  \ifthenelse{\equal{#1}{true}}{
      \setwriteonexam{true}
      \setwriteonsecaonly{false}
  }{
      \ifthenelse{\equal{#1}{secaonly}}{
          \setwriteonexam{true}
          \setwriteonsecaonly{true}
          \showIDcollection{true}
      }{
          \setwriteonexam{false}
      }
  }
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% THE END and rough pages macros                  %%
%% USAGE:                                          %%
%% \roughpages{5} set number of pages (default: 0) %%
%% \theend End exam & print rough pages (if any)   %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcounter{roughpagecounter}
\newcommand{\printrp}[2]{
\setcounter{roughpagecounter}{0}
\whiledo{\value{roughpagecounter}<#1}{#2\stepcounter{roughpagecounter}}
}

\newcommand{\theend}[1][print]{%
\checkTotalMarks{\the\qtotalcountA}{\the\qtotalcountB}{\ifnum\the\qcountB=0 \arabic{qcounter}\else\the\qcountB\fi}%
\write\@auxout{\gdef \noexpand\qtotal {\arabic{qcounter}}}%
\write\@auxout{\gdef \noexpand\qAtotal {\the\qcountA}}%
\write\@auxout{\gdef \noexpand\qBtotal {\the\qcountB}}%
\write\@auxout{\gdef \noexpand\qmarkstotalA {\the\qtotalcountA}}%
\write\@auxout{\gdef \noexpand\qmarkstotalB {\the\qtotalcountB}}%
\write\@auxout{\gdef \noexpand\sectletA {\sectletA}}%
\write\@auxout{\gdef \noexpand\sectletB {\sectletB}}%
{\bfseries%
  {\centering{\vbox{THE END\par}}}\label{theend}%
  \ifthenelse{\equal{\@showmarkboxes}{true}}{%
    \vfill%
    \vbox{
      \buildmarkbox%
      {\small For use by the marker only:}\\[0.25\baselineskip]
      \begin{tabular}{|\qctdata}
      \hline
      \qcadata\qcbdata\\[1.75ex]
      \qcsdata\\
      \hline
      \end{tabular}
      \hfill\begin{tabular}{|c|}
      \hline
      Total\\[1.75ex]
      {\qquad\quad/\@settotalmarks}\\
      \hline
      \end{tabular}
      \@ifundefined{qtotal}{}{%
        \ifnum\qtotal>9
          \begin{tabular}{|\qctextradata}
          \hline
          \qcaextradata\qcbextradata\\[1.75ex]
          \qcsextradata\\
          \hline
          \end{tabular}
        \fi
      }%
    }
  }{}%
}%
\ifthenelse{\equal{#1}{print} \AND \equal{\@solutionshow}{hidden} \OR \equal{\@forceroughpages}{true}}{%
  \printroughpages%
}{}%
}

\newcommand{\printroughpages}{
  \newpage \fancyhead[L]{} \chead{\bfseries {\color{black} \@roughpagetext}} \rhead{}
  \rfoot{} \printrp{\@roughpages}{\vspace*{1cm}\newpage}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Formula sheet environment                       %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newenvironment{formulasheet}{
\newpage
\fancyhead[L]{}
\chead{\bfseries Formula Sheet}
\rhead{}
\rfoot{}
}{
\newpage
\setheadfootexam
\ifthenelse{\equal{\@writeonexam}{true}}{\fancyhead[L]{{\marginline}\textcolor{MyGray}{Please leave margins blank}}}{}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Solution macro                             %%
%% USAGE:                                     %%
%% \solutionshow{full, hidden or none}        %%
%%   full: show full solutions                %%
%%   hidden: Show only "Answer:" and create   %%
%%           blank space with size of the     %%
%%           solution size                    %%
%%   none: show nothing at all                %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcount\answerCount
\newcount\answerContinued
\answerCount 0\relax
\newcounter{pc}
\newcount\totalheight
\newcount\boxheight
\def\solution{\@ifstar\solutionrawstar\solutionraw}

\newcommand{\insertBlankBoxes}[1]{
  \boxheight=\dimexpr5pt\relax
  \totalheight=\dimexpr#1\relax
  \divide\totalheight by\boxheight\relax%totalheight is now number of boxes
  \setcounter{pc}{0}%
  {\baselineskip=0pt\relax\lineskip=0pt\relax\lineskiplimit=1000pt\relax\large
  \whiledo{\value{pc}<\totalheight}{\hbox{\raisebox{0pt}[0pt][5pt]{}}\stepcounter{pc}}%
  }
}

\newcommand{\solutionrawstar}[2][none]{%
  \ifthenelse{\skipthisquestion=1}{}{%
    \global\advance\answerCount 1\relax%
    \global\answerContinued 0\relax%
    \ifthenelse{\equal{\@solutionshow}{full}}{%
      \ifthenelse{\insidequestion=1}{}{\begin{enumerate} \item[] }%
      \vskip0.5em\answerStart{\solutionsize\color{\@solutioncolor}%
      #2 \color{black}}%
      \answerEnd%
      \ifthenelse{\insidequestion=1}{}{\end{enumerate}}%
    }{%
      \ifthenelse{\equal{\@solutionshow}{hidden} \AND \equal{\@writeonexam}{true}}{%
        \ifthenelse{\insidequestion=1}{}{\begin{enumerate} \item[] }%
        \vskip0.5em\answerStart{\solutionsize%
        \ifthenelse{\equal{#1}{none}}{\hideit{2}{#2}}{\insertBlankBoxes{#1}}}%
        \answerEnd%
        \ifthenelse{\insidequestion=1}{}{\end{enumerate}}%
      }{}%
    }%
  }%
}%

\newcommand{\solutionraw}[2][none]{%
  \ifthenelse{\skipthisquestion=1}{}{%
    \global\advance\answerCount 1\relax%
    \global\answerContinued 0\relax%
    \ifthenelse{\equal{\@solutionshow}{full}}{%
      \ifthenelse{\insidequestion=1}{}{\begin{enumerate} \item[] }%
      \vskip0.5em{\bf Answer:}\answerStart\vskip0.5em{\solutionsize\color{\@solutioncolor}%
       #2 \color{black}}%
      \answerEnd%
      \ifthenelse{\insidequestion=1}{}{\end{enumerate}}%
    }{%
      \ifthenelse{\equal{\@solutionshow}{hidden} \AND \equal{\@writeonexam}{true}}{%
        \ifthenelse{\insidequestion=1}{}{\begin{enumerate} \item[] }%
        \vskip0.5em{\bf Answer:}\answerStart\vskip0.5em{\solutionsize%
        \ifthenelse{\equal{#1}{none}}{\hideit{2}{#2}}{\insertBlankBoxes{#1}}}%
        \answerEnd%
        \ifthenelse{\insidequestion=1}{}{\end{enumerate}}%
      }{}%
    }%
  }%
}%
\newcommand{\customlabel}[1]{%
  \label{#1}
  \ifthenelse{\equal{\sectlet}{ }}{
    \immediate\write\@auxout{\string\expandafter \string\gdef \noexpand\csname #1bk\string\endcsname{\theqcounter}}
  }{
    \immediate\write\@auxout{\string\expandafter \string\gdef \noexpand\csname #1bk\string\endcsname{{\sectlet}\theqcounter}}
  }
}
\def\answerStart{\customlabel{pcount\the\answerCount start}}
\def\answerEnd{\customlabel{pcount\the\answerCount end}}
\newcounter{qcac}
\def\printAnswerContinued{
  \ifthenelse{\equal{\@showanswercontinued}{true}}{%
    \setcounter{qcac}{0}%
    \whiledo{\value{qcac}<\answerCount}{%
      \stepcounter{qcac}%
      \ifthenelse{\thepage > \pageref{pcount\theqcac start} \AND\NOT \thepage > \pageref{pcount\theqcac end}}{%
        \ifthenelse{\equal{\ref{pcount\theqcac start}}{}}{
        \itshape\ Answer to question \csname pcount\theqcac startbk\endcsname\ continued%
        }{%
        \itshape\ Answer to question \ref{pcount\theqcac start}\ continued%
        }%
      }{}%
    }%
  }{}%
}%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Answergap macro                           %%
%% Alternative to typesetting solutions:     %%
%% USAGE:                                    %%
%% \answergap{4cm}                           %%
%%   create a gap of 4cm.The gap will honour %%
%%   \solutionshow's none option             %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\answergap{\@ifstar\answergaprawstar\answergapraw}
\newcommand{\answergapraw}[1]{%
\solution[#1]{~}
}
\newcommand{\answergaprawstar}[1]{%
\solution*[#1]{~}
}

\newcommand{\comment}[1]{%
  \ifthenelse{\equal{\@solutionshow}{full}}{%
    \ifthenelse{\skipthisquestion=1}{}{%
      {\color{\@commentcolor}#1}
    }%
  }{}%
}%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Error message macro                                   %
%% Show an error message when total exam marks is wrong. %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\checkTotalMarks}[3]{%
\newcount\qtotalcount
\newcount\qtotalcounttmp
\newcount\qtotalcountN
\newcount\qtotalcountM
\newcount\qtotalset
\ifthenelse{\equal{\@attempt}{??} \OR \equal{\@attempt}{1}}{%
  \qtotalcount #1\relax
  \advance \qtotalcount by #2\relax
  \ifthenelse{\qtotalcount=\@settotalmarks}{}{%
    \colorbox{red}{\strut\textcolor{white}{[\bf \questionsize WARNING: EXAM HAS \the\qtotalcount\ MARKS. EXPECTED \@settotalmarks\ MARKS!]}}\\[0.5em]%
    \textcolor{red}{\bf\centering Fix total number of marks and recompile exam twice.}%
  }%
}{%
  \qtotalcount #1\relax
  \multiply\qtotalcount by 1000\relax
  \qtotalset \@settotalmarks\relax
  \multiply\qtotalset by 1000\relax
  \qtotalcounttmp #2\relax
  \multiply\qtotalcounttmp by 1000\relax
  \qtotalcountN \@attempt\relax
  \ifthenelse{\equal{#3}{0}}{\colorbox{red}{\strut\textcolor{white}{[\bf \questionsize WARNING: EXAM HAS 0 MARKS. EXPECTED \@settotalmarks\ MARKS!]}}\\[0.5em]}{%
    \ifthenelse{\equal{#3}{??}}{\textcolor{red}{\bf WARNING: EXAM HAS ?? MARKS. Recompile exam...}}{%
      \qtotalcountM #3\relax
      \multiply\qtotalcounttmp by \qtotalcountN\relax
      \divide\qtotalcounttmp by \qtotalcountM\relax
      \advance \qtotalcount by \qtotalcounttmp\relax
      \ifthenelse{\qtotalcount>\qtotalset}{%
        \colorbox{red}{\strut\textcolor{white}{[\bf \questionsize EXAM HAS TOO MANY MARKS. EXPECTED \@settotalmarks\ MARKS!]}}\\[0.5em]%
        \textcolor{red}{\bf\centering Fix total number of marks and recompile exam twice.\\ This is an ``attempt N of M'' type exam; also check that your optional questions are all worth the same number of marks.\\}%
      }{}%
      \ifthenelse{\qtotalcount<\qtotalset}{%
        \colorbox{red}{\strut\textcolor{white}{[\bf \questionsize EXAM HAS TOO FEW MARKS. EXPECTED \@settotalmarks\ MARKS!]}}\\[0.5em]%
        \textcolor{red}{\bf\centering Fix total number of marks and recompile exam twice.\\ This is an ``attempt N of M'' type exam; also check that your optional questions are all worth the same number of marks.\\}%
      }{}%
    }%
  }%
}%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%    Rubrics
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Rubric A  %% For stage 1 papers, stage 4 papers w/ no choice

\def\makerubric{

{\if A\rubric
\hyphenation{indicated University relative}
{\em Candidates should attempt all questions. 
Marks for each question are indicated. However you are advised that
marks may be adjusted in accordance with the University's
Moderation and Scaling Policy.

There are {\getNumQ{qtotal}} questions and {\@settotalmarks} marks on this exam.

\ifthenelse{\equal{\@writeonexam}{true}}{Answers to questions should be entered
directly on this question paper in the spaces provided.
\ifthenelse{\equal{\@insertblankpages}{true} \AND \@roughpages>0}{%
Rough work should be done on the blank sides of pages or on the blank 
pages at the end of the paper. You must clearly indicate any rough work 
that you wish to be marked.
}{%
  \ifthenelse{\equal{\@insertblankpages}{true}}{%
    Rough work should be done on the blank sides of pages. You must 
    clearly indicate any rough work that you wish to be marked.
  }{%
    \ifthenelse{\@roughpages>0}{%
      Rough work should be done on the blank pages at the end of the 
      paper. You must clearly indicate any rough work that you wish 
      to be marked.
    }{{\bf\color{red}Error: On a write on exam you must either add 
    some rough pages with \texttt{\textbackslash roughpages\{N\}} or turn on 
    blank pages with \texttt{\textbackslash insertblankpages\{true\}}}}%
  }%
}%
This question paper must be handed in,
attached inside an anonymised cover sheet,
at the end of the examination.}{}

{\ifthenelse{\equal{\@extrainstructions}{}}{}{\@extrainstructions\par}}
{\ifthenelse{\equal{\@calculatorpermitted}{true}}{Calculators may be used.}{}}%
{\ifthenelse{\equal{\@calculatorpermitted}{false}}{Calculators may not be used.}{}}%
}
\fi}

%% Rubric B  %% For stage 2-3 papers, and stage 4 variants

{\if B\rubric
\hyphenation{indicated University relative}
{\em Candidates should attempt all questions. 
Marks for each question are indicated. However you are advised that
marks may be adjusted in accordance with the University's
Moderation and Scaling Policy.

There are {\getNumQ{qAtotal}} questions in Section {\sectletA}  
and {\getNumQ{qBtotal}} questions in Section {\sectletB}.

There are {\@settotalmarks} marks on this exam, {\getNumMarks{A}} marks
for Section {\sectletA} and {\getNumMarks{B}} marks for Section {\sectletB}.

\ifthenelse{\equal{\@writeonexam}{true}}{%
\ifthenelse{\equal{\@writeonsecaonly}{true}}{%
Answers to questions in Section {\sectletA} 
}{%
Answers to questions
}%
should be entered directly on this question paper in the spaces provided.
\ifthenelse{\equal{\@insertblankpages}{true} \AND \@roughpages>0}{%
Rough work should be done on the blank sides of pages or on the blank 
pages at the end of the paper. You must clearly indicate any rough work 
that you wish to be marked.
}{%
  \ifthenelse{\equal{\@insertblankpages}{true}}{%
    Rough work should be done on the blank sides of pages. You must 
    clearly indicate any rough work that you wish to be marked.
  }{%
    \ifthenelse{\@roughpages>0}{%
      Rough work should be done on the blank pages at the end of the 
      paper. You must clearly indicate any rough work that you wish 
      to be marked.
    }{{\bf\color{red}Error: On a write on exam you must either add 
    some rough pages with \texttt{\textbackslash roughpages\{N\}} or turn on 
    blank pages with \texttt{\textbackslash insertblankpages\{true\}}}}%
  }%
}%
This question paper must be handed in,
\ifthenelse{\equal{\@writeonsecaonly}{true}}
{attached inside the anonymised answer book used for Section {\sectletB},}
{attached inside an anonymised cover sheet,}
at the end of the examination.}{}

{\ifthenelse{\equal{\@extrainstructions}{}}{}{\@extrainstructions\par}}
{\ifthenelse{\equal{\@calculatorpermitted}{true}}{Calculators may be used.}{}}%
{\ifthenelse{\equal{\@calculatorpermitted}{false}}{Calculators may not be used.}{}}%
}
\fi}

%% Rubric D  %% For stage 4 papers w/ choice

{\if D\rubric
{\em There are {\getNumQ{qtotal}} questions on this paper. 
Credit will be given for the best {\getNumQ{@attempt}} answers.
No credit will be given for other answers and students 
are strongly advised not to spend time producing answers 
for which they will receive no credit.

Marks for each question are indicated. However you are advised that
marks may be adjusted in accordance with the University's
Moderation and Scaling Policy.

There are {\@settotalmarks} marks on this exam.

{\ifthenelse{\equal{\@extrainstructions}{}}{}{\@extrainstructions\par}}
{\ifthenelse{\equal{\@calculatorpermitted}{true}}{Calculators may be used.}{}}%
{\ifthenelse{\equal{\@calculatorpermitted}{false}}{Calculators may not be used.}{}}%
}
\fi}

%% Rubric E  %% For MAS8114 only
%% 

{\if E\rubric
\hyphenation{indicated University relative}
{\em Credit will be given for ALL answers to questions in Section {\sectletA}, 
and for the BEST answer to a question in Section {\sectletB}.
No credit will be given for other answers and students are 
strongly advised not to spend time producing answers for 
which they will receive no credit.

Marks for each question are indicated. However you are advised that
marks may be adjusted in accordance with the University's
Moderation and Scaling Policy.

There are {\getNumQ{qAtotal}} questions in Section {\sectletA}  
and {\getNumQ{qBtotal}} questions in Section {\sectletB}.

There are {\@settotalmarks} marks on this exam, {\getNumMarks{A}} marks
for Section {\sectletA} and {\getNumMarks{B}} marks for Section {\sectletB}.

{\ifthenelse{\equal{\@extrainstructions}{}}{}{\@extrainstructions\par}}
{\ifthenelse{\equal{\@calculatorpermitted}{true}}{Calculators may be used.}{}}%
{\ifthenelse{\equal{\@calculatorpermitted}{false}}{Calculators may not be used.}{}}%
}
\fi}

%% Rubric F  %% Answer n of m in section B.
{\if F\rubric
{\em Credit will be given for all answers to questions 
in Section A, and for the best {\getNumQ{@attempt}} answers to questions
in Section B. No credit will be given for other answers and students are 
strongly advised not to spend time producing answers for 
which they will receive no credit. 

Marks for each question are indicated. However you are advised that
marks may be adjusted in accordance with the University's
Moderation and Scaling Policy.

There are {\getNumQ{qAtotal}} questions in Section {\sectletA}  
and {\getNumQ{qBtotal}} questions in Section {\sectletB}.

There are {\@settotalmarks} marks on this exam, {\getNumMarks{A}} marks
for Section {\sectletA} and {\getNumMarks{B}} marks for Section {\sectletB}.

{\ifthenelse{\equal{\@extrainstructions}{}}{}{\@extrainstructions\par}}
{\ifthenelse{\equal{\@calculatorpermitted}{true}}{Calculators may be used.}{}}%
{\ifthenelse{\equal{\@calculatorpermitted}{false}}{Calculators may not be used.}{}}%
}
\fi}

%% Rubric N %% For Eng Maths. Simple rubric provided by David Swailes.
{\if N\rubric
{\em\ \\Answer all questions. Show all working.

The General Formula Sheet is available.

The total marks on this paper is \ifthenelse{\equal{\@rubricNtotalmarks}{false}}{\@settotalmarks}{\@rubricNtotalmarks}.

{\ifthenelse{\equal{\@extrainstructions}{}}{}{\@extrainstructions\par}}
{\ifthenelse{\equal{\@calculatorpermitted}{true}}{Calculators may be used.}{}}%
{\ifthenelse{\equal{\@calculatorpermitted}{false}}{Calculators may not be used.}{}}%
}
\fi}


%% Rubric M %% %gws: Add for mid semester tests.
{\if M\rubric
{\em Credit will be given for all answers.
Marks for each question are indicated. However you are advised that
marks may be adjusted in accordance with the University's
Moderation and Scaling Policy.

\ifthenelse{\equal{\getNumQ{qAtotal}}{ZERO}}{
  There are {\getNumQ{qtotal}} questions and {\@settotalmarks} marks on this exam.
}{%
  \ifthenelse{\equal{\getNumQ{qBtotal}}{ZERO}}{
    There are {\getNumQ{qAtotal}} questions in Section {\sectletA}.
    
    There are {\@settotalmarks} marks on this exam.
    }{%
    There are {\getNumQ{qAtotal}} questions in Section {\sectletA} and %
    {\getNumQ{qBtotal}} questions in Section {\sectletB}.
    
    There are {\@settotalmarks} marks on this exam, {\getNumMarks{A}} marks
    for Section {\sectletA} and {\getNumMarks{B}} marks for Section {\sectletB}.
  }%
}%

\ifthenelse{\equal{\@writeonexam}{true}}{Answers to questions should be entered
directly on this question paper in the spaces provided.
\ifthenelse{\equal{\@insertblankpages}{true} \OR \@roughpages>0}{%
Blank pages have been provided for rough work. You must clearly indicate any rough work 
that you wish to be marked.}{}%
}{}%

{\ifthenelse{\equal{\@extrainstructions}{}}{}{\@extrainstructions\par}}
{\ifthenelse{\equal{\@calculatorpermitted}{true}}{Calculators may be used.}{}}%
{\ifthenelse{\equal{\@calculatorpermitted}{false}}{Calculators may not be used.}{}}%
}
\fi}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%    Utility Macros
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\printmarks}[1]{%
\ifthenelse{\equal{#1}{1}}{1 mark}{#1 marks}%
}
\newcommand{\getNumMarks}[1]{%
\@ifundefined{qmarkstotal#1}{??}{\csname qmarkstotal#1\endcsname}%
}
\newcommand{\getNumQ}[1]{%
    \if{\@ifundefined{#1}{??}{\csname #1\endcsname}}{0}=0%
        ZERO%
    \else\ifnum\pdf@strcmp{\@ifundefined{#1}{??}{\csname #1\endcsname}}{1}=0%
        ONE%%
    \else\ifnum\pdf@strcmp{\@ifundefined{#1}{??}{\csname #1\endcsname}}{2}=0%
        TWO%%
    \else\ifnum\pdf@strcmp{\@ifundefined{#1}{??}{\csname #1\endcsname}}{3}=0%
        THREE%%
    \else\ifnum\pdf@strcmp{\@ifundefined{#1}{??}{\csname #1\endcsname}}{4}=0%
        FOUR%%
    \else\ifnum\pdf@strcmp{\@ifundefined{#1}{??}{\csname #1\endcsname}}{5}=0%
        FIVE%%
    \else\ifnum\pdf@strcmp{\@ifundefined{#1}{??}{\csname #1\endcsname}}{6}=0%
        SIX%%
    \else\ifnum\pdf@strcmp{\@ifundefined{#1}{??}{\csname #1\endcsname}}{7}=0%
        SEVEN%%
    \else\ifnum\pdf@strcmp{\@ifundefined{#1}{??}{\csname #1\endcsname}}{8}=0%
        EIGHT%%
    \else\ifnum\pdf@strcmp{\@ifundefined{#1}{??}{\csname #1\endcsname}}{9}=0%
        NINE%%
    \else\ifnum\pdf@strcmp{\@ifundefined{#1}{??}{\csname #1\endcsname}}{10}=0%
        TEN%%
    \else\ifnum\pdf@strcmp{\@ifundefined{#1}{??}{\csname #1\endcsname}}{11}=0%
        ELEVEN%%
    \else\ifnum\pdf@strcmp{\@ifundefined{#1}{??}{\csname #1\endcsname}}{12}=0%
        TWELVE%%
    \else\ifnum\pdf@strcmp{\@ifundefined{#1}{??}{\csname #1\endcsname}}{13}=0%
        THIRTEEN%%
    \else\ifnum\pdf@strcmp{\@ifundefined{#1}{??}{\csname #1\endcsname}}{14}=0%
        FOURTEEN%%
    \else\ifnum\pdf@strcmp{\@ifundefined{#1}{??}{\csname #1\endcsname}}{15}=0%
        FIFTEEN%%
    \else\ifnum\pdf@strcmp{\@ifundefined{#1}{??}{\csname #1\endcsname}}{16}=0%
        SIXTEEN%%
    \else\ifnum\pdf@strcmp{\@ifundefined{#1}{??}{\csname #1\endcsname}}{17}=0%
        SEVENTEEN%%
    \else\ifnum\pdf@strcmp{\@ifundefined{#1}{??}{\csname #1\endcsname}}{18}=0%
        EIGHTEEN%%
    \else\ifnum\pdf@strcmp{\@ifundefined{#1}{??}{\csname #1\endcsname}}{19}=0%
        NINETEEN%%
    \else\ifnum\pdf@strcmp{\@ifundefined{#1}{??}{\csname #1\endcsname}}{20}=0%
        TWENTY%%
    \else\ifnum\pdf@strcmp{\@ifundefined{#1}{??}{\csname #1\endcsname}}{21}=0%
        TWENTY ONE%%
    \else\ifnum\pdf@strcmp{\@ifundefined{#1}{??}{\csname #1\endcsname}}{22}=0%
        TWENTY TWO%%
    \else\ifnum\pdf@strcmp{\@ifundefined{#1}{??}{\csname #1\endcsname}}{23}=0%
        TWENTY THREE%%
    \else\ifnum\pdf@strcmp{\@ifundefined{#1}{??}{\csname #1\endcsname}}{24}=0%
        TWENTY FOUR%%
    \else\ifnum\pdf@strcmp{\@ifundefined{#1}{??}{\csname #1\endcsname}}{25}=0%
        TWENTY FIVE%%
    \else\ifnum\pdf@strcmp{\@ifundefined{#1}{??}{\csname #1\endcsname}}{26}=0%
        TWENTY SIX%%
    \else\ifnum\pdf@strcmp{\@ifundefined{#1}{??}{\csname #1\endcsname}}{27}=0%
        TWENTY SEVEN%%
    \else\ifnum\pdf@strcmp{\@ifundefined{#1}{??}{\csname #1\endcsname}}{28}=0%
        TWENTY EIGHT%%
    \else\ifnum\pdf@strcmp{\@ifundefined{#1}{??}{\csname #1\endcsname}}{29}=0%
        TWENTY NINE%%
    \else\ifnum\pdf@strcmp{\@ifundefined{#1}{??}{\csname #1\endcsname}}{30}=0%
        THIRTY%
    \else%
        \@ifundefined{#1}{??}{ZERO}%
    \fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi %
}
\makeatletter
\def\instring#1#2{TT\fi\begingroup
  \edef\x{\endgroup\noexpand\in@{#1}{#2}}\x\ifin@}
\makeatother
