\def\classdate{7 September 2025}
\def\classversion{1.2}
\def\classshortdate{2025/09/07}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Document class for lecture handout document.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Version History
% v1.0    Initial version, rebuilt from deprecated lecturehandout class.
% v1.0.1  Tweaked header so it always fits.
% v1.0.2  Corrected page number generation.
% v1.0.3  Normalised package info messages.
% v1.1    Switched to built-in LaTeX page hooks.
% v1.2    Stopped using tabular* for the footer because it broke under 
%         TeX Live 2025.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\NeedsTeXFormat{LaTeX2e}[2001/06/01]
\ProvidesClass{otagohandout}%
         [\classdate\space v\classversion\space Otago lecture handout]


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


% Load required packages.
\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\ Lecture Material},%
		pdfsubject={\ShortClassTitle},%
		colorlinks=false%
	}%
}


% Set up notes 6-up page layout using pgfpages.
% This only looks good in landscape, but then it's not intended for
% any other layout anyway...
%
% If there aren't enough frames to fill the final page, any missing ones
% will appear as empty boxes. Meh.
\pgfpagesdeclarelayout{handout 6up}{%
    \def\pgfpageoptionborder{0pt}%
}{%
    \pgfpagesphysicalpageoptions{%
        logical pages=6,%
        physical height=\pgfpageoptionheight,%
        physical width=\pgfpageoptionwidth,%
    }%
    \pgfpageslogicalpageoptions{1}
    {%
        border code=\pgfsetlinewidth{0.6pt}\pgfstroke,%
        border shrink=\pgfpageoptionborder,%
        resized width=.3\pgfphysicalwidth,%
        resized height=.5\pgfphysicalheight,%
        center=\pgfpoint{.183\pgfphysicalwidth}{.67\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{2}
    {%
        border code=\pgfsetlinewidth{0.6pt}\pgfstroke,%
        border shrink=\pgfpageoptionborder,%
        resized width=.3\pgfphysicalwidth,%
        resized height=.5\pgfphysicalheight,%
        center=\pgfpoint{.5\pgfphysicalwidth}{.67\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{3}
    {%
        border code=\pgfsetlinewidth{0.6pt}\pgfstroke,%
        border shrink=\pgfpageoptionborder,%
        resized width=.3\pgfphysicalwidth,%
        resized height=.5\pgfphysicalheight,%
        center=\pgfpoint{.817\pgfphysicalwidth}{.67\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{4}
    {%
        border code=\pgfsetlinewidth{0.6pt}\pgfstroke,%
        border shrink=\pgfpageoptionborder,%
        resized width=.3\pgfphysicalwidth,%
        resized height=.5\pgfphysicalheight,%
        center=\pgfpoint{.183\pgfphysicalwidth}{.33\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{5}
    {%
        border code=\pgfsetlinewidth{0.6pt}\pgfstroke,%
        border shrink=\pgfpageoptionborder,%
        resized width=.3\pgfphysicalwidth,%
        resized height=.5\pgfphysicalheight,%
        center=\pgfpoint{.5\pgfphysicalwidth}{.33\pgfphysicalheight}%
    }%
    \pgfpageslogicalpageoptions{6}
    {%
        border code=\pgfsetlinewidth{0.6pt}\pgfstroke,%
        border shrink=\pgfpageoptionborder,%
        resized width=.3\pgfphysicalwidth,%
        resized height=.5\pgfphysicalheight,%
        center=\pgfpoint{.817\pgfphysicalwidth}{.33\pgfphysicalheight}%
    }%
}

\pgfpagesuselayout{handout 6up}[a4paper, landscape]


% 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 to dangle the "header"
% and "footer" off the first logical page.
% (Adapted from <https://tex.stackexchange.com/a/83270>.)
\newlength{\headerwidth}
% Three slides @ 0.3\pgfphysicalwidth plus two 5mm gaps.
\setlength{\headerwidth}{0.9\pgfphysicalwidth+1cm}

\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 determined empirically.
                at ($(current page.north) + (\paperwidth+7.2mm,0.2175\paperheight)$) {
                \resizebox*{\headerwidth}{!}{\PaperCode, \PaperTitle, \TeachingPeriod\ \YearOffered}
            };
            \node[below right, scale=1.414, font=\sffamily\bfseries\large]
                at ($(current page.north west) + (0,-2.459\paperheight)$) {
                \ClassType\ \thechapter: \ShortClassTitle
            };
            \node[below, scale=1.414, font=\sffamily\bfseries\large]
                at ($(current page.north) + (\paperwidth+7.2mm,-2.459\paperheight)$) {
                Lecture Slides
            };
            \node[below left, scale=1.414, font=\sffamily\bfseries\large]
                at ($(current page.north east) + (2\paperwidth+14.4mm,-2.459\paperheight)$) {
                \the\numexpr(\thepage-1)/6+1
            };
        \fi%
    \end{tikzpicture}
}
\makeatother


\endinput
