%% yorkthesis.cls
%% Copyright 2020 A. Pezzoni
%% Copyright 2022 A. Grau
%
% This work may be distributed and/or modified under the conditions of the LaTeX
% Project Public License, either version 1.3 of this license or (at your option)
% any later version, with the exception that distribution of Derived Work is not
% subject to the requirements of section 6.2.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX version
% 2005/12/01 or later.
%
% Current version: v1.0
% Changelog:
% 2020/09/29: Upload of the initial version by A. Pezzoni on
%				https://github.com/donbex/yorkthesis
% 2022/07/07: Recuperation by A. Grau for distribution to the York maths dpt
% 2022/07/08: Add possibility of subtitle, and create helper commands to set margins
%
% This work has the LPPL maintenance status `maintained`.
%
% The Current Maintainer of this work is A. Grau.
%
% This work consists of all the files listed in the README.md,
% and provides a copy of the original hosted on
% https://git.tigrau.ovh/Ambroise/LaTeX-Templates/York-Thesis.


% Comments starting with FORMAT highlight requirements from the UoY thesis
% regulations, as of July 2022.
% See https://www.york.ac.uk/research/graduate-school/academic/thesis/format/#tab-2

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{yorkthesis}[2022/07/08 v1.0 UoY Maths Thesis]


%%% Set class options

\DeclareOption*{%
  \PassOptionsToClass{\CurrentOption}{memoir}
}

\PassOptionsToClass{twoside}{memoir}

% FORMAT: Avoid blank pages.
\PassOptionsToClass{openany}{memoir}

% FORMAT: Font size must be at least 11pt.
%         The document must be on A4 paper.
\PassOptionsToClass{12pt,a4paper}{memoir}

\ProcessOptions
\LoadClass{memoir}


%%% Formatting options

% FORMAT: Use 1.5 line spacing.
\OnehalfSpacing

% FORMAT: GRS states as a recommendation that: 
%         spine margin should be at least 40mm,
%         other margins should be at least 15mm.
% In effect, their view is the following:
% "We would recommend that they stick with the guidance re. margin on binding edge merely because we still get examiners who prefer to make themselves a physical copy to mark up, rather than using the digital annotation."
% I have taken the stand to set the default value to 3cm on both sizes horizontally, 
% while putting 4cm vertically. The implementation below prevents the use of setting
% the blocksize and then letting the margins be defined by a value relatively to that 
% blocksize. If you want to use these instead, you need to delete the macros below.
% DOC: Check memoir package sec2.4.2 p.17-21
\newcommand{\horizontalmarginsvalue}[2][*]{\def\margin@spine{#2} \def\margin@foredge{#1}}
\newcommand{\horizontalmarginsratio}[2][*]{\def\margin@spine{#2} \def\margin@hratio{#1}}
\newcommand{\verticalmarginsvalue}[2][*]{\def\margin@up{#2} \def\margin@low{#1}}
\newcommand{\verticalmarginsratio}[2][*]{\def\margin@up{#2} \def\margin@vratio{#1}}

\@ifundefined{margin@spine}{ % none of the horizontal commands have been called
        \setlrmarginsandblock{3cm}{*}{1} % default with 3cm each side
    }{ % if margin@spine is defined, so is one of margin@foredge or margin@hratio
        \@ifundefined{margin@foredge}{ % at least ratio value is defined
            \setlrmarginsandblock{\margin@spine}{*}{\margin@hratio}
        }{ % at least foredge value is defined; hratio value is discarded if defined
            \setlrmarginsandblock{\margin@spine}{\margin@foredge}{*}
        }
    }

\@ifundefined{margin@up}{ % none of the vertical commands have been called
        \setulmarginsandblock{4cm}{*}{1} % default with 3cm each side
    }{ % if margin@up is defined, so is one of margin@low or margin@vratio
        \@ifundefined{margin@low}{ % at least ratio value is defined
            \setulmarginsandblock{\margin@up}{*}{\margin@vratio}
        }{ % at least low value is defined; vratio value is discarded if defined
            \setulmarginsandblock{\margin@up}{\margin@low}{*}
        }
    }
% Call the layout to respect the values set
\checkandfixthelayout

% FORMAT: Table of contents must include all document subdivisions.
%         Do not go below subsubsections.
\settocdepth{subsubsection}
\setsecnumdepth{subsubsection}

% FORMAT: The font size of the headings should be smaller than the font size of
%         the main text.
% NOTE: Hyperref messes up the capitalisation of headings containing a reference
%       label, so we use small-caps headings as a workaround.
%       See https://tex.stackexchange.com/q/562773/27664
\nouppercaseheads
\pagestyle{headings}
\makeevenhead{headings}{\thepage}{}{\small\slshape\scshape\leftmark}
\makeoddhead{headings}{\small\slshape\scshape\rightmark}{}{\thepage}

% FORMAT: A bibliography should contain every item consulted in writing the
%         thesis, while a list of references only contains the items explicitly
%         mentioned in the document.
\renewcommand{\bibname}{References}


%%% Title page

\newcommand{\department}[1]{\def\@department{#1}}
\newcommand{\qualification}[1]{\def\@qualification{#1}}
\newcommand{\submitdate}[1]{\def\@submitdate{#1}}
\newcommand{\subtitle}[1]{\gdef\cfg@subtitle{#1}}

% Empty submitdate defaults to the current month and year.
% Taken from the yorkThesis class by J H Pickering:
% https://www.york.ac.uk/language/current/resources/software-resources/latex/
\def\@submitdate{%
  \ifcase\the\month\or
  January\or February\or March\or April\or May\or June\or
  July\or August\or September\or October\or November\or December\fi
  \space \number\the\year}

\newlength{\droplen}
\newcommand*{\makethesistitle}{\begingroup%
  % FORMAT: The title page should have number 1 (not visible),
  %         hence the starred version of `titlingpage`.
  \begin{titlingpage*}
    % Centre the title with respect to the physical page, not the typeblock
    \calccentering{\unitlength}
    \begin{adjustwidth*}{\unitlength}{-\unitlength}
      \droplen=0.1\textheight
      \vspace*{\droplen}
      \centering
      {\Huge \@title}
      \@ifundefined{cfg@subtitle}{}{\\[\baselineskip]{\large\scshape\cfg@subtitle}}  \\[2\baselineskip]
      {\Large \itshape \@author}\par
      \begin{vplace}[2]  % Split surrounding vertical space with a 2:1 ratio
        {\large \scshape \@qualification}\\[2\baselineskip]
        {\LARGE \scshape University of York}\\[.5\baselineskip]
        {\LARGE \scshape \@department}
      \end{vplace}
      {\large \@submitdate}\par
      \vspace*{\droplen}
    \end{adjustwidth*}
    % FORMAT: The abstract should be on page 2, so we prevent clearing to
    %         the next recto page.
    \titlingpageend{\clearforchapter}{\clearforchapter}
  \end{titlingpage*}
\endgroup}
