\def\classdate{16 July 2025}
\def\classversion{1.1}
\def\classshortdate{2025/07/16}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Document class for lecture slide notes.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Version History
% v1.0    Initial version, rebuilt from deprecated lecturenotes class.
% v1.0.1  Corrected page number generation.
% v1.0.2  Normalised package info messages.
% v1.1    Switched to built-in LaTeX page hooks.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\NeedsTeXFormat{LaTeX2e}[2001/06/01]
\ProvidesClass{otagonotes}%
         [\classdate\space v\classversion\space Otago lecture speaker notes]


\DeclareOption*{\PassOptionsToClass{\CurrentOption}{otagoslides}}
\ProcessOptions
\LoadClass{otagoslides}


% Load required packages.
\RequirePackage{array}
\RequirePackage{pgfpages}


\if@chapters
    \SetClassType{Chapter}
\fi


% hyperref setup. We have to use a slightly convoluted method, because the
% macros referenced here haven't been set yet. Define the hyperref setup
% as a new macro, and call this AFTER initialising the paper and chapter
% variables.
\renewcommand{\inithyperref}{%
	\hypersetup{%
		pdftitle={\PaperCode\ \ClassType\ \thechapter\ Speaker Notes},%
		pdfsubject={\ShortClassTitle},%
		colorlinks=false%
	}%
}


% Set up notes 6-up page layout using pgfpages.
% This only looks good in portrait, but then it's not intended for
% any other layout anyway...
\pgfpagesdeclarelayout{notes 6up}{%
    \edef\pgfpageoptionheight{\the\paperwidth} % landscaped by default
    \edef\pgfpageoptionwidth{\the\paperheight}%
    \def\pgfpageoptionborder{0pt}%
}{%
    \pgfpagesphysicalpageoptions{%
        logical pages=6,%
        physical height=\pgfpageoptionheight,%
        physical width=\pgfpageoptionwidth,%
    }%
    \pgfpageslogicalpageoptions{1}
    {%
        border shrink=\pgfpageoptionborder,%
        resized width=.5\pgfphysicalwidth,%
        resized height=.25\pgfphysicalheight,%
        center=\pgfpoint{.26\pgfphysicalwidth}{.76\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{2}
    {%
        border shrink=\pgfpageoptionborder,%
        resized width=.5\pgfphysicalwidth,%
        resized height=.25\pgfphysicalheight,%
        center=\pgfpoint{.74\pgfphysicalwidth}{.76\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{3}
    {%
        border shrink=\pgfpageoptionborder,%
        resized width=.5\pgfphysicalwidth,%
        resized height=.25\pgfphysicalheight,%
        center=\pgfpoint{.26\pgfphysicalwidth}{.5\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{4}
    {%
        border shrink=\pgfpageoptionborder,%
        resized width=.5\pgfphysicalwidth,%
        resized height=.25\pgfphysicalheight,%
        center=\pgfpoint{.74\pgfphysicalwidth}{.5\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{5}
    {%
        border shrink=\pgfpageoptionborder,%
        resized width=.5\pgfphysicalwidth,%
        resized height=.25\pgfphysicalheight,%
        center=\pgfpoint{.26\pgfphysicalwidth}{.24\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{6}
    {%
        border shrink=\pgfpageoptionborder,%
        resized width=.5\pgfphysicalwidth,%
        resized height=.25\pgfphysicalheight,%
        center=\pgfpoint{.74\pgfphysicalwidth}{.24\pgfphysicalheight}%
    }%
}

\pgfpagesuselayout{notes 6up}[a4paper]


% Set transparent slide background colour.
\setbeamercolor{background canvas}{bg=}


% Set up "headers" and "footers". Because pgfpages converts the original
% pages to logical pages, we can't use normal techniques like fancyhdr,
% because that will get applied to the logical pages, not the physical
% ones. We therefore use a slightly bizarre hack with the background
% package and a tabular to dangle the "header" and "footer" off the
% first logical page.
% (Adapted from <https://tex.stackexchange.com/a/83270>.)
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\newlength{\headerwidth}
% Determined empirically for A4 paper.
\setlength{\headerwidth}{0.85\pgfphysicalwidth+1mm}


\makeatletter
\AddToHook{shipout/background}{
    % Index off the first logical page, because it's the only one
    % we can guarantee to exist on any given page.
    \begin{tikzpicture}[remember picture, overlay]
        \ifnum\pgf@currentshipout=1%
            % Scale factor of 1.414 looks about right, and seems logical.
            \node[scale=1.414, font=\sffamily\bfseries\Huge]
                % Offsets from current page.north east determined empirically.
                % at ($(current page.north) + (\paperwidth+7.2mm,0.2175\paperheight)$) {
                at ($(current page.north east) + (1.155mm,-0.8015\paperheight)$) {
                \PaperCode, \TeachingPeriod\ \YearOffered
            };
            \node[scale=1.414, font=\sffamily\bfseries\large]
                at ($(current page.north east) + (1.2mm,-4.3775\paperheight)$) {
                \begin{tabular*}{\headerwidth}{@{}L{2\headerwidth/3}@{}R{\headerwidth/3}@{}}%
                    \ClassType\ \thechapter: \ShortClassTitle\ --- Notes & \the\numexpr(\thepage-1)/6+1 \\%
                \end{tabular*}%
            };
        \fi%
    \end{tikzpicture}
}
\makeatother


\endinput
