% \iffalse meta-comment
%
% Copyright (C) 1994-2025 by Pieter van Oostrum <pieter@vanoostrum.org>
% -------------------------------------------------------
%
% This file 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.
% 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.
%
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{\jobname.dtx}
%</driver>
%
%    \begin{macrocode}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\NeedsTeXFormat{LaTeX2e}
%<fancyhdr>\ProvidesPackage{fancyhdr}%
%<fancyheadings>\ProvidesPackage{fancyheadings}
%<extramarks|extramarks-v4>\ProvidesPackage{extramarks}
%<fancyhdr|extramarks>           [2025/01/01 v5.0
%<fancyhdr>                  Extensive control of page headers and footers]%
%<fancyheadings>           [2022/11/09 v4.1
%<fancyheadings>                  Legacy package to call fancyhdr]
%<extramarks>                  Extra marks for LaTeX]
%<fancyhdr|extramarks>% Copyright (C) 1994-2025 by Pieter van Oostrum <pieter@vanoostrum.org>
%<extramarks-v4>           [2024/11/30 v4.5
%<extramarks-v4>                 Legacy version of extramarks for LaTeX]
%<extramarks-v4>% Copyright (C) 1994-2024 by Pieter van Oostrum <pieter@vanoostrum.org>
%<extramarks-v4>%%
%<extramarks-v4>%% This is a legacy version (v4) of extramarks.sty
%<extramarks-v4>%%
%<fancyheadings>% Public domain
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%    \end{macrocode}
%
%<*driver>
% Specify options for the url and hyperref packages
\PassOptionsToPackage{hyphens}{url}
\PassOptionsToPackage{colorlinks,linktocpage}{hyperref}
\documentclass[a4paper]{l3doc}
\addtolength\textheight{2cm}
\usepackage{multicol}
\usepackage{float}
\usepackage{makeidx}
\usepackage{robustindex}
\makeindex
\usepackage{layout}
\usepackage{array}
\usepackage{booktabs}
\usepackage{boxedminipage}
\usepackage{tikz}
\usetikzlibrary{decorations.fractals}

% The package fourier-orns defines \danger, which conflicts
% with the 'danger' environment of l3doc.
% We save, clear and restore \danger arounf this \usepackage
\let \liiidocxxxsavedanger\danger
\let\danger\undefined
\usepackage{fourier-orns}
\let\danger\liiidocxxxsavedanger
% End of conflict resolution
\usepackage[T1]{fontenc}
\usepackage[hyphens]{url}
\usepackage[fit]{truncate}
\usepackage{fancyvrb}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage[colorlinks,linktocpage]{hyperref}
\usepackage{ifthen}
\usepackage{xparse}
\usepackage{enumitem}
\setlist{itemsep=1pt}
\usepackage{fancyhdr}
\GetFileInfo{fancyhdr.sty}
\def\ams/{\protect\pAmS}
\def\pAmS{{\the\textfont2
        A\kern-.1667em\lower.5ex\hbox{M}\kern-.125emS}}
\newcommand{\bs}{\symbol{'134}}
\providecommand\env{}
\renewcommand{\env}[1]{\texttt{#1}}
\renewcommand{\partname}{Part}
\DisableCrossrefs
\CodelineIndex
\RecordChanges
\newcommand\bsbs{\char`\\\char`\\}
\newcommand{\Cmd}[1]{\texttt{\def\{{\char`\{}\def\}{\char`\}}\bs#1}}
%\newcommand{\CmdIndex}[1]{\index{#1=\string\texttt{\bs#1}}}
\newcommand{\CmdIndex}[1]{\setbox0\hbox{\cs{#1}}}
\newcommand{\TTindex}[1]{\index{#1=\string\texttt{#1}}\ignorespaces}
\newcommand{\PSindex}[1]{\index{page style>#1=\string\texttt{#1}}}
\newcommand{\OPTindex}[1]{\index{option>#1=\string\texttt{#1}}}
\newcommand{\PKGindex}[1]{\index{package>#1=\string\pkg{#1}}\ignorespaces}
\renewcommand{\topfraction}{0.9}
\renewcommand{\bottomfraction}{0.9}
\renewcommand{\textfraction}{0.05}
\setlength{\emergencystretch}{4em}
\addtolength{\textheight}{-0.5in} % make it print better on US letter paper
\setlength\IndexMin{4cm}
\makeatletter
\renewcommand\l@section      {\@dottedtocline{1}{1.5em}{2.3em}}
\makeatother
\newenvironment{block}{\vspace{8pt}\noindent\begin{minipage}{\textwidth}}{\end{minipage}\vspace{8pt}}
\newenvironment{fblock}{\vspace{8pt}\noindent\begin{boxedminipage}{\textwidth}}{\end{boxedminipage}\vspace{8pt}}
\newcommand{\showrule}{\\[-1.5ex]\noindent\makebox[\textwidth]{\hrulefill}\\[\baselineskip]}
\newenvironment{xvspace}{\vspace{1ex}}{{\vspace{1ex}}}
\setcounter{tocdepth}{1}

% Compare section numbers in references
\usepackage{refcount}
\newcounter{secnum}
% #1=current section label #2=ref section label
\newcommand{\smartref}[2]{%
  \setcounterref{secnum}{#1}%
  \addtocounter{secnum}{-1}%
  \ifnum\value{secnum}=\getrefnumber{#2}
    the previous section%
  \else
    \addtocounter{secnum}{2}%
    \ifnum\value{secnum}=\getrefnumber{#2}
      the next section%
    \else
      section~\ref{#2}%
    \fi
  \fi
}

% Our own page style:
\pagestyle{fancy}
\setlength{\headheight}{16.0pt}
\addtolength{\headwidth}{\marginparsep}
\addtolength{\headwidth}{0.5\marginparwidth}
\fancyhf{}
\fancyhead[L]{\rightmark}
\fancyhead[R]{\textbf{\thepage}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\quad#1}}

% Page style for demonstrating struts, \headruleskip and \footruleskip.

\newcommand{\strutheader}{%
  \texttt{\textbackslash strut}:\{
  \rule{10pt}{0.7\normalbaselineskip}%
  \hspace{-3pt}\rule[-0.3\normalbaselineskip]{10pt}{0.3\normalbaselineskip}%
  \raisebox{-0.3\normalbaselineskip}[0pt][0pt]%
    {\ifdim \headruleskip>0pt
      \rule[-\headruleskip]{10pt}{\headruleskip}
      \else
      \rule{10pt}{-\headruleskip}
     \fi}%
  \raisebox{-0.3\normalbaselineskip}[0pt][0pt]{$\swarrow$}\texttt{\textbackslash headruleskip}%
}
\newcommand{\strutfooter}{%
  \raisebox{0pt}[0pt][0pt]{%
    \texttt{\textbackslash strut}:\{
    \rule[-0.3\normalbaselineskip]{10pt}{0.3\normalbaselineskip}%
    \hspace{-3pt}\rule{10pt}{0.7\normalbaselineskip}%
    \rule[0.7\normalbaselineskip]{10pt}{\footruleskip}
    \raisebox{0.3\normalbaselineskip}{$\nwarrow$}\texttt{\textbackslash footruleskip}}%
}
\fancypagestyle{showstruts}{%
  \fancyhead[L]{%
    \ifthenelse{\value{page}=\getpagerefnumber{showstruts}}%
      {\strutheader}%
      {\rightmark}%
  }
  \fancyfoot[L]{%
    \ifthenelse{\value{page}=\getpagerefnumber{showstruts}}%
      {\strutfooter}%
      {}%
  }
  \fancyheadinit{%
    \ifthenelse{\value{page}=\getpagerefnumber{showstruts}}%
      {\renewcommand{\headruleskip}{4pt}}%
      {\renewcommand{\headruleskip}{0pt}}%
  }
  \fancyfootinit{%
    \ifthenelse{\value{page}=\getpagerefnumber{showstruts}}%
      {\renewcommand{\footrulewidth}{0.4pt}}%
      {\renewcommand{\footrulewidth}{0pt}}%
  }
}

\newcommand\exampleindex{}
\def\exampleindex#1#2#3\relax{%
  \ifx \relax#2\relax \index{Example 0#1=Example #1}\else \index{Example #1#2#3}\fi}

\newcommand{\ExampleLabel}{}
\NewDocumentCommand \Example { m o }
  {
    \renewcommand{\ExampleLabel}{Example~#1\IfNoValueF{#2}{\\#2}}%
    \exampleindex#1\relax\relax%
  }

\newcommand{\NoExample}[1]
  {%
    \renewcommand{\ExampleLabel}{#1}%
%    \index{Example `#1'}%
  }
\newcommand{\formatexample}{\hbox{\hspace{-2.5cm}%
    \raisebox{-2\baselineskip}[0pt][0pt]{%
      \parbox[t]{2.5cm}{\normalfont\normalsize
        \centering
        \ExampleLabel}}}\nopagebreak}

\newcommand{\FExample}[1]{%
  \noindent\makebox[0pt][l]{\hspace{-2.5cm}\parbox[t][0pt]{2.5cm}{%
      \normalfont\normalsize
      \centering
      Example #1}}%
  \exampleindex#1\relax\relax%
}%

\newcommand{\VL}{0.008} % size of vertical line segments
\newcommand{\upperline}{%
  \hbox to 0pt{%
    \kern\leftmargin
    \begin{picture}(0pt,0pt)
      \setlength{\unitlength}{\linewidth}
      \put(0,0){\line(1,0){1}}
      \put(0,0){\line(0,-1){\VL}}
      \put(1,0){\line(0,-1){\VL}}
    \end{picture}\hss}%
  \nopagebreak
}

\newcommand{\lowerline}{
\nopagebreak\noindent
\begin{picture}(0pt,0pt)
  \setlength{\unitlength}{\linewidth}
  \put(0,\VL){\line(1,0){1}}
  \put(0,\VL){\line(0,1){\VL}}
  \put(1,\VL){\line(0,1){\VL}}
\end{picture}\par
}

% fancyvrb hacking
\makeatletter
\def\FV@Frame@mylines{%
  \let\FV@BeginListFrame\upperline
  \let\FV@LeftListFrame\relax
  \let\FV@RightListFrame\relax
  \let\FV@EndListFrame\lowerline}
\makeatother

% print the Example number in the margin and gobble the % sign of ltxdoc
\DefineVerbatimEnvironment{example}{Verbatim}{formatcom=\formatexample,gobble=1,
                                              vspace=0pt,frame=mylines}
\DefineVerbatimEnvironment{code}{Verbatim}{gobble=1,vspace=0pt,frame=mylines}

\newlength{\normalparindent}
\setlength{\normalparindent}{\parindent}

% Macro to print a header in the running text
\makeatletter
\newcommand\showheader[2][O]{%
% #1 optional = O/E (odd/even header)
% #2 = fancyhdr code for the header
%
  \begingroup
  \let\headwidth\textwidth
  #2
  \noindent\makebox[\textwidth][c]{\if O#1\@oddhead\else\@evenhead\fi}
  \endgroup
}
\makeatother

% l3doc resets section counters at \part.
% I don't want that.
\makeatletter
\@removefromreset{section}{part}
\makeatother

\begin{document}
  \DeleteShortVerb{\|}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \CheckSum{0}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
% \DoNotIndex{\#,\$,\%,\&,\@,\\,\{,\},\^,\_,\~,\ }
% \expandafter\DoNotIndex\expandafter{\bslash} ^^A BUG in l3doc
% \DoNotIndex{\@ne,\box,\box_new:N,\csx,\csn,\copy,\color}
% \DoNotIndex{\@opcol,\@opcol:,\expandafter,\errorcontextlines}
% \DoNotIndex{\advance,\begingroup,\catcode,\closein,\centering,\dbltextfloatsep}
% \DoNotIndex{\begin,\end,\bgroup,\egroup}
% \DoNotIndex{\closeout,\day,\def,\edef,\else,\empty,\endgroup,\everypar}
% \DoNotIndex{\cs_gset_eq:NN,\cs_gset_protected:Npn,\cs_if_exist:NTF,\cs_if_exist:cTF}
% \DoNotIndex{\cs_new:Nn,\cs_new_eq:NN,\cs_new_protected:Npn,\cs_set_eq:NN}
% \DoNotIndex{\csname,\endcsname,\end,\endinput,\def,\dimen,\do,\dp}
% \DoNotIndex{\dim_set:Nn,\c_max_dim,\endlinechar,\errmessage}
% \DoNotIndex{\exp_args:Nc,\exp_last_unbraced:Nv,\exp_not:n,\exp_not:v}
% \DoNotIndex{\ExplSyntaxOn,\ExplSyntaxOff,\fi,\gdef,\global}
% \DoNotIndex{\group_begin:,\group_end:,\hbox,\hfil,\hfill,\hsize,\hskip,\hss,\ht}
% \DoNotIndex{\hrule,\hrulefill,\if,\ifdim,\ifnum,\ifx,\ifvmode,\ifvoid,\ifdefined}
% \DoNotIndex{\index,\int_new:N,\int_set:Nn,\int_set_eq:NN,\c_max_int}
% \DoNotIndex{\label,\leavevmode,\let,\llap,\LM,\long,\macro}
% \DoNotIndex{\makeatletter,\makeatother,\MessageBreak,\mark,\maxdimen}
% \DoNotIndex{\msg_error:nnn,\msg_error:nnnn,\msg_new:nnn,\mysaved,\newif}
% \DoNotIndex{\newmarks,\newtoks,\nobreak,\noexpand,\noindent,\output,\outputdblcol}
% \DoNotIndex{\parbox,\patchcmd,\plain,\pretocmd,\protect,\providecommand}
% \DoNotIndex{\protect,\ProvidesPackage,\quad,\relax,\renewcommand,\RequirePackage,\restorecr}
% \DoNotIndex{\restylefloat,\rlap,\rm,\rmfamily,\scan_stop:}
% \DoNotIndex{\seq_map_inline:Nn,\seq_new:N,\seq_put_right:Nn,\setAP,\setbox}
% \DoNotIndex{\setcounter,\setlength,\settowidth,\sl,\slshape,\space,startAP,\string}
% \DoNotIndex{\strut,\temp@a,\temp@a:,\@[xy]lhead,\@botlist,\@chapapp,\@currbox,\@currsize,\@empty}
% \DoNotIndex{\@firstoftwo,\@for,\@forloop,\@fortmp,\@gobble,\@gobbletwo,\@height}
% \DoNotIndex{\@ifnextchar,\@ifpackageloaded,\@ifstar,\@ifundefined,\@makecol,\@minus}
% \DoNotIndex{\@mkboth,\@namedef,\@nameuse,\@newbaseline,\@nil,\@normalcr,\@normalsize}
% \DoNotIndex{\@outputbox,\@outputdblcol,\@outputpage,\@parboxrestore,\@plus}
% \DoNotIndex{\@secondoftwo,\@spaces,\@tempa,\@tempboxa,\@tempdima,\@tempdimb,\@tempdimc}
% \DoNotIndex{\@tempskipa,\@tempskipb,\@temptokena,\@temptokenb,\@tfor,\@themark}
% \DoNotIndex{\@toplist,\@topnewpage,\@undefined,\@unexpandable@protect,\@width}
% \DoNotIndex{\c@page,\c@secnumdepth,\col@number,\color@begingroup,\color@endgroup}
% \DoNotIndex{\color@endbox,\color@vbox,\f@nch@,\insert@nonemptymarks,\latex@makecol}
% \DoNotIndex{\m@ne,\partial@page,\pcol@@outputpage,\pcol@combinefloats}
% \DoNotIndex{\pcol@getcurrcol,\pcol@imakeflushedpage,\pcol@ioutputelt,\pcol@makecol,\pcol@prevdepth}
% \DoNotIndex{\pcol@makeflushedpage,\pcol@outputpage@l,\pcol@outputpage@r,\pcol@prec_depth}
% \DoNotIndex{\pcol@rightpage,\pcol@spanning,\pcol@synccolumn,\ps@xxx,\return@nonemptymark}
% \DoNotIndex{\scr@topnewpage,\set@keptmarks,\temp@a,\temp@b,\temp@c,\temp@ty,\tmpf@ra}
% \DoNotIndex{\toks@,\unrestored@protected@xdef,\voidb@x,\z@,\z@skip}
% \DoNotIndex{\tex_marks:D,\tex_splitmaxdepth:D,\tex_vbadness:D,\textbf,\textsl,\texttt}
% \DoNotIndex{\textfraction,\textwidth,\the,\c_empty_tl}
% \DoNotIndex{\tl_gset:cn,\tl_gset:Nn,\tl_gset:No,\tl_gset:co,\tl_gset_eq:NN,\tl_gset_eq:cc,\tl_gset_eq:cN}
% \DoNotIndex{\tl_if_empty:cF,\tl_if_empty:cTF,\tl_if_empty:cT,\tl_if_empty:NF,\tl_if_empty:NTF}
% \DoNotIndex{\tl_new:N,\tl_new:c,\tl_set_eq:cc,\tl_to_str:c,\token_to_str:c,\token_to_str:N}
% \DoNotIndex{\undefined,\unskip,\unvbox,\unvcopy,\use:n,\use:N,\use:c,\use_none:n,\usepackage}
% \DoNotIndex{\var,\vbox,\vbox_set:Nn,\vbox_set_split_to_ht:NNn,\vboxed,\vskip,\vspace}
% \DoNotIndex{\vsplit,\vss,\xdef,\xlap,\xxxx,\xmarks}
% \DoNotIndex{\newcommand,\NewDocumentCommand,\NewExpandableDocumentCommand,\newlength,\newtoks}
%
% \ProvideDocElement[printtype=\textit{hook},idxtype=hook,idxgroup=Hooks]{Hook}{hookdecl}%
% \newcommand\makedate{}
% \def\makedate#1/#2/#3/{%
%   \date{%
%     \ifcase #2\or
%     Jan\or Feb\or March\or Apr\or May\or June\or
%     July\or Aug\or Sept\or Oct\or Nov\or Dec\fi~\number #3,
%     #1}%
% }
% \expandafter\makedate\filedate/
%
% \title{The \pkg{fancyhdr} and \pkg{extramarks}
% packages\\{\normalsize version \fileversion.}}
% \author{Pieter van Oostrum\thanks{Part of this documentation was
%     written by  George
%     Gr\"atzer (University of Manitoba) in \emph{Notices Amer. Math. Soc.}
%     Thanks, George!}\\
%     Dept.\ of Computer Science\thanks{This was my employer at the time I developed this package.
% I am now retired.}\\
%   Utrecht University}
%
% \maketitle
% \begin{abstract}
%   This document describes how to customize the page layout of your LaTeX
%   documents, i.e., how to change page margings and sizes,
%   headers and footers, and the
%   proper placement of figures and tables (collectively called floats) on
%   the page.
%
% This documentation describes version 5.0 or later of the
% \pkg{fancyhdr} and \pkg{extramarks} packages. The user
% documentation is also mostly valid for the versions 4.0 or later of
% the \pkg{fancyhdr} and \pkg{extramarks} packages (except
% for the changes mentioned in sections~\ref{sec:version-4} and
% \ref{sec:version-5}).
%
% \end{abstract}
%
% \tableofcontents
% \markright{Contents}
%
% \begin{documentation}
% \part{Introduction}
%
% This document contains four parts:
%
% Part I is a short documentation on
% the user commands of the \pkg{fancyhdr} and \pkg{extramarks} packages.
%
% Part II contains elaborate documentation on page layout in \LaTeX.
% This used to be the complete documentation of \pkg{fancyhdr} and
% \pkg{extramarks} for several years.
%
% Part III contains Questions and Answers.
%
% Part IV contains the annotated implementation.
%
% This document describes version 5 of \pkg{fancyhdr}. This version
% is an extension of \pkg{fancyhdr} version 4, which is described in
% the \emph{The \LaTeX{} Companion, Third Edition}. It just
% has some additional commands that are not mentioned in \emph{The
% \LaTeX{} Companion}.
% The differences between these versions
% are summarized in section~\ref{sec:version-4} on
% page~\pageref{sec:version-4}, and  section~\ref{sec:version-5} on
% page~\pageref{sec:version-5}.
% Throughout this documentation it is mentioned when a specific feature
% is only available in version 4 or a later version, or when there are
% differences between version 4 and 5.
%
% This document also describes version 5 of \pkg{extramarks}. This
% is a new implementation that differs significantly from the previous
% versions. See section~\ref{sec:extramarks-5} on
% page~\pageref{sec:extramarks-5} for more details.
%
% This documentation contains several examples. Most of the examples are
% available for download from Github, see section~\ref{sec:contact}.
% These examples are indicated with their name in the margin. If the
% margin says ``Example \meta{n}'', where \meta{n} is numeric, maybe
% followed by a letter, then the file name will be
% \texttt{example\meta{n}.tex}. When it is followed by a letter in
% parentheses like (A), it means an item in the file. Other names
% without the word ``Example'' are just the file name without extension,
% for example ``with-beamer'' indicates the file name \texttt{with-beamer.tex}.
%
% \section{Installation}
% \label{sec:installation}
%
% The preferred way to install this package is with a package installer.
% If you want to install it by hand, then first run the command
% `\verb+tex fancyhdr.ins+' and then move the files \pkg{fancyhdr.sty},
% \pkg{extramarks.sty}, \pkg{extramarks-v4.sty}
% and \pkg{fancyheadings.sty} to a place
% where \LaTeX{} can find it, preferably in a directory similar to
% \texttt{.../texmf/tex/latex/fancyhdr/} in your \TeX{} directory tree.
% To get the documentation, run `\verb+pdflatex fancyhdr.dtx+'.
%
% \section{Using \pkg{fancyhdr}}
% \label{sec:using-fancyhdr}
%
% The package \pkg{fancyhdr} gives you several commands to define
% headers and footers of the pages in a \LaTeX{} document.
% You load the package with the following command in the preamble:
% \begin{quote}
% \cs{usepackage}\oarg{options}\verb|{fancyhdr}|
% \end{quote}
% (Options are available since version~4.0.
% See \smartref{sec:using-fancyhdr}{sec:options} for the details.)
%
% \begin{function}{\fancyhead,\fancyfoot,\fancyhf}
% \begin{syntax}
%   \cmd{\fancyhead}\oarg{places}\marg{field} \\
%   \cmd{\fancyfoot}\oarg{places}\marg{field} \\
%   \cmd{\fancyhf}\oarg{places}\marg{field}
% \end{syntax}
% Here \meta{places} is a comma-separated list of places where
% \meta{field} will be placed. There are 12 places defined: Left,
% Center and Right Headers and Footers, and both can be on Even or Odd
% pages. Each place therefore has 3 coordinates which are the inital
% letters of the above description: (1) \texttt{E} or \texttt{O}, (2) \texttt{L}, \texttt{C} or
% \texttt{R}, (3) \texttt{H} or \texttt{F}. So a place is given with 3
% letters, like \texttt{EOH}. A missing coordinate means: all
% possibilities, except for \cs{fancyhead} where \texttt{H} is implied
% and \cs{fancyfoot} where \texttt{F} is implied. Although in this
% documentation always uppercase letters are used in the \meta{places},
% lowercase is also acceptable.
% \end{function}
%
% \begin{function}{\fancyheadoffset,\fancyfootoffset,\fancyhfoffset}
% \begin{syntax}
%   \cmd{\fancyheadoffset}\oarg{places}\marg{length} \\
%   \cmd{\fancyfootoffset}\oarg{places}\marg{length} \\
%   \cmd{\fancyhfoffset}\oarg{places}\marg{length}
% \end{syntax}
% These define offsets to let the headers stick into the margin (or to
% the inside if negative). Places cannot contain the \texttt{C}
% specifier. See sections~\ref{sec:book-examples}  and
% \ref{sec:headwidth} for more details.
% \end{function}
%
% \begin{function}{\fancyheadwidth,\fancyfootwidth,\fancyhfwidth}
% \begin{syntax}
%   \cmd{\fancyheadwidth}\oarg{places}\marg{length} \\
%   \cmd{\fancyfootwidth}\oarg{places}\marg{length} \\
%   \cmd{\fancyhfwidth}\oarg{places}\marg{length}
% \end{syntax}
% These define widths for the header and footer fields. The fields will
% be typeset in a \cs{parbox} of this width, which can be different for
% each \emph{place}. If the width of a field is not specified, it
% defaults to \cs{headwidth}, which may cause them to overlap. See
% section~\ref{sec:field-width} for more details.
% \end{function}
%
% \begin{variable}{\headrulewidth, \footrulewidth, \headruleskip,
%                  \footruleskip, \headwidth}
% \cs{headrulewidth} and \cs{footrulewidth} are macros to define the
% thickness of a line under the header and above the footer.
% \cs{headruleskip} and \cs{footruleskip} are macros that define the
% distance between the lines and the header and footer text,
% respectively. (But \cs{headruleskip} is only available since version~4.0.)
% And \cs{headwidth} is a length parameter that defines the
% total width of the headers and footers. See section~\ref{sec:headwidth} for more details.
% \end{variable}
%
% \begin{function}{\headrule, \footrule}
% \cs{headrule} and \cs{footrule} are macros to completely redefine
% these lines.
% \end{function}
%
% \begin{function}{\fancyheadinit, \fancyfootinit, \fancyhfinit}
% \cs{fancyheadinit} and \cs{fancyfootinit} can be used to define
% initialisation code for the header and footer, respectively, and
% \cs{fancyhfinit} defines both of these. These commands are only
% available in \pkg{fancyhdr} version~4.0 and later. See section~\ref{sec:curpagestyle}.
% \end{function}
%
% \begin{function}{\fancyfootalign}
% \begin{syntax}
% \cmd{\fancyfootalign}\texttt{\{\}}\\
% \cmd{\fancyfootalign}\marg{length}
% \end{syntax}
% \end{function}
% The command \cs{fancyfootalign} allows you to fine-tune the vertical
% position of the footer with respect to the page bottom. This command
% is only available in \pkg{fancyhdr} 5.0 and later.
% See section~\ref{sec:fancyfootalign}.
%
% \begin{function}{\fancycenter}
% \begin{syntax}
% \cs{fancycenter}\oarg{dist}\oarg{stretch}\marg{left-field}\marg{center-field}\marg{right-field}
% \end{syntax}
% (Only in version~4.0 and later.) The command \cs{fancycenter} packs 3
% header fields into a full-width header. See section~\ref{sec:fancycenter}.
% \end{function}
%
% \begin{function}{\fancyhdrbox}
% \begin{syntax}
% \cs{fancyhdrbox}\oarg{alignment}\oarg{width}\marg{lines separated by \cmd{\\}}
% \end{syntax}
% (Only in version~5.0 and later.) The command \cs{fancyhdrbox} can be
% used to align multi-line parts vertically and horizontally.
% See section~\ref{sec:fancyhdrbox}.
% \end{function}
%
% \begin{function}{\iftopfloat, \ifbotfloat, \iffloatpage, \iffootnote}
% The macros \cs{iftopfloat}, \cs{ifbotfloat}, \cs{iffloatpage} and
% \cs{iffootnote} are used to detect if there is a float on the top or
% the bottom of the page, or the page is a float page, or if there is
% a footnote at the bottom of the page. These can be used to choose
% different headers and/or footers if these conditions are met. See
% section~\ref{sec:float} for more details.
% \end{function}
%
% \begin{function}{\fancypagestyle,\fancypagestyle*}
% \begin{syntax}
% \cs{fancypagestyle}\marg{style-name}\oarg{base-style}\marg{definitions}\\
% \cs{fancypagestyle}*\marg{style-name}\oarg{base-style}\marg{definitions}
% \end{syntax}
% This command lets you (re)define page styles for use in special
% situations. See sections~\ref{sec:pagestyle-plain} and
% \ref{sec:fancypagestyle} for more details.
% \end{function}
%
% \begin{function}{\fancypagestyleassign}
% \begin{syntax}
% \cs{fancypagestyleassign}\marg{ps1}\marg{ps2}
% \end{syntax}
% This command assigns page style \meta{ps2} to \meta{ps1}. This causes
% \meta{ps1} to be an exact copy of \meta{ps2}, but completely
% independent of \meta{ps2}. Or you could say that \meta{ps1} becomes a
% new name for page style \meta{ps2}.
% See section~\ref{sec:tocstyle} for an example.
% \end{function}
%
% \begin{function}{\fancyhdrsettoheight}
% \begin{syntax}
% \cs{fancyhdrsettoheight}\marg{lengthvar}\marg{header/footer}
% \end{syntax}
% Sets \meta{lengthvar} to the height of the \meta{header/footer}, which must
% be one of \texttt{oddhead}, \texttt{evenhead}, \texttt{oddfoot} or \texttt{evenfoot}.
% Please note: You usually use this outside of a header or a footer (for
% example in the \emph{preamble}, but then if you use marks with a non-standard height
% in your headers or footers, the calculated height may be wrong, as marks don't work well
% outside of a header or footer.
% \end{function}
%
% \section{Package \pkg{fancyhdr} options}
% \label{sec:options}
%
% \textbf{NOTE:} This section applies to \pkg{fancyhdr} version~4.0 and later.
%
% You can supply options to the \cs{usepackage} command:
% \begin{quote}
% \verb|\usepackage|\oarg{options}\verb|{fancyhdr}|
% \end{quote}
% The following options are supported:
% \begin{center}
% \OPTindex{nocheck}\OPTindex{compatV3}\OPTindex{twoside}\OPTindex{headings}\OPTindex{myheadings}%
% \begin{tabular}{ l l }
% \toprule
% Option & Meaning \\
% \midrule
% \texttt{nocheck} & do not check the heights of the header and footer \\
% \texttt{compatV3} & keep some behaviour (now considered undesirable) as in version~3 \\
% \texttt{twoside} & use two-sided headers and footers even in one-sided documents \\
%          & for \pkg{fancyhdr}-based page styles (version 4.1 or later) \\
% \texttt{headings} & redefine the \texttt{headings} page style to be fancy-based \\
% \texttt{myheadings} & redefine the \texttt{myheadings} page style to be fancy-based \\
% \bottomrule
% \end{tabular}
% \end{center}
%
% \begin{itemize}
% \item Options \texttt{nocheck} and \texttt{compatV3} are described in
% section~\ref{sec:warning} on page~\pageref{page:warning}.
%
% \item \OPTindex{compatV3}Option \texttt{compatV3} keeps two \pkg{fancyhdr}
%   version~3.x (or earlier) features that are now considered undesirable.
%   \begin{enumerate}
%   \item The automatic adjustment of \cs{headheight} or \cs{footskip}
%     when these are too small. This causes the page layout to become
%     inconsistent.
%   \item In these previous versions the changes to the
%     \pkg{fancyhdr} headers and footers (including those by
%     \cs{fancyhead}, \cs{fancyheadoffset} and similar commands) are
%     made globally, except within a page style defined by
%     \cs{fancypagestyle}. That is, when these commands are given inside
%     a \LaTeX{} group, they affect the whole document, not only the
%     group. If your document depends on this behaviour, you can give
%     the \texttt{compatV3} package option. However, this is only
%     considered a short-time solution. You should change your document
%     as soon as possible to work around this problem. In version~4.0 and
%     later, without this option, the changes are always local.
%   \end{enumerate}
%   This option is deprecated in version~5.0 of
%   \pkg{fancyhdr}. It will disappear in a later release. Please
%   don't use this option anymore, but rather change your document.
%
% \item \OPTindex{twoside}Option \texttt{twoside} implements two-sided headers
% and footers in one-sided documents (version 4.1 or later). This applies only
% for \pkg{fancyhdr}-based page styles. This option doesn't do anything
% special for two-sided documents (\texttt{twoside} documentclass option), as
% these already have that functionality. And with the \texttt{twoside}
% documentclass option that does apply to other page styles as well.
%
% \item \PSindex{headings}\PSindex{myheadings}\OPTindex{headings}\OPTindex{myheadings}
%   The options \texttt{headings} and \texttt{myheadings} redefine the
%   corresponding page style with \pkg{fancyhdr} commands (including a
%   decorative line under the header), so that you can later select this
%   page style as the page style for (part of) the
%   document\footnote{These options were copied from the
%   \PKGindex{nccfancyhdr}
%   \pkg{nccfancyhdr} package, but contrary to that package, they are
%   not automatically selected.}.
% \end{itemize}
%
% \PSindex{headings}
% The page style \texttt{headings} is in some aspects similar to the default page
% style \texttt{fancy} settings. In the \texttt{fancy} page style, the
% page number is in the footer, but in the \texttt{headings}
% page style it is in the header. The header fields look similar, however.
%
% Please note that these page styles redefine the \cs{chaptermark} and/or
% \Cmd{[sub]sectionmark} commands (see section~\ref{sec:scoop}), as do the
% standard \LaTeX{} page styles. The consequence is, that if you select
% e.g., \Cmd{pagestyle\{headings\}}, the definitions of
% \Cmd{pagestyle\{fancy\}} are overridden.
% Also when you change the headers and/or footers
% while such a page style is in effect, and you then switch back to this
% page style, for
% example with \verb|\pagestyle{headings}|, they revert to the built-in
% settings. Therefore it is not advisable to change the headers or
% footers in this way, but instead define your
% own page style, as explained in section~\ref{sec:fancypagestyle}.
%
% \section{Using \pkg{extramarks}}
% \label{sec:using-extramarks}
%
% Standard \LaTeX{} has two marks: a left one and a right one. The standard
% command \cs{leftmark} gives you the last left mark on a page, and
% \cs{rightmark} gives you the first right one. These are to be used in
% the headers and footers of a page. These are derived from information
% that is given by the \cs{markboth} and \cs{markright} commands in the
% text body.
%
% \begin{function}{\firstleftmark, \lastrightmark, \firstrightmark, \lastleftmark}
% These macros give you the other combinations, where
% \cs{firstrightmark} = \cs{rightmark} and \cs{lastleftmark} = \cs{leftmark}.
% \end{function}
%
% \begin{function}{\extramarks, \extramarksleft, \extramarksright}
% \begin{syntax}
% \cs{extramarks}\marg{left-text}\marg{right-text}
% \cs{extramarksleft}\marg{left-text}\\
% \cs{extramarksright}\marg{right-text}
% \end{syntax}
%
% The command \cs{extramarks}\marg{$m_1$}\marg{$m_2$} defines two extra marks,
% similar to the standard ones by \LaTeX, where \meta{$m_1$} is the left
% mark and \meta{$m_2$} is the right mark.
%
% \label{sec:extramarks-5}
% In versions before 5.0, the \texttt{extramarks} are connected to each
% other and to the original \LaTeX{} marks; they are not independent.
% For example, if you use \cs{markboth} or \cs{markright}, this
% introduced empty \texttt{extramarks} or duplicated existing ones. This is
% also true in the other direction. This sometimes caused unwanted effects.
%
% Since version 5.0 this is no longer the case.
% Now the \texttt{extramarks} are independent of the traditional marks,
% and they can also be set independently of each other by the commands
% \cs{extramarksleft}\marg{$m_1$} and \cs{extramarksright}\marg{$m_2$}.
% \end{function}
%
% \begin{variable}{extramarks-left, extramarks-right}
% \begin{syntax}
% \texttt{extramarks-left}\\
% \texttt{extramarks-right}
% \end{syntax}
%   These are the `mark classes' for the two marks.
% \end{variable}
%
% \medskip
%
% \textbf{NOTE}: The implementation of \pkg{extramarks} version 5
% only is available if your \LaTeX{} release is the
% November 2022 \LaTeX{} release or newer. It uses the new \LaTeX{}
% marks introduced in that release. These marks are described in \emph{The \LaTeX{}
% Companion, Third Edition}, section 5.3.5 (Part I). Of course you can
% also use these new marks directly, or use additional ones if you need
% more. Some examples in this manual use these.
%
% This manual contains several examples of the use of \pkg{extramarks},
% where its features are essential, but in future releases of this
% manual these examples will be rewritten to use the new \LaTeX{} marks directly.
%
% \subsubsection*{Extramarks commands to be used in the headers or footers}
%
% \begin{function}{\firstleftxmark, \firstrightxmark, \topleftxmark,
%                  \toprightxmark, \lastleftxmark, \lastrightxmark, \firstxmark,
%                  \lastxmark, \topxmark}
% \begin{flushleft}
% These commands are used to extract the marks defined by
% \cs{extramarks}\marg{$m_1$}\marg{$m_2$}, \cs{extramarksleft}\marg{$m_1$}\ and
% \cs{extramarksright}\marg{$m_2$} described above.\\
% They are used in the headers or footers, similar to the ones without
% the \texttt{x}.
% \end{flushleft}
% \end{function}
%
% If you want to keep the old behaviour of \pkg{extramarks}, you can use:
% \begin{verbatim}
% \usepackage{extramarks}[=v4]
% \end{verbatim}
% or if you have an old \LaTeX{} system where the above doesn't work
% \begin{verbatim}
% \usepackage{extramarks-v4}
% \end{verbatim}
% Please note that in that case the
% \cs{topleftxmark}, \cs{toprightxmark} and \cs{topxmark} commands may
% give you unexpected results.
%
% See sections~\ref{sec:scoop} and \ref{sec:xmarks} for more details
% about the use of the package.
%
% \newpage
% \part{Page Layout in \LaTeX}
%
% \section{Introduction}
% \label{sec:intro}
%
% A page in a \LaTeX{} document is built from various elements as shown in
% figure \ref{fig:layout}.
% \begin{figure}[htbp]
%   \begin{center}
%     \leavevmode
%     \layout
%     \vspace{1.5cm}
%     \caption{Page elements. The values shown are those in effect in the current document, not the defaults.}
%     \label{fig:layout}
%   \end{center}
% \end{figure}
% \thispagestyle{plain}
% The body contains the main text of the document
% together with the so called floats (tables and figures).
%
% The pages are constructed by \LaTeX's output routine, which is quite
% complicated and should therefore not be modified. Some of the packages
% described in this paper contains small modifications to the output routine
% to accomplish things that cannot be done in another way. You should use
% these packages to get the desired result rather than fiddling with the
% output routine yourself.
%
% There are a number of things that you must be aware of:
% \begin{enumerate}
% \item The margins on the left are not called \cs{leftmargin}, but
%   \cs{evensidemargin} (on even-numbered pages) and \cs{oddsidemargin}
%   (on odd-numbered pages). In one-sided documents
%   \cs{oddsidemargin} is used for either.  \cs{leftmargin} is also a valid
%   \LaTeX{} parameter but it has a different use (namely the indentation of
%   lists).
% \item Most of the parameters should not be changed in the middle of a
%   document. Some changes might work at a pagebreak. If you want to change
%   the height of a single page, you can use the \cs{enlargethispage}
%   command.
% \end{enumerate}
%
% The margin notes
% area contains small pieces of information created by the \cs{marginpar}
% command. On two-sided documents the margin notes appear on the left and right
% alternatively. The margin notes are not on fixed places with respect to the
% paper but at approximately the same height as the paragraph in which they
% appear. Due to the algorithm used to decide the placement of margin notes,
% in a two-sided document unfortunately
% they may appear on the wrong side if they are close
% to a page break.
% If you want to put information on fixed places in the margins you may use
% the technique described in sections \ref{sec:movie} and \ref{sec:thumb}.
%
% The first part of this paper describes how to change the header and footer
% areas. The last part describes how to get your floats at the desired place.
%
% \section{Page headers and footers}
%
% The page headers and footers in \LaTeX{} are defined by the
% \cs{pagestyle} and \cs{pagenumbering} commands. \cs{pagestyle}
% defines the general contents of the headers and footers (e.g., where the
% page number will be printed), while
% \cs{pagenumbering} defines the format of the page number.
% \LaTeX{} has four standard page styles:
%
% \begin{center}
% \noindent
%   \begin{tabular}{>{\tt}lp{9cm}}
%   \toprule
%     empty & no headers or footers \\
%     plain & no header, footer contains page number centered \\
%     headings & no footer, header contains name of chapter/section and/or
%     subsection and page number \\
%     myheadings & no footer, header contains page number and user
%     supplied information \\
%     \bottomrule
% \end{tabular}
% \end{center}
%
% Although these are useful styles, they are quite limited. Additional page
% styles can be defined by defining commands of the form \cs{ps@xxx}. This
% command is executed when a \Cmd{pagestyle\{xxx\}} is given in the document.
% The  \cs{ps@xxx} command should define the following
% commands for the contents of the headers and footers:
%
% \begin{center}
%   \noindent
%   \begin{tabular}{lp{9cm}}
%   \toprule
% \cs{@oddhead} & header on odd numbered pages in two-sided documents
% (on all pages in one-sided) \\
% \cs{@evenhead} & header on even numbered pages in two-sided documents \\
% \cs{@oddfoot} & footer on odd numbered pages in two-sided documents
% (on all pages in one-sided) \\
% \cs{@evenfoot} & footer on even numbered pages in two-sided documents \\
% \bottomrule
% \end{tabular}
% \end{center}
%
% These are not user commands, but rather ``variables'' that are used by
% \LaTeX's output routine.
% As the command names contain the character `\texttt{@}', they should
% be defined in a package file, or otherwise be sandwiched between the commands
% \cs{makeatletter} and \cs{makeatother}.
%
% The \cs{pagenumbering} command defines the layout of the page number. It
% has a parameter from the following list:
%
% \begin{center}
%   \begin{tabular}{>{\tt}ll}
%   \toprule
% arabic & arabic numerals \\
% roman & lower case roman numerals \\
% Roman & upper case roman numerals \\
% alph & lower case letter \\
% Alph & upper case letter \\
% \bottomrule
% \end{tabular}
% \end{center}
%
% The  \Cmd{pagenumbering\{xxx\}} defines the command \cs{thepage} to be the
% expansion of the page number in the given notation \texttt{xxx}.
% The page style command
% then would include \cs{thepage} in the appropriate place. Additionally
% the  \cs{pagenumbering} command resets the page number to~1.
% The  \cs{pagestyle} and  \cs{pagenumbering} apply to the page that is
% being constructed, so they should be used at a location where it is clear
% to what page they apply (see section \ref{sec:change}).
%
%
% \section{What is \pkg{fancyhdr}}
%
% The \pkg{fancyhdr} macro package allows you to customize
% in \LaTeX{} your page headers and footers in an easy way.  You can
% define:
% \begin{itemize}
% \item three-part headers and footers
% \item decorative lines in headers and footers
% \item headers and footers wider than the width of the text
% \item multi-line headers and footers
% \item separate headers and footers for even and odd pages
% \item different headers and footers for chapter pages
% \item different headers and footer on pages with floats
% \end{itemize}
%
% Of course, you also have complete control over fonts, uppercase
% and lowercase displays, etc.
%
% \section{Simple use of \pkg{fancyhdr}}  To use this package
% install it in a place where \LaTeX{} can find it (see
% section~\ref{sec:installation})\footnote{In most modern \TeX{}
% installation the package is already included.}, and include in the preamble of your document
% the commands:
%
% \PSindex{fancy}
% \begin{code}
% \usepackage{fancyhdr}
% \pagestyle{fancy}
% \end{code}
%
% We can visualize the page layout we can create with \pkg{fancyhdr}
% as follows:
%
% \begin{fblock}
% \noindent\makebox[\textwidth]{LeftHeader\hfill
% CenteredHeader\hfill RightHeader}\showrule
% \noindent\makebox[\textwidth]{\hfill page body\hfill}\\[\baselineskip]
% \noindent\makebox[\textwidth]{\hrulefill}
% \noindent\makebox[\textwidth]{LeftFooter\hfill
% CenteredFooter\hfill RightFooter}
% \end{fblock}
%
% The LeftHeader and LeftFooter are left justified; the
% CenteredHeader and CenteredFooter are centered; the
% RightHeader and RightFooter are right justified.
%
% We define each of the six ``fields'' and the two decorative lines
% separately.
%
% \begin{fblock}
%   \textbf{NOTE:} In \pkg{fancyhdr} version 4.3 and later, paragraph
% hooks will not work inside \pkg{fancyhdr} headers and footers to
% avoid unwanted interactions with the main text.
% \end{fblock}
%
% \section{A simple example}
% \label{sec:simple}
%
% K. Grant is writing a report to Dean
% A. Smith, on ``The performance of new graduates'' with the
% following page layout:
%
% \begin{fblock}
% \noindent\makebox[\textwidth]{\hfill\textbf{The performance of new
% graduates}}\showrule
% \noindent\makebox[\textwidth]{\hfill page body\hfill}\\[\baselineskip]
% \noindent\makebox[\textwidth]{\rule{\textwidth}{2pt}}
% \noindent\makebox[\textwidth]{From: K. Grant\phantom{3}\hfill
% To: Dean A. Smith\hfill \phantom{From: K. Grant}3}
% \end{fblock}
%
% \noindent where ``3'' is the page number.  The title: ``The
% performance of new graduates'' is bold. The rule above the footer is a
% bit thicker (2pt).
%
% This is accomplished by these commands following
% \Cmd{pagestyle\{fancy\}}\footnote{Note that version 1 of \pkg{fancyheadings}
% used the \cs{setlength} command to change the \texttt{\bs...rulewidth} parameters.}:
% \CmdIndex{fancyhead}
% \CmdIndex{fancyfoot}
% \CmdIndex{headrulewidth}
% \CmdIndex{footrulewidth}
% \Example{1}
% \begin{example}
% \fancyhead[L,C]{}
% \fancyhead[R]{\textbf{The performance of new graduates}}
% \fancyfoot[L]{From: K. Grant}
% \fancyfoot[C]{To: Dean A. Smith}
% \fancyfoot[R]{\thepage}
% \renewcommand{\headrulewidth}{0.4pt}
% \renewcommand{\footrulewidth}{2pt}
% \end{example}
% (The \cs{thepage} macro displays the current page number.
% \cs{textbf} puts its argument in bold face.)
%
% This is now fine, except that the first page does not need
% all these headers and footers.  To eliminate all but the
% centered page number, issue the command
%
% \Example{2}
% \CmdIndex{thispagestyle}
% \begin{example}
% \thispagestyle{plain}
% \end{example}
% after the
% \Cmd{begin\{document\}}
% and the
% \CmdIndex{maketitle}
% \cs{maketitle}
% commands.
%
% Alternatively, issue
% \begin{code}
% \thispagestyle{empty}
% \end{code}
% if you do not want any headers or footers.
%
% In fact the standard \LaTeX{} classes have the command \cs{maketitle}
% defined in such a way that a \Cmd{thispagestyle\{plain\}} is automatically
% issued. So if you \emph{do} want the fancy layout on a page containing
% \cs{maketitle} you must issue a \Cmd{thispagestyle\{fancy\}} after the
% \cs{maketitle}.
%
% \section{The default layout}
% \label{sec:default}
% Let us use the \pkg{book.cls} documentclass and the default settings for
% \pkg{fancyhdr}; so we don't use any of the page style options in
% the \Cmd{usepackage\{fancyhdr\}} command, and we don't redefine any
% headers or footers. So just:
% \begin{code}
% \usepackage{fancyhdr}
% \pagestyle{fancy}
% \end{code}
% and let \pkg{fancyhdr} take care of everything.  As mentioned
% before, we get a layout that is similar to the page style
% \texttt{headings}, but it is not exactly the same. If you want to have
% the same layout as the page style \texttt{headings}, but with a line
% under the header, use (you need \pkg{fancyhdr} version 4 or later
% for this):
% \PSindex{headings}
% \begin{code}
% \usepackage[headings]{fancyhdr}
% \pagestyle{headings}
% \end{code}
%
% On the pages where new chapters start, we get a centered page number
% in the footer; there is nothing in the header, and there are no decorative lines.
%
% On an even page, we get the layout:
%
% \begin{fblock}
% \noindent\makebox[\textwidth]{\textsl{1.2  EVALUATION}\hfill
%  \textsl{CHAPTER 1. INTRODUCTION}}\showrule
% \noindent\makebox[\textwidth]{\hfill page body\hfill}\\[2\baselineskip]
% \noindent\makebox[\textwidth]{\hfill2\hfill}
% \end{fblock}
%
% On an odd page, we get the layout:
%
% \begin{fblock}
% \noindent\makebox[\textwidth]{\textsl{CHAPTER 1. INTRODUCTION}\hfill
%  \textsl{1.2  EVALUATION}}\showrule
% \noindent\makebox[\textwidth]{\hfill page body\hfill}\\[2\baselineskip]
% \noindent\makebox[\textwidth]{\hfill
% 3\hfill }
% \end{fblock}
%
% \noindent where the header text is slanted uppercase.
%
% In the \texttt{article} documentclass, we get section and subsection
% instead of chapter and section.
%
% And in a one-sided document, all pages get the same layout as the even pages
% above. It would probably have been more logical to choose the odd page
% layout, but changing that now would break some existing documents.
% Anyway, you can change the layout easily yourself.
%
% This default layout is produced by the following commands:
% \Example{5}
% \CmdIndex{rightmark}
% \CmdIndex{leftmark}
% \begin{example}
% \fancyhead[LE,RO]{\textsl{\rightmark}}
% \fancyhead[LO,RE]{\textsl{\leftmark}}
% \fancyfoot[C]{\thepage}
% \end{example}
%
% The following settings are used for the decorative lines:
%
% \begin{tabbing}
% \CmdIndex{headrulewidth}
% \noindent \cs{headrulewidth}\qquad \qquad \qquad \=0.4\=pt\\
% \CmdIndex{footrulewidth}
% \Cmd{footrulewidth}\>0\>pt
% \end{tabbing}
%
% The header text is turned into all uppercase by the standard \LaTeX{}
% code in \pkg{book.cls}.
%
% \section{An example of two-sided printing}\label{two-sided}
%
% \TTindex{twoside}
% Some document classes, such as \pkg{book.cls}, print two-sided by default: the even
% pages and the odd pages have different layouts; other document classes
% use the \texttt{twoside} option to print two-sided.
%
% Now let us print the report two-sided.  Let the above page
% layout be used for the odd (right-side) pages, and the
% following for the even (left-side) pages:
%
% \begin{fblock}
%
% \noindent\makebox[\textwidth]{\textbf{The performance of new
% graduates}\hfill}\showrule
% \noindent\makebox[\textwidth]{\hfill page body\hfill}\\[\baselineskip]
% \noindent\makebox[\textwidth]{\hrulefill}
% \noindent\makebox[\textwidth]{4\phantom{To: Dean A. Smith}\hfill
%  From: K. Grant\hfill \phantom{4}To: Dean A. Smith}
%
% \end{fblock}
% \noindent where ``4'' is the page number.
%
% Here are the commands:
%
% \Example{3}
% \begin{example}
% \fancyhead{} % clear all header fields
% \fancyhead[RO,LE]{\textbf{The performance of new graduates}}
% \fancyfoot{} % clear all footer fields
% \fancyfoot[LE,RO]{\thepage}
% \fancyfoot[LO,CE]{From: K. Grant}
% \fancyfoot[CO,RE]{To: Dean A. Smith}
% \renewcommand{\headrulewidth}{0.4pt}
% \renewcommand{\footrulewidth}{0.4pt}
% \end{example}
%
% \CmdIndex{fancyhead}
% \CmdIndex{fancyfoot}
% The commands \cs{fancyhead} and \cs{fancyfoot}
% have an additional parameter between square brackets that specifies
% for which pages and/or parts of the header/footer they apply. The first
% \cs{fancyhead} command above omits this parameter, and thus applies to all
% header fields. In general this is only useful to get rid of the defaults or
% a previous definition, as is done here. Similarly the \cs{fancyfoot} command
% without square brackets clears all footer fields. In this particular
% example it could be omitted as all footer fields have a value specified.
% The selectors that can be used
% between the square brackets are given in figure~\ref{fig:sel}. Selectors can be combined so
% \Cmd{fancyhead[LE,RO]\{text\}}
% will define the field for both the left header on even pages and the right
% header on odd pages. If you don't give an \texttt{E} or \texttt{O} the definition applies to
% both. Similar for \texttt{LRC}.
% The selectors may be given as uppercase or lowercase letters.
% \begin{figure}[htb]
%   \begin{center}
%     \leavevmode
%     \begin{tabular}{|l|l|}
%       \hline
%       E & Even page         \\
%       O & Odd page          \\
%       \hline
%       L & Left field        \\
%       C & Center field      \\
%       R & Right field       \\
%       \hline
%       H & Header            \\
%       F & Footer            \\
%       \hline
%     \end{tabular}
%   \end{center}
%   \caption{Selectors}
%   \label{fig:sel}
% \end{figure}
%
% \CmdIndex{fancyhf}
% There is also a more general command \cs{fancyhf} that you can use to
% combine the specifications for headers and footers. This allows additional
% selectors \texttt{H} (header) and \texttt{F} (footer). In fact  \cs{fancyhead} and
% \cs{fancyfoot} are just  \cs{fancyhf} with \texttt{H} and \texttt{F} pre-specified, respectively.
%
% Again, you may
% use  \Cmd{thispagestyle\{plain\}} for a simple page layout for
% page~1.
%
% \section{Specifying the widths of the header and footer fields}
% \label{sec:field-width}
%
% In \pkg{fancyhdr} version 5.0 and later you can specify the width of
% each header and footer field individually. In older versions each of
% the fields was typeset in a \cs{parbox} of width \cs{headwidth}, which
% could cause them to overlap (see e.g., section~\ref{sec:longtitles}).
%
% In \pkg{fancyhdr} version 5.0 and later, this is still the default but
% now you can override this with the commands \cs{fancyheadwidth},
% \cs{fancyfootwidth} or \cs{fancyhfwidth}. These work exactly like the
% \cs{fancyhead} etc. commands but instead of a header/footer value they
% have a \meta{length} as parameter.
%
% \begin{syntax}
%   \cmd{\fancyheadwidth}\oarg{places}\marg{length} \\
%   \cmd{\fancyfootwidth}\oarg{places}\marg{length} \\
%   \cmd{\fancyhfwidth}\oarg{places}\marg{length}
% \end{syntax}
% Field widths that are not specified default to \cs{headwidth}.
%
% NOTE: The widths will be stored as expressions, not as calculated
% values. The values will be calculated when the header or footer is
% constructed. So they can change, for example when different pages have
% different \cs{headwidth} and you use e.g., \texttt{0.3\cs{headwidth}}
% or another expression with a `variable' as value. Note, however, that
% at definition time, the width is assigned to a temporary length
% variable, to check if it is a legal \meta{length}. So any variables
% used in it should have a value, although this may be different from
% the value at its final use.
%
% The fields are typeset in a \cs{parbox} with the specified width or
% the default \cs{headwidth}. It is still possible to get overlaps if
% the sum of the width in a particular header or footer is larger than \cs{headwidth}.
%
% The fields will be positioned in a space of width \cs{headwidth} as
% follows:
% \begin{itemize}
% \item the left field will be positioned at the left edge of this space
% \item the right field will be positioned at the right edge of this space
% \item the position of the center field by default will be in the
% horizontal center of the header/footer. But it depends on the available space:\\[1ex]
%   Let $W_L$, $W_C$, and $W_R$ be the width of the left, center and
%   right field, respectively.
%   \begin{itemize}
%   \item if the total width of the three fields
%     $ \sum_{i \in \{L, C, R\}}{W_i} > \cs{headwidth}$,
%     the center field will be centered in the
%     header/footer, i.e., its midpoint will be at
%     $\frac{1}{2}\cs{headwidth}$ from each side.\\
%     NOTE: this also includes the default situation if no widths are
%     specified. This ensures that documents that don't specify widths
%     get the same output as before version 5.0.
%   \item otherwise ($ \sum_{i \in \{L, C, R\}}{W_i} \le \cs{headwidth}$):
%   \item if there would be an overlap between any of the fields, i.e.,\\
%     $ W_L +\frac{1}{2}W_C > \frac{1}{2}\cs{headwidth}$ or
%     $ W_R +\frac{1}{2}W_C > \frac{1}{2}\cs{headwidth}$, then the
%     center field will be centered between the left and right fields,
%     with equal distances to both.
%   \item otherwise (there is enough space, and no overlap), the center
%     field is centered in the header/footer, like the first case above.
%   \end{itemize}
% \end{itemize}
% Here are some examples. The header fields have a colored bar in them
% that indicates their width.
%
% In the first example, the sum of the field widths $> \cs{headwidth}$,
% so the center field will be in the center of the header, but there
% will be overlaps.
% \NoExample{testheadwidth\\(1.2)}
% \begin{example}
% \usepackage{fancyhdr}
% \pagestyle{fancy}
% \fancyhead[L]{llllll llllll llllll llllll llllll llllll llllll\\
%               \color{red}\rule {\linewidth}{4mm}}
% \fancyhead[C]{cccc cccc cc cc cccc cccc\\
%               \color{green}\rule{\linewidth}{2mm}}
% \fancyhead[R]{rrrrrr rrrrrr rrrrrr rrrrrr rrrrrr rrrrrr rrrrrr\\
%               \color{blue}\rule {\linewidth}{4mm}}
% \fancyheadwidth[L]{0.3\headwidth}
% \fancyheadwidth[C]{0.5\headwidth}
% \fancyheadwidth[R]{0.4\headwidth}
% \end{example}
% \showheader{%
% \setlength{\headheight}{40pt}
% \fancyhead[L]{llllll llllll llllll llllll llllll llllll llllll\\
%               \color{red}\rule {\linewidth}{5mm}}
% \fancyhead[C]{cccc cccc cc cc cccc cccc\\
%               \color{green}\rule{\linewidth}{3mm}}
% \fancyhead[R]{rrrrrr rrrrrr rrrrrr rrrrrr rrrrrr rrrrrr rrrrrr\\
%               \color{blue}\rule {\linewidth}{2mm}}
% \fancyheadwidth[L]{0.3\headwidth}
% \fancyheadwidth[C]{0.5\headwidth}
% \fancyheadwidth[R]{0.4\headwidth}
% }
%
% In the second example, the sum of the field widths $\le \cs{headwidth}$.
% And there is no overlap between the center field and the other ones
% (the center and right fields just touch each other), so the center
% field is still centered in the header.
% \NoExample{testheadwidth\\(1.3)}
% \begin{example}
% \fancyheadwidth[L]{0.3\headwidth}
% \fancyheadwidth[C]{0.2\headwidth}
% \fancyheadwidth[R]{0.4\headwidth}
% \end{example}
% \showheader{%
% \setlength{\headheight}{40pt}
% \fancyhead[L]{llllll llllll llllll llllll llllll llllll llllll\\
%               \color{red}\rule {\linewidth}{5mm}}
% \fancyhead[C]{cccc cccc cc cc cccc cccc\\
%               \color{green}\rule{\linewidth}{3mm}}
% \fancyhead[R]{rrrrrr rrrrrr rrrrrr rrrrrr rrrrrr rrrrrr rrrrrr\\
%               \color{blue}\rule {\linewidth}{2mm}}
% \fancyheadwidth[L]{0.3\headwidth}
% \fancyheadwidth[C]{0.2\headwidth}
% \fancyheadwidth[R]{0.4\headwidth}
% }
%
% In the last example, the sum of the field widths still is $\le \cs{headwidth}$.
% But there would be overlap between the center field and the right
% field if the center field was centered horizontally in the
% header. So now it is centered between the left and right fields.
% \NoExample{testheadwidth\\(1.4)}
% \begin{example}
% \fancyheadwidth[L]{0.3\headwidth}
% \fancyheadwidth[C]{0.25\headwidth}
% \fancyheadwidth[R]{0.4\headwidth}
% \end{example}
% \showheader{%
% \setlength{\headheight}{40pt}
% \fancyhead[L]{llllll llllll llllll llllll llllll llllll llllll\\
%               \color{red}\rule {\linewidth}{5mm}}
% \fancyhead[C]{cccc cccc cc cc cccc cccc\\
%               \color{green}\rule{\linewidth}{3mm}}
% \fancyhead[R]{rrrrrr rrrrrr rrrrrr rrrrrr rrrrrr rrrrrr rrrrrr\\
%               \color{blue}\rule {\linewidth}{2mm}}
% \fancyheadwidth[L]{0.3\headwidth}
% \fancyheadwidth[C]{0.25\headwidth}
% \fancyheadwidth[R]{0.4\headwidth}
% }
% \smallskip
%
% \textbf{NOTE:} The \cs{fancyheadwidth},
% \cs{fancyfootwidth} and \cs{fancyhfwidth} commands are still
% experimental. This means that they have not been thoroughly tested, so
% there can still be bugs in them. And the implementation could change
% in a following release. Use at your own risk, and please, report any bugs.
%
% \section{Fancy Centering}
% \label{sec:fancycenter}
%
% \textbf{Note:} This section only applies to \pkg{fancyhdr} version~4.0
% and later\footnote{This comes from the \pkg{nccfancyhdr}
% \PKGindex{nccfancyhdr}
% package by Alexander I. Rozhenko.}.
%
% The fields in a fancy header and footer are prepared using \cs{parbox}
% command. So, you can use multiline fields. In the header, they are
% aligned to the bottom line, but, in the footer, they are aligned to
% the top line. The maximum width of every field is by default equal to the
% \cs{headwidth} (unless changed by the commands \cs{fancyheadwidth},
% \cs{fancyfootwidth} or \cs{fancyhfwidth} from section~\ref{sec:field-width}.)
% This can lead to overlapping of neighbouring fields.
%
% \CmdIndex{fancycenter}
% If you want to prepare headers/footers in more traditional way in a line
% not exceeding the \cs{headwidth}, you can use
% the following command in any header/footer command:
% \begin{quote}
% \cs{fancycenter}\oarg{distance}\oarg{stretch}\\
% \verb|            |\marg{left-field}\marg{center-field}\marg{right-field}
% \end{quote}
% \CmdIndex{fancycenter}
% This command works like
% \begin{center}
% \verb|\hbox to\linewidth{|\marg{left-field}\cs{hfil}\marg{center-field}\cs{hfil}\marg{right-field}\verb|}|
% \end{center}
% but does this more carefully trying to exactly center the central part of
% the text if possible. The solution for exact centering
% is applied if the width of \meta{center-field} is less than
% \begin{quote}
% \verb|\linewidth - 2*(|\meta{stretch}\verb|*|\meta{distance}\verb| +|\\
% \verb|                max(width(|\meta{left-field}\verb|), width(|\meta{right-field}\verb|)))|.
% \end{quote}
% Otherwise the \meta{center-field} will slightly migrate to a shorter item
% (\meta{left-field} or \meta{right-field}), but at least \meta{distance}
% space between all parts of line is provided.
% The default values of \meta{distance} and \meta{stretch}
% are 1em and 3.
%
% If the \meta{center-field} is empty, the \cs{fancycenter} is equivalent to
% the following command:
% \begin{center}
% \verb|\hbox to\linewidth {|\marg{left-field}\verb|\hfil |\marg{right-field}\verb|}|
% \end{center}
%
% You would use this in a header for example with
% \begin{quote}
% \verb|\fancyhead[C]{|\cs{fancycenter}\oarg{distance}\oarg{stretch}\\
% \verb|            |\marg{left-field}\marg{center-field}\marg{right-field}\verb|}|
% \end{quote}
% and leave the \texttt{[L,R]} parts empty.
%
% \textbf{Note 1}:
% When \cs{fancycenter} is used inside a header or
% \CmdIndex{linewidth}%
% \CmdIndex{headwidth}%
% footer, \cs{linewidth} usually is the same as \cs{headwidth}. Only
% when  \cs{fancycenter} is used inside a box with a different width,
% \cs{linewidth} will be the width of that box.
%
% \textbf{Note 2}:
% If the whole of the \cs{fancycenter} is wider than
% \cs{linewidth} it will stick out on the right. See
% section~\ref{sec:longtitles} for possible solutions.
%
% \textbf{Note 3}: The usage of the \cs{fancycenter} command is not limited to the
% argument of headers/footers. You can use it anywhere in your
% document. Then \cs{linewidth} will be the width of the box or text in
% which it is used.
%
% \section{The \cs{fancyhdrbox} command}
% \label{sec:fancyhdrbox}
%
% The \cs{fancyhdrbox} command can be used to align multi-line header
% and footer fields and, for example, images. It is modeled after the
% \PKGindex{makecell}
% \pkg{makecell} package by Olga Lapko, but it is a bit simplified, and also has extra
% vertical alignments \texttt{T} and \texttt{B}. And the vertical
% centering of \cs{fancyhdrbox} is better than the one from
% \pkg{makecell}. The \cs{fancyhdrbox} command is primarily meant for
% use in headers and footers, but can be used anywhere in a document.
%
% The command is used as follows:
%
% \cs{fancyhdrbox}\oarg{alignment}\oarg{width}\marg{lines separated by \cmd{\\}}
%
% Here \meta{alignment} specifies both the vertical and the horizontal
% alignment of the contents with respect to other text on the same line
% (including other \cs{fancyhdrbox} instances). The result of the
% command is a box in horizontal mode (in \LaTeX{} parlance an \textbf{LR
% box}), similar to \cs{parbox} or \cs{makebox}.
%
% The \meta{alignment} optional parameter consists of two letters: a vertical
% alignment, which indicates where the baseline of the complete box will
% be, followed by a horizontal alignment that specifies how the lines will be
% positioned horizontally in the box.
%
% The possibilities for the vertical alignment are:
% \begin{description}
% \item [\texttt{T}] The baseline of the box is on the top of the first line, i.e., just
% where the top of the tallest character or item in the line is.
% \item [\texttt{t}] The baseline of the box is the baseline of the first line.
% \item [\texttt{c}] The baseline of the box is on the vertical center
% of the box.
% \item [\texttt{b}] The baseline of the box is the baseline of the last line.
% \item [\texttt{B}] The baseline of the box is on the bottom of the last line, i.e., just
% where the bottom of the deepest character or item in the line is.
% \end{description}
% The horizontal alignment can be
% \begin{description}
% \item[\texttt{l}] left aligned
% \item[\texttt{c}] centered
% \item[\texttt{r}] right aligned
% \end{description}
% These are the same as, for example, in \texttt{tabular} columns.
%
% Each of the vertical and horizontal alignments can be omitted. The
% default is \texttt{c} for the vertical alignment, and \texttt{l} for
% the horizontal alignment. If a single \texttt{c} is specified, it
% counts as both the vertical and horizontal alignment, i.e., as \texttt{cc}.
%
% When multiple boxes are put next to each other (i.e., on the same
% line), their baselines will be aligned. Therefore in general it makes
% not much sense to specify different vertical alignments for them,
% unless you want a special effect. And in that case the results may be surprising.
%
% The second optional parameter, \meta{width}, specifies the width of
% the box. If this is not given, the box has its ``natural'' width,
% detemined by its contents. With the \meta{width} parameter, the width
% of the box is fixed to this value, independent of the contents. Note
% that there will be no automatic line breaking of the lines if they
% don't fit in the specified width. If a line is too long it will just
% stick out of the box, and may overlap the following text. If you want
% automatic line breaking, use a \cs{parbox}, a \texttt{tabular} with a
% \texttt{p\{..\}} column, or something similar.
%
% The lines (rows) in the box are separated by \cmd{\\} just like in a
% tabular. You can even use \cmd{\\}\oarg{length} to add extra
% vertical space (or decrease the vertical space with a negative length).
% Also allowed is \cs{hline} after \cmd{\\}.
%
% Here are examples of all the vertical alignment options, with some
% variations of the horizontal alignment. Some lines use a bigger font
% than others, in order to make the alignment non-trivial. All the
% \cs{fancyhdrbox} boxes are enclosed in a tight \cs{fbox} to show how
% big they are. The red horizontal line is the common baseline.
%
% \begingroup ^^A for some local definitions
% \setlength\fboxsep{0pt}
% ^^A Vertical bar to indicate measurements
% \newcommand{\R}{\rule[-0.3\baselineskip]{0.1pt}{\baselineskip}\hspace{-0.1pt}}
% \newcommand{\showbaseline}{^^A
%   \begin{picture}(0,0)
%     \put(-1cm,0){\color{red}\rule{7cm}{0.1pt}}
%   \end{picture}^^A
% }
%
% \subsubsection*{T-aligned boxes:}
%
% \begin{code}
% \fancyhdrbox[T]{%
%    ABC \\
%    xyz \\ XYZ \\
%   \Huge DEF ghij
% }%
% \fancyhdrbox[T]{%
%   {\Huge ABC} \\
%   DEF ghij}
% \end{code}
%
% \fbox{\showbaseline\fancyhdrbox[T]{^^A
%    ABC \\
%    xyz \\ XYZ \\
%   \Huge DEF ghij
% }^^A
% \fancyhdrbox[T]{{\Huge ABC} \\
%   DEF ghij}}
%
% \subsubsection*{t-aligned boxes:}
%
% This example also uses right-alignment in the boxes, but the first
% line in the left box has a 1cm space added to the right, so it is
% shifted left 1cm.
%
% \begin{code}
% \fancyhdrbox[tr]{%
%    ABC\hspace{1cm} \\
%    xyz \\ XYZ \\
%   \Huge DEF ghij
% }%
% \fancyhdrbox[tr]{%
%   {\Huge ABC} \\
%   DEF ghij
% }
% \end{code}
%
% \fbox{\showbaseline\fancyhdrbox[tr]{^^A
%    ABC\hspace{1cm} \\
%    xyz \\ XYZ \\
%   \Huge DEF ghij
% }^^A
% \fancyhdrbox[tr]{^^A
%   {\Huge ABC} \\
%   DEF ghij
% }}
%
% \subsubsection*{b-aligned boxes:}
%
% \begin{code}
% \fancyhdrbox[b]{%
%    ABC \\
%    xyz \\ XYZ \\
%    \Huge DEF ghij
% }%
% \fancyhdrbox[b]{%
%   {\Huge ABC} \\
%   DEF ghij}
% \end{code}
%
% \fbox{\showbaseline\fancyhdrbox[b]{^^A
%    ABC \\
%    xyz \\ XYZ \\
%    \Huge DEF ghij
% }^^A
% \fancyhdrbox[b]{^^A
%   {\Huge ABC} \\
%   DEF ghij}}
%
% \subsubsection*{B-aligned boxes:}
%
% \begin{code}
% \fancyhdrbox[B]{%
%    ABC \\
%    xyz \\ XYZ \\
%   \Huge DEF ghij
% }%
% \fancyhdrbox[B]{%
%   {\Huge ABC} \\
%   DEF ghij}
% \end{code}
%
% \fbox{\showbaseline\fancyhdrbox[B]{^^A
%    ABC \\
%    xyz \\ XYZ \\
%   \Huge DEF ghij
% }^^A
% \fancyhdrbox[B]{^^A
%   {\Huge ABC} \\
%   DEF ghij}}
%
% \subsubsection*{c-aligned boxes:}
%
% The first box has an explicit \texttt{[c]} positioning, which implies
% both vertical and horizontal centering. The second one uses the
% default positioning (i.e., it is not explicitely specified), which make
% it \texttt{[cl]}, i.e., horizontally left aligned.
%
% \begin{code}
% \fancyhdrbox[c]{%
%    ABC \\
%    xyz \\ XYZ \\
%   \Huge DEF ghij
% }%
% \fancyhdrbox{%
%   {\Huge ABC} \\
%   DEF ghij}
% \end{code}
%
% \fbox{\showbaseline\fancyhdrbox[c]{^^A
%    ABC \\
%    xyz \\ XYZ \\
%   \Huge DEF ghij
% }^^A
% \fancyhdrbox{^^A
%   {\Huge ABC} \\
%   DEF ghij}}
%
% \subsubsection*{c-aligned with \meta{width}:}
%
% This example shows the use of the second optional argument of
% \cs{fancyhdrbox}, the width of the box.
%
% \begin{code}
% \fancyhdrbox[c][5cm]{%
%   ABC \\ xyz \\ XYZ\texttt{\textbackslash\textbackslash[10pt]} \\[10pt]
%   \Huge DEF ghij%
% }%
% \fancyhdrbox[c][3cm]{%
%   {\Huge ABC}\\
%   DEF ghij}
% \end{code}
%
% \noindent\R\makebox[5cm]{5cm}\R\makebox[3cm]{3cm}\R\\[2pt]
% \fbox{\showbaseline\fancyhdrbox[c][5cm]{^^A
%   ABC \\ xyz \\ XYZ\texttt{\textbackslash\textbackslash[10pt]} \\[10pt]
%   {\Huge DEF ghij}^^A
% }^^A
% \fancyhdrbox[c][3cm]{^^A
%   {\Huge ABC}\\
%   DEF ghij}}
%
% \subsubsection*{Different vertical alignments}
%
% Here is an example with two different vertical alignments in boxes
% next to each other, one with the \texttt{[b]} alignment and the other
% one with \texttt{[t]}.
%
% \begin{code}
%   \fbox{\showbaseline\fancyhdrbox[b]{%
%      first line \\
%      second line [b]
%   }}
%   baseline
%   \fbox{\fancyhdrbox[t]{%
%     first line [t] \\
%     second line}}
% \end{code}
%
% \fbox{\showbaseline\fancyhdrbox[b]{^^A
%    first line \\
%    second line [b]
% }}
% baseline
% \fbox{\fancyhdrbox[t]{^^A
%   first line [t] \\
%   second line}}
%
% \endgroup
%
% \bigskip
%
% It may be surprising that the \texttt{[b]} box is on top and the
% \texttt{[t]} box on the bottom of the total line, but if you look at
% the baselines, it should become clear why this is so. This is just the
% way vertical alignment works in \LaTeX. So if that is what you want,
% just use it.
%
% \subsubsection*{Two headers with \cs{fancyhdrbox} parts}
% Finally, two headers with \cs{fancyhdrbox} parts. Note that these are
% in different header fields (left and right).
%
% \NoExample{image+\\twolineheader}
% \begin{example}
% \setlength{\headheight}{68pt}
% \pagestyle{fancy}
% \fancyhf{}
% \rhead{\fancyhdrbox{\Large First Long Title \\ \large Second title}}
% \lhead{\fancyhdrbox{\includegraphics[width=3cm]{example-image}}}
% \end{example}
%
% \bigskip
%
% \showheader{%
%   \setlength{\headheight}{68pt}
%   \pagestyle{fancy}
%   \fancyhf{}
%   \rhead{\fancyhdrbox{\Large First Long Title \\ \large Second title}}
%   \lhead{\fancyhdrbox{\includegraphics[width=3cm]{example-image}}}
% }
%
% In the next example the left header has an image in a \cs{fancyhdrbox}
% with the default alignment. The right header has two
% \cs{fancyhdrbox}|es, one with an explicit width of 4cm, the second
% one with its natural width.
%
% \NoExample{threeboxes}
% \begin{example}
%   \setlength{\headheight}{20pt}
%   \pagestyle{fancy}
%   \fancyhf{}
%   \fancyhead[L]{\sffamily
%     \fancyhdrbox{\includegraphics[height=3\normalbaselineskip]
%                                  {example-image}}
%   }
%   \fancyhead[R]{%
%     \fancyhdrbox[][4cm]{Our Office \\ Street 1 \\ City1 }%
%     \fancyhdrbox[l]{ Our Factory \\ Street 2 \\ City2 }
%   }
% \end{example}
%
% \bigskip
%
% \showheader{%
%   \setlength{\headheight}{40pt}
%   \pagestyle{fancy}
%   \fancyhf{}
%   \fancyhead[L]{\sffamily
%     \fancyhdrbox{\includegraphics[height=3\normalbaselineskip]
%                                  {example-image}}
%   }
%   \fancyhead[R]{%
%     \fancyhdrbox[][4cm]{Our Office \\ Street 1 \\ City1 }%
%     \fancyhdrbox[l]{ Our Factory \\ Street 2 \\ City2 }
%   }
% }
%
% \bigskip
%
% \section{Redefining page style \texttt{plain}}
% \label{sec:pagestyle-plain}
%
% Some \LaTeX{} commands, like \cs{chapter}, use the \cs{thispagestyle}
% command to automatically switch to the \texttt{plain} page style, thus
% ignoring the page style currently in effect.
%
% They do this by issuing a \Cmd{thispagestyle\{plain\}} command. The most
% well-known places where this could happen are:
% \begin{itemize}
% \item The first pages of chapters in the \texttt{book} and \texttt{report} class
% \item The first page of a document in the \texttt{article} class when \cs{maketitle} is used
% \item The first page of an index
% \end{itemize}
% but it could happen at other places depending on the class and the packages used.
%
% To customize even such pages you must redefine the \texttt{plain}
% page style. As we indicated before you could do this by defining the
% \cs{ps@plain} command, but \pkg{fancyhdr} gives you an easier way
% \CmdIndex{fancypagestyle}
% with the \cs{fancypagestyle} command. This command can be used to
% redefine existing page styles (like \texttt{plain}) or to define new ones,
% e.g., if part of your document needs a different page style. This command
% has two mandatory parameters and an optional one in between: the first parameter
% is the name of the page style to be defined, then an optional
% parameter of an existing base page style can be given, and the last
% parameter consists of commands that change the headers and/or footers, i.e.,
% \verb|\fancyhead| etc. Also allowed are changes to \cs{headrulewidth} and
% \cs{footrulewidth} or even \cs{headrule} and \cs{footrule}.
% The (re)defined page style uses the standard
% \texttt{fancy} definitions, amended by the optional base style, and
% finally the definitions in the last
% parameter. For details see \smartref{sec:pagestyle-plain}{sec:fancypagestyle}.
% In particular, if the last parameter is empty, i.e., given
% as \verb+{}+, then the new page style is equal to base style.
%
% As an example, let us redefine the \texttt{plain} style so that it will
% be the same as the standard page style \texttt{fancy}:
% \begin{code}
% \fancypagestyle{plain}[fancy]{}
% \end{code}
% If you have not redefined page style \texttt{fancy} with \cs{fancypagestyle},
% this is equivalent to:
% \begin{code}
% \fancypagestyle{plain}{}
% \end{code}
% Now when these special pages use the \texttt{plain} page style, they use your redefined version.
%
% As another example, let us
% redefine the \texttt{plain} style for the report in section~\ref{two-sided} by
% making the page number bold and enclosing it in en-dashes without any rules.
% \PSindex{plain}
%
% \Example{4}
% \begin{example}
% \fancypagestyle{plain}{%
%   \fancyhf{}% clear all header and footer fields
%   \fancyfoot[C]{\textbf{--~\thepage~--}} % except the center
%   \renewcommand{\headrulewidth}{0pt}%
%   \renewcommand{\footrulewidth}{0pt}%
% }
% \end{example}
%
%
% \section{Defining other page styles}
% \label{sec:fancypagestyle}
%
% Just like redefining the \texttt{plain} page style in the previous
% section, you can define or redefine other page styles based on page
% style \texttt{fancy}. This is also done with the \cs{fancypagestyle}
% command.
%  With \texttt{*} it defines a ``\emph{closed}'' page style, otherwise an
% ``\emph{open}'' one. The difference is that the \emph{open} page style
% does not necessarily have all the information in itself that is
% necessary to construct the headers and footers. So it will need to pick up the
% remaining elements from the environment of the text.
% The \emph{closed} page style, however, will pick up all necessary
% elements from the environment at the moment it is \textbf{defined},
% rather than when it is used, and carries that with it.
% The information that is picked up consists of:
% \begin{itemize}
% \item The header and footer fields in all variants (EO,LRC,HF) (12 items)
% \item The header and footer field widths in all variants (EO,LRC,HF) (12 items)
% \item The header and footer offsets (EO,LR,HF) (8 items), see
% section~\ref{sec:book-examples} and~\ref{sec:headwidth}
% \item The header and footer init values (2 items), see section~\ref{sec:curpagestyle}
% \item \cs{headrule}, \cs{headrulewidth}, \cs{footrule}, \cs{footrulewidth} (4 items)
% \item the \texttt{[nocheck]} option
% \end{itemize}
% The \emph{closed} versions can come handy when you are switching back
% and forth between different page styles, as explained in section~\ref{sec:change}.
%
% Here is an example of a simple (\emph{open}) definition:
% \begin{code}
% \fancypagestyle{toc}{%
%   \fancyhf{}%
%   \fancyhead[RO]{\thepage}%
%   \fancyhead[RO]{\textsl{TABLE OF CONTENTS}}%
%   \fancyfoot[C]{\thepage}
% }
% \end{code}
% This defines a special page style \texttt{toc} for use in the table
% of contents with \Cmd{pagestyle\{toc\}}.
% Inside the definition you can define the headers and/or footers,
% change the header and footer rules, and redefine commands like
% \cs{chaptermark} (see section~\ref{sec:scoop} for an example). The
% headers and footers and marks that are not redefined inside the
% \cs{fancypagestyle} definition, are taken from the global fancy page style
% values.
%
% The general form of the command is:
% \begin{quote}
% \CmdIndex{fancypagestyle}
% \verb|\fancypagestyle|\marg{style-name}\oarg{base-style}\marg{definitions}
% \verb|\fancypagestyle*|\marg{style-name}\oarg{base-style}\marg{definitions}
% \end{quote}
% As you see, there is an optional \oarg{base-style} argument between the two mandatory arguments.
%
% If you give this optional base page style to the
% \cs{fancypagestyle} command, then the new page style will be based on
% that base style. This base style must be a \texttt{fancyhdr}-defined
% style. Also you should take care not to
% create circular dependencies.
% When no base style is given, an internal base style, which has the
% default values is used. This is the same as page style
% \texttt{fancy}, unless the latter has been redefined.
% The order of picking up the
% definitions (headers, footers, marks, etc.) is:
% \begin{enumerate}
% \item The definitions from the base style are taken.
% \item The definitions given in the \cs{fancypagestyle} command
% override and/or augment these.
% \item Any definitions that are not given by the two rules above, are taken
% from the environment, for an \emph{open} page style at the time the
% new page style is used, for a \emph{closed} page style at the time it
% is defined.
% \end{enumerate}
% In an \emph{open} page style, i.e., if you use the  form \cs{fancypagestyle}\oarg{base-style}\marg{style-name}\ldots\ {}
% only the first two parts are embedded in the page style.
%
% The optional base style argument is only available in \pkg{fancyhdr} version~4.0 and later.
% In these versions it is also possible to redefine page style \texttt{fancy}
% in this way. In version~3.x and earlier this was not possible.
% The starred (\emph{closed}) version is only available in
% \pkg{fancyhdr} version~5.0 and later.
%
% \subsubsection*{The page style \texttt{fancydefault}}
% \label{sec:fancydefault}
%
% If you want to restore the original default definitions
% from page style \texttt{fancy}  as described in section~\ref{sec:default}, you can use
% \begin{code}
% \fancypagestyle{myfancy}[fancydefault]{
%   . . . override some here
% }
% \end{code}
% \PSindex{fancydefault}
% Page style \texttt{fancydefault} is the version of page style
% \texttt{fancy} that has all the initialisation embedded, including the relevant
% definitions of \cs{chaptermark} and \Cmd{[sub]sectionmark}. Contrary to
% this, page style \texttt{fancy} as defined in the package uses the
% same defaults, but doesn't have them embedded. It picks them up from
% the environment. So if the environment changes, because you redefine
% headers, footers, mark commands, etc, the functioning of page style
% \texttt{fancy} changes with it. The page style \texttt{fancydefault}
% does not change, however. It is in fact the \emph{closed} version of
% page style \texttt{fancy}, defined with
% \Cmd{fancypagestyle*\{fancydefault\}}\marg{initialisation code} just after
% \pkg{fancyhdr}'s initialisation.  However, \texttt{fancydefault}
% is only available since \pkg{fancyhdr} version~4.0.
%
% If you don't like the defaults, you can redefine it yourself. For
% example if you don't want to include the \Cmd{...mark} definitions,
% just put \Cmd{fancypagestyle*\{fancydefault\}\{\}} after
% \Cmd{usepackage\{fancyhdr\}}. Or if you want to include your own
% header and/or footer definitions, use
% \Cmd{fancypagestyle*\{fancydefault\}}\marg{your definitions}.
%
%\subsection{The \cs{fancypagestyleassign} command}
% \label{sec:fancypagestyleassign}
%
% The \cs{fancypagestyleassign} command is only available in
% \pkg{fancyhdr} version 5.0 and later.
% \begin{raggedright}
% The command \cs{fancypagestyleassign}\marg{ps1}\marg{ps2} makes
% page style \meta{ps1} an exact copy of page style \meta{ps1}.
% The effect is similar to the command
% \cs{fancypagestyle}\marg{ps1}\oarg{ps2}\texttt{\{\}}, but there are
% important differences:
% \begin{itemize}
% \item with \cs{fancypagestyleassign}\marg{ps1}\marg{ps2} the page
% style \meta{ps1} will be completely independent from \meta{ps2}. On
% the other hand, with
% \cs{fancypagestyle}\marg{ps1}\oarg{ps2}\texttt{\{\}} the page
% style \meta{ps1} will depend on \meta{ps2}. If
% \meta{ps2} later changes (for example with a redefinition with
% \cs{fancypagestyle}), the page style \meta{ps1} will change accordingly.
% \item with \cs{fancypagestyle}\marg{ps1}\oarg{ps2}\texttt{\{\}} you
% must take care that you don't get cyclical dependencies, whereas with
% \cs{fancypagestyleassign}\ you can't create cyclical dependencies.
% \item with \cs{fancypagestyle}\marg{ps1}\oarg{ps2}\texttt{\{\}} the
% page style \meta{ps2} must be a page style that is defined by
% \pkg{fancyhdr} (with \cs{fancypagestyle} or predefined), but
% \cs{fancypagestyleassign}\marg{ps1}\marg{ps2} can also be used if
% \meta{ps2} is not defined by \pkg{fancyhdr}, for example a standard
% \LaTeX{} page style like \texttt{plain}.
% \end{itemize}
% If \meta{ps2} is defined by \pkg{fancyhdr}, then also \meta{ps1} is
% considered to be defined by \pkg{fancyhdr}. If \meta{ps2} is a
% \emph{closed} page style, then \meta{ps1} is also \emph{closed}.
% \end{raggedright}
%
% \cs{fancypagestyleassign} comes especially handy if you want to
% temporaryly redefine a page style, and later to restore it to its
% original value. For example, if we have a page style
% \texttt{special}, and we want temporarily to define page style
% \texttt{plain} to be equal to this, but later to restore it to its
% original definition, you can do this as follows:
% \begin{code}
% \fancypagestyleassign{origplain}{plain}
% \fancypagestyleassign{plain}{special}
% . . .  code where plain is equal to special
% \clearpage
% \fancypagestyleassign{plain}{origplain}
% . . . code where plain has its original meaning
% \end{code}
% Note that you couldn't do this with \cs{fancypagestyle} because (1)
% this would introduce a cyclical dependency, (2) you cannot use
% \texttt{plain} as the base page style, because it is not \pkg{fancyhdr}-based.
%
% See section~\ref{sec:tocstyle} for an example.
%
% \section{The scoop on \LaTeX's marks}
% \label{sec:scoop}
%
% Usually, for documents of class \verb|book| and \verb|report|, you may
% want to use chapter and section information in the headings (chapter only
% for one-sided printing), and for documents of class \verb|article|,
% section and subsection information (section only for one-sided
% printing).  \LaTeX{} uses a marker mechanism to remember the chapter and
% section (section and subsection) information for a page; this is
% discussed in detail in \emph{The \LaTeX{} Companion, Third Edition},
% section 5.3.4 (Part I).
%
% There are two ways you can use and change the higher- and lower-level
% sectioning information available to you.  The macros:
% \CmdIndex{rightmark}
% \CmdIndex{leftmark}
% \cs{leftmark}
% (higher-level) and \cs{rightmark} (lower-level) contain the information
% processed by \LaTeX, and you can use them directly as shown in
% section~\ref{sec:default}.
%
% These marks are set by the commands
% \cs{markboth}\marg{leftmark}\marg{rightmark} and \cs{markright}\marg{rightmark}.
% These commands are usually used inside commands like \cs{chaptermark}
% and \cs{sectionmark} but they can be also be given directly in your
% document, although this not very usual.
%
% The \cs{leftmark} contains the \textbf{L}eft argument of the \emph{\textbf{L}ast}
% \cs{markboth} on the page, the \cs{rightmark} contains the \textbf{R}ight
% argument of the \emph{fi\textbf{R}st} \cs{markboth} or the only argument of the
% \emph{fi\textbf{R}st} \cs{markright} on the page. If no marks are present on a
% page they are ``inherited'' from the previous page.
%
% You can influence how chapter, section, and subsection
% information (only two of them!) is displayed by redefining the
% \CmdIndex{chaptermark}
% \cs{chaptermark},
% \CmdIndex{sectionmark}
% \CmdIndex{subsectionmark}
% \cs{sectionmark}, and \cs{subsectionmark} commands\footnote{There are
%   similar commands for \texttt{paragraph} and \texttt{subparagraph} but
%   they are seldom used.}. You must put the redefinition
% after the first call of \Cmd{pagestyle\{fancy\}} as this
% sets up the defaults.
%
% Let us illustrate this with chapter info. It is made up of three parts:
% \begin{itemize}
% \CmdIndex{thechapter}
% \item  the number (say, 2), displayed by the macro \cs{thechapter}
% \item  the name (in English, Chapter), displayed by the macro
% \CmdIndex{chaptername}
%    \cs{chaptername}
% \item  the title, contained in the  argument of
%    \cs{chapter}.
% \end{itemize}
% We combine these below with \cs{markboth} in \cs{chaptermark}.
%
% For the lower-level sectioning information, we do the same with
% \cs{markright} in \cs{sectionmark}.
%
% So if ``2. Implementation'' is the current chapter
% and ``2.1.\ First steps'' is the current section, then
%
% \Example{6}
% \begin{example}
% \renewcommand{\chaptermark}[1]{%
%   \markboth{\chaptername\ \thechapter.\ #1}{}}
% \renewcommand{\sectionmark}[1]{\markright{\thesection.\ #1}}
% \end{example}
% will give
%    ``Chapter 2. Implementation'' and ``2.1.\ First steps''
%
% Redefining the \cs{chaptermark} and \cs{sectionmark} commands may
% not eliminate all uppercaseness. E.g., the bibliography will have a title
% \CmdIndex{uppercase}
% \index{BIBLIOGRAPHY}
% of \textsc{bibliography} in the header, as the \cs{MakeUppercase} is
% explicitly given in the definition of \cs{thebibliography}. Similar for
% \index{INDEX}
% \textsc{index} etc.\ If you don't want to redefine these commands, you can
% use the \cs{nouppercase} command that \pkg{fancyhdr} makes available in the header
% and footer fields. Note that this may screw other things, like uppercase
% roman numerals in your headers, so it should be used with care. Essentially
% this command typesets its argument in an environment where
% \cs{MakeUppercase} and \cs{uppercase} are changed into identity operations.
%
% \Example{7}
% \begin{example}
% \fancyhead[L]{\nouppercase{\rightmark}}
% \fancyhead[R]{\nouppercase{\leftmark}}
% \end{example}
%
% Figure~\ref{fig:markers} shows some variants for ``Chapter 2.\ Implementation''
% (the last example is appropriate in some non-English languages). The \texttt{\%}
% signs at the end of the lines are to prevent unwanted space. Normally you
% would continue the lines and remove these \texttt{\%} signs\footnote{The \texttt{\bs
%     MakeUppercase} command is used in \LaTeX{} to generate uppercase text,
%   while \cs{uppercase} is the plain \TeX{} command for this. The difference is
%   that \cs{MakeUppercase} also deals with non-ASCII letters.}.
%
% \begin{figure}[tb]
% \CmdIndex{chaptermark}
% \CmdIndex{uppercase}
% \CmdIndex{MakeUppercase}
% \setlength{\columnsep}{20pt}\small
% \begin{multicols}{2}
% \noindent Code:\\
% \mbox{}\\
% \FExample{8}^^A
% \verb|\renewcommand{\chaptermark}[1]{%|\\
% \verb| \markboth{\chaptername|\\
% \verb| \ \thechapter.\ #1}{}}|\\
% \mbox{}\\
% \FExample{9}^^A
% \verb|\renewcommand{\chaptermark}[1]{%|\\
% \verb| \markboth{\MakeUppercase{%|\\
% \verb| \chaptername}\ \thechapter.%|\\
% \verb| \ #1}{}}|\\
% \mbox{}\\
% \FExample{10}^^A
% \verb|\renewcommand{\chaptermark}[1]{%|\\
% \verb| \markboth{\MakeUppercase{%|\\
% \verb| \chaptername\ \thechapter.%|\\
% \verb| \ #1}}{}}|\\
% \mbox{}\\
% \FExample{11}^^A
% \verb|\renewcommand{\chaptermark}[1]{%|\\
% \verb| \markboth{#1}{}}|\\
% \mbox{}\\
% \FExample{12}^^A
% \verb|\renewcommand{\chaptermark}[1]{%|\\
% \verb| \markboth{\thechapter.\ #1}{}}|\\
% \mbox{}\\
% \FExample{13}^^A
% \verb|\renewcommand{\chaptermark}[1]{%|\\
% \verb| \markboth{\thechapter.%|\\
% \verb| \ \chaptername.\ #1}{}}|\\
% Prints:\\
% \mbox{}\\
% Chapter 2.\ Implementation\\
% \mbox{}\\
% \mbox{}\\
% \mbox{}\\
% CHAPTER 2.\ Implementation\\
% \mbox{}\\
% \mbox{}\\
% \mbox{}\\
% \mbox{}\\
% CHAPTER 2.\ IMPLEMENTATION\\
% \mbox{}\\
% \mbox{}\\
% \mbox{}\\
% \mbox{}\\
% Implementation\\
% \mbox{}\\
% \mbox{}\\
% 2.\ Implementation\\
% \mbox{}\\
% \mbox{}\\
% 2.\ Chapter.\ Implementation\\
% \mbox{}\\
% \end{multicols}
% \caption{Marker variants}\label{fig:markers}
% \end{figure}
%
% It should be noted that the \LaTeX{} marking mechanism works fine with
% chapters (which always start on a new page) and sections (which are
% reasonably long).  It does not work quite as well with short sections and
% subsections.  This is a problem with \LaTeX, not with
% \pkg{fancyhdr}.
%
% As an example let's take a page layout where the leftmarks are generated by
% the sections and the rightmarks by the subsections (as is default in the
% \texttt{article} class). Take a page with some short sections, e.g.,
%
% \begin{samepage}
% \noindent Section 1.\\
% subsection 1.1\\
% subsection 1.2\\
% Section 2.
% \end{samepage}
%
% As the leftmark contains the \emph{last} mark of the page it will be
% ``Section 2.'', and the rightmark will be ``subsection 1.1'' as it will be
% the \emph{first} mark of the page. So the page header info will combine
% section 2 with subsection 1.1 which isn't very nice.
% One thing you can do in these cases is use only the \cs{rightmark}s and
% redefine \cs{sectionmark} accordingly.
%
% However, the \pkg{extramarks} package described in section~\ref{sec:xmarks}
% contains a command
% \CmdIndex{firstleftmark}
% \cs{firstleftmark} that can be used to get the first of the leftmarks
% on the page in the header. This might be the best solution in this
% situation. Now the header will contain ``Section 1.'' in the situation
% described above.
%
% \Example{14}
% \begin{example}
% \usepackage{extramarks}
%  . . .
% \fancyhead[R]{\firstleftmark}
% \end{example}
%
% Another problem with the marks in the standard \LaTeX{} classes is that the
% higher level sectioning commands (e.g., \cs{chapter}) call
% \cs{markboth} with an empty right argument. This means that on the first
% page of a chapter (or a section in article style) the \cs{rightmark}
% will be empty. The underlying problem is that the original \TeX{} machinery had
% only one \cs{mark}. All the marks had to be packed together in
% this one. So there were no independent left or right marks.
% Modern \LaTeX{} distributions, however, do have independent marks, so
% this problem can be solved. See Example~35 in
% section~\ref{sec:firstsubsection} for an example.
%
% \section{Headers for unnumbered chapters, sections, etc.}
% \label{sec:unnumbered}
%
% In the standard \LaTeX{} documentclasses the \verb|*| forms of the
% \cs{chapter} etc.\ commands do \emph{not} call the mark commands. So
% these don't appear in the header. Neither are they put in the Table of
% Contents.
% So, for example, if you want your Preface to set the header info but
% not be numbered, you must issue the \cs{markboth} command yourself,
% e.g.,
%
% \begin{code}
% \chapter*{Preface}
% \markboth{Preface}{}
% \end{code}
%
% Or for a section:
% \begin{code}
% \section*{Preface}
% \markboth{Preface}{}
% \end{code}
%
% It can be a bit annoying to have to repeat the title. If you don't
% want that, it is possible to redefine the \cs{chapter} and/or
% \cs{section} command, in such a way that the \texttt{*} version
% \emph{does} set the header info. For a chapter this is usually done
% with the \cs{markboth} command. For a section in a chapter-oriented
% documentclass with \cs{markright}, otherwise also with \cs{markboth}.
%
% Here is a definition that accomplishes this.
% Redefine the \cs{chapter} command:\\
% \cs{chapter}\oarg{header}\marg{title}\\
% \cs{chapter*}\marg{title}\\
% For the \cs{chapter*} version, we insert a \cs{markboth} command.
% For the non-\texttt{*} version we just pass the arguments to the
% original \cs{chapter} command.
%
% We use the \cs{RenewDocumentCommand} to redefine the \cs{chapter}
% command because it allows us to redefine also the \texttt{*} variant,
% which is much more difficult with \cs{renewcommand}\footnote{If you
% have an older \LaTeX{} distribution that doesn't have the
% \cs{RenewDocumentCommand}, include \PKGindex{xparse}\Cmd{usepackage\{xparse\}} in your
% preamble. Or better: update your \LaTeX{} installation.}.
%
% The \verb|{som}| in the definition defines the arguments of the
% \cs{chapter} command:
% \begin{enumerate}
% \item \texttt{s} - a \texttt{*} which can be present or absent. This
% is checked with \Cmd{IfBooleanTF\{\#1\}}
% \item \texttt{o} - an optional argument. the presence of the optional
% argument is checked with \Cmd{IfNoValueTF\{\#2\}}
% \item \texttt{m} - a mandatory argument
% \end{enumerate}
% We first save the original definition of \cs{chapter} in
% \cs{originalchapter} with the \cs{let} statement. The
% \Cmd{newcommand\cs{originalchapter}\{\}} is just a precaution to get an error message
% if \cs{originalchapter} was already defined, for example by
% another package.
%
% \NoExample{unnumbered}
% \begin{example}
% \newcommand\originalchapter{}% check that we can define this name
% \let\originalchapter\chapter
% \RenewDocumentCommand \chapter {som}{%
%   \IfBooleanTF{#1}
%     {% \chapter*
%       \originalchapter*{#3}%
%       \markboth{#3}{}%
%       % we can also put it in the Table of Contents
%       \addcontentsline{toc}{chapter}{#3}
%     }%
%     {% normal \chapter
%       \IfNoValueTF{#2}
%         {\originalchapter{#3}}
%         {\originalchapter[#2]{#3}}%
%     }%
% }
% \end{example}
% We can do the same for the \cs{section} command, but we use
% \cs{markright} instead of \cs{markboth}. Note that the \cs{mark..}
% commands are called \textbf{after} the original command, because the
% \cs{chapter} command begins with a page break, and a \cs{section}
% could have a page break before it, but not after it.
%
% \textbf{NOTE:} We don't use \cs{chaptermark} or \cs{sectionmark} here
% because these often include the chapter/section number, which doesn't
% make sense for an unnumbered one.
%
% \NoExample{unnumbered}
% \begin{example}
% \newcommand\originalsection{}% check that we can define this name
% \let\originalsection\section
% \RenewDocumentCommand \section {som}{%
%   \IfBooleanTF{#1}
%     {% \section*
%       \originalsection*{#3}%
%       \markright{#3}%
%       % we can also put it in the Table of Contents
%       \addcontentsline{toc}{section}{#3}
%     }%
%     {% normal \section
%       \IfNoValueTF{#2}
%         {\originalsection{#3}}
%         {\originalsection[#2]{#3}}%
%     }%
% }
% \end{example}
%
% Please note that, contrary to the original \LaTeX{} commands,
% these new command do accept an optional argument with
% the \texttt{*} version, but if it is given, they don't use it. It is
% not difficult to add additional code to process this optional argument
% similar to the non-\texttt{*} case.
% This is left as an exercise for the reader, or look at the example
% files \texttt{unnumberedart1.tex} and \texttt{unnumberedart2.tex}.
%
% \section{Dictionary style headers}
%
% \index{dictionary}
% \index{concordance}
% Dictionaries and concordances usually have a header containing the first
% word defined on the page or both the first and the last words. This can
% easily be accomplished with \pkg{fancyhdr} and \LaTeX's
% \texttt{mark} mechanism. Of course if you use the marks for dictionary
% style headers, you cannot use them for chapter and section information, so
% if there are also chapters and sections present, you must redefine the
% \cs{chaptermark} and \cs{sectionmark} to make them harmless:
%
% \begin{code}
% \renewcommand{\chaptermark}[1]{}
% \renewcommand{\sectionmark}[1]{}
% \end{code}
%
% Now you do a \Cmd{markboth\{\#1\}\{\#1\}} for each dictionary or concordance
% entry \verb|#1| and use \cs{rightmark} for the first entry defined on
% the page and \cs{leftmark} for the last one.
%
% If you want to use a header entry of the form \textsf{firstword--lastword}
% it would be nice if this would be reduced to just the form
% \textsf{firstword} if both are the same. This could happen if there is just
% one entry on the page. In this case a test must be made to check if the
% marks are the same.  However, \TeX's marks are strange beasts, which
% cannot be compared out of the box with the plain \TeX{} \Cmd{if} commands.
% Fortunately the \PKGindex{ifthen}\pkg{ifthen} package works well:
%
% \Example{15}
% \begin{example}
% \newcommand{\mymarks}{
%   \ifthenelse{\equal{\leftmark}{\rightmark}}
%     {\rightmark} % if equal
%     {\rightmark--\leftmark}} % if not equal
% \fancyhead[LE,RO]{\mymarks}
% \fancyhead[LO,RE]{\thepage}
% \end{example}
%
% \section{Fancy layouts}
% \label{sec:fancy}\label{sec:warning}
%
% \index{multi-line}
% You can make a multi-line field with the \bsbs{} command.  It is also
% possible to put extra space in a field with the \cs{vspace} command.
% Note that if you do this you will probably have to increase the height of
% \CmdIndex{headheight}
% \CmdIndex{footskip}
% the header (\cs{headheight}) and/or of the footer (\cs{footskip}),
% \index{Overfull \verb+\vbox+ \ldots}
% otherwise you may get error messages ``Overfull \cs{vbox} \ldots has
% occurred while \cs{output} is active''^^A
% \footnote{If you use \texttt{11pt} or \texttt{12pt} you will probably also
%   have to do this, because \LaTeX's defaults are quite small}. See the
% warning below.
% See also section 5.1 and 5.2 of the \LaTeX{} \emph{Companion, Third Edition}, (Part I)
% for detail.
%
% For instance, the following code will place  the section title and the
% subsection title of an article in two lines in the upper right hand
% corner:
%
% \Example{16}
% \begin{example}
% \documentclass{article}
% \usepackage{fancyhdr}
% \pagestyle{fancy}
% \addtolength{\headheight}{\baselineskip}
% \renewcommand{\sectionmark}[1]{\markboth{#1}{}}
% \renewcommand{\subsectionmark}[1]{\markright{#1}}
% \fancyhead[R]{\leftmark\\\rightmark}
% \end{example}
%
% Note that if you want to use header or footer layouts with multiline
% parts that have to be aligned, you can do this with
% the \cs{fancyhdrbox} command. See section~\ref{sec:fancyhdrbox}.
%
% You can also customize the decorative lines.  You can make the decorative
% line in the header quite thick with
%
% \CmdIndex{headrulewidth}
% \begin{code}
% \renewcommand{\headrulewidth}{0.6pt}
% \end{code}
% or you can make the decorative line in the footer disappear with
% \CmdIndex{footrulewidth}
% \begin{code}
% \renewcommand{\footrulewidth}{0pt}
% \end{code}
%
% The decorative lines, themselves, are defined in the two macros
% \cs{headrule} and \cs{footrule}.  For instance,
% if you want a dotted line rather than a solid line in the header,
% redefine the command \cs{headrule}:
%
% \begin{code}
% \renewcommand{\headrule}{\vbox to 0pt
%     {\makebox[\headwidth]{\dotfill}\vss}}
% \end{code}
%
% The redefined \cs{headrule} should preferably take up no vertical space, as in the example above, and as in the standard definition. If it does take vertical space, the header may come too close to the text, or even intrude in the text. In that case \pkg{fancyhdr} will give you a warning that \cs{headheight} is too small. Like
% \begin{code}
% Package fancyhdr Warning: \headheight is too small (12.0pt):
% (fancyhdr)                Make it at least 14.0pt, for example:
% (fancyhdr)                \setlength{\headheight}{14.0pt}.
% (fancyhdr)                You might also make \topmargin smaller:
% (fancyhdr)                \addtolength{\topmargin}{-2.0pt}.
% \end{code}
% \label{page:warning}\index{Warning}
% You will probably get this warning on every page.
% \textbf{Note:} Before version 4.0, \pkg{fancyhdr} would change the
% \cs{headheight} itself, causing the text on the following pages
% to come out lower than on this page. This appeared to be confusing, so
% since version~4.0 this is no longer done (except when you give the
% \OPTindex{compatV3}%
% \texttt{compatV3} package option. You should not give this as a
% permanent solution, however, but solve the problem). Therefore you are strongly advised to
% redefine \cs{headheight} in the preamble, like this:
% \begin{code}
% \setlength{\headheight}{14pt}
% \end{code}
% This would cause the main text to be put 2pt lower on the page, which
% might be undesirable. You can compensate this by making \cs{topmargin}
% correspondingly smaller, for example
% \begin{code}
% \addtolength{\topmargin}{-2pt}
% \end{code}
% A similar change would be necessary for \cs{footskip} if the footer comes out too tall.
%
% You can also eliminate this check completely by using the
% \OPTindex{nocheck}
% \texttt{nocheck} option of the package. But this may risk unwanted
% run-ins of the header or footer with other text. So this is generally
% discouraged. It is better to change \cs{headheight}, \cs{footskip},
% and/or \cs{topmargin}. But in cases where you generate the \LaTeX{}
% code automatically, and the software does not know how tall the header
% or footer will be, this may be handy.
%
% As an alternative to changing \cs{headrulewidth} to 0 to have the rule
% disappear, you can also make it empty with
% \begin{code}
% \renewcommand{\headrule}{}
% \end{code}
% Visually this makes no difference, but it is more difficult to restore
% it later to its default value.
%
% Finally, let us make a real `decorative' line\footnote{Based upon an idea by Wayne Chan.}.
% \PKGindex{fourier-orns}%
% \begin{code}
% \usepackage{fourier-orns}
% ...
% \renewcommand\headrule{%
%       \hrulefill
%       \raisebox{-2.1pt}
%           {\quad\decofourleft\decotwo\decofourright\quad}%
%       \hrulefill}
% \end{code}
% This gives us the following headrule:
%
% \noindent\makebox[\textwidth]{\hrulefill
% \raisebox{-2.1pt}[10pt][10pt]{\quad\decofourleft\decotwo\decofourright\quad}\hrulefill}
%
% Note that we haven't taken care to make this decorative line occupy
% zero vertical space. The consequence is that it will extend towards
% the text and that we will get the warning about \cs{headheight} too small. So we
% should change \cs{headheight} as given above. Another problem is that
% the distance between the line and the header text is quite big. We can
% reduce this by putting a negative \cs{vspace} above it, like
% \Example{17}
% \begin{example}
% \renewcommand\headrule{%
%       \vspace{-6pt}
%       \hrulefill
%       \raisebox{-2.1pt}
%           {\quad\decofourleft\decotwo\decofourright\quad}%
%       \hrulefill}
% \end{example}
%
% We can use the same code for the \cs{footrule}, but we wouldn't need
% the \cs{vspace}. If you want to change the distance between that
% decorative line and the footer text you need to adjust the
% \CmdIndex{footruleskip}
% parameter \cs{footruleskip}. It
% defines the distance between the decorative line in the footer and the top
% of the footer text line. By default it is set to 30\% of the normal line
% distance. You may want to adjust it if you use unusally large or small
% fonts in the footer. Change it with \cs{renewcommand}.
%
% You can also change the distance between the baseline of the header text and the
% decorative line in the header. Normally this distance is determined by
% the maximum depth of possible descenders in the text, which is 30\% of
% the normal line distance. You can increase or decrease this distance
% \CmdIndex{headruleskip}
% by defining the macro \cs{headruleskip}, similar to \cs{footruleskip}.%
% \footnote{But \cs{headruleskip} is only available since version~4.0.}
% This defines the extra distance. The default value is 0pt, and
% positive values make the distance larger, and negative values make the
% distance shorter.
% Please note that this does not change the position
% of the decorative line with respect to the page, but it shifts the
% header text. If you want to keep the header text fixed, but move the
% decorative line, then you must also change the parameter \cs{headsep}
% (see figure~\ref{fig:layout}). \strut
%
% \pagestyle{showstruts}
% \noindent\begin{minipage}{\textwidth}
% \parindent=\normalparindent
% \phantomsection\label{showstruts}
% \indent The header and footer in this page
% show the \emph{strut} (the amount of space in the text area above and
% below the baseline), and the \cs{headruleskip} and \cs{footruleskip}.
% For this page \cs{headruleskip} is 4pt and \cs{footruleskip} is 3.6pt
% (0.3\cs{normalbaselineskip}).
%
% The code for this can be found in section~\ref{sec:curpagestyle}.
% \end{minipage}
%
% \paragraph{Fine-tuning the footer position.}
% \label{sec:fancyfootalign}
% By default \LaTeX{} positions the baseline of the footer on the bottom
% edge of the bottom margin (the lower line of the footer box in
% figure~\ref{fig:layout}). Most of the time this is what you want, but
% it means that any descenders in the footer (symbols that extend below
% the baseline, e.g., p and g or parentheses). See
% figure~\ref{fig:footalign-default}, where the horizontal line denotes the
% bottom border.
%
% \begin{figure}[hbpt]
%   \subcaptionsetup{format=hang}
%   \centering\LARGE\setlength\parindent{0pt}
%     \subcaptionbox{default footer positioning\label{fig:footalign-default}}
%       {\mbox{\rule{5cm}{0.2pt}\hspace{-4cm}\fbox{\strut Page Footer}}}
%   \hspace{2cm}
%     \subcaptionbox{\raggedright footer positioning with \cmd{\fancyfootalign}\marg{length}\label{fig:footalign-length}}[0.5\linewidth][l]
%       {\mbox{\rule{6cm}{0.2pt}\hspace{-5.5cm}%
%           \raisebox{0.7\baselineskip}[0.8\baselineskip][0pt]{\fbox{\strut Page Footer}}%
%                \normalsize\hspace{-4pt}\raisebox{5pt}{$\swarrow$}\raisebox{10pt}{\meta{length}}}}
%   \caption{Vertical footer positioning}
% \end{figure}
%
% In some cases this is undesirable, for example when the bottom border
% is completely missing (\texttt{0pt}). In that case the descenders are
% cut off because they are outside of the paper, and even if there are
% no descenders, the resulting layout with the footer at the edge of the
% paper isn't esthetic. The beamer class has this layout.
%
% In this case we can shift the footer up with the \cs{fancyfootalign}
% command (only available in \pkg{fancyhdr} 5.0 and later).
% This command has two versions:
% \begin{flushleft}
% \cs{fancyfootalign}\texttt{\{\}} -- This selects the default alignment,
% as in figure~\ref{fig:footalign-default}.\\
% \cs{fancyfootalign}\marg{length} -- This gives extra space of
% \meta{length} between \emph{the bottom of the footer, (including the
% space for descenders and the interline space)}, and the border. See
% figure~\ref{fig:footalign-length}.
% \end{flushleft}
% Usually a \meta{length} of \texttt{0pt} is sufficient; this means that
% the bottom of the footer box coincides with the bottom border. You can
% also use negative \meta{length} values, so that the footer box only
% partially sticks out under the border. A given \meta{length} applies
% to all subsequent footers (but is subject to the local group
% structure). It can be cancelled by \cs{fancyfootalign}\texttt{\{\}}.
% See section~\ref{sec:beamer} for an example.
%
% \section{Two book examples}
% \label{sec:book-examples}
%
% The following definitions give an approximation of the style
% used in L. Lamport's \LaTeX{} book.
%
% Lamport's header overhangs the outside margin.  This is done as follows.
%
% The width of headers and footers is \cs{headwidth}, which by default
% equals the width of the text: \cs{textwidth}. You can make the width
% \CmdIndex{headwidth}
% wider (or narrower) by redefining \cs{headwidth} with the
% \cs{setlength} and \cs{addtolength} commands.
% To overhang the outside margin where the marginal notes are
% \CmdIndex{marginparsep}
% \CmdIndex{marginparwidth}
% printed, add both \cs{marginparsep} and \cs{marginparwidth} to
% \cs{headwidth} with the commands:
%
% \begin{code}
% \addtolength{\headwidth}{\marginparsep}
% \addtolength{\headwidth}{\marginparwidth}
% \end{code}
%
% \begin{flushleft}
% It is safest to issue these commands \emph{after} the first
% \Cmd{pagestyle\{fancy\}} command.
% \end{flushleft}
%
% And now a complete definition of Lamport's book style. The header has
% the width of the text plus the marginpar area. The header on even
% pages has the page number on the left, and the chapter title on the
% right. On odd pages it has the section title preceded by the section
% number on the left and the page number on the right. All in boldface.
% There is no footer. The \texttt{plain} style is redefined to have no
% header and no footer. (In the \LaTeX{} book this makes sense because
% each chapter begins with a page that contains only a drawing. In most
% other cases you probably would want a page number on the page.)
%
% \Example{18}
% \begin{example}
% \documentclass{book}
% \usepackage{fancyhdr}
% \pagestyle{fancy}
% \addtolength{\headwidth}{\marginparsep}
% \addtolength{\headwidth}{\marginparwidth}
% \renewcommand{\chaptermark}[1]{\markboth{#1}{}}
% \renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}
% \fancyhf{}
% \fancyhead[LE,RO]{\textbf{\thepage}}
% \fancyhead[LO]{\textbf{\rightmark}}
% \fancyhead[RE]{\textbf{\leftmark}}
% \fancypagestyle{plain}{%
%    \fancyhead{} % get rid of headers
%    \renewcommand{\headrulewidth}{0pt} % and the line
% }
% \end{example}
% \PSindex{plain}
%
% Notice that the \cs{chaptermark} and \cs{sectionmark} commands have
% been redefined to eliminate the chapter numbers and the uppercaseness.
%
% For more control about the horizontal position of the headers and/or
% footers, \pkg{fancyhdr} has additional commands to specify the
% offset of the header and/or footer elements. Use
% \Cmd{fancyhfoffset[place]\{length\}}
% \CmdIndex{fancyhfoffset}
% to offset one or more elements. The \texttt{place} parameter is like
% the optional parameter of \cs{fancyhf}, like \texttt{L R E O}, except
% that \texttt{C} cannot be used. It specifies for which elements the
% offset should be applied. The \texttt{length} parameter specifies the
% actual offset. Positive values move the element outward (into the
% margin), negative values inward. There are also specialised commands
% \cs{fancyheadoffset} and \cs{fancyfootoffset}, which have the
% \texttt{H} and \texttt{F} parameter pre-applied, respectively.
% \CmdIndex{fancyheadoffset}
% \CmdIndex{fancyfootoffset}
%
% When you use these commands, \LaTeX{} will recalculate \cs{headwidth},
% based on the given parameters.
%
% So the above example could also have been done with (N.B. You can only
% use such an expression as a length parameter if the \PKGindex{calc}\pkg{calc}
% package is used):
%
% \Example{19}
% \begin{example}
% \fancyheadoffset[LE,RO]{\marginparsep+\marginparwidth}
% \end{example}
%
% \textbf{NOTE:} If you change the \cs{textwidth} in the middle of your
% document, for example by using the \PKGindex{geometry}\pkg{geometry} package,
% by default the \cs{headwidth} will not change, as it picks up the value
% of \cs{textwidth} at the beginning of the document. If you want it to track
% the changes to \cs{textwidth}, you should use the command \verb|\fancyhfoffset{0pt}|
% in the neighborhood of your header/footer definitions, unless you
% already use such an \texttt{\ldots offset} command, of course.
% \vspace{\baselineskip}
% For the second example, we take the \ams/-\LaTeX{} book\footnote{George Gratzer,
% \textit{Math into LaTeX, An Introduction to \LaTeX{} and \ams/-\LaTeX}, Birkhauser, Boston.}.
%
% Chapter pages have no headers or footers.  So we declare
%
% \begin{code}
% \thispagestyle{empty}
% \end{code}
% for every chapter page, and we do not need to redefine plain.
%
% Chapter and section titles appear in the form: 2.\ IMPLEMENTATION, so we have
% to redefine \cs{chaptermark} and \cs{sectionmark} as follows (see
% Section~\ref{sec:scoop}):
%
% \begin{code}
% \renewcommand{\chaptermark}[1]%
%    {\markboth{\MakeUppercase{\thechapter.\ #1}}{}}
% \renewcommand{\sectionmark}[1]%
%    {\markright{\MakeUppercase{\thesection.\ #1}}}
% \end{code}
%
% On an even page, the page number is printed as the left header and
% the chapter info as the right header; on an odd page, the section info
% is printed as the left header and the page number as the right header.  The
% center headers are empty.  There are no footers.
%
% There is a decorative line in the header.  It is 0.5pt wide, so we need
% the commands:
%
% \begin{code}
% \renewcommand{\headrulewidth}{0.5pt}
% \renewcommand{\footrulewidth}{0pt}
% \end{code}
%
% The font used in the headers is 9 pt bold Helvetica.  The \textsf{PSNFSS} system
% (originally by the late Sebastian Rahtz) uses the short (Karl Berry) name \texttt{phv} for
% Helvetica. The more modern \LaTeX{} solution is to use the \TeX{} Gyre font
% Heros, which uses the short name \texttt{qhv} so this font is selected with the commands^^A
% \footnote{See \emph{The \LaTeX{} Companion, Third Edition}, Part I, section
% 9.5.2, and Part II, section 10.8.16.}:
% \begin{code}
% \fontfamily{qhv}\fontseries{b}\fontsize{9}{11}\selectfont
% \end{code}
% Let us define a shorthand for this:
% \begin{code}
% \newcommand{\helv}{%
%    \fontfamily{qhv}\fontseries{b}\fontsize{9}{11}\selectfont}
% \end{code}
%
% Now we are ready for the page layout:
%
% \Example{20}
% \begin{example}
% \documentclass{book}
% \usepackage{fancyhdr}
% \pagestyle{fancy}
% \renewcommand{\chaptermark}[1]%
%    {\markboth{\MakeUppercase{\thechapter.\ #1}}{}}
% \renewcommand{\sectionmark}[1]%
%    {\markright{\MakeUppercase{\thesection.\ #1}}}
% \renewcommand{\headrulewidth}{0.5pt}
% \renewcommand{\footrulewidth}{0pt}
% \newcommand{\helv}{%
%    \fontfamily{qhv}\fontseries{b}\fontsize{9}{11}\selectfont}
% \fancyhf{}
% \fancyhead[LE,RO]{\helv \thepage}
% \fancyhead[LO]{\helv \rightmark}
% \fancyhead[RE]{\helv \leftmark}
% \end{example}
%
%
%\section{Summary of \cs{headwidth} calculation}
%\label{sec:headwidth}
%
% Here is a summary of the calculation of the widths of headers and
% footers, as illustrated in \smartref{sec:headwidth}{sec:book-examples}.
% \begin{itemize}
% \CmdIndex{headwidth}%
% \CmdIndex{textwidth}%
% \CmdIndex{fancyhfoffset}%
% \CmdIndex{fancyheadoffset}%
% \CmdIndex{fancyfootoffset}%
% \item If no \cs{fancy...offset} commands are given, the default value
% for \cs{headwidth} is \cs{textwidth}. This is used for the width of
% both the header and the footer. It is possible to change the value of
% \cs{headwidth}, for example with \cs{setlength} or \cs{addtolength}.
% The excess or deficit will be applied to the right for a onesided
% document, and for a two-sided document to the right on odd pages and to
% the left on even pages. The header and the footer will have the same
% width, \cs{headwidth}.
% \item If some \cs{fancy...offset} command is given, the header and
% footer widths are independently calculated by adding the appropriate
% offsets to \cs{textwidth}. Any changes made to \cs{headwidth} will not
% be taken into account. The header/footer will stick in/out at the proper
% side(s) specified by the offsets.
% \end{itemize}
% The file \texttt{example-headwidth.tex} in the \texttt{Examples}
% branch of the repository illustrates this.
%
% \section{Special page layout for float pages}
% \label{sec:float}
%
% \index{float page}
% Some people want to have a special layout for float pages (pages only
% containing floats). As these pages
% are generated autonomically by \LaTeX, the user doesn't have any control
% over them. There is no \cs{thispagestyle} for float pages and any change
% of the page style will at least also affect the page before the float page.
% With \pkg{fancyhdr}, however, you can specify in each of
% the header- or footer fields
%
% \medskip
% \Cmd{iffloatpage\{}\meta{value for float page}\verb|}{|\meta{value for other pages}\verb|}|
% \medskip
%
% You can even use this to get rid of the decorative line on float pages only
% by defining:
%
% \Example{21}
% \begin{example}
% \renewcommand{\headrulewidth}{\iffloatpage{0pt}{0.4pt}}
% \end{example}
%
% \textbf{NOTE:} There is also a package \PKGindex{floatpag}%
% \pkg{floatpag}\footnote{\url{https://www.ctan.org/pkg/floatpag}} by Vytas
% Statulevičius and Sigitas Tolušis that has a command
% \cs{floatpagestyle}\marg{pagestyle}, that applies \meta{pagestyle} to
% all float pages, where \meta{pagestyle} can be defined with
% \cs{fancypagestyle} (or by any other means). In some cases this might
% be simpler than putting \cs{iffloatpage} in various headers or footers.
%
% Sometimes you may want to change the layout also for pages that contain a
% float on the top of the page, a float on the bottom of the page or a footnote on the bottom of the page.
%
% \pkg{fancyhdr} gives you the commands \cs{iftopfloat}, \cs{ifbotfloat} and \cs{iffootnote}
% similar to \cs{iffloatpage}. For example:
% \begin{code}
% \fancyhead[R]{\iftopfloat{This page has a topfloat}
%                          {There is no topfloat here}}
% \end{code}
%
% Note: Marks in floats will not be visible in \LaTeX's output routine, so
% it is not useful to put marks in floats. So there is currently no way to
% let a float (e.g., a figure caption) influence the page header or footer.
%
% \section{Those blank pages}
% \label{sec:blank}
%
% In the \texttt{book} class when the \texttt{openany} option is not given or
% in the \texttt{report} class when the \texttt{openright} option is given,
% chapters start at odd-numbered pages, half of the time causing a blank page
% to be inserted. Some people prefer this page to be completely empty, i.e.,
% without headers and footers. This cannot be done with \cs{thispagestyle}
% as this command would have to be issued on the \emph{previous} page. There
% is, however, no magic necessary to get this done:
%
% \PSindex{empty}
% \CmdIndex{clearpage}
% \CmdIndex{cleardoublepage}
% \begin{code}
% \clearpage\begingroup\pagestyle{empty}\cleardoublepage\endgroup
% \end{code}
%
% As the \Cmd{pagestyle\{empty\}} is enclosed in a group it only affects the
% page that may be generated by the \cs{cleardoublepage}.  You can of
% course put the above in a private command. If you want to have this done
% automatically at each chapter start or when you want some other text on the
% page then you must redefine the \cs{cleardoublepage} command.
% \index{blank page}
% \begin{code}
% \makeatletter
% \def\cleardoublepage{\clearpage\if@twoside \ifodd\c@page\else
%  \begingroup
%   \mbox{}
%   \vspace*{\fill}
%   \begin{center}
%     This page intentionally contains only this sentence.
%   \end{center}
%   \vspace{\fill}
%   \thispagestyle{empty}
%   \newpage
%   \if@twocolumn\mbox{}\newpage\fi
%  \endgroup\fi\fi}
% \makeatother
% \end{code}
%
% \section{\textsf{N} of \textsf{M} style page numbers}
% \label{sec:nofm}
%
% Some document writers prefer the pages to be numbered as \textsf{n} of
% \textsf{m} where \textsf{m} is the number of pages in the document.
% There is a package \PKGindex{lastpage}\pkg{lastpage}
% available which you can use with \pkg{fancyhdr} as follows:
%
% \Example{22}
% \begin{example}
% \usepackage{lastpage}
% ...
% \fancyfoot[C]{\thepage\ of \pageref{LastPage}}
% \end{example}
% Because you want the pages with page style \texttt{plain} to contain
% the same style of page numbers, you will have to redefine this
% page style too.
% \begin{code}
% \fancypagestyle{plain}{\fancyhead{}\renewcommand{\headrule}{}}
% \end{code}
% We clear all the headers including its rule. The footer will be
% ``inherited'' from the page style \texttt{fancy}.
%
% The value of the \TTindex{LastPage}\texttt{LastPage} label can be used to
% make different headers or footers on the last page of a document. E.g., if
% you want the footer of every odd page, except if it is the last one, to
% contain the text ``Please turn over'', this can be done by checking if
% the page number is odd, and if it is equal to the number of the last page.
%
% We use the macro \cs{getpagerefnumber} from the package
% \PKGindex{refcount}%
% \pkg{refcount}, because \cs{pageref} isn't always usable in a
% numerical context (it is meant for typesetting only). This is also done in
% following similar examples.
%
% \PKGindex{ifthen}\PKGindex{lastpage}\PKGindex{refcount}
% \begin{code}
% \usepackage{ifthen}
% \usepackage{lastpage}
% \usepackage{refcount}
% ...
% \fancyfoot[R]{%
%   \ifthenelse{\isodd{\value{page}} \and
%     \not \( \value{page}=\getpagerefnumber{LastPage} \) }%
%       {Please turn over}{}%
% }
% \end{code}
%
% In order to get the number of pages correctly used, you usually have
% to do one additional \LaTeX{} run.
%
%
% \section{Chapter or section related page numbers}
%
% In technical documentation very often page numbers are used of the form
% 2-10 where the first number is the chapter number and the second is the
% pagenumber relative to the chapter. Sometimes section is used rather than
% chapter. The package \PKGindex{chappg}\pkg{chappg} can be used to get this format.
%
% Basically this package redefines \cs{thepage} as
% \cs{thechapter}\cs{chappgsep}\Cmd{arabic\{page\}}, where
% \cs{chappgsep} by default is `-'. If you want do use a different
% separator, you must redefine \cs{chappgsep}, for example to use an
% en-dash:
%
% \begin{code}
% \renewcommand{\chappgsep}{--}
% \end{code}
%
% To use a different prefix, for example the section number, use the
% \Cmd{pagenumbering\{bychapter\}} command with an optional argument
% specifying the prefix.
% \CmdIndex{pagenumbering}
%
% \Example{23}
% \begin{example}
% \clearpage
% \pagenumbering[\thesection]{bychapter}
% \end{example}
%
% What the package also does is reset the page number to 1 at the beginning
% of each chapter.
%
% In general it is advisable to give a \cs{clearpage} or
% \cs{cleardoublepage} before changing the page numbering.
%
% In the frontmatter of your document (for example the Table of
% Contents) there will be no chapter numbers. Therefore a simple page
% number will be used there. This may be confusing, so you might prefer to use roman pagenumbers
% in the front matter. Do this by using \verb+\pagenumbering{roman}+ in
% the beginning of the document and \verb+pagenumbering{bychapter}+
% after the first \cs{chapter} command. If you want to do it before the
% \cs{chapter} command you must precede it
% by a \cs{newpage} command (see the next section).
% \begin{code}
% \pagenumbering{roman}
% \tableofcontents
% \newpage
% \pagenumbering{bychapter}
% \chapter{Introduction}
% \end{code}
%
% There is a caveat when you have appendices in your document. Before the
% \cs{appendix} command you should give a \cs{clearpage} or
% \cs{cleardoublepage}. See the \pkg{chappg} documentation for
% details.
%
% There is a fundamental difference between the page numbering of the style
% ``\emph{m} of \emph{n}'' as described in the previous section and the
% current one. The \emph{m} of \emph{n} style is only used in the page header
% or footer, but not in the table of contents, index, or references like
% ``\emph{See page \emph{xx}}''. Therefore it does not change the command
% \cs{thepage}. The page numbering style ``2-10'', however should be used
% in all references to the page number, therefore it must be done by redefining
% \cs{thepage}.
%
% \section{Switching page styles}
% \label{sec:switching}
%
% Page style \texttt{fancy}, if not redefined, does not have the definitions of the
% headers and footers built-in, but they are defined in the document,
% globally, or locally in a group. This also applies to the definitions of the
% \cs{chaptermark} and/or \Cmd{[sub]sectionmark} commands. So if you want
% to switch from another page style to the \texttt{fancy} page style
% later in the document, and that other page style has changed for
% example the \cs{chaptermark} and/or \Cmd{[sub]sectionmark} commands,
% you will have to redefine these yourself and maybe also
% the definitions of the headers and footers, at that point.
% For example
% \begin{code}
% \pagestyle{fancy}
% \renewcommand{\chaptermark}[1]{\markboth{Chapter \thechapter. #1}{}}
% \renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}
% \end{code}
%
% If the previous page style was one of the standard \LaTeX{} page
% styles, or some page style that is not based on \pkg{fancyhdr},
% then the definitions of \cs{fancyhead} or \cs{fancyfoot} are not
% affected. So strictly you don't have to include them. But if it was
% based on \pkg{fancyhdr} and had different definitions, you will
% get the wrong headers and/or footers when you switch back to page
% style \texttt{fancy}. So it is safer to include them anyway.
%
% A better possibility is to define your own page style, and include
% these definitions in that page style:
% \begin{code}
% \fancypagestyle{myfancy}{
%   \renewcommand{\chaptermark}[1]{\markboth{Chapter \thechapter. ##1}{}}
%   \renewcommand{\sectionmark}[1]{\markright{\thesection\ ##1}}
%   \fancyhead{...}
% }
% ...
% \pagestyle{myfancy}
% \end{code}
% Please note that you now have to double the \verb|#| signs, because the
% definitions are inside a macro.
%
% In general, when you use only one page style \texttt{fancy} in your
% document, with the occasional \cs{thispagestyle} excursion to page
% style \texttt{plain} or \texttt{empty}, you can just keep the
% definitions globally in your document, but as soon as you use more
% than one page style, and switch between them, it is highly advisable
% to define them (including page style \texttt{fancy}) with
% \cs{fancypagestyle} and put all the relevant definitions inside them.
%
% There is another caveat, when switching page styles, if they have
% different definitions of \cs{chaptermark} in the \texttt{book} or
% \texttt{report} document class or similar ones. When you put the
% \cs{pagestyle} command \emph{after} the \cs{chapter} command, then the
% \cs{chapter} command calls the \cs{chaptermark} of the previous page
% style, which is probably not what you intended. So you must issue the
% \cs{pagestyle} command \emph{before} the \cs{chapter} command. But
% this would probably change the page style of the previous page, which
% is too early. Therefore you would have to give a \cs{newpage},
% \cs{clearpage} or \cs{cleardoublepage} command
% before the \cs{pagestyle} command, so that the last page will be
% finished with the previous page style. I.e., the proper sequence is:
% \begin{code}
% \newpage % (or \clearpage or \cleardoublepage)
% \pagestyle{newstyle}
% \chapter{My New Chapter}
% \end{code}
%
% Finally, in this section, we give an example that illustrates why
% using \emph{closed} page styles is recommendable.
%
% Suppose we have a part of our document, maybe one or more chapters,
% that need a different style headers and/or footers than the rest of
% the document. We can do this by defining a new page style for this
% part with \cs{fancypagestyle}. First we use the traditional
% (\emph{open}) form:
% \NoExample{switchstyle1}
% \begin{example}
% \pagestyle{fancy}
% \fancyhf{}
% \fancyhead[L]{\leftmark}
% \fancyhead[R]{\rightmark}
% \fancyfoot[C]{\thepage}
%
% \fancypagestyle{special}{%
%   \fancyhf{}
%   \renewcommand{\headrulewidth}{0pt}
%   \fancyhead[L]{Special Page Style \nouppercase\leftmark}
%   \fancyfoot[R]{\thepage}
% }
% . . .
% \chapter{Special Chapter}
% \pagestyle{special}
% Chapter text
%
% \chapter{Another Chapter}
% \pagestyle{fancy}
% Chapter text
% \end{example}
% Now the last chapter will not use the headers and footers that we
% defined in the beginning, but those that are defined in page style
% \texttt{special}. This is because the command
% \Cmd{pagestyle\{special\}} will just execute the definitions inside
% it, and so it changes the definitions of \cs{fancyhead[]} etc. Also
% the definition of \cs{headrulewidth} will not be restored.
%
% To remedy this we would need to put the relevant definitions inside
% the page style \texttt{fancy}. First we try this with the \emph{open}
% \cs{fancypagestyle}.
%
% \NoExample{switchstyle2}
% \begin{example}
% \fancypagestyle{fancy}{%
%   \renewcommand{\headrulewidth}{0.4pt}
%   \fancyhf{}
%   \fancyhead[L]{\leftmark}
%   \fancyhead[R]{\rightmark}
%   \fancyfoot[C]{\thepage}
% }
%
% \fancypagestyle{special}{%
%   \fancyhf{}
%   \renewcommand{\headrulewidth}{0pt}
%   \fancyhead[L]{Special Page Style \nouppercase\leftmark}
%   \fancyfoot[R]{\thepage}
% }
% \pagestyle{fancy}
% . . .
% \chapter{Special Chapter}
% \pagestyle{special}
% Chapter text
%
% \chapter{Another Chapter}
% \pagestyle{fancy}
% Chapter text
% \end{example}
% We now have the relevant definitions also embedded in page style
% \texttt{fancy}. Note that we have to include the (default) definition
% of \cs{headrulewidth}, although it looks unlogical that we have to do this.
% But we need it because page style \texttt{special} changes it. And if
% we had another page style that would change for example the
% \texttt{offsets} (see section~\ref{sec:book-examples}) then we would
% also have to include these. This is the reason for the existence of
% the \emph{closed} form \cs{fancypagestyle*}. So now we give the
% solution with these. This solves the problem in an elegant and robust way.
%
% \NoExample{switchstyle3}
% \begin{example}
% \fancypagestyle*{fancy}{%
%   \fancyhf{}
%   \fancyhead[L]{\leftmark}
%   \fancyhead[R]{\rightmark}
%   \fancyfoot[C]{\thepage}
% }
%
% \fancypagestyle*{special}{%
%   \fancyhf{}
%   \renewcommand{\headrulewidth}{0pt}
%   \fancyhead[L]{Special Page Style \nouppercase\leftmark}
%   \fancyfoot[R]{\thepage}
% }
% \pagestyle{fancy}
% . . .
% \chapter{Special Chapter}
% \pagestyle{special}
% Chapter text
%
% \chapter{Another Chapter}
% \pagestyle{fancy}
% Chapter text
% \end{example}
%
% \section{When to change the headers and footers?}
% \label{sec:change}
%
% In \smartref{sec:change}{sec:switching} we switched page styles at a
% point that has a clear page break (the beginning of a chapter).
% Sometimes you want to change only a header or footer without changing
% the whole page style.
%
% It should be noted that although the \pkg{fancyhdr} commands like
% \cs{fancyhead} take effect immediately, this does not mean that any
% ``variables'' used in these commands get the value they have at the place
% where these commands are given. E.g., if \Cmd{fancyfoot[C]\{}\Cmd{thepage\}} is given
% the page number that will be inserted in the footer is not the page number
% of the page where this command is given, but rather the page number of
% the actual page where the footer is constructed. Of course for the page
% number this is what you expect, but it is also true for other
% commands. There is a difference, however. The page number is
% incremented \emph{after} the page has been constructed. When we have
% our own ``variables'', however, these are usually changed in the
% middle of our text.
%
% As an example we take a book where each chapter is written by a different author.
% If we want the name of the author in the header opposite the chapter
% title, we can use the following commands:
%
% \Example{24}
% \begin{example}
% \newcommand{\TheAuthor}{}
% \newcommand{\Author}[1]{\renewcommand{\TheAuthor}{#1}}
% \fancyhead[LE,RO]{\TheAuthor}
% \end{example}
%
% \noindent and start each chapter with the command
% \Cmd{Author\{Real Name\}}.
% If, however, the author name would be changed before a page is
% completed the wrong author could come in the header. This would be the case
% if you gave the above command \emph{before} the \cs{chapter} command
% rather than after it. So we give the \cs{Author} command after the
% \cs{chapter} command:
% \begin{code}
% \chapter{Chapter Title}
% \Author{Author Name}
% \end{code}
% As a chapter starts on a new page, we can be sure that the \cs{Author}
% command comes at the same page as the chapter start.
%
% Another source of problems is the fact that \TeX's output routine processes
% commands ahead, so it may already have processed some commands that produce
% text that will appear on the next page. So if our book was not divided
% into chapters, but into sections, we cannot use the similar system:
% \begin{code}
% %%% NOTE: This may not work %%%%
% \section{Chapter Title}
% \Author{Author Name}
% \end{code}
% because in this case, when this command comes at the end of a page,
% the ``variable'' \cs{TheAuthor} could be set at that page, but then
% \TeX{} could decide to move the section title to the next page. And
% then the author name would appear one page too early. This problem can
% be solved using marks. In fact this is the whole reason the mark
% mechanism was developed in \TeX. See section~\ref{sec:xmarks}.
%
% The same applies to other changes in the middle of a page, e.g., to change the page
% numbering from roman to arabic (with \cs{pagenumbering}). For the same
% reason \Cmd{thispagestyle\{mystyle\}} will not always work in the
% middle of a page.
%
% Some of these changes can be accomplished by using the mark
% mechanism as may be seen in \smartref{sec:change}{sec:scoop} and
% \smartref{sec:change}{sec:xmarks}.
%
% In the remainder of this section we look at two different cases of
% changing the page style in the middle of a page: changing the style of
% the current page and changing the style of the next page.
%
% \subsection{Changing the page style of the current page}
% \label{sec:curpagestyle}
%
% So now we are giving an example how to change the headers and footers,
% only on the current page. In some cases this can be done by the
% \CmdIndex{thispagestyle}%
% \cs{thispagestyle} command. This changes the page style for the
% ``current'' page only. But then we may be hit by the problem mentioned
% above. \LaTeX{} may have a different idea about
% the ``current'' page than you. The use of \cs{thispagestyle} is OK if
% you can be sure that the text where the command \cs{thispagestyle} is
% executed is the same page as where the surrounding text appears. So
% for example directly after a \cs{chapter} command, or after a
% \cs{newpage}. However, when the command is given near the end of a
% page, \LaTeX{} may execute the command, and then decide that the page
% is full and move the text that contains the command to the next page.
% So now the page style is changed on one page earlier than was intended.
%
% A good solution to this problem is to put a label, like
% \Cmd{label\{otherpagestyle\}} in the text where you want the different
% page style, and then in the header and/or footer definitions compare
% the page number with the label page number and choose the proper
% value. For example, if we want to replace the section title on the
% special page with ``MYFANCY SECTION'', like in
% \begin{code}
% \fancypagestyle{myfancy}{
%   \fancyhead[LE,RO]{MYFANCY SECTION}
% }
% \end{code}
% \Example{25 (a)}
% we define a new page style that makes the choice:
% \PKGindex{ifthen}\PKGindex{refcount}
% \begin{example}
% \usepackage{ifthen}
% \usepackage{refcount}
% . . .
% \fancypagestyle{switch}{
%   \fancyhead[LE,RO]{%
%     \ifthenelse{\value{page}=\getpagerefnumber{otherpagestyle}}
%       {MYFANCY SECTION}
%       {\textsl{\rightmark}}}
% }
% \end{example}
% \CmdIndex{ifthenelse}
% where \verb|\textsl{\rightmark}| is the normal value of the header
% field from \Cmd{pagestyle\{fancy\}}.
% Now we choose \Cmd{pagestyle\{switch\}} before our text, or even for
% the whole document.
%
% There can still be some ambiguity on which page gets the different
% header. For example, if the text says:
% \begin{quote}
%   This page gets a different header than the surrounding pages.
% \end{quote}
% where do you put the \cs{label}? \LaTeX{} could break the page between
% ``This'' and ``page'', and then would you want the special heading on
% the page where ``This'' appears, or on the page where ``page''
% appears. It depends on the positioning of the \cs{label} command.
% Probably it is safer to make sure the sentence isn't broken. This can
% be done by putting the text in a \cs{parbox} or \texttt{minipage} environment.
% \begin{code}
% \noindent
% \begin{minipage}{\textwidth}
%   This page should have a different header than the surrounding pages.
%   \label{otherpagestyle}
%   It is done with the \verb|\pagestyle{switch}| command, that
%   has tests in the header field definitions. This chooses the actual
%   header depending on the page number.
% \end{minipage}
% \end{code}
% The \cs{noindent} is necessary, otherwise the whole \texttt{minipage}
% will be shifted right by the paragraph indentation.
%
% Note that you cannot reset the page style immediately after this code,
% as this may still influence the current page. If you want to reset it,
% for example to \Cmd{pagestyle\{fancy\}}, you must be sure that it
% happens on a following page. But in this case it isn't even necessary,
% as the special page style acts as the default on all pages except the
% special page.
%
% The special header and footer in page~\pageref{showstruts}, which
% show the struts are done in a similar way, although the header and
% footer are a bit more elaborated there.
% Also there is another complication there, as we also want to make both
% \cs{headruleskip} and \cs{footrulewidth} dependent on the page number.
% Unfortunately, this cannot be done with a simple \cs{ifthenelse}
% \CmdIndex{ifthenelse}
% command.
% Both \cs{headruleskip} and \cs{footrulewidth} are eventually
% used as length parameters, and this requires that they are
% \emph{expandable}. However, the \cs{ifthenelse} construct is not
% expandable, so you will get strange error messages if you use
% something like
% \begin{code}
% %%% NOTE: This does not work %%%%
% \renewcommand{\footrulewidth}{%
%   \ifthenelse{\value{page}=\getpagerefnumber{otherpagestyle}}{0.4pt}{0pt}%
% }
% \end{code}
% \DescribeMacro{\fancyheadinit}
% \DescribeMacro{\fancyfootinit}
% \DescribeMacro{\fancyhfinit}
% \phantomsection\label{sec:hfinit}
% For cases like this \pkg{fancyhdr} version~4.0 and later has some
% new commands \cs{fancyheadinit}, \cs{fancyfootinit} and \cs{fancyhfinit}.
% \CmdIndex{fancyheadinit}
%
% With \Cmd{fancyheadinit\{\meta{code}\}} you can define some code that will be executed
% just before the construction of the header. As it is executed in the
% header, it can test the correct page number, because the counter
% \texttt{page} is guaranteed to have the correct value in the headers
% and footers. Similarly, the code in \Cmd{fancyfootinit\{\meta{code}\}} is executed in
% \CmdIndex{fancyfootinit}
% the footer. And \Cmd{fancyhfinit\{\meta{code}\}} sets its code for both the header
% \CmdIndex{fancyhfinit}
% and the footer. Now we can set for example \cs{headruleskip} or
% \cs{footrulewidth} depending on the page number. So instead of putting
% the test inside the definition of \cs{headruleskip}, we can put it
% outside, and then we can use the command \cs{ifthenelse}. So we put
% the following in \Cmd{pagestyle\{switch\}}\footnote{Assuming we have
% already loaded package \PKGindex{refcount}\pkg{refcount}.}:
% \begin{code}
%   \fancyheadinit{%
%     \ifthenelse{\value{page}=\getpagerefnumber{otherpagestyle}}
%       {\renewcommand{\headruleskip}{4pt}}
%       {\renewcommand{\headruleskip}{0pt}}
%   }
%   \fancyfootinit{%
%     \ifthenelse{\value{page}=\getpagerefnumber{otherpagestyle}}
%       {\renewcommand{\footrulewidth}{0.4pt}}
%       {\renewcommand{\footrulewidth}{0pt}}
%   }
% \end{code}
% Now here is the definition of the page style used for page~\pageref{showstruts}.
% \Example{25 (b)}
% \begin{example}
% \fancypagestyle{showstruts}{%
%   \fancyhead[L]{%
%     \ifthenelse{\value{page}=\getpagerefnumber{showstruts}}%
%       {\strutheader}%
%       {\rightmark}%
%   }
%   \fancyfoot[L]{%
%     \ifthenelse{\value{page}=\getpagerefnumber{showstruts}}%
%       {\strutfooter}%
%       {}%
%   }
%   \fancyheadinit{%
%     \ifthenelse{\value{page}=\getpagerefnumber{showstruts}}%
%       {\renewcommand{\headruleskip}{4pt}}%
%       {\renewcommand{\headruleskip}{0pt}}%
%   }
%   \fancyfootinit{%
%     \ifthenelse{\value{page}=\getpagerefnumber{showstruts}}%
%       {\renewcommand{\footrulewidth}{0.4pt}}%
%       {\renewcommand{\footrulewidth}{0pt}}%
%   }
% }
% \end{example}
% The label used on that page is \texttt{showstruts}. \cs{strutheader}
% and \cs{strutfooter} are macros that contain the code to draw these
% pictures. In this example the values for \cs{headruleskip} and
% \cs{footrulewidth} in the \emph{else} case are the same as the global
% values. So we could have left these \emph{else} parts empty. Then they
% would keep the global values.
% However, often explicit is better than implicit.
%
% These initialisation commands cannot be used to make global changes to
% the page, for example to \cs{headheight}. Neither can you use them to
% change \cs{fancyhead} or \cs{fancyfoot}, because these have already
% been set up. But you can use it to set the color and font of the
% header and/or footer, for example to get large, red text in the
% headers and footers on this specific page:
% \begin{code}
%  \fancyhfinit{%
%    \ifthenelse{\value{page}=\getpagerefnumber{otherpagestyle}}
%      {\color{red}\Large}
%      {}
%  }
% \end{code}
%
% \subsection{Changing the page style of the next page}
% \label{sec:nextpagestyle}
%
% If you want the change of the page style to take effect
% at the next page you must make sure that the current page is finished. In
% most cases this can be done by issuing a
% \CmdIndex{clearpage}\CmdIndex{newpage}%
% \cs{newpage} or \cs{clearpage} command before % any changes. However,
% this will immediately end the current page, possibly leaving you with a
% half-empty page, which may be undesirable.
%
% If this is not what you want, you can use the
% \PKGindex{afterpage}%
% \pkg{afterpage} package with:\\[1ex]
% \CmdIndex{afterpage}\Cmd{afterpage\{}\Cmd{fancyhead[L]\{new value\}\}} or\\
% \CmdIndex{pagenumbering}\Cmd{afterpage\{}\Cmd{pagenumbering\{roman\}\}}.
% \\[1ex]
% You cannot use \cs{afterpage}
% to change the \cs{pagestyle} as the commands issued by \cs{afterpage}
% are local in a group, and the \cs{pagestyle} command makes only local
% changes. The \cs{pagenumbering} and the \cs{thispagestyle}
% command make global changes, as well as changes to \LaTeX's counters,
% such as \cs{setcounter} and \cs{addtocounter}.
% So these can be used\footnote{In \pkg{fancyhdr} version~3 and
% earlier the commands like \cs{fancyhead} and \cs{fancyfoot} also made
% global changes. This is no longer the case in version~4.0 and later.}.
% Here is an example to change the page style of the next page with \cs{afterpage}:
%
% \Example{26}
% \begin{example}
% \usepackage{afterpage}
% \usepackage{fancyhdr}
% \fancypagestyle{myfancy}{
%   \fancyhead[LE,RO]{\textbf{MYFANCY SECTION}}
%   \fancyhead[LO,RE]{\textbf{MYFANCY CHAPTER}}
%   \fancyfoot[C]{\textbf{--~\thepage~--}}
% }
% . . .
% \afterpage{\thispagestyle{myfancy}}
% \end{example}
% Then the page after this code will have the page style \texttt{myfancy}.
%
% \subsection{Changing the page style in a \TeX{} group}
% \label{sec:change-group}
%
% Special care has to be taken when you change the page style inside a
% \TeX{} group. This can be any environment, text between
% \cs{begingroup} and \cs{endgroup}, between \{~and~\}, and other
% similar situations. \TeX{} definitions inside such a group are local
% to this group, unless they are declared to be global. All
% definitions pertaining to the page style (i.e., \cs{fancypagestyle},
% \cs{pagestyle}, \cs{fancyhead}, etc.) are local definitions, i.e.,
% they disappear at the end of the group. The only
% exception is \cs{thispagestyle}, which is global, i.e., its setting
% survives the end of the group.
%
% An example is the \env{appendices} environment of the package
% \PKGindex{appendix}\pkg{appendix}\footnote{Use the command `\texttt{texdoc appendix}' to
% see its documentation.} that you use to get special layout for your
% appendices. If you also want to change the page headers and/or footers
% for the appendices, you could use
% \begin{code}
% \clearpage
% \begin{appendices}
%   \pagestyle{appendices}
%   \chapter{My Appendix}
%   Appendix text.
% \end{appendices}
% \chapter*{Bibliograpy}
% \end{code}
% Note that we put a \cs{clearpage} before the environment to prevent
% that the page before this environment gets the new page style, as
% indicated in sections~\ref{sec:curpagestyle} and \ref{sec:nextpagestyle}.
% In the example above, it is probable that the \texttt{appendices}
% environment does not end with a \verb|\newpage| or \verb|\clearpage|.
% Then a page break will be given by the following \verb|\chapter|
% command, but then the `Special' page style will no longer be current,
% so the last page of the \texttt{appendices} environment will have the
% headers and footers that were current before the environment started.
% If there were still floats to be output at the end of the
% \texttt{appendices} environment, this could even be several pages. So
% we should put a \verb|\clearpage| before the \verb|\end{appendices}|.
%
% \smallskip
%
% Here follows a more stylized example. The intention is to give the
% pages of the environment the header ``Special Header''. First, the
% ``wrong'' implementation.
% \Example{26G}[(a)]
% \begin{example}
% \fancypagestyle{Special}{
%   % setting the header in beginning of environment
%   \fancyhead[C]{Special Header}
% }%
%
% \newenvironment{Special}[1]{%
%   \pagestyle{Special}%
%   \section*{Special Environment #1}
% }{%
% }
% ...
% \begin{Special}{a}
%
% Some text or a lot of text.
%
% \end{Special}
% \end{example}
% Now the last page of this environment, which may be the first page if
% the environment fits on one page, will get the wrong page header.
%
% The first solution would be to end the environment with a \cs{newpage}
% or \cs{clearpage} as described above. Generally, it is best to use
% \cs{clearpage}, because it also takes care of extra pages with floats.
% \Example{26G}[(b)]
% \begin{example}
% \begin{Special}{b}
%
% Some text or a lot of text.
%
% \clearpage
% \end{Special}
% \end{example}
% It is also possible to add the \cs{clearpage} to the definition of the
% \env{Special} environment if you define this environment yourself. If
% you use an existing environment you may use a \LaTeX{} environment
% hook to inject a \cs{clearpage}, for example in the case of the
% \env{appendices} environment:
% \begin{code}
% \AddToHook{env/appendices/end}{\clearpage}
% \end{code}
%
% Of course this will always cause a page break. If you don't want a
% page break at the
% end of your environment, you will have to decide
% what to do with the page that is partially filled with the special
% environment and partially with the following text. Which page style to
% use: the Special page style or the normal page style? If you do
% nothing it will be the normal page style. If you still want the
% Special page style, you can put a \Cmd{thispagestyle\{Special\}} at
% the end of the environment. Again, at the use of the environment, at
% the definition, or using a hook.
% \Example{26G}[(c)]
% \begin{example}
% \begin{Special}{c}
%
% Some text or a lot of text.
%
% \thispagestyle{Special}
% \end{Special}
% \end{example}
% Note, however, that this only works if the \texttt{Special} page style
% is defined outside of the environment, as is done in this example.
% However if the \texttt{Special} page style was defined inside the environment,
% it will have disappeared at the end of the page, and \LaTeX{} will
% silently ignore it. It doesn't even give an error message. The
% following pages will then get the normal header again.
%
% \section{\pkg{Fancyhdr} hooks}
% \label{sec:hooks}
%
% \LaTeX{} has a system of \emph{hooks}\index{hooks} since the 2020/10/01 release.
% This allows packages and classes (and other \LaTeX{} software) to
% define points in its code where other \LaTeX{} code can insert a piece
% of code. For more details, see  \emph{The \LaTeX{} Companion, Third
% Edition}, part I, pp. 671 ff. or the documentation that can be read
% with the command `\texttt{texdoc lthooks-doc}'.
%
% \pkg{Fancyhdr} version 4.5 or later defines a number of hooks to be executed at the
% beginning or end of the header and/or footer, if your \LaTeX{} version
% supports it. The hooks are defined in
% mirrored pairs, which means the second one of the pair is executed in the
% reverse order compared to the first one (see the hooks documentation).
% \begin{description}
% \item[\texttt{fancyhdr/before}, \texttt{fancyhdr/after}]
% \TTindex{fancyhdr/before}\TTindex{fancyhdr/after}
% these are executed before the
% header or footer is constructed, and after the header or footer is
% finished, respectively.
% \item[\texttt{fancyhdr/head/begin}, \texttt{fancyhdr/head/end}]
% \TTindex{fancyhdr/head/begin}\TTindex{fancyhdr/head/end}
% these are run at the
% beginning and the end of the header construction, respectively
% \item[\texttt{fancyhdr/foot/begin}, \texttt{fancyhdr/foot/end}]
% \TTindex{fancyhdr/foot/begin}\TTindex{fancyhdr/foot/end}
% these are run at the
% beginning and the end of the footer construction, respectively
% \end{description}
%
% The interaction of the hooks and the \cs{fancyhfinit} code described
% on page~\pageref{sec:hfinit} in section~\ref{sec:hfinit} with the
% construction of the header and footer is as follows: for the header
% construction
% \begin{itemize}
% \item first the \texttt{fancyhdr/before} hooks are run, then the
% \texttt{fancyhdr/head/begin} hooks, then the \cs{fancyheadinit} code.
% Then the header is constructed. Finally, the
% \texttt{fancyhdr/head/end} hooks are run followed by the
% \texttt{fancyhdr/after} hooks.
% \item For the construction of the footer, it is similar, just replace
% \texttt{head} by \texttt{foot}.
% \item Note that between the construction of the header and the footer,
% \LaTeX{} builds the body of the page. This process consists mainly of
% putting boxes next to each other, and \pkg{fancyhdr} does not
% interfere with this, and neither should the hook code.
% \end{itemize}
%
% The reason there are separate \texttt{fancyhdr/before} and
% \texttt{fancyhdr/after} hooks and the \texttt{head} and \texttt{foot}
% hooks, is
% \begin{enumerate}
% \item If you want to use the same hooks for headers and footers, use
% the \texttt{fancyhdr/before} and \texttt{fancyhdr/after} hooks. This
% prevents you to have to specify the same hook code twice.
% \item If you want to have different hooks for the header and footer,
% use the \texttt{head} and \texttt{foot} hooks.
% \end{enumerate}
% The \texttt{after} and \texttt{end} hooks are meant to undo changes
% made in the \texttt{before} and \texttt{begin} hooks, respectively. If
% the hooks make only local changes (which is recommended), the \TeX{}
% grouping mechanism will take care of this, so you can leave out the
% \texttt{after} and \texttt{end} hooks in that case.
%
% At first sight it may seem that the \cs{fancyhfinit} mechanism is no
% longer useful with the introduction of hooks. One reason it exists is
% that hooks were not available at the time it was introduced, and for
% compatibility reasons it remains. However, there are some significant
% differences between the \cs{fancyhfinit} mechanism and the hook
% mechanism, so you should choose carefully which one to use.
% \begin{itemize}
% \item Hooks are global, but the \cs{fancyhfinit} declarations are
% local. That is, if \cs{fancyhfinit} (or its siblings
% \cs{fancyheadinit} or \cs{fancyfootinit}) are given in a \TeX{}
% group, they last until the end of the group. They will disappear
% outside of the group, or be reset to the value they had outside of the group.
% \item \cs{fancyhfinit} is meant to be used by the user who writes the
% document, i.e., it is meant for the current document.
% \cs{fancyhfinit} \emph{should not be used by package or class writers
% and similar. They should use the hooks mechanism.} On the other hand
% the user can also use hooks in the document instead of, or in addition
% to the \cs{fancyhfinit} mechanism.
% \item Hooks can be added multiple times, but the \cs{fancyhfinit} code
% can only be given once (i.e., a new one overwrites the previous one).
% \item The \cs{fancyhfinit} code is stored in a \emph{closed} page
% style (see section~\ref{sec:fancypagestyle}). Hooks are not.
% \item \cs{fancyhfinit} has no corresponding \texttt{exit} function, so
% if you need some code to be executed after the construction of the header or
% footer, you have to use hooks.
% \item The hooks can also be given if \pkg{fancyhdr} is not used.
% This can be used as a precautionary measure in packages and classes
% that may have a bad interaction with \pkg{fancyhdr} otherwise. If
% \pkg{fancyhdr} is not used in a document, the hooks don't do anything.
% \end{itemize}
%
% \section{Headers and footers induced by the text}
% \label{sec:xmarks}
%
% We have seen how we can use \LaTeX's marks to get information from the
% document contents to the headers and footers. The marks mechanism is the
% only reliable mechanism that you can use to get changing information to the
% headers or footers. This is because \LaTeX{} may be processing your document
% ahead before deciding to break the page.
%
% Sometimes the two marks that \LaTeX{} offers are not enough. An example is
% the following:
% \begin{quote}
%  If a solution to an exercise goes across a page break, then I would like
%  to have ``(Continued on next page\ldots)'' at the bottom of the
% \index{Continued\ldots}
%  first page and ``(Continued\ldots)'' at the top in the margin of the next page.
% \end{quote}
%
% You cannot use \LaTeX's mark mechanisms for this if you also want to use
% chapter and section information.
%
% The \pkg{extramarks} package gives you
% two extra marks that can be used in this situation.
% Here is a way to use this package:
%
% \Example{27}
% \begin{example}
% \usepackage{extramarks}
% ...
% \pagestyle{fancy}
% \fancyhead[L]{\firstleftxmark} % = \firstxmark
% \fancyfoot[R]{\lastrightxmark} % = \lastxmark
% \fancypagestyle{plain}{\fancyhead{}\renewcommand{\headrule}{}}
% ...
% \extramarks{}{}% 1
% \extramarks{Continued\ldots}{Continued on next page\ldots}% 2
% ...
% Some text that may or may not cross a page boundary...
% ...
% \extramarks{Continued\ldots}{}% 3
% \extramarks{}{}% 4
% \end{example}
%
% \CmdIndex{extramarks}
% Note that we redefine the \texttt{plain} page style, so that on the
% first page of a chapter also the footer will be given if necessary. We
% assume that a `Continued' block will not cross chapter boundaries, so no
% header will be necessary on these pages.
% Also the \cs{extramarks} command must be close to the text, i.e., no
% empty lines (paragraph boundaries) should intervene. Otherwise the page may
% be broken at that boundary and the \texttt{extramarks} would come on the wrong page.
%
% Explanation: There are two new marks that can be used in the page layout with this
% package: If commands of the form
% \verb|\extramarks{|$m_1$\verb|}{|$m_2$\verb|}| are given
% \CmdIndex{firstxmark}
% \CmdIndex{lastxmark}
% \cs{firstxmark} gives you the first $m_1$ value and
% \cs{lastxmark} gives you the last $m_2$ value
% of the current page. In the above example, when the complete block
% falls on the same page, the \cs{firstxmark} will be the empty
% parameter of the first \cs{extramarks} command (indicated by
% \texttt{\%~1}), and
% the \cs{lastxmark} will be the empty parameter from the last
% \cs{extramarks} command (indicated by \texttt{\%~4}).
%
% However, when the page break falls inside the block, the mark generated by
% \texttt{\%~2} will be the last one on the first page. Therefore on that page
% \cs{lastxmark} will be `Continued on next page\ldots'. On the following
% pages, there are two possibilities: (1) when the block ends
% on that page the first mark will be \texttt{\%~3}, therefore
% \cs{firstxmark} will be `Continued\ldots'; (2) the block ends at a
% later page, therefore it does not contribute any marks to that page,
% and the marks are `inherited' from the last values of the previous page,
% i.e., those from \texttt{\%~2}.
% On all of the pages after the block the values of \texttt{\%~4} will be used,
% i.e., empty ones.
% This final \verb+\extramarks{}{}+ is to prevent the `Continued\ldots'
% header to spill over to the following pages.
% Of course in real life you would leave out the numbers.
%
% In case you want the last $m_1$ value or the first $m_2$ value, you
% can use the \cs{lastleftxmark} or \cs{firstrightxmark}, respectively.
% For symmetry reasons there are also commands \cs{firstleftxmark}
% (=\cs{firstxmark}), \cs{lastrightxmark} (=\cs{lastxmark}),
% \cs{topleftxmark} (=\cs{topxmark}) and \cs{toprightxmark}. The
% top-marks are basically the last-marks of the previous page.
% \CmdIndex{lastleftxmark}
% \CmdIndex{firstrightxmark}
% \CmdIndex{firstleftxmark}
% \CmdIndex{lastrightxmark}
% \CmdIndex{topleftxmark}
% \CmdIndex{toprightxmark}
%
% The package also gives you the \cs{firstleftmark} and \cs{lastrightmark}
% commands that complement the standard \LaTeX{} marks.
% \CmdIndex{firstleftmark}
% \CmdIndex{lastrightmark}
%
% \medskip
%
% In the above example the text ``Continued'' appears in the page header. It
% \index{margin}
% may be nicer to put it in the  margin. This can be easily
% accomplished by positioning it at a fixed place relative to the page header.
% In plain \TeX{} you would use a concoction of \Cmd{hbox to 0pt},
% \Cmd{vbox to 0pt}, \cs{hskip},\cs{vskip}, \cs{hss} and
% \cs{vss} but fortunately
% \LaTeX's \texttt{picture} environment gives a much cleaner way to do this.
% In order not to disturb the normal header layout we put the text in a zero-sized
% \texttt{picture}. Generally this is the best way to position things on fixed
% places on the page. You can then also use the normal headings. See also
% section~\ref{sec:thumb} for another example of
% this technique.
%
% \Example{28}
% \TTindex{picture}
% \begin{example}
% \fancyhead[L]{\setlength{\unitlength}{\baselineskip}%
% \begin{picture}(0,0)
%   \put(-2,-3){\makebox(0,0)[r]{\firstxmark}}
%   \end{picture}\rightmark} % \rightmark = section title
% \end{example}
%
% This solution can of course also be used for the footer. Make sure you put the
% \texttt{picture} as the first thing in left-handside entries and last in right-handside
% ones.
%
% \medskip
%
% Finally you may want to put ``(Continued\ldots)'' in the \emph{text}
% rather than in the header or the margin. Then you have to use the
% \PKGindex{afterpage}%
% \pkg{afterpage} package. We also decide to make a separate
% environment \env{continued} for it\footnote{In the example files for
% examples 27 and 28 this is also done; it is just not documented here.}.
%
% The first thought might be to use
% \cs{afterpage}\texttt{\{\cs{firstxmark}\}}. But the marks can only be
% used in the headers and footers, not in the running text%
% \footnote{NOTE: This used to be different in \pkg{extramarks} version 4 and earlier.}.
% Moreover, we
% need the value that will become \cs{firstxmark} (=\cs{firstleftxmark})
% on the next page, but on the current page it will be in \cs{lastleftxmark}.
%
% Then you might think that the \cs{afterpage} command could be put in
% a header or footer, but unfortunately it appears that then the timing is wrong.
% The \cs{afterpage} text will appear one page too late.
%
% So what we do is, we put the \cs{lastleftxmark} in a variable during the
% footer processing and then use this variable in \cs{afterpage}. As the
% footer processing is done inside a \TeX{} group, we must use a global
% definition. Also the mark must be expanded so that we get the contents
% of the mark in our variable and not just the name. We can do this with
% the primitive \TeX{} command \cs{xdef}. There is no \LaTeXe{} command
% for this.
%
% First we give a simple (but
% incomplete) solution.
%
% \NoExample{Incomplete!}
% \begin{example}
% \newcommand\ContiText{}
% \fancyhead[L]{Example 29a}
% \fancyhead[R]{\rightmark}
% \fancyfoot[R]{\lastrightxmark}
% \fancyfoot[L]{\xdef\ContiText{\lastleftxmark}}
%
% \newenvironment{continued}{%
%   \par
%   \extramarks{}{}%
%   \extramarks{\noindent Continued\ldots\\[1ex]}%
%              {Continued on next page\ldots}%
%   \afterpage{\ContiText}%
%   \ignorespaces
%  }{%
%   \unskip
%   \extramarks{\noindent Continued\ldots\\[1ex]}{}%
%   \extramarks{}{}\par
% }
% \end{example}
%
% The header contains document information: the name of the document on
% the left and the section title on the right. The footer contains the
% ``Continued'' information like in the previous examples. The
% \cs{extramarks} contain essentially the same information as in the
% previous examples, just formatted a little differently.
% But the \cs{...leftxmark} is not put in the header, but is eventually used as
% the argument in \cs{afterpage} so that it will appear at the top of the
% next page body. This is also the reason for the \verb|\\[1ex]|, to
% separate it from the rest of the page text.
%
% Note how we use \cs{ignorespaces}, \cs{unskip} and \texttt{\%} to
% prevent unwanted spaces to creep into the text.
%
% However, there are some problems with this simple solution:
% \begin{enumerate}
% \item If the block spans more than one page boundary, the
% \cs{afterpage} is not repeated on the following page breaks
% (\cs{afterpage} only applies to the next page). So on
% these pages the ``Continued'' header will be missing.
%
% We can solve this by repeating the \cs{afterpage} command in the
% \cs{afterpage} text. To do this we have to put it in a macro
% (\texttt{AP} stands for \texttt{afterpage}):
% \begin{code}
% \newcommand{\setAP}{\afterpage{\ContiText\setAP}}
% \end{code}
% There is a disadvantage that the \cs{afterpage} will be continued on
% all pages after the block has ended. But as \cs{lastleftxmark} will be
% empty then, no harm will be done. However, the following subsection (\ref{sec:afterpage2})
% will give a solution that stops this repetition.
% \item If the page break comes out such that the beginning of the block
% is pushed to the next page, but the \cs{afterpage} is given while
% \LaTeX{} was still at the previous page, the \cs{afterpage} text will
% be inserted before the block begins.
%
% Fortunately the \cs{lastleftxmark} on this page is empty, so the
% \cs{afterpage} on this page is essentially harmless, and because we have it made
% repeating by the previous point, it will be picked up at the
% proper place.
% \item If there is more than one \env{continue} block on the same
% page (with the last one crossing the page boundary) there will be an
% \cs{afterpage} for each block, thereby repeating the ``Continued''
% text multiple times at the top of the page. Therefore we should start
% the \cs{afterpage} only once, not once for each block. As the
% \cs{afterpage} is repeated on each page by the previous solution we
% don't need multiple starts of \cs{afterpage}.
%
% We could do this by inserting the \cs{afterpage} command before the
% first block instead of inside it, but that is error-prone.
%
% The solution is to define a command \cs{startAP} that sets the
% \cs{afterpage} command, and then redefines itself do do nothing.
% Because the \cs{startAP} is called inside a \TeX{} group (the
% \env{continued} environment) we must do a \emph{global} redefine.
% \LaTeXe{} does not have a command for this, so we use the low-level
% \TeX{} command \cs{gdef} for this.
% \begin{code}
% \newcommand{\startAP}{\setAP\gdef\startAP{}}
%  . . .
% \newenvironment{continued}{%
%   . . .
%   \startAP
% } . . .
% \end{code}
% \end{enumerate}
%
% We also put some thick black rules around the environment.
% And because the text for the left mark is used twice we put that in
% a macro \cs{LM}. The order of the commands is chosen such that the
% `Continued' marks don't go to the wrong page.
% This makes the total solution like this:
%
% \Example{29a}
% \begin{example}
% \newcommand\ContiText{}
% \newcommand{\LM}{\noindent\hl{Continued from previous page\ldots}\\[1ex]}
% \newcommand{\setAP}{\afterpage{\ContiText\setAP}}
% \newcommand{\startAP}{\setAP\gdef\startAP{}}
%
% \fancyhead[L]{Example 29a}
% \fancyhead[R]{\rightmark}
% \fancyfoot[R]{\lastrightxmark}
% \fancyfoot[L]{\xdef\ContiText{\lastleftxmark}}
%
% \newenvironment{continued}{%
%   \par\startAP
%   \extramarks{}{}%
%   \noindent\rule{\textwidth}{1mm}%
%   \extramarks{\LM}{Continued on next page\ldots}%
%   \\*\ignorespaces
% }{%
%    \unskip\noindent\rule{\textwidth}{1mm}%
%    \extramarks{\LM}{}%
%    \extramarks{}{}\par
% }
% \end{example}
%
% \subsection{More sophisticated solutions}
% \label{sec:afterpage2}
%
% In this subsection we present some more sophisticated, and therefore
% a little more tricky solutions and variations to the previous example.
% If you want to avoid that trickery, you can just skip this subsection.
%
% First we change the example such that the sequence of \cs{afterpage}
% invocations will stop as soon as possible. We do this by not using a
% fixed text as argument for \cs{afterpage} but by using a macro \cs{APcommand}
% as argument. When we want to stop the sequence of \cs{afterpage} calls,
% we make this macro empty. To get a proper timing we reset this macro
% in the righthand footer field when this is empty, which indicates that
% we are outside of a `Continued' block.
%
% We must then take care of restarting the \cs{afterpage} sequence when
% a new `Continued' block is started, and making sure that we don't get
% more than one such sequence activated. We do this by changing
% \cs{startAP} such that it only start an \cs{afterpage} if
% \cs{APcommand} is empty.
%
% \Example{29b}
% \newcommand{\R}{\color{red}}
% \begin{example}[numbers=left,numbersep=1pt,commandchars=|\(\)]
% \newcommand\ContiText{}
% \newcommand*{\LM}{\noindent Continued from previous page\ldots\\[1ex]}
% |R\newcommand*{\APcommand}{}|label(line:apcommand)
% |R\newcommand*{\setAPcommand}{\gdef\APcommand{\ContiText\setAP}}|label(line:set)
% |R\newcommand*{\clearAPcommand}{\gdef\APcommand{}}|label(line:clr)
% |R\newcommand*{\setAP}{\afterpage{\APcommand}}|label(line:setap)
% |R\newcommand*{\startAP}{\ifx\APcommand\empty\setAPcommand\setAP\fi}|label(line:startap)
%
% \fancyhead[L]{Example 29b}
% \fancyhead[R]{\rightmark}
% \fancyfoot[R]{\lastrightxmark}
% \fancyfoot[L]{\xdef\ContiText{\lastleftxmark}}
% \fancypagestyle{plain}{\fancyhead{}\renewcommand{\headrule}{}}
%
% \newenvironment{continued}{%
%   \par\startAP
%   \extramarks{}{}%
%   \noindent\rule{\textwidth}{1mm}%
%   \extramarks{\LM}{Continued on next page\ldots}%
%   \\*\ignorespaces
% }{%
%    \unskip\noindent\rule{\textwidth}{1mm}%
%    \extramarks{\LM}{}%
% |R  \extramarks{}{\protect\clearAPcommand}\par|label(line:callclr)
% }
% \end{example}
%
% We have numbered the lines for easy reference. The changes are in
% the red lines (\ref{line:apcommand}--\ref{line:startap} and \ref{line:callclr}). \\
% \ref{line:apcommand}.~Here we define \cs{APcommand}.\\
% \ref{line:set},~\ref{line:clr}.~These are commands to set en clear \cs{APcommand}, respectively.\\
% \ref{line:setap}.~The \cs{afterpage} now uses \cs{APcommand} as argument.\\
% \ref{line:startap}.~\cs{startAP} now checks if \cs{APcommand} is empty, and if it is,
% it first fills \cs{APcommand} with the required value and then starts a
% new \cs{afterpage} (with the \cs{setAP} command).
% When \cs{APcommand} is not empty this means that an \cs{afterpage} is
% already active.\\
% \ref{line:callclr}.~In the right part of the marks we now call \cs{clearAPcommand} to
% clear our variable \cs{APcommand}. This effectively stops the
% \cs{afterpage} sequence.
%
% \textbf{Note 1.} We use \cs{gdef} to change \cs{APcommand} because
% these occur inside a \TeX{} group (\env{continued} environment and
% footer). With \cs{renewcommand} they would be local to these groups
% but we need them outside of these groups, therefore we use \cs{gdef}
% to make the change globally.
%
% \textbf{Note 2.} We define \cs{APcommand} with \cs{newcommand*} rather
% than \cs{newcommand} to make it compatible with \cs{gdef}. Without the
% \texttt{*} it would be compatible with \cs{long}\cs{gdef}, but then it
% would not compare equal to \cs{empty} in line \ref{line:startap}. For the other
% definitions it does not make a difference, but it looks nicer to also use
% it there.
%
% \textbf{Note 3.} In line \ref{line:callclr} we use \cs{protect} to
% delay the expansion of \cs{clearAPcommand}. The marks in
% \cs{extramarks} are expanded at the time they are given, so that they
% can pick up section numbers and titles and similar information at that
% point. However, \cs{clearAPcommand} should not be expanded at that
% moment, but when it is used in the footer. That is exactly what
% \cs{protect} does.
%
% \textbf{Note 4.} We test the value of \cs{APcommand} with \cs{ifx},
% not with \cs{ifthenelse} from the \PKGindex{ifthen}\pkg{ifthen} package. The
% latter completely expands its parameters, and because \cs{APcommand}
% has a recursive definition when it is not empty, that would cause
% \TeX{} to fail. We only want to check the definition of
% \cs{APcommand}, not its expansion.
%
% \textbf{Note 5.} For debugging we can add some text in the
% \cs{afterpage} command in line \ref{line:setap}, to see the difference between an
% empty \cs{afterpage} and no \cs{afterpage} at all.
% Similarly we can add some text in the footer in line \ref{line:callclr}, to see where
% the \cs{clearAPcommand} is called.
%
% \subsubsection*{Another use}
%
% If you would need the information further on in the page you must
% remember the state of the marks in your own variable. You can set this
% in one of the \pkg{fancyhdr} header or footer fields, like in
% example 29a. For example
% if you want to add something \emph{after} the broken block of text you
% can use the following:
%
% \Example{29c}
% \begin{example}
% \newcommand{\ContiText}{}
% \fancyhead[L]{Example 29c}
% \fancyhead[R]{\rightmark}
% \fancyfoot[R]{\lastrightxmark}
% \fancyfoot[L]{\xdef\ContiText{\lastleftxmark}}
% \fancypagestyle{plain}{\fancyhead{}\renewcommand{\headrule}{}}
%
% \newenvironment{continued}{%
%   \par
%   \extramarks{}{}%
%   \noindent\rule{\textwidth}{1pt}%
%   \extramarks{\\[1ex]\noindent\textbf{[Continued
%               from previous page]}}{Continued on next page\ldots}%
%   \\*\ignorespaces
% }{%
%    \unskip\noindent\rule{\textwidth}{1pt}%
%    \extramarks{}{}%
%    \ContiText\par
% }
% \end{example}
%
% Now if the block crosses a page boundary, the \cs{lastleftxmark} has
% the text that should be put under the block.
% In the \texttt{[L]} footer field we put
% this information in the macro \cs{ContiText}, and this is
% typeset after the block ends. If the block doesn't cross the page
% boundary, this text is empty.
%
% \textbf{NOTE:} This example is not completely safe; there can still be timing
% issues. For example, when the end of the block has already been typeset,
% including an empty value of \cs{ContiText}, but then is pushed to the
% next page. So you have to be very careful in using this kind of mechanism.
%
% If you want to include one of the marks or other varying information in the
% saved text, you must use \cs{xdef} rather than \cs{gdef}.
%
% \section{Page styles for Table of Content, List of Figures, Bibliography, etc.}
% \label{sec:tocstyle}
%
% Some special sections of a documents, such as the Table of Contents,
% List of Figures/Tables, Bibliography, Index, and similar ones
% sometimes cause difficulties if you want them to have special page
% styles, especially if you also want the first page of these to have
% a special page style.
%
% Supose you have defined a special page style \texttt{tocstyle} for the
% Table of contents. The Table of contents is generated by the command
% \cs{tableofcontents} and it can be several pages long, all generated
% by this simple command. So if you want this to have the page style
% \texttt{tocstyle}, you must give the command \Cmd{pagestyle\{tocstyle\}}
% before the \cs{tableofcontents}. But then you have to make sure that
% the previous page (for example a title page) doesn't get this page
% style too. As we have seen before we can do this by inserting a
% \cs{newpage} first. Like
% \begin{code}
% \newpage
% \pagestyle{tocstyle}
% \tableofcontents
% \end{code}
%
% If we use a chapter based documentclass, like the standard classes
% \pkg{report} and \pkg{book}, with this setup the first page of the Table
% of Contents, and similar parts of the document will stil use the
% \texttt{plain} page style. Usually this is the best choice, but there
% may be cases where you want these also to use the \texttt{tocstyle}
% page style (or another special page style). The \texttt{plain} page
% style is set by a \Cmd{thispagestyle\{plain\}} command embedded
% in a \cs{chapter*} command that is used in \cs{tableofcontents}. So
% it is not easy to overwrite. It can be overwritten by a
% \Cmd{thispagestyle\{tocstyle\}} command, but that must be given after
% the \cs{chapter*} command, but before the first page of the Table of
% Contents is finished. So in fact we must break in into the
% \cs{tableofcontents} command. The other special parts have similar challenges.
% In this section we give a number of solutions.
%
% The first solution applies to the Table of Contents and List of Figures/Tables.
% We can add additional code in these lists with the \cs{addtocontents}
% command. We can use this to insert a \cs{thispagestyle} as the first
% entry,
% \NoExample{tocpagestyle\\(a)}
% \begin{example}
% \newpage
% \pagestyle{tocstyle}
% \addtocontents{toc}{\protect\thispagestyle{tocstyle}}
% \tableofcontents
% \end{example}
% and similar for the List of Figures (use \texttt{lof}) and List of
% Tables (use \texttt{lot} instead of \texttt{toc}). The \cs{protect} is
% necessary to prevent the \cs{thispagestyle} to be executed too early.
%
% \textbf{NOTE:} If you are using the package
% \pkg{tocloft}\PKGindex{tocloft}, some of the solutions given here for
% the Table of Contents and List of Figures/Tables may not work (but
% others may). This is because this package changes the layout of these.
% In particular, the first page of these will by default have page style
% \texttt{plain}, even in a documentclass that has no chapters, like
% \cls{article}. However, the package has a command to set the page
% style for these first pages. This will set it for all three.
%\begin{code}
% \tocloftpagestyle{tocstyle}
% \end{code}
%
% Alternatively, you can use \Cmd{usepackage[titles]\{tocloft\}} which
% will keep the original \LaTeX{} code, so then the solutions mentioned
% in this section will apply.
%
% \medskip
%
% For the Bibliography and Index the above solutions cannot be used. So
% we need a different solution for these.
%
% For the Bibliography we can use the \LaTeX{} hook system (available since the
% 2020/10/01 \LaTeX{} release). The command \cs{bibliography} reads the
% file \texttt{\meta{jobname}.bbl}, which contains a
% \env{thebibliography} environment, that contains a \cs{bibitem} for
% each reference. The \Cmd{begin\{bibliography\}} sets up the chapter
% header and starts a \env{list} environment. Unfortunately, the
% \LaTeX{} hook system doesn't have a hook that is executed just after
% this point (i.e., before any bibliography items are added).
%
% However, we can add a hook at the first \cs{bibitem} as follows:
% \NoExample{tocpagestyle\\(c)}
% \begin{example}
% \AddToHookNext{cmd/bibitem/before}{\thispagestyle{tocstyle}}
% \end{example}
%
% An alternative would be to use an `\texttt{after}' hook on the
% \cs{thebibliography} command. This command is internally used to do
% the setup work for the \env{thebibliography} environment, but this
% is an implementation detail, so theoretically this could change in a
% future implementation, or an implementation in a different
% documentclass. But this is the hook that is executed at the right
% moment: after the setup, and before any bibliography items are added.
%
% \NoExample{tocpagestyle\\(c)}
% \begin{example}
% \AddToHook{cmd/thebibliography/after}{\thispagestyle{tocstyle}}
% \end{example}
%
% For the Index there is no hook that can be used in a similar manner.
% Maybe we could use a hook in the \cs{item} command that is used for
% the index items, but this is just a too general command that is not
% exclusively used in the Index. However, we can use a hook on the
% \cs{thispagestyle} command that is used in the internal
% \cs{chapter*} command in the \cs{printindex} command (the
% \cs{thispagestyle} that gives us these problems). We just use an
% `\texttt{after}' hook to insert another \cs{thispagestyle} that
% replaces the built-in one. In fact we could have used this same
% solution for each of the cases mentioned in this document.
%
% \NoExample{tocpagestyle\\(d)}
% \begin{example}
% \newpage
% \pagestyle{tocstyle}
% \AddToHookNext{cmd/thispagestyle/after}{\thispagestyle{tocstyle}}
% \printindex
% \end{example}
%
% Finally, we can use the solution from
% section~\ref{sec:pagestyle-plain} (redefining page style
% \texttt{plain}). For example:
% \begin{code}
% \fancypagestyle{plain}[tocstyle]{}
% \end{code}
% But this would also change the \texttt{plain} page style for the
% chapters in the normal text, which we don't want. So the page style
% \texttt{plain} should be reset in the main text. We can do this with
% the new (\pkg{fancyhdr} version 5) command \cs{fancypagestyleassign}.
% We can use this to `save' the original \texttt{plain} page style and
% set it equal to \texttt{tocstyle}. And later we can reset
% \texttt{plain} to the saved page style.
%
% \NoExample{tocpagestyle\\(b)}
% \begin{example}
% \fancypagestyleassign{origplain}{plain}
% \fancypagestyleassign{plain}{tocstyle}
% \listoftables % \tableofcontents / \listoffigures, etc.
%
% % Here the main document text starts
%
% \clearpage
% \fancypagestyleassign{plain}{origplain}
% \end{example}
% All definitions for the page style (including the
% \cs{fancypagestyleassign} commands above) are local to the \TeX{}
% group in which they are defined (see section~\ref{sec:change-group}).
% So we could eliminate the saving and restoring to \texttt{origplain}
% if we do the change in a group. For example:
%
% \begin{code}
% {
% \fancypagestyleassign{plain}{tocstyle}
% % or use the older method \fancypagestyle{plain}[tocstyle]{}
% \listoftables
% }
% \end{code}
% After the group, page style \texttt{plain} has its original value.
% However, it is not advised to place large parts of your document
% inside a group.
%
% \section{A movie}
% \label{sec:movie}
%
% \index{movie}
% \TTindex{picture}
% If you put at each page on the same place a picture that slightly changes
% from page to page you can get a movie-like effect by flipping through the
% pages. You can create such a movie easily with \pkg{fancyhdr}. For
% simplicity we assume that we use a PDF-producing \LaTeX{} (such as
% \texttt{pdflatex}) and each picture is in a PNG file called
% \texttt{pic}$\langle n\rangle$.\texttt{png}\footnote{With
% \texttt{pdflatex} we could also use PDF or JPG
% pictures. With a DVI based \texttt{latex} we could use PS or EPS
% pictures. Or any other supported image format.}
% where $\langle n\rangle$ is
% the page number and that we use the \pkg{graphics} or  \pkg{graphicx} package.
% \PKGindex{graphics}
% \PKGindex{graphicx}
% To put the movie in the  righthandside bottom corner the following will work:
%
% \Example{30}
% \begin{example}
% \fancyfoot[R]{\setlength{\unitlength}{1mm}
%   \begin{picture}(0,0)
%     \put(5,-20){\includegraphics[width=1cm]{pic\thepage}}
%   \end{picture}}
% \end{example}
%
% If the document is two-sided, it would be better to put them only on
% the odd pages, by specifying \verb|\fancyfoot[RO]|.
%
% Notice that the \cs{unitlength} parameter should
% be set locally in the \pkg{fancyhdr} field in order to avoid unwanted
% interference with its value in the text.
%
% \section{Thumb-indexes}
% \label{sec:thumb}
%
% \index{bible}
% Some railroad guides and expensive bibles have so called
% \index{thumb-index}
% \emph{thumb-indexes}, i.e., there are marks on the sides of the pages that
% indicate where the chapters are. You can create these by printing black
% blobs in the margin of the pages. The vertical position should be
% determined by the chapter number or some other counter. As the position is
% independent of the contents of the page, we print these blobs as part of
% the header in a zero-sized \texttt{picture} as described in the previous
% section.
%
% Of course we have to take care of two-sided printing, and we may want to
% have an index page with all the blobs in the correct position. The solution
% requires some hand-tuning to get the blobs nicely spaced out vertically.
% For the application that I originally designed this for, there were 12 sections,
% so I made the blobs
% 18 mm apart, i.e., 9~mm blob separated by 9~mm whitespace. In order to avoid
% calculations they are set in a \texttt{picture} environment with the
% \cs{unitlength} set to 18~mm. Page numbers are set in the headers at the
% outer sides, and the blobs are attached to these. In this example the
% chapter numbers are used to position the blobs, but you can replace this
% with any numeric value.
% See figure~\ref{fig:overview} for the resulting overview page.
%
% ^^A Picture with thumb index overview
%
% \begin{figure}[!htbp]
% \setlength{\unitlength}{9mm}
% \newcommand{\blob}{\rule[-.2\unitlength]{1\unitlength}{.5\unitlength}}
% \newcounter{line}
% \newcommand{\chapname}[1]{\addtocounter{line}{1}%
%   \put(1,-\value{line}){\blob}
%   \put(-7.5,-\value{line}){\arabic{line}}
%   \put(-7,-\value{line}){#1}}
%
% \newcommand{\overview}{1
%   \begin{picture}(0,0)
%     \chapname{Introduction}
%     \chapname{Another chapter}
%     \chapname{Third case}
%   \end{picture}}
%
%   \begin{center}
%     \leavevmode
%     \begin{picture}(11.3,5)
%       \put(0,0){\framebox(11.3,5)[tr]{}}
%       \put(9,4.5){\overview}
%     \end{picture}
%   \end{center}
%   \caption{Thumb-index overview page}
%   \label{fig:overview}
% \end{figure}
%
% ^^A The code
% \Example{31}
% \begin{example}
% \setlength{\unitlength}{18mm}
% \newcommand{\blob}{%
%   \rule[-.2\unitlength]{2\unitlength}{.5\unitlength}}
%
% \newcommand\rblob{\thepage
%   \begin{picture}(0,0)
%     \put(1,-\value{chapter}){\blob}
%   \end{picture}}
%
% \newcommand\lblob{%
%   \begin{picture}(0,0)
%     \put(-3,-\value{chapter}){\blob}
%   \end{picture}%
%   \thepage}
%
% \pagestyle{fancy}
% \fancyfoot{}
%
% \newcounter{line}
% \newcommand{\chapname}[1]{\addtocounter{line}{1}%
%   \put(1,-\value{line}){\blob}
%   % Adjust these numbers for the proper indentation
%   \put(-5.5,-\value{line}){\Large \arabic{line}}
%   \put(-5,-\value{line}){\Large #1}}
%
% \newcommand{\overview}{%
%   \begin{picture}(0,0)
%     \chapname{Introduction}
%     \chapname{Another chapter}
%     \chapname{Third case}
%     . . .
%   \end{picture}
% }
% \end{example}
% The overview page:\\
% The page doesn't have `contents' --
% all the visual contents is generated
% by the \verb|\overview| command in the header
% \Example{31}[(continued)]
% \begin{example}
% \fancyhead[L]{Overview}
% \fancyhead[R]{\overview}
% \mbox{}\newpage % This produces the overview page
%
% % Front matter -- doesn't have blobs.
%
% \fancyhead[RE]{\rightmark}
% \fancyhead[RO,LE]{}
% \fancyhead[LO]{\leftmark}
%
% \pagenumbering{roman}
% \thispagestyle{plain}
% \tableofcontents
%  . . .
% \newpage
%
% % Here the document begins
%
% \pagenumbering{arabic}
%
% % Now activate the blobs
%
% \fancyhead[RO]{\rblob}
% \fancyhead[LE]{\lblob}
%
% % Page style 'plain' does not have the usual header,
% % but it does have the blobs.
%
% \fancypagestyle{plain}{%
%   \fancyhead[RE,LO]{}
%   \renewcommand{\headrule}{}%
% }
% \end{example}
%
% \section{Float placement}
%
% \textbf{Note: This section is not about \pkg{fancyhdr}, but about
% page layout, especially about the placement of floats.}
%
% Floats are page elements that float with respect to the rest of the
% document. Standard floats are tables and figures, but with the
% \PKGindex{float}%
% \pkg{float} package you can easily make new ones, like algorithms. Most
% of the time floats work satisfactory, but sometimes \LaTeX{} seems too
% stubborn to do what you want. This section describes how you can influence
% \LaTeX{} so that it will do most of the time what you want. There might,
% however, be some pathological cases where it is impossible to convince
% \LaTeX{} to do things your way. In the following we will use figures as an
% example but everything applies to other floats as well.
%
% The most encountered problems with floats are:
% \begin{enumerate}
% \item You want a float at a certain position in the text, but \LaTeX{}
%   moves it, usually to the next page.
% \item From a certain point, \LaTeX{} moves all your floats to the end of
%   the document or the end of a chapter.
% \item \index{Too many floats}%
%   \LaTeX{} complains about ``Too many floats''.
% \end{enumerate}
%
% In the first two cases you must first check if you have given the correct
% ``placement'' parameter to you float, e.g., \Cmd{begin\{figure\}[htp]}
% specifies that your figure may be placed either: Here (i.e., in the text
% position where the command is given), on the Top of a page (which may be
% the page where you put the command), or on a separate Page of floats. You
% could also have specified ``\texttt{b}'' for Bottom of the page. The order of the
% letters is insignificant, you cannot force \LaTeX{} to try Bottom first
% and then Top by specifying \texttt{[bt]}.
%
% If \LaTeX{} doesn't put the float at the place where you expected it, it is
% usually caused by the following:
% \begin{enumerate}
% \item The float didn't fit on the page. In this case it has to move to the
%   next page or even further. If you didn't specify either \texttt{[t]} or
%   \texttt{[b]} in the position parameter, \LaTeX{} must save it until it has
%   enough for a page of floats. So don't specify only \texttt{[h]}. If you
%   want to give \LaTeX{} a chance to put the float on a page of floats, you
%   must also specify ``\texttt{p}''.
% \item The placement would violate the constraints imposed by \LaTeX's
%   float placement parameters. This is one of the most occurring causes and
%   it can easily be corrected by changing the parameters. Here is a list of
%   them with their default values:
% \end{enumerate}
% \begin{center}
% \small
% \CmdIndex{topfraction}
% \CmdIndex{bottomfraction}
% \CmdIndex{textfraction}
% \CmdIndex{floatpagefraction}
% \TTindex{topnumber}
% \TTindex{bottomnumber}
% \TTindex{totalnumber}
%   \begin{tabular}{>{\tt}llc}
%     \toprule
%     \multicolumn{3}{c}{Counters -- change with \cs{setcounter}}\\
%     \midrule
%     topnumber & max. number of floats at top of page & 2 \\
%     bottomnumber & max. number of floats at bottom of page & 1 \\
%     totalnumber &  max. number of floats on a page & 3\\
%     \midrule
%     \multicolumn{3}{c}{Other -- change with \cs{renewcommand}}\\
%     \midrule
%     \bs topfraction & max fraction of page for floats at top & 0.7 \\
%     \bs bottomfraction & max fraction of page for floats at bottom & 0.3 \\
%     \bs textfraction & min fraction of page for text & 0.2 \\
%     \bs floatpagefraction & min fraction of floatpage that should have
%     floats  & 0.5 \\
%     \bottomrule
%   \end{tabular}
% \end{center}
%
% There are also some others for double column floats in two-column documents.
%
% The default values are for the standard
% \LaTeX{} classes. Other classes could use different defaults. As you see
% with the default values a float will not be put in the bottom of a page if
% its height is more than 30\% of the page height. So if you specify \texttt{[hb]} for
% a float which is taller it has to move to a float page. But if it is less
% than 50\% of the page height it will have to wait until some more floats
% are given before a float page can be filled to satisfy the
% \phantomsection\label{page:floatpagefraction}^^A
% \cs{floatpagefraction} parameter. If you have this kind of behaviour you
% can easily adapt the parameters, e.g., with:
% \begin{code}
% \renewcommand{\textfraction}{0.05}
% \renewcommand{\topfraction}{0.95}
% \renewcommand{\bottomfraction}{0.95}
% \renewcommand{\floatpagefraction}{0.35}
% \setcounter{totalnumber}{5}
% \end{code}
% You may want to be careful not to make \cs{floatpagefraction} too small,
% otherwise you may get too many small floatpages.
%
% You can force \LaTeX{} to ignore most of the parameters for one specific float
% occurrence by
% including an exclamation mark (\texttt{!}) in the placement parameters, e.g.,
% \begin{code}
% \begin{figure}[!htb]
% \end{code}
%
% Floats which contain a ``\texttt{t}'' in the position parameter could be
% placed before the place where they are referenced (but on the same page).
% This is normal behaviour for \LaTeX{} but some people just don't like it.
% There are a number of ways to prevent this:
% \begin{enumerate}
% \item Of course deleting the ``\texttt{t}'' will help, but in general this is
%   undesirable, as you may want the float to be placed at the top of the
%   next page.
% \item \PKGindex{flafter}%
%   use the \pkg{flafter} package which causes floats never to be
%   placed ``backwards''.
% \item use the command \cs{suppressfloats[t]}.  This
% \CmdIndex{suppressfloats}%
%   command will cause floats for the top position \emph{on this page} to be
%   moved to the next page. This can also be done with \texttt{[b]} or
%   without parameter for all floats on this page.
% \end{enumerate}
%
% If in spite of all your attempts \LaTeX{} still moves your floats to the end
% of the document or the end of a chapter, you can insert a \cs{clearpage}
% command. This will start a new page and insert all pending floats before
% continuing. If it is undesirable to have a pagebreak you can use the
% \pkg{afterpage} package and the following command:
% \PKGindex{afterpage}\CmdIndex{afterpage}\CmdIndex{clearpage}%
% \begin{code}
% \afterpage{\clearpage}
% \end{code}
%
% This will wait until the current page is finished and then flush all
% outstanding floats. In some pathological circumstances \pkg{afterpage}
% may give strange results, however.
%
% Finally, if you want a float only at the place where you define it, without
% \LaTeX{} moving it whatsoever, you can use the \pkg{float} package and
% give the command:
% \PKGindex{float}\CmdIndex{restylefloat}%
% \begin{code}
% \restylefloat{figure}
% \end{code}
%  in the preamble. Now you will be able to
% specify \texttt{[H]} as the position parameter, which will mean ``HERE
% and only HERE''. This may cause an unwanted page break however.
% If you want to avoid the unwanted pagebreak, i.e., let
% \LaTeX{} move the float only if it doesn't fit on the page, then use the
% \pkg{afterpage} package with:
% \PKGindex{afterpage}\CmdIndex{afterpage}\CmdIndex{clearpage}%
% \begin{code}
% \afterpage{\clearpage \begin{figure}[H] ... \end{figure}}
% \end{code}
%
% \index{Too many floats}%
% Complaints from \LaTeX{} about ``Too many floats'' are usually caused by one
% of the above problems: floats not being able to be placed and \LaTeX{}
% collecting too many of them. The solutions given above, especially those
% with \cs{clearpage} in them will usually help. In some cases there
% really are too many floats, as \LaTeX{} has a limited number of ``boxes'' to
% store the floats. The package
% \PKGindex{morefloats}%
% \pkg{morefloats} can be used to increase
% this number. If you need still more then you must edit a private copy of
% this file, but even then there will be some limit that you cannot pass.
% Then your only resort will be to change your document.
%
% A much more elaborate article about float placement by Frank
% Mittelbach appeared in 2014 in TUGboat\footnote{%
% Frank Mittelbach, \emph{How to influence the position of float
% environments like figure and table in LATEX?}, TUGboat, Volume 35
% (2014), No. 3, pp. 248–254.\\
% \url{https://www.latex-project.org/publications/2014-FMi-TUB-tb111mitt-float-placement.pdf}
% \\Also on Stackexchange:\\
%  \url{https://tex.stackexchange.com/questions/39017/how-to-influence-the-position-of-float-environments-like-figure-and-table-in-lat}}.
%
% \section{Multipage Floats}
%
% \LaTeX's floats cannot be split across pages. Sometimes, however, you want
% to have a table or figure that doesn't fit on one page. The easiest way is
% to split these into multiple table or figure environments, but this has a
% number of undesirable effects:
% \begin{itemize}
% \item Where do you split it? This is generally a more difficult decision
%   for tables than for figures.
% \item How do you keep them together?
% \item You don't want more than one entry in the list of figures/tables.
% \end{itemize}
%
% Although these problems are not fully solvable in all cases, here are a
% couple of suggestions:
%
% \subsection{Tables}
%
% For tables longer than a page you can use the \pkg{longtable} package.
% \PKGindex{longtable}
% This package defines a \texttt{longtable} environment that is a kind of
% amalgamation of \env{table} and \env{tabular}. It has approximately
% the same syntax as the  \env{tabular} environment, but it adds some
% features of \env{table}, like captions. Longtables will be automatically
% split when they don't fit on the page. And they will be entered in the list
% of tables when a caption is given. They will not float, however, and cannot
% be used inside a float environment. This could mean that another
% \texttt{table} environment, which was defined before the
% \texttt{longtable}, will float past it, and therefore the numbers may get
% out of order. Another problem could be that the \texttt{longtable} starts
% rather far down the page, which isn't a pleasant sight. If you want the
% \texttt{longtable} to start at the top of the page, the best thing to do is
% to include it in an \cs{afterpage} command (using the \PKGindex{afterpage}\pkg{afterpage}
% package). As a \env{longtable} is by definition large, it is best to put
% it in a separate file, and \cs{input} it in the \cs{afterpage} command:
% \CmdIndex{afterpage}\PKGindex{afterpage}\CmdIndex{clearpage}%
% \begin{code}
% \afterpage{\input{mytable}}
% \end{code}
% or
% \begin{code}
% \afterpage{\clearpage\input{mytable}}
% \end{code}
% The last form has the additional advantage that most of the outstanding
% floats will be printed first.
%
% \subsection{Figures}
%
% There isn't an equivalent ``\texttt{longfigure}'' solution, so for figures you
% will have to split yourself. In general this is less of a
% problem. However, the problem you get now is how to keep them together,
% i.e., how to get the parts on subsequent pages, and how to get a single
% entry in the list of figures.
%
% You will have to split the figure into pieces and put each part
% in a separate \texttt{figure} environment. The first
% part would then get a \cs{caption}, the subsequent parts would be used
% without a caption, or a caption that will not go to the list of figures.
% If you want to add a caption-like text, enter it as
% normal text rather than a \cs{caption}, so that it will not be entered in
% the list of figures. It may also be desirable to issue a \cs{clearpage}
% first, just like we did for the \texttt{longtable}.
%
% We give a series of possible solutions here, which can be found in
% Example 33.
%
% First we include the \texttt{figure}s with the \texttt{[!htbp]}
% position option to give \LaTeX{} maximum freedom to place them. This
% way we hope they keep them together, although there is no guarantee.
%
% \Example{33}[(A)]
% \begin{example}
% \newcommand{\fakecaption}[2]{% #1 = figure label #2 = caption
%   \par Figure~\ref{#1}: #2
% }
% \begin{figure}[!htbp]
%   \centering
%   \includegraphics[scale=0.5]{example-image-a}
%   \caption[This is a multipart figure] % For the list of figures
%           {This is a multipart figure (a)}
%   \label{fig:first}
% \end{figure}
% \begin{figure}[!htbp]
%   \centering
%   \includegraphics[scale=0.5]{example-image-b}
%   \fakecaption{fig:first}{This is a multipart figure (b)}
% \end{figure}
%  . . .
% \end{example}
%
% There will probably be some of the normal text between the figure
% parts, unless they happen to fit perfectly on the page, which isn't
% very probable. But, what also can come between them is other floats,
% such as a \env{table}. We can prevent that previous floats intrude
% here by issuing a \cs{clearpage} command, but this will abruptly end
% the current page. As we have seen before, we can do better by
% \CmdIndex{afterpage}\CmdIndex{clearpage}%
% including the \cs{clearpage} command in \cs{afterpage}, and we would
% also put the figures in the \cs{afterpage}. To keep the \cs{afterpage}
% command more tidy, it is advised to put the code for the figures in a
% macro, or in a file that is included with \cs{input}. For example:
%
% \Example{33}[(B)]
% \begin{example}
% \newcommand{\myfigures}{%
%   \begin{figure}[!htbp]
%     \centering
%     \includegraphics[scale=0.5]{example-image-a}
%     \caption[This is a multipart figure] % For the list of figures
%             {This is a multipart figure (a)}
%     \label{fig:second}
%   \end{figure}
%   \begin{figure}[!htbp]
%     \centering
%     \includegraphics[scale=0.5]{example-image-b}
%     \fakecaption{fig:second}{This is a multipart figure (b)}
%  . . .
% }
%  . . .
% \afterpage{\clearpage\myfigures}
% \end{example}
% \CmdIndex{afterpage}
%
% If you want your multipage figure to start at a lefthand-side
% (even-numbered) page you can use a test in the \cs{afterpage} command
% (using the \PKGindex{ifthen}\pkg{ifthen} package):
% \CmdIndex{afterpage}
% \CmdIndex{ifthenelse}
% \begin{code}
% \afterpage{\clearpage
%   \ifthenelse{\isodd{\value{page}}
%     {\afterpage{\myfigures}} % odd page
%     {\myfigures}}} % even page
% \end{code}
%
% If there are too many floats on the skipped page, this may still fail to
% start your multipage figure on an even page, however.
%
% But if there is enough space left on a page, some of the text will go
% between the figures. Also, if there is still some figure part of a
% previous sequence that has not yet found a place, it will be forced out
% because of the \cs{clearpage} and the a new page will start, with the
% previous page not optimally filled.
%
% So using \cs{clearpage} may also not be optimal. We could also try
% to put the figure parts only on float pages, so that no intervening
% text will come between them. This can be done by using the position
% parameter \texttt{[p]}. This could cause them to be pushed towards
% the back of the document. This is because float pages need to be
% reasonably full before they are generated. You could try to cure this
% for example by adding some \cs{vspace} to the last part, or by
% tweaking the \cs{floatpagefraction}
% \CmdIndex{floatpagefraction} parameter (see
% section~\ref{page:floatpagefraction} on page~\pageref{page:floatpagefraction}).
% To prevent previous floats to intrude in the float page, we also
% combine this with the \cs{afterpage} and \cs{clearpage}, as in the
% previous example, but this will probably push the figures even further
% towards the back.
%
% \Example{33}[(C)]
% \begin{example}
% \newcommand{\myfigures}{%
%   \begin{figure}[p]
%     \centering
%     \includegraphics[scale=0.5]{example-image-a}
%     \caption[This is a multipart figure] % For the list of figures
%             {This is a multipart figure (a)}
%     \label{fig:third}
%   \end{figure}
%   \begin{figure}[p]
%     \centering
%     \includegraphics[scale=0.5]{example-image-b}
%     \fakecaption{fig:third}{This is a multipart figure (b)}
%  . . .
% }
%  . . .
% \afterpage{\clearpage\myfigures}
% \end{example}
%
% So maybe just use the previous example without \cs{afterpage} and
% \cs{clearpage}.
%
% \Example{33}[(D)]
% \begin{example}
% \myfigures % (with the [p] placement)
% \end{example}
%
% The defects of the above approach are
% \begin{enumerate}
% \item It is clumsy to make the captions of all but the first part of the figure
% \item It is hard to refer to the parts separately
% \end{enumerate}
% \PKGindex{subcaption}%
% For this the \pkg{subcaption} package comes to the rescue. First it
% \CmdIndex{ContinuedFloat}%
% has a \cs{ContinuedFloat} command to indicate that a figure is a
% continuation of a previous one, and therefore will not get a new
% number, and if you wish, neither a separate entry in the list of figures.
%
% \CmdIndex{subcaptionbox}\TTindex{subfigure}%
% Second, it has a \cs{subcaptionbox} command and a \env{subfigure}
% environment for the parts, where a subcaption can be given, that can
% also have a \cs{label} to refer to in the document. The
% \cs{subcaptionbox} is a specialized \cs{parbox} but its \emph{width}
% parameter is optional. The \env{subfigure} environment is a
% specialized \env{minipage}, so it has the same parameters.
%
% These should be used inside a \env{figure} environment, so all the
% placement methods of the previous part (Examples 33 A–D) should still apply.
%
% The \env{subfigure} environment has a \cs{subcaption} command for
% the subcaption; the \cs{subcaptionbox} has the subcaption (with its
% \cs{label} if desired) as its first argument. When more than one
% \cs{subcaptionbox} is horizontally next to each other, the subcaptions
% will be aligned.
%
% In the following example (figure~\ref{fig:subfigures})
%  we use a \cs{subcaptionbox} for the first two
% parts, which are together in a single \env{figure} environment. We
% use a \env{subfigure} environments for the other two, each one in
% its own \env{figure} environment. These use a
% \cs{caption}\texttt{[]\{\ldots\}}. The empty optional argument
% \texttt{[]} causes the caption not to appear in the list of figures.
% The last subfigure (\ref{subfig:last} on page~\pageref{subfig:last})
% has a label on the \cs{subcaption} that we refer to in this sentence.
%
% \Example{33}[(E)]
% \begin{example}
% \begin{figure}[p]
%   \centering
%     \subcaptionbox{a subfigure in a \cs{subcaptionbox}}
%       {\includegraphics[scale=0.3]{example-image-a}}
%   \quad
%     \subcaptionbox{another subfigure, also in a \cs{subcaptionbox}}
%       {\includegraphics[scale=0.4]{example-image-b}}
%   \caption{A figure with subfigures}
%   \label{fig:subfigures}
% \end{figure}
%
% \begin{figure}[p]\ContinuedFloat
%   \begin{subfigure}{\textwidth}
%     \centering
%     \includegraphics[scale=0.5]{example-image-c}
%     \subcaption{subfigure}
%   \end{subfigure}
%   \caption[]{A figure with subfigures}
% \end{figure}
%
% \begin{figure}[p]\ContinuedFloat
%   \begin{subfigure}{\textwidth}
%     \centering
%     \includegraphics[scale=0.5]{example-image}
%     \subcaption{last subfigure}
%     \label{subfig:last}
%   \end{subfigure}
%   \caption[]{A fake caption just for demo}
% \end{figure}
% \end{example}
%
% \begin{figure}[p]
%   \centering
%     \subcaptionbox{a subfigure in a \cs{subcaptionbox}}
%       {\includegraphics[scale=0.3]{example-image-a}}
%   \quad
%     \subcaptionbox{another subfigure, also in a \cs{subcaptionbox}}
%       {\includegraphics[scale=0.4]{example-image-b}}
%   \caption{A figure with subfigures}
%   \label{fig:subfigures}
% \end{figure}
%
% \begin{figure}[p]\ContinuedFloat
%   \begin{subfigure}{\textwidth}
%     \centering
%     \includegraphics[scale=0.5]{example-image-c}
%     \subcaption{subfigure}
%   \end{subfigure}
%   \caption[]{A figure with subfigures (cont.)}
% \end{figure}
%
% \begin{figure}[p]\ContinuedFloat
%   \begin{subfigure}{\textwidth}
%     \centering
%     \includegraphics[scale=0.5]{example-image}
%     \subcaption{last subfigure}
%     \label{subfig:last}
%   \end{subfigure}
%   \caption[]{A fake caption just for demo}
% \end{figure}
%
% \section{Deprecated commands}
% \label{sec:depr}
%
% This section contains the description of deprecated commands. These
% were parts of the original implementation of \pkg{fancyheadings}.
% They continue to work for compatibility reasons,
% but it is recommended not to use them anymore. This description is
% given so that you know what they mean and how to convert them to the
% standard commands. To be honest, I use these sometimes myself in quick
% examples, because \cs{lhead} is less typing than \Cmd{fancyhead[L]}.
%
% These commands for specifying the header or footer fields and their translation to
% the modern commands are given in table~\ref{tab:depr}.
%
% \begin{table}[h!t]
% \begin{function}{\lhead, \chead, \rhead, \lfoot, \cfoot, \rfoot}
%   \begin{tabular}{ll}
%   \toprule
%   \Cmd{lhead\{xx\}} & \Cmd{fancyhead[L]\{xx\}} \\
%   \Cmd{lhead[xx]\{yy\}} & \Cmd{fancyhead[LE]\{xx\}} \Cmd{fancyhead[LO]\{yy\}} \\
%   \Cmd{chead\{xx\}} & \Cmd{fancyhead[C]\{xx\}} \\
%   \Cmd{chead[xx]\{yy\}} & \Cmd{fancyhead[CE]\{xx\}} \Cmd{fancyhead[CO]\{yy\}} \\
%   \Cmd{rhead\{xx\}} & \Cmd{fancyhead[R]\{xx\}} \\
%   \Cmd{rhead[xx]\{yy\}} & \Cmd{fancyhead[RE]\{xx\}} \Cmd{fancyhead[RO]\{yy\}} \\
%   \midrule
%   \Cmd{lfoot\{xx\}} & \Cmd{fancyfoot[L]\{xx\}} \\
%   \Cmd{lfoot[xx]\{yy\}} & \Cmd{fancyfoot[LE]\{xx\}} \Cmd{fancyfoot[LO]\{yy\}} \\
%   \Cmd{cfoot\{xx\}} & \Cmd{fancyfoot[C]\{xx\}} \\
%   \Cmd{cfoot[xx]\{yy\}} & \Cmd{fancyfoot[CE]\{xx\}} \Cmd{fancyfoot[CO]\{yy\}} \\
%   \Cmd{rfoot\{xx\}} & \Cmd{fancyfoot[R]\{xx\}} \\
%   \Cmd{rfoot[xx]\{yy\}} & \Cmd{fancyfoot[RE]\{xx\}} \Cmd{fancyfoot[RO]\{yy\}} \\
%   \bottomrule
% \end{tabular}
% \caption{Deprecated commands and their translation\label{tab:depr}}
% \end{function}
% \end{table}
%
% As you see, if there is an optional parameter, this one applies to the
% even pages, whereas the required parameter applies to the odd pages. Of
% course this only works if the \texttt{twoside} option is given in the
% documentclass. If there is no optional parameter, the required
% parameter applies to both even and odd pages.
%
% \begin{function}{\fancyplain}
% \PSindex{fancyplain}
% There was also a special page style \texttt{fancyplain} that could be
% used to define both the page style \texttt{fancy} and to redefine the
% page style \texttt{plain} at the same time. In order to use that you say
% \begin{code}
% \pagestyle{fancyplain}
% \end{code}
% and then in the headers/footers you use for example:
% \begin{code}
% \fancyhead[L]{\fancyplain{value for `plain' page}
%              {value for other pages}}}
% \end{code}
%
% The \cs{fancyplain} command is only useful within the page style \texttt{fancyplain}.
% Nowadays you would just redefine page style \texttt{plain} with the
% \Cmd{fancypagestyle\{plain\}\{xxxx\}} command (see section~\ref{sec:pagestyle-plain}).
% \end{function}
%
% \begin{variable}{\plainheadrulewidth, \plainfootrulewidth}
% There are also \cs{plainheadrulewidth} and \cs{plainfootrulewidth}
% commands to define the values of \cs{headrulewidth} and
% \cs{footrulewidth} to be used on `\texttt{plain}' pages. This also
% only works with the page style \texttt{fancyplain}, not when you
% redefine page style \texttt{plain} with the \cs{fancypagestyle} command.
% \end{variable}
%
% \end{documentation}
%
% \section{Contact information}
% \label{sec:contact}
%
% Pieter van Oostrum\\
% E-mail: pieter@vanoostrum.org \\
% WWW: http://pieter.vanoostrum.org
% \\[1ex]
% The source code can be found on Github:\\
% \url{https://github.com/pietvo/fancyhdr}\\
% Bugs and suggestions for improvements can be reported at\\
% \url{https://github.com/pietvo/fancyhdr/issues}\\
% Example files can be found at\\
% \url{https://github.com/pietvo/fancyhdr-examples}
%
% \section{Version information}
% \begin{itemize}
% \item  Version 1.0. March 11,  2003. This is the version that was distributed for a
%   long time on CTAN. Version history before this has been lost.
% \item Version 2.0. August 27, 2016:
%   \begin{itemize}
%   \item Removed references to fixmarks.sty as that is no longer used.
%   \item References to older \LaTeX{} versions removed.
%   \item Removed obsolete source code of \pkg{extramarks.sty}
%   \item Changed font commands to \cs{textbf} and \cs{textsl}.
%   \item Added description of the \Cmd{fancy\ldots offset} commands.
%   \item Added various \Cmd{\ldots xmark} commands from \pkg{extramarks.sty}.
%   \item Various corrections applied.
%   \item Updated contact information.
%   \item Added Version information. :)
%   \end{itemize}
% \item Version 2.1. August 28, 2016
%   \begin{itemize}
%   \item Explain what the top-marks are.
%   \end{itemize}
% \item Version 2.1. Sept. 6, 2016
%   \begin{itemize}
%   \item Add \verb|\string| to special indexing commands to get a neater index file.
%   \item Add a decorative headrule example.
%   \end{itemize}
% \item Version 3.9, October 13, 2016.
%   \begin{itemize}
%   \item Documentation integrated in \pkg{fancyhdr.dtx}.
%   \item Version number unified with \pkg{fancyhdr.sty}.
%   \item All deprecated commands moved to a separate section (\ref{sec:depr}).
%   \item Documentation expanded.
%   \end{itemize}
% \item Version 3.9a, June 30, 2017.
%   \begin{itemize}
%   \item Updated contact information.
%   \item Restore \cs{newtoks}\cs{@temptokenb}
%   \end{itemize}
% \item Version 3.10, Januari 25, 2019
%   \begin{itemize}
%   \item Distribution based on fancydhr.dtx.
%   \item Use \cs{f@nch@ifundefined} instead of \cs{ifx} or \cs{@ifundefined}.
%   \item Replace \cs{def} with \cs{newcommand} in several places.
%   \item Don't use \cs{global}\cs{setlength}.
%   \item Put \cs{footrule} in a \cs{vbox} to accommodate for flexible
%     footrules, and then \cs{unvbox} that. Move the \cs{footruleskip}
%     vertical space outside of the definition of \cs{footrule}.
%   \end{itemize}
% \end{itemize}
% \subsection{Changes in version 4}
% \label{sec:version-4}
% Version 4 is a significant rewrite of the package. It also introduces
% a number of new features.
% \begin{itemize}
% \item Version 4.0, March 15, 2019--Jan 04, 2021
%   \begin{itemize}
%   \item Options introduced on the \cs{usepackage} command.
%   \item The check whether the header or footer fits in
%     \cs{headheight} and \cs{footskip}, respectively, no longer
%     adjusts these values for the following pages. This appeared to be too
%     confusing. However, when the package option \texttt{compatV3} is given, the old
%     behaviour is kept. \\
%     The \texttt{nocheck} option now eliminates these checks completely, on
%     your own risk. (See section~\ref{sec:warning} on page~\pageref{page:warning}.)
%   \item Eliminated global definitions. All definitions are now local.
%     The \cs{global} case was originally so that you could do definitions in
%     a group and they would be applied globally. This was a mistake. If
%     you make them locally they should stay local. And it caused
%     problems with switching page styles, because then the global style
%     would be changed, which you generally don't want.\\
%     However, when the package option \texttt{compatV3} is given, the old
%     behaviour is kept. (See section~\ref{sec:options}.)
%   \item The page style \texttt{fancydefault}.
%   \item The \cs{headruleskip} parameter.
%   \item The \cs{fancyheadinit}, \cs{fancyfootinit}, and
%     \cs{fancyhfinit} commands.
%   \item[] \textbf{Note:} The following changes were mostly copied from the
%     \PKGindex{nccfancyhdr}\pkg{nccfancyhdr} package by Alexander I. Rozhenko.
%   \item The \cs{fancycenter} command (section~\ref{sec:fancycenter}).
%   \item The \texttt{headings} and \texttt{myheadings} package options
%     (see section~\ref{sec:options}).
%   \item The \cs{fancypagestyle} command has an optional parameter \oarg{base-style}.
%
%   \end{itemize}
%
% \item Version 4.0.1, Jan 28, 2021
%   \begin{itemize}
%   \item Some documentation corrections, especially in sections
%     \ref{sec:xmarks} and \ref{sec:movie}.
%   \end{itemize}
% \item Version 4.0.2, May 9, 2022
%   \begin{itemize}
% \item Added \cs{leavevmode}\cs{ignorespaces} to each header/footer
%   field. The \cs{leavevmode} prevents a bug when a field starts with a
%   \cs{color} command. The \cs{ignorespaces} skips initial spaces in
%   the parameter, as is usual in a \cs{parbox}, for backwards
%   compatibility. However, there are some rare cases where spurious
%   spaces can still show up in the header/footer fields. In that case
%   the user will have to eliminate these.
%   \end{itemize}
% \item Version 4.0.3, May 18, 2022
%   \begin{itemize}
%   \item Initialize \cs{@mkboth} in \pkg{extramarks.sty} so that
%     it will pick up changes to \cs{markboth}.
%   \end{itemize}
% \item Version 4.1, Sept 6-Nov 9, 2022
%   \begin{itemize}
%   \item Implement \texttt{twoside} package option to allow two-sided headers
%   and footers in one-sided documents.
%   \item Make \pkg{fancyhdr} compatible with the document class \pkg{newlfm}.
%   \item Make \cs{nouppercase} compatible with newer definitions of \cs{MakeUppercase}.
%   \end{itemize}
% \item Version 4.2, April 19, 2024
%   \begin{itemize}
%   \item Reset catcodes to their default values in order to facilitate
%         \cs{input} in headers/footers when \texttt{verbatim} is active.
%         (Issue \# 8 \url{https://github.com/pietvo/fancyhdr/issues/8}.)
%   \end{itemize}
% \item Version 4.3, July 17, 2024
%   \begin{itemize}
%   \item Changed \cs{f@nch@everypar}. If the LaTeX kernel has
%     \texttt{expl3}, use \cs{tex\_everypar:D}, and reset \cs{par},
%     \cs{@@par} and \cs{endgraf} to their original \TeX{} definitions, so that
%     no paragraph hooks will intrude in \pkg{fancyhdr} code\footnote
%     {See \url{https://tex.stackexchange.com/q/691262/113546}}.
%     Therefore paragraph hooks will not work inside \pkg{fancyhdr} headers and
%     footers to avoid unwanted interactions with the main text.
%   \end{itemize}
% \item Version 4.3.1, July 23, 2024
%   \begin{itemize}
%   \item Also reset \cs{everypar} to its original \TeX{} value
%     \cs{tex\_everypar:D} in \cs{f@nch@resetpar}, otherwise environments
%     based on \cs{trivlist} will not work properly in \pkg{fancyhdr}
%     headers and footers.
%   \end{itemize}
% \item Version 4.4, Nov 20, 2024
%   \begin{itemize}
%   \item Add setting the new style marks for \cs{leftmark}
%     (\texttt{2e-left}) and \cs{rightmark} (\texttt{2e-right} and
%     \texttt{2e-right-nonempty}) in \pkg{extramarks.sty}.
%   \end{itemize}
% \item Version 4.5, Nov 21-30, 2024
%   \begin{itemize}
%   \item \pkg{extramarks}: Don't redefine \cs{leftmark} and \cs{rightmark} in
%   \LaTeX{} kernel 2025-06-01 and later.
%   \item \pkg{fancyhdr}: use a better method to disable paragraph
%   hooks than the v4.3 code.
%   \item \pkg{extramarks-v4} (legacy version): add commands
%   \cs{extramarksleft} and \cs{extramarksright}.
%   \item \pkg{fancyhdr}: added hooks.
%   \end{itemize}
% \end{itemize}
%
% \subsection{Changes in version 5}
% \label{sec:version-5}
% Version 5 adds several new features. Most notable is a new
% implementation of the \pkg{extramarks} package, which now has
% independent marks.
%
% \begin{itemize}
% \item Version 5.0, Feb 11, 2021-Jan 1, 2025
%   \begin{itemize}
%   \item Shorten Warning message about \cs{headheight}/\cs{footskip} too large.
%   \item If the option \texttt{[nocheck]} is given, just keep quiet and
%     don't change the \cs{headheight}/\cs{footskip} even if the
%     \texttt{[compatV3]} option is given.
%   \item Added \cs{fancypagestyle*} variant.
%   \item Added command \cs{fancyhdrsettoheight}.
%   \item New implementation of package \pkg{extramarks} with
%     fallback to \pkg{extramarks-v4}.
%   \item Mark the \texttt{compatV3} option deprecated.
%   \item Added command \cs{fancyfootalign}.
%   \item Added command \cs{fancyhdrbox} (section~\ref{sec:fancyhdrbox}).
%   \item Added command \cs{fancypagestyleassign} (section~\ref{sec:fancypagestyleassign}).
%   \item Added commands  \cs{fancyheadwidth}, \cs{fancyfootwidth} and
%     \cs{fancyhfwidth} (section~\ref{sec:field-width}).
%   \item Many documentation improvements.
%   \end{itemize}
% \end{itemize}
%
%
%
% \StopEventually{%
% \PrintChanges
% \PrintIndex}
%
% \part{Questions \& Answers}
%
% This part contains answers to questions that have been emailed to me,
% or have been asked at various internet forums, and don't have a
% logical place in the other documentation. It is expected to grow
% gradually.
%
%
% \section{Long chapter/section titles}
% \label{sec:longtitles}
%
% \index{long titles}
% Sometimes a chapter or section title is too long to fit in the header
% or footer. It may take more than one line in the header/footer, or it
% may overwrite other parts. How can we shorten these titles in the
% header/footer without changing the actual title?
%
% Here is an example:
% \begin{code}
% \fancyhead[LE,RO]{\nouppercase{\rightmark}} % Section title
% \fancyhead[LO,RE]{\nouppercase{\leftmark}} % Chapter title
% \fancyfoot[C]{\thepage}
%  . . .
% \chapter{This is a very long chapter title}
%  . . .
% \section{This is a very long section title that will not fit in the header}
%  . . .
% \end{code}
% With these settings the header will come out as:
%
% \medskip
% {\noindent\hbox to \textwidth{^^A
%    \rlap{\parbox[b]{\textwidth}{\raggedright Chapter 1. This is a very long chapter title}}^^A
%    \hfill
%    \llap{\parbox[b]{\textwidth}{\raggedleft 1.2. This is a very long
%                   section title that will not fit in the header}}^^A
%  }\strut
%  \hrule height\headrulewidth width\textwidth}
% \bigskip
% \noindent which isn't very nice.
% There are basically three options to solve this problem.
%
% \subsection{Using optional arguments}
%
% As we have seen in section~\ref{sec:scoop}, the header info comes from
% the marks. So if we want the text in the header to be shorter we have
% to supply shorter marks. This can be done by giving these as optional
% arguments in the \cs{chapter} and \cs{section} commands.\footnote{At
% least in the \texttt{book} and \texttt{report} documentclasses. In the
% \texttt{article} class this would be the \cs{section} and
% \cs{subsection} commands.}
% \Example{34a}
% \begin{example}
% \chapter[This is a not so long chapter title]
%         {This is a very long chapter title to see if we can give
%          fancyhdr a shorter one that fits in the header}
% . . .
% \section[Short section title]
%         {This is a very long section title that will not fit in
%          the header}
% \end{example}
% The short titles will now appear in the header. However, these will
% also appear in the table of contents. If that is what you want then
% you are ready. But if you want to use the long titles in the table of
% contents, you have to use some trickery. In particular you have to
% supply the marks yourself.
%
% \subsection{Using explicit marks}
%
% First we show how you can supply a different value for the chapter
% title in the heading, because this is the easiest. Remember from
% section~\ref{sec:scoop} that this mark is defined by calling
% \cs{chaptermark}\CmdIndex{chaptermark}. Also, because it is used as \cs{leftmark}, the last
% value of this mark on the page is used. So we can easily overrule the
% value that is supplied by the \cs{chapter} command, by supplying an
% additional \cs{chaptermark} command after the \cs{chapter} command,
% like this:
% \Example{34b}
% \begin{example}
% \chapter{This is a long chapter title that does not fit in the header}
% \chaptermark{This is a not so long chapter title}
% \end{example}
% For the section titles the situation is more complicated. Here we use
% the \cs{rightmark}, which uses the first mark of its kind on the page.
% So you might think putting a \cs{sectionmark}\CmdIndex{sectionmark} before the \cs{section}
% command would be the solution. Unfortunately, it is not that simple.
% In many cases, this will work, but not when there is a page break just
% before the section title, because in that case the \cs{sectionmark}
% will stay behind on the previous page. However, we can put the
% \cs{sectionmark} inside the argument of the \cs{section} command.
% Because \LaTeX{} first typesets the title (which will execute the
% included \cs{sectionmark} command), and after that executes its own
% \cs{sectionmark}, our \cs{sectionmark} will be the first. But
% there is one case in which this fails: if the next page does not have any
% \cs{sectionmark} commands, it will inherit the \textbf{last} mark from the page
% before it, which will be the long title. To correct this we must also
% give an additional \cs{sectionmark} with the short title
% \textbf{after} the \cs{section} command.
%
% As if this isn't enough, there is still a problem with this setup. Our
% section title is not only used to typeset the title in the text, but
% it is also included in the table of contents. But the table of
% contents does not accept a \cs{sectionmark} in its title. It will
% generate an ugly error message. To prevent this we must give the long
% title (that we want to appear in the table of contents) also as the
% optional argument to the \cs{section} command. Of course this will
% also generate a mark for the header, but this will be overruled by
% our included \cs{sectionmark} commands
%
% So the complete code would be:
% \begin{code}
% \section[Long title]{Long title\sectionmark{Short title}}
% \sectionmark{Short title}
% \end{code}
% To avoid all the repetitions, it is better to make a macro:
% \Example{34b}[(continued)]
% \begin{example}
% \newcommand{\Section}[2]{%
%             \section[#1]{#1\sectionmark{#2}}\sectionmark{#2}}
%  . . .
% \Section{This is a long section title that will not fit in
%          the header}{Shortened section title}
% \end{example}
% And if you want to use yet a different text in the table of contents,
% you can make a macro with three parameters. The third parameter is the
% text to be put in the table of contents. We use this parameter as the
% optional argument for the \cs{section} command.
% \Example{34b}[(continued)]
% \begin{example}
% \newcommand{\Sectionx}[3]{%
%             \section[#3]{#1\sectionmark{#2}}\sectionmark{#2}}
%  . . .
% \Sectionx{This is another long section title that will not
%             fit in the header}{Short section title 3}
%            {This is the section title in the table of contents}
% \end{example}
% Please note that if you use the \texttt{article} class, instead of
% \cs{chaptermark} and \cs{sectionmark}, you would probably use
% \cs{sectionmark} and \cs{subsectionmark}.
%
% \subsection{Using automatic truncation}
%
% For this solution we use the \PKGindex{truncate}\pkg{truncate} package by Donald
% Arseneau. This has a \cs{truncate}\CmdIndex{truncate} command that
% truncates a text to a maximum size, when it exceeds that size. We put
% both headers in \cs{truncate} to limit it to half the \cs{headwidth}.
% Of course it is also possible to make asymmetric arrangements.
% \Example{34c}
% \begin{example}
% \usepackage[fit]{truncate}
% \fancyhead[LE,RO]{\nouppercase{%
%          \truncate{0.5\headwidth}{\rightmark}}} % Section title
% \fancyhead[LO,RE]{\nouppercase{%
%          \truncate{0.5\headwidth}{\leftmark}}} % Chapter title
% \end{example}
% We don't have to make any changes to the chaper and section titles
% because \cs{truncate} will take care of this.
% This arrangement gives the following header when both titles are too
% big, like in the example above:
%
% \medskip
% {\noindent\hbox to \textwidth{^^A
%    \truncate{0.5\textwidth}{Chapter 1. This is a very long chapter title that does not fit in the header}^^A
%    \hfill
%    \truncate{0.5\textwidth}{1.2. This is a very long section title that will not fit in the header}^^A
%  }\strut
%  \hrule height\headrulewidth width\textwidth}
% \bigskip
%
% Note that we have used the \texttt{[fit]} option of the
% \pkg{truncate} package. Otherwise the right header will not be
% right aligned, but it will start at halfway the header.
% Note also that, as each part can occupy half of the available width,
% they could theoretically touch each other. This can be prevented by
% making the widths slightly smaller. And when there is only one title
% in the header, you can make the width equal to or slightly smaller
% than \cs{headwidth}. A more sophisticated solution would be to check
% if one of the header parts is small enough and then truncate the other
% one for the remaining space.
%
% \section{I lost my chapter/section titles}
%
% Some time ago I got a question like this (edited to get the
% essentials):
%
% ``I redefined the \Cmd{pagestyle\{fancy\}} to get my own kind of headings. Also, I
% redefined the \cs{chaptermark}.
% I need the \texttt{fancy} style from chapter 1 and on (mainmatter part), but, until the Introduction
% chapter (that I included into the frontmatter part) I need the \texttt{myheadings}\PSindex{myheadings} style.
%
% When I set the \texttt{myheadings} style into
% the frontmatter the \texttt{fancy} style doesn't show the chapter title any more.
%
% What can I do in order to reestablish the right behavior of the
% \texttt{fancy} style?''
%
% The solution to this problem is actually very simple. The page style
% \texttt{myheadings} (as well as \texttt{headings}\PSindex{headings}) redefines the
% \cs{chaptermark} and \cs{sectionmark}, so when you return to page style
% \texttt{fancy}, the definitions you had given before (or the ones that
% \pkg{fancyhdr} provided) are lost. You
% just have to repeat them at the point where you switch back to
% page style \texttt{fancy}.
%
% \begin{code}
% \begin{document}
% \frontmatter
% \pagestyle{myheadings}
%  . . .
% \mainmatter
% \pagestyle{fancy}
% \renewcommand{\chaptermark}[1]{....}
% \end{code}
%
%
% \section{Can I use \pkg{fancyhdr} with the \pkg{beamer} class?}
% \label{sec:beamer}
%
% \TTindex{beamer}%
% The \pkg{beamer} class has its own provisions for headers and
% footers with the \texttt{headline} and \texttt{footline} templates.
% The advantage of these is that they blend well with the
% \pkg{beamer} theme in use.
%
% Still people sometimes ask if \pkg{fancyhdr} can be used for
% header and footers because they are more familiar with this. I would
% advice to use the standard \pkg{beamer} features if possible, but
% actually it isn't difficult to use \pkg{fancyhdr} if you take
% provisions that the header and the footer don't
% interfere with the \pkg{beamer} layout. This can be done with
% \begin{code}[commandchars={+[]}]
% \setbeamertemplate{headline}{\vspace{+meta[headheight]}}
% \setbeamertemplate{footline}{\vspace{+meta[footskip]}}
% \end{code}
% \CmdIndex{headheight}\CmdIndex{footskip}%
% Note that \pkg{beamer} sets \cs{headheight} and \cs{footskip} to
% its own vales, so it doesn't make sense to set these in your document.
% Instead you supply the desired values with \cs{setbeamertemplate} as above.
% Also it is advised to add \cs{fancyfootalign}\texttt{\{0pt\}} to
% prevent the footer to be too close to the bottom edge; see section~\ref{sec:fancyfootalign}
% on page~\pageref{sec:fancyfootalign}.\\
% Here is a complete example:
% \NoExample{with-beamer}
% \begin{example}
% \documentclass{beamer}
% \usepackage{graphicx}
% \usepackage{fancyhdr}
% \pagestyle{fancy}
% \fancyhead[L]{\includegraphics[width=0.1\textwidth]{example-image}}
% \fancyhead[R]{Course Name}
% \fancyhead[C]{\textbf{Subject}\\Author}
% \fancyfoot[L]{LEFT page footer}
% \fancyfoot[R]{RIGHT page footer}
% \fancyfoot[C]{{\thepage}}
% \fancyfootinit{\tiny}
% \renewcommand{\footrulewidth}{0.4pt}
%
% \setbeamertemplate{headline}{\vspace{30pt}}
% \setbeamertemplate{footline}{\vspace{14pt}}
% \fancyfootalign{0pt}
%
% \begin{document}
%
% \begin{frame}{Subject Title}
% Text of the slide
% \end{frame}
%
% \end{document}
% \end{example}
%
% \section{I want the first section and the first subsection in my headers}
% \label{sec:firstsubsection}
%
% A question that is regularly asked (e.g., on
% \texttt{tex.stackexchange.com}\footnote{See for example
% \url{https://tex.stackexchange.com/q/586066/113546}}) is how to get
% both the first section title and the first subsection title in the
% headers in the \pkg{article} documentclass. Unfortunately,
% traditional \LaTeX{} (releases before November 2022) can't give you
% the first subsection on the page. There are two problems:
% \begin{itemize}
% \item Traditional \LaTeX{} uses left marks for the section title and
% right marks for the subsection title. But it only has commands to
% extract the last left mark (\cs{leftmark}) and the first right mark
% (\cs{rightmark}). This means that if there are two or more sections on
% the page you get the last one, which can be counter-intuitive.
% The newer \LaTeX{} releases (November 2022 or newer) have a command to
% get the first of the left marks, however. \textbf{We assume in the
% following code that your \LaTeX{} is recent enough.}
% \item \LaTeX{} uses \cs{markboth} at a section title in the
% \pkg{article} class. This also sets an empty right mark. So in
% some cases you would get an empty subsection title in the header. To
% avoid this, \LaTeX{} now also has a mark that saves the non-empty
% right mark in a separate mark \texttt{2e-right-nonempty}
% \end{itemize}
%
% If there is no \cs{section} command on the page, it `inherits' the
% last section title of a previous page. Similarly for subsections.
% This gives us the following code:
%
% \Example{35}[(basic)]
% \begin{example}
% \usepackage{fancyhdr}
% \pagestyle{fancy}
% \fancyhead[L]{\FirstMark{2e-left}}
% \fancyhead[R]{\FirstMark{2e-right-nonempty}}
% \fancyfoot[C]{\thepage}
%
% \begin{document}
% \section{Section One}
% \subsection{Subsection One}
%  . . .
% \end{example}
% This effectively solves the problem, but it has some undesirable
% properties, see below. Therefore, some refinement is
% possible. But we need an extra mark for this. So we are going
% to use the \LaTeX{} marks directly, instead of using the standard
% marks. This gives us a bit more control. We need two marks for this,
% one for the section title, and one for the subsection title. We call
% these \texttt{section} and \texttt{subsection} respectively. We
% replace the marking code above with the following, which gives
% the same result:
% \Example{35}[(new marks)]
% \begin{example}
% \usepackage{fancyhdr}
% \pagestyle{fancy}
% \NewMarkClass{section}
% \NewMarkClass{subsection}
% \fancyhead[L]{\FirstMark{section}}
% \fancyhead[R]{\FirstMark{subsection}}
% \renewcommand{\sectionmark}[1]{%
%     \InsertMark{section}{\thesection. #1}}
% \renewcommand{\subsectionmark}[1]{%
%     \InsertMark{subsection}{\thesubsection. #1}}
% \end{example}
%
% When you have a \cs{section} command on a page, but it doesn't have a
% subsection for an extended length, then, the
% previous subsection title is `inherited'. This may be sub-optimal,
% because it combines the title of section $n$ with a subsection title
% that belongs to a previous section, so the subsection isn't even
% present on the page, which looks unnatural. You may want to
% suppress the subsection title in the right header in this case. With the new
% \LaTeX{} marks this is possible.
%
% What we want is essentially the following:
% \begin{enumerate}
% \item If there is at least one subsection on the page, use the first one.
% \item Otherwise, if the previous page ended in a subsection (i.e., the page
% break was inside a subsection), use that subsection title.
% \item Otherwise (the page break was inside a section that had no
% subsections thus far), leave the right header empty.
% \end{enumerate}
%
% The first test can be done by comparing the subsection `topmark' and
% `firstmark'. The topmark is the last mark from the previous page
% (which might even have been inherited from an earlier page). If there
% is no subsection mark on the current page, firstmark is made equal to
% topmark. If there is a subsection mark on the page, firstmark will be
% a different mark. \LaTeX{} now has a command to compare the
% marks, like this:\\
% \hspace*{\parindent}\cs{IfMarksEqualTF}\marg{mark}\marg{pos1}\marg{pos2}\marg{true
% code}\marg{false code}\\
% where the \emph{pos} arguments are \texttt{top}, \texttt{first} or \texttt{last}.
% Please note that this test makes only sense in a header or
% footer\footnote{Or more generally, when \LaTeX's page building is active.}.
% So if we use\\
% \hspace*{\parindent}\cs{IfMarksEqualTF}\texttt{\{subsection\}\{top\}\{first\}}\ldots\\
% the \meta{true code} is executed when there is no subsection title on the
% current page, and the \meta{false code} when there is at least one.
%
% However, there is no command to see if the pagebreak is at the section
% or subsection level, because the two are independent. And we
% cannot use \LaTeX{} variables for this, because of the asynchronous
% processing of the page breaking. But we can do this if we introduce a
% new mark `\texttt{which}', that is used by both \cs{section} and
% \cs{subsection}. We let \cs{sectionmark} put ``0'' in it, and
% \cs{subsectionmark} ``1''. The lastmark of `\texttt{which}' on a page then
% indicates if the page ends within a subsection or not. And as the
% topmark on the following page is the same as lastmark on the previous
% page, we can use this topmark to see whether the text at the beginning
% of the page comes from a subsection (topmark = 1 means yes).
%
% There is one caveat, however. If the page begins immediately with a
% section title, the topmark may indicate that the previous page ended
% with a subsection, but that subsection did not extend past the
% pagebreak. It is tricky to detect this situation, but we will propose
% a solution at the end of the current section.
% So the new code for the right header becomes the following. We use the
% package \PKGindex{ifthen}\pkg{ifthen} for the test.
%
% \Example{35}[(continued)]
% \begin{example}
% \NewMarkClass{which}  % Preamble
% \InsertMark{which}{0} % Initialize so there is always a valid value
%
% \fancyhead[R]{%
%   \IfMarksEqualTF{subsection}{top}{first}
%     {% no subsection mark on this page
%       \ifthenelse{\TopMark{which}=1} % previous page ended in a subsection
%         % then use that subsection
%         {\TopMark{subsection}}% = \FirstMark{subsection}
%         {}% otherwise empty right header
%     }
%     {% there is a subsectionmark on the page, use it
%       \FirstMark{subsection}%
%     }%
% }
% \renewcommand{\sectionmark}[1]{%
%   \InsertMark{section}{\thesection. #1}%
%   \InsertMark{which}{0}%
% }
% \renewcommand{\subsectionmark}[1]{%
%   \InsertMark{subsection}{\thesubsection. #1}%
%   \InsertMark{which}{1}%
% }
% \end{example}
%
% Now we want to suppress the `inheritance' of the subsection if there
% is a section title at the top of the page and the page contains no
% subsection title. The information whether a section title is at the
% top of the page is not available in the marks, so we need some other
% way to detect this.
%
% \TeX{} has two variables that can help us, \cs{pagegoal} is the vertical size
% that is still available on the page, and \cs{pagetotal} is the amount we
% have used so far\footnote{This doesn't include floats and footnotes.}.
% So if \cs{pagetotal}=\texttt{0pt} we are at the top of
% the page, otherwise somewhat further down.  We can use this information to
% communicate to the header that no inheritance should take place. In
% reality, sometimes there is already a small amount of whitespace on
% the page, so the test should be less strict than
% \cs{pagetotal}=\texttt{0pt}. We might even choose to not inherit the
% subsection title if only a few lines of the previous subsection are
% present at the top of the page. Or maybe your design asks for no
% inheritance if the subsection at the top of the page before the
% section header is smaller than 1/3 of the page. The test would then be
% \cs{pagetotal}$<$0.33\cs{pagegoal}. In the code below we choose a few
% lines as the limit.
%
% We have to do the test just before the \cs{section} command is
% processed. This can be done with a \LaTeX{} \emph{hook}.
%
% The next question is how to communicate the fact that the section
% starts at (or near) to top of the page to the header. A simple way to
% do this is to set the `\texttt{which}' mark to $-1$ instead of 0.
% (Another way could be to define a special page style that does not do
% the inheritance, and activate that with \cs{thispagestyle}.)
%
% \begin{code}
% \AddToHook{cmd/section/before}{%
%   % use whatever test your design requires
%   \ifthenelse{\lengthtest{\pagetotal<4\baselineskip}}
%     {\InsertMark{which}{-1}}
%     {\InsertMark{which}{0}}
% }
% \end{code}
%
% It appears, however that this is the wrong place to set the mark. A
% pagebreak could occur between this code and the actual section title,
% so that the mark is put on the wrong page. The proper place for setting the
% mark is in the \cs{sectionmark} command, as that is guaranteed to
% always be together with the section title. So we must put the required
% value for the mark in a variable.
%
% There is another situation where a section title could end up at the
% top of the page: when it is processed at the bottom of the page, but
% there isn't enough space left to place it there. It will then be
% pushed to the next page. In that case the \cs{pagetotal} will be in
% the neighbourhood of \cs{pagegoal}. In the code below we test for
% \cs{pagetotal}$>$0.9\cs{pagegoal}, but some experimentation may be
% necessary to find a proper value.
% In the example 35 file you can see this in section~6.
%
% \emph{(Another way could be to put the current page number in a mark
% and then check in the header if this is the same as the page number
% where the header is put on the page. If different, don't
% inherit. This is left as an exercice for the reader.)}
%
% \Example{35}[(continued)]
% \begin{example}
% \newcommand{\which}{}
% \AddToHook{cmd/section/before}{%
%   \ifthenelse{\lengthtest{\pagetotal<4\baselineskip}\or
%               \lengthtest{\pagetotal>0.9\pagegoal}}{%
%   {\renewcommand\which{-1}}%
%   {\renewcommand\which{0}}%
% }
% \renewcommand{\sectionmark}[1]{%
%   \InsertMark{section}{\thesection\ #1}%
%   \InsertMark{which}{\which}%
% }%
% \end{example}
%
% The final code for the right header will now be:
% \Example{35}[(continued)]
% \begin{example}
% \fancyhead[R]{%
%   \IfMarksEqualTF{subsection}{top}{first}
%     {% no subsection mark on this page
%       \ifthenelse{\TopMark{which}=1}{%
%         % use previous subsection unless suppressed by which = -1
%         \ifthenelse{\FirstMark{which}<0} % -1
%           {\FirstMark{subsection}}% = \TopMark{subsection}
%       }
%     }
%     {% if there is a subsectionmark on the page, use it
%       \FirstMark{subsection}%
%     }%
% }
% \end{example}
% In the actual code in the Example 35 file there is also some debugging
% code added.
%
% \section{How to change shapes and traits of horizontal lines in headers/footers?}
% \label{sec:headrules}
%
% Sometimes one wants a decorative line in the header or footer that is
% a bit more sophisticated than a straight line\footnote{See \url{https://tex.stackexchange.com/q/717266/113546}}.
%
% If you just want to change the thickness, redefine \cs{headrulewidth}. For example:
% \begin{code}
% \renewcommand{\headrulewidth}{0.1pt}
% \end{code}
% For more complicated forms you have to redefine \cs{headrule}. One
% example has already been given in section~\ref{sec:fancy}, and we will
% repeat it here:
% \PKGindex{fourier-orns}
% \begin{code}
% \usepackage{fourier-orns}
% ...
% \renewcommand\headrule{%
%       \vspace{-6pt}
%       \hrulefill
%       \raisebox{-2.1pt}
%           {\quad\decofourleft\decotwo\decofourright\quad}%
%       \hrulefill}
% \end{code}
% This gives us the following headrule:
%
% \noindent\makebox[\textwidth]{\hrulefill
% \raisebox{-2.1pt}[10pt][10pt]{\quad\decofourleft\decotwo\decofourright\quad}\hrulefill}
%
% Here a simple \cs{headrule}, but with color, and a bit thicker.
% \PKGindex{xcolor}
% \begin{code}
% \usepackage{xcolor}
% . . .
% \renewcommand\headrule{%
%   \nointerlineskip
%   \smash{\color{blue}\rule{\headwidth}{2.5pt}}%
% }
% \end{code}
% The \cs{interlineskip} is to prevent \LaTeX{} to insert the normal
% vertical space between lines, and the \cs{smash} to let the
% \cs{headrule} not occupy any vertical space. Whether you want that is
% up to you, but if it occupies vertical space it may distort the page layout.\\
% This gives us the following headrule:
%
% \noindent\makebox[\textwidth]{\smash{\color{blue}\rule{\textwidth}{2.5pt}}}\\
%
% Now some dotted and dashed headrules:
%
% With spaced dashes:
% \begin{code}
% \newbox\dashbox\setbox\dashbox\hbox{-\,}
% \renewcommand{\headrule}{%
%   \smash{\makebox[\headwidth][c]{\xleaders\copy\dashbox\hfill-}}%
% }
% \end{code}
% This gives:
%
% \newbox\dashbox\setbox\dashbox\hbox{-\,}
% \noindent\makebox[\textwidth][c]{\xleaders\copy\dashbox\hfill-}\\
%
% With longer dashes:
% \begin{code}
% \newbox\dashbox\setbox\dashbox\hbox{---\,}
% \renewcommand{\headrule}{%
%   \smash{\makebox[\headwidth][c]{\xleaders\copy\dashbox\hfill---}}%
% }
% \end{code}
% This gives:
%
% \newbox\dashbox\setbox\dashbox\hbox{---\,}
% \noindent\makebox[\textwidth][c]{\xleaders\copy\dashbox\hfill---}\\
%
% With spaced dots:
% \begin{code}
% \newbox\dashbox\setbox\dashbox\hbox{.\,}
% \renewcommand{\headrule}{%
%   \smash{\makebox[\headwidth][c]{\xleaders\copy\dashbox\hfill.}}%
% }
% \end{code}
% This gives:
%
% \setbox\dashbox\hbox{.\,}
% \noindent\makebox[\textwidth][c]{\xleaders\copy\dashbox\hfill.}\\
%
% With unspaced dots:
% \begin{code}
% \newbox\dashbox\setbox\dashbox\hbox{.}
% \renewcommand{\headrule}{%
%   \smash{\makebox[\headwidth][c]{\xleaders\copy\dashbox\hfill.}}%
% }
% \end{code}
% This gives:
%
% \setbox\dashbox\hbox{.}
% \noindent\makebox[\textwidth][c]{\xleaders\copy\dashbox\hfill.}\\
%
% Here is an example with a color gradient, using \PKGindex{tikz}\pkg{tikz}:
% \CmdIndex{tikz}
% \begin{code}
% \usepackage{tikz}
% \renewcommand{\headrule}{%
%   \tikz \fill [left color=green,right color=yellow]
%               (0,0) rectangle (\headwidth,2.5pt);
% }
% \end{code}
% This gives:
%
% \noindent\makebox[\textwidth][c]{%
%   \tikz \fill [left color=green,right color=yellow] (0,0) rectangle (\textwidth,2.5pt);}\\
%
% Here is a particularly interesting one. It draws a Koch Snowflake at
% the end of the headrule\footnote{From Logan Weinert,
% \url{https://tex.stackexchange.com/q/529474/113546}}.
% \PKGindex{tikz}
% \CmdIndex{tikz}
% \begin{code}
% \usepackage{tikz}
% \usetikzlibrary{decorations.fractals}
% . . .
% \renewcommand\headrule{%
%     \vspace{-0.75in}
%     \hrulefill
%     {\tikz[decoration=Koch snowflake]{%
%           \draw decorate{ decorate{ decorate{ (0,-2) -- (3,-2) }}};}}
% }
% \end{code}
% This gives:
%
% \noindent\makebox[\textwidth][c]{^^A
% \hrulefill
%     {\tikz[decoration=Koch snowflake]{\draw decorate{ decorate{ decorate{ (0,-2) -- (3,-2) }}};}}
% }\\
% Of course you would have to make sure not to place anything in the
% right part of the header.
%
% \newpage
% \part{Implementation}
%
% \begin{implementation}
% \section{\pkg{fancyhdr.st}y}
%
%<*fancyhdr>
% \changes{fancyhdr v1.4}{1994/09/16}{Correction for use with \cs{reversemarginpar}}
%
% \changes{fancyhdr v1.5}{1994/09/29}{Added the \cs{iftopfloat},
% \cs{ifbotfloat} and \cs{iffloatpage} commands}
%
% \changes{fancyhdr v1.6}{1994/10/04}{Reset single spacing in headers/footers for use with
% \pkg{setspace.sty} or \pkg{doublespace.sty}}
%
% \changes{fancyhdr v1.7}{1994/10/04}{Changed \cs{let}\cs{@mkboth}\cs{markboth} to
% \texttt{\cs{def}\cs{@mkboth}\{\cs{protect}\cs{markboth}\}} to make it more robust.}
%
% \changes{fancyhdr v1.8}{1994/12/05}{corrections for
% \pkg{amsbook}/\pkg{amsart}: define \cs{@chapapp} and (more
% importantly) use the \cs{chapter/sectionmark} definitions from \texttt{ps@headings} if
% they exist (which should be true for all standard classes).}
%
% \changes{fancyhdr v1.9}{1995/03/31}{The proposed
% \texttt{\cs{renewcommand}\{\cs{headrulewidth}\}} \texttt{\{\cs{iffloatpage}\ldots}
% construction in the doc did not work properly with the \texttt{fancyplain} style.}
%
% \changes{fancyhdr v1.91}{1995/06/01}{The definition of \cs{@mkboth} wasn't
% restored on subsequent \texttt{\cs{pagestyle}\{fancy\}}'s.}
%
% \changes{fancyhdr v1.92}{1995/06/01}{The sequence
% \texttt{\cs{pagestyle}\{fancyplain\} \cs{pagestyle}\{plain\}
% \cs{pagestyle}\{fancy\}} would erroneously select the plain version.}
%
% \changes{fancyhdr v1.93}{1995/06/01}{\cs{fancypagestyle} command added.}
%
% \changes{fancyhdr v1.94}{1995/12/11}{(suggested by Conrad Hughes
% \texttt{<chughes@maths.tcd.ie!>}): added \cs{footruleskip} to allow control over footrule
% position (old hardcoded value of .3\cs{normalbaselineskip} is far too high
% when used with very small footer fonts).}
%
% \changes{fancyhdr v1.95}{1996/01/31}{call \cs{@normalsize} in the reset code if that is defined,
% otherwise \cs{normalsize}. This is to solve a problem with
% \pkg{ucthesis.cls}, as this doesn't define \cs{@currsize}.
% Unfortunately for latex209 calling \cs{normalsize} doesn't
% work as this is optimized to do very little, so there \cs{@normalsize} should
% be called. Hopefully this code works for all versions of LaTeX known to
% mankind.}
%
% \changes{fancyhdr v1.96}{1996/04/25}{Initialise \cs{headwidth} to a
% magic (negative) value to catch most common cases that people change
% it before calling \texttt{\cs{pagestyle}\{fancy\}}.
% Note it can't be initialised when reading in this file, because
% \cs{textwidth} could be changed afterwards. This is quite probable.
% We also switch to \cs{MakeUppercase} rather than \cs{uppercase} and introduce a
% \cs{nouppercase} command for use in headers. and footers.}
%
% \changes{fancyhdr v1.97}{1996/05/03}{Two changes: \\
% 1. Undo the change in version 1.8
% (using the \texttt{\cs{pagestyle}\{headings\}} defaults
% for the chapter and section marks). The current version of amsbook and
% amsart classes don't seem to need them anymore. Moreover the standard
% \LaTeX{} classes don't use \cs{markboth} if twoside isn't selected, and this is
% confusing as \cs{leftmark} doesn't work as expected.\\
% 2. Include a call to \cs{ps@empty}
% in \cs{ps@@fancy}. This is to solve a problem
% in the amsbook and amsart classes, that make global changes to \cs{topskip},
% which are reset in \cs{ps@empty}. Hopefully this doesn't break other things.}
%
% \changes{fancyhdr v1.98}{1996/05/07}{Added \% after the line  \cs{def}\cs{nouppercase}}
%
% \changes{fancyhdr v1.99}{1996/05/07}{This is the alpha version of fancyhdr 2.0\\
% Introduced the new commands \cs{fancyhead}, \cs{fancyfoot}, and \cs{fancyhf}.
% Changed \cs{headrulewidth}, \cs{footrulewidth}, \cs{footruleskip} to
% macros rather than length parameters, In this way they can be
% conditionalized and they don't consume length registers. There is no need
% to have them as length registers unless you want to do calculations with
% them, which is unlikely. Note that this may make some uses of them
% incompatible (i.e., if you have a file that uses \cs{setlength} or \cs{xxxx}!=)}
%
% \changes{fancyhdr v1.99a}{1996/05/10}{Added a few more \% signs.}
%
% \changes{fancyhdr v1.99b}{1996/05/10}{Changed the syntax of
% \cs{f@nch@for} to be resistent to catcode changes of \texttt{:!=}.\protect\\
% Removed the \texttt{[1]} from the defs of \cs{lhead} etc. because the parameter is
% consumed by the \cs{@[xy]lhead} etc. macros.}
%
% \changes{fancyhdr v1.99c}{1997/06/24}{Corrected \cs{nouppercase} to
% also include the protected form of \cs{MakeUppercase}.\\
% \cs{global} added to manipulation of \cs{headwidth}.\\
% \cs{iffootnote} command added.\\
% Some comments added about \cs{f@nch@head} and \cs{f@nch@foot}.}
%
% \changes{fancyhdr v1.99d}{1998/08/24}{Changed the default
% \cs{ps@empty} to \cs{ps@@empty} in order to allow
% \texttt{\cs{fancypagestyle}\{empty\}} redefinition.}
%
% \changes{fancyhdr v2.0}{2000/10/11}{Added LPPL license clause.\\
% A check for \cs{headheight} is added. An errormessage is given (once) if the
% header is too large. Empty headers don't generate the error even if
% \cs{headheight} is very small or even 0pt. \\
% Warning added for the use of '\texttt{E}' option when \texttt{twoside} option is not used.
% In this case the '\texttt{E}' fields will never be used.}
%
% \changes{fancyhdr v2.1beta}{2002/03/10}{New command:
% \texttt{\cs{fancyhfoffset}[place]\{length\}} defines offsets to be applied to
% the header/footer to let it stick into the margins (if length $!>$ 0).
% \texttt{place} is like in \cs{fancyhead}, except that only \texttt{E,O,L,R} can be used.
% This replaces the old calculation based on \cs{headwidth} and the marginpar
% area.
% \cs{headwidth} will be dynamically calculated in the headers/footers when
% this is used.}
%
% \changes{fancyhdr v2.1beta2}{2002/03/26}{\cs{fancyhfoffset} now also
% takes \texttt{H,F} as possible letters in the argument to
% allow the header and footer widths to be different.\\
% New commands \cs{fancyheadoffset} and \cs{fancyfootoffset} added comparable to
% \cs{fancyhead} and \cs{fancyfoot}.\\
% Errormessages and warnings have been made more informative.}
%
% \changes{fancyhdr v2.1x=fancyhdr v2.1}{2002/12/09}{The defaults for
% \cs{footrulewidth}, \cs{plainheadrulewidth} and
% \cs{plainfootrulewidth} are changed from \cs{z@skip} to 0pt. In this
% way when someone inadvertantly uses \cs{setlength} to change any of these, the value
% of \cs{z@skip} will not be changed, rather an errormessage will be given.}
%
% \changes{fancyhdr v3.0}{2004/03/03}{Release of version 3.0.}
%
% \changes{fancyhdr v3.1}{2004/10/07}{Added '\texttt{\cs{endlinechar}!=13}' to
% \cs{f@nch@reset} to prevent problems with \cs{includegraphics} in
% header/footer when \env{verbatiminput} is active.}
%
% \changes{fancyhdr v3.2}{2005/03/22}{Reset \cs{everypar} (the real one)
% in \cs{f@nch@reset} because spanish.ldf does strange things with
% \cs{everypar} between \guillemotleft\ and \guillemotright.}
%
% \changes{fancyhdr v3.3}{2016/08/20}{Replace
% `\texttt{\cs{@ifundefined}\{chapter\}}' with `\cs{ifx}\cs{chapter}\cs{@undefined}'
% because the former subtly makes \cs{chapter} equal to \cs{relax}, which may be
% undesirable in some cases.}
%
% \changes{fancyhdr v3.4}{2016/08/21}{Replace \cs{rm} by
% \cs{normalfont}\cs{rmfamily} and \cs{sl} by \cs{normalfont}\cs{slshape}.}
%
% \changes{fancyhdr v3.5}{2016/08/21}{Don't define \cs{footruleskip} if it is already defined.}
%
% \changes{fancyhdr v3.6}{2016/08/27}{Added a \cs{ProvidesPackage} line.\\
% Updated contact information.}
%
% \changes{fancyhdr v3.7}{2016/08/28}{Removed \cs{normalfont} from default values, as every field
% is already initialised with \cs{normalfont}.\\
% Set \cs{hsize} to \cs{headwidth} in header/footer.}
%
% \changes{fancyhdr v3.8}{2016/09/06}{Reset \bsbs, \cs{raggedleft},
% \cs{raggedright} and \cs{centering} to their default values to avoid a
% clash with the tabu package.\\
% Move the redefinition of \cs{@makecol} to \Cmd{begin\{document\}} to
% avoid a clash with the \pkg{footmisc} package (and maybe others).\\
% Define a working \cs{iffootnote} command.}
%
% \changes{fancyhdr v3.9}{2016/10/11}{Put everything in a \texttt{.dtx} file.}
% \changes{fancyhdr v3.9}{2016/10/12}{Rename some macros to have 'f@nch@'
% in their names, to get a more uniform naming scheme for internal macros.}
%
% \begin{macro}[internal]{\if@nch@mpty}
% This macro tests if its argument is empty.
%    \begin{macrocode}
\newcommand\if@nch@mpty[1]{\def\temp@a{#1}\ifx\temp@a\@empty}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[internal]{\iff@nch@check}
% Boolean for the \texttt{nocheck} option.
% \changes{fancyhdr v4.0}{2019/03/15}{Implement the \texttt{nocheck} option}
% \OPTindex{nocheck}
%    \begin{macrocode}
\newif\iff@nch@check
\f@nch@checktrue
\DeclareOption{nocheck}{%
  \f@nch@checkfalse
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[internal]{\f@nch@gbl}
% Initialise \cs{f@nch@gbl} to do nothing (except with the \texttt{compatV3}
% option).
% \changes{fancyhdr v4.0}{2019/03/19}{Remove the \cs{global} in definitions}
% \changes{fancyhdr v4.0.2}{2021/02/24}{Untangle the code
% for\cs{f@nch@gbl} and the \texttt{compatV3} option}
%    \begin{macrocode}
\let\f@nch@gbl\relax
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[internal]{\iff@nch@compatViii}
% Define \cs{iff@nch@compatViii} to track the \texttt{compatV3} option.
% \changes{fancyhdr v4.0}{2019/03/16}{Implement the \texttt{compatV3} option}
% \changes{fancyhdr v5.0}{2024/05/08}{Add warning that the
% \texttt{compatV3} option is deprecated}
%    \begin{macrocode}
\newif\iff@nch@compatViii
\DeclareOption{compatV3}{%
  \PackageWarningNoLine{fancyhdr}{The `compatV3' option is deprecated.\MessageBreak
    It will disappear in one of the following releases.\MessageBreak
    Please change your document to work\MessageBreak
    without this option}
  \let\f@nch@gbl\global
  \f@nch@compatViiitrue
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[internal]{\iff@nch@twoside}
% Boolean for the \texttt{twoside} option.
% \changes{fancyhdr v4.1}{2022/09/06}{Implement \texttt{twoside} option.}
% \OPTindex{twoside}
% This is only set if the document itself is not two-sided.
%    \begin{macrocode}
\newif\iff@nch@twoside
\f@nch@twosidefalse
\DeclareOption{twoside}{%
  \if@twoside\else\f@nch@twosidetrue\fi
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[internal]{\f@nch@def}
% This macro defines another macro (usually a header or footer field).
% Depending on the value of \cs{f@nch@gbl} the definition will be
% global or local.
% Default it is always local. But with the \texttt{compatV3} option it
% is \cs{global} in the normal definitions, and local in \cs{fancypagestyle}.
% The \cs{global} case is now considered a bug (or at least undesirable).
%
% If the value (argument 2) is empty, a \cs{leavevmode} will be substituted.
% If it is not empty, a \cs{strut} will be added.
%
%    \begin{macrocode}
\newcommand\f@nch@def[2]{\if@nch@mpty{#2}\f@nch@gbl\def#1{\leavevmode}\else
                                   \f@nch@gbl\def#1{#2\strut}\fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[internal]{\f@nch@ifundefined}
% This macro tests if a command is undefined. Older versions of
% fancyhdr used \cs{@ifundefined}, but this had an undesired side effect
% in the original \LaTeX{} (the command was made equal to \cs{relax} if
% it was undefined). Another way was\\
% \verb+\ifx\thecommand\undefined ...+ or \verb+\ifx\thecommand\@undefined ...+
% but that could conflict with packages that use the \cs{@ifundefined}
% method. \LaTeX{} versions later than 2018 have a definition of
% \cs{@ifundefined} that avoids these problems, but not everybody may
% have such a version installed. Therefore we define our own version
% \cs{f@nch@ifundefined}.
% This definition is copied from the \PKGindex{tocloft}\pkg{tocloft} package by Peter
% Wilson and Will Robertson.
%    \begin{macrocode}
\newcommand{\f@nch@ifundefined}[1]{%
  \begingroup\expandafter\expandafter\expandafter\endgroup
  \expandafter\ifx\csname #1\endcsname\relax
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
    \fi}
%    \end{macrocode}
% \end{macro}
%
% \changes{fancyhdr v4.0}{2019/03/17}{Added \texttt{headings} and
% \texttt{myheadings} options.}
% Standard styles are redefined optionally. These definitions are
% borrowed from the \PKGindex{nccfancyhdr}\pkg{nccfancyhdr} package
% by by Alexander I. Rozhenko.
%
% \begin{macro}[internal]{\ps@myheadings}
% The redefinition of the \texttt{myheadings} style is conditional.
% We test the existence of the \cs{chapter} command and redefine the
% style accordingly.
%
% \OPTindex{myheadings}\PSindex{myheadings}
%    \begin{macrocode}
\DeclareOption{myheadings}{%
  \f@nch@ifundefined{chapter}{%
%    \end{macrocode}
% An article-like class without chapters:
%    \begin{macrocode}
    \def\ps@myheadings{\ps@f@nch@fancyproto \let\@mkboth\@gobbletwo
      \fancyhf{}
      \fancyhead[LE,RO]{\thepage}%
      \fancyhead[RE]{\slshape\leftmark}%
      \fancyhead[LO]{\slshape\rightmark}%
      \let\sectionmark\@gobble
      \let\subsectionmark\@gobble
    }%
  }%
%    \end{macrocode}
% A book/report-like class with chapters:
%    \begin{macrocode}
  {\def\ps@myheadings{\ps@f@nch@fancyproto \let\@mkboth\@gobbletwo
      \fancyhf{}
      \fancyhead[LE,RO]{\thepage}%
      \fancyhead[RE]{\slshape\leftmark}%
      \fancyhead[LO]{\slshape\rightmark}%
      \let\chaptermark\@gobble
      \let\sectionmark\@gobble
    }%
  }%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[internal]{\ps@headings}
% The redefinition of the \texttt{headings} style also differs for
% book-like and article-like classes. It also differs for
% one-side and two-side modes.
% \changes{fancyhdr v4.0.3}{2022/05/18}{Changed definition of
% \cs{@mkboth} from \cs{let}\cs{@mkboth}\cs{markboth} to
% \cs{def}\Cmd{@mkboth\{\cs{protect}\cs{markboth}\}} so that it will pick up changes
% to \cs{markboth}}
%    \begin{macrocode}
\DeclareOption{headings}{%
  \f@nch@ifundefined{chapter}{%
    \if@twoside
%    \end{macrocode}
% An article in two-side mode:
%    \begin{macrocode}
      \def\ps@headings{\ps@f@nch@fancyproto \def\@mkboth{\protect\markboth}
        \fancyhf{}
        \fancyhead[LE,RO]{\thepage}%
        \fancyhead[RE]{\slshape\leftmark}%
        \fancyhead[LO]{\slshape\rightmark}%
        \def\sectionmark##1{%
          \markboth{\MakeUppercase{%
            \ifnum \c@secnumdepth >\z@ \thesection\quad \fi##1}}{}}%
        \def\subsectionmark##1{%
          \markright{%
            \ifnum \c@secnumdepth >\@ne \thesubsection\quad \fi##1}}%
      }%
    \else
%    \end{macrocode}
% An article in one-side mode:
%    \begin{macrocode}
      \def\ps@headings{\ps@f@nch@fancyproto \def\@mkboth{\protect\markboth}
        \fancyhf{}
        \fancyhead[LE,RO]{\thepage}%
        \fancyhead[RE]{\slshape\leftmark}%
        \fancyhead[LO]{\slshape\rightmark}%
        \def\sectionmark##1{%
          \markright {\MakeUppercase{%
            \ifnum \c@secnumdepth >\z@ \thesection\quad \fi##1}}}%
        \let\subsectionmark\@gobble % Not needed but inserted for safety
      }%
    \fi
  }{\if@twoside
%    \end{macrocode}
% A book in two-side mode:
%    \begin{macrocode}
      \def\ps@headings{\ps@f@nch@fancyproto \def\@mkboth{\protect\markboth}
        \fancyhf{}
        \fancyhead[LE,RO]{\thepage}%
        \fancyhead[RE]{\slshape\leftmark}%
        \fancyhead[LO]{\slshape\rightmark}%
        \def\chaptermark##1{%
          \markboth{\MakeUppercase{%
            \ifnum \c@secnumdepth >\m@ne \if@mainmatter
              \@chapapp\ \thechapter. \ \fi\fi##1}}{}}%
        \def\sectionmark##1{%
          \markright {\MakeUppercase{%
            \ifnum \c@secnumdepth >\z@ \thesection. \ \fi##1}}}%
      }%
    \else
%    \end{macrocode}
% A book in one-side mode:
%    \begin{macrocode}
      \def\ps@headings{\ps@f@nch@fancyproto \def\@mkboth{\protect\markboth}
        \fancyhf{}
        \fancyhead[LE,RO]{\thepage}%
        \fancyhead[RE]{\slshape\leftmark}%
        \fancyhead[LO]{\slshape\rightmark}%
        \def\chaptermark##1{%
          \markright{\MakeUppercase{%
            \ifnum \c@secnumdepth >\m@ne \if@mainmatter
              \@chapapp\ \thechapter. \ \fi\fi##1}}}%
        \let\sectionmark\@gobble % Not needed but inserted for safety
      }%
    \fi
  }%
}
%    \end{macrocode}
% \end{macro}
%
% Process the options.
% \changes{fancyhdr v4.0}{2019/03/15}{Process package options.}
% \SpecialUsageIndex{\ProcessOptions}
%    \begin{macrocode}
\ProcessOptions*
%    \end{macrocode}
%
% \changes{fancyhdr v5.0}{2024/05/08}{Removed \cs{f@nch@errmsg} and
% \cs{f@nch@warning} and used \cs{PackageError} and \cs{PackageWarning} directly.}
% \begin{macro}[internal]{\f@nch@forc}
%   Usage: \cs{f@nch@forc} \cs{var} \texttt{\{charstring\}\{body\}}.\\
%   Execute the body for each character in \texttt{charstring} bound
%   to \cs{var}.
%   This is similar to \LaTeX's \cs{@tfor}, but it expands the \texttt{charstring}.
%    \begin{macrocode}
% \changes{fancyhdr v3.10}{2019/01/25}{Use \cs{newcommand} instead of \cs{def}.}
% \changes{fancyhdr v4.0.2}{2021/05/29}{Make \cs{f@nch@rc} \cs{long}.}
\newcommand{\f@nch@forc}[3]{\expandafter\f@nchf@rc\expandafter#1\expandafter{#2}{#3}}
\newcommand{\f@nchf@rc}[3]{\def\temp@ty{#2}\ifx\@empty\temp@ty\else
                                    \f@nch@rc#1#2\f@nch@rc{#3}\fi}
\long\def\f@nch@rc#1#2#3\f@nch@rc#4{\def#1{#2}#4\f@nchf@rc#1{#3}{#4}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}[internal]{\f@nch@for}
%   Usage: \cs{f@nch@for}\cs{var}\texttt{\{list\}}\texttt{\{body\}} \\
%   Execute the body for each element of the list, bound to \cs{var}.
%   List elements are separated by commas. This is like \LaTeX's \cs{@for} but an empty
%   list is treated as a list with an empty element.
%
%    \begin{macrocode}
\newcommand{\f@nch@for}[3]{\edef\@fortmp{#2}%
  \expandafter\@forloop#2,\@nil,\@nil\@@#1{#3}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}[internal]{\f@nch@default}
%   Usage: \cs{f@nch@default} \cs{var}\texttt{\{defaults\}\{argument\}} \\
%   Sets \cs{var} to the characters
%   from \texttt{defaults} appearing in \texttt{argument}, or to \texttt{defaults} if it would be
%   empty. All characters are lowercased first.
%
%    \begin{macrocode}
\newcommand\f@nch@default[3]{%
  \edef\temp@a{\lowercase{\edef\noexpand\temp@a{#3}}}\temp@a \def#1{}%
  \f@nch@forc\tmpf@ra{#2}%
  {\expandafter\f@nch@ifin\tmpf@ra\temp@a{\edef#1{#1\tmpf@ra}}{}}%
  \ifx\@empty#1\def#1{#2}\fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[internal]{\f@nch@ifin}
%   Usage: \cs{f@nch@ifin} \meta{char} \meta{set} \meta{truecase} \meta{falsecase} \\
% If \meta{char} is in \meta{set}, then \meta{truecase} else \meta{falsecase}.
%    \begin{macrocode}
\newcommand{\f@nch@ifin}[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}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\fancyhead}
% \changes{fancyhdr v3.9}{2016/10/12}{Let \cs{newcommand} do the
% handling of the optional parameter.}
% \begin{macro}{\fancyfoot}
% \changes{fancyhdr v3.9}{2016/10/12}{Let \cs{newcommand} do the
% handling of the optional parameter.}
% \begin{macro}{\fancyhf}
% \changes{fancyhdr v3.9}{2016/10/12}{Let \cs{newcommand} do the
% handling of the optional parameter.}
% These are the principal user macros. Pick up the parameters,
% and supply an 'h' (\cs{fancyhead}) or 'f' (\cs{fancyfoot}).
%    \begin{macrocode}
\newcommand{\fancyhead}[2][]{\f@nch@fancyhf\fancyhead h[#1]{#2}}%
\newcommand{\fancyfoot}[2][]{\f@nch@fancyhf\fancyfoot f[#1]{#2}}%
\newcommand{\fancyhf}[2][]{\f@nch@fancyhf\fancyhf {}[#1]{#2}}%
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\fancyheadoffset}
% \changes{fancyhdr v3.9}{2016/10/12}{Let \cs{newcommand} do the
% handling of the optional parameter.}
% \begin{macro}{\fancyfootoffset}
% \changes{fancyhdr v3.9}{2016/10/12}{Let \cs{newcommand} do the
% handling of the optional parameter.}
% \begin{macro}{\fancyhfoffset}
% \changes{fancyhdr v3.9}{2016/10/12}{Let \cs{newcommand} do the
% handling of the optional parameter.}
%   The commands for offsets.
% Pick up the parameters,
% and supply an 'h' (\cs{fancyheadoffset}) or 'f' (\cs{fancyfootoffset}).
%    \begin{macrocode}
\newcommand{\fancyheadoffset}[2][]{\f@nch@fancyhfoffs\fancyheadoffset h[#1]{#2}}%
\newcommand{\fancyfootoffset}[2][]{\f@nch@fancyhfoffs\fancyfootoffset f[#1]{#2}}%
\newcommand{\fancyhfoffset}[2][]{\f@nch@fancyhfoffs\fancyhfoffset {}[#1]{#2}}%
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}[internal]{\f@nch@fancyhf@Echeck}
% \changes{fancyhdr v4.1}{2022/09/06}{Implement \texttt{twoside} option.}
% Macro for warning if `E' is used without `twoside' option.
%    \begin{macrocode}
\def\f@nch@fancyhf@Echeck#1{%
  \if@twoside\else
    \iff@nch@twoside\else
      \if\f@nch@@eo e%
        \PackageWarning{fancyhdr} {\string#1's `E' option without twoside option is useless.\MessageBreak
          Please consider using the `twoside' option}%
  \fi\fi\fi
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[internal]{\f@nch@fancyhf}
% \changes{fancyhdr v4.0.2}{2022/05/10}{Make \cs{f@nch@fancyhf} \cs{long}.}
% \changes{fancyhdr v4.1}{2022/09/06}{Implement \texttt{twoside} option.}
% This macro interprets the parameters for the headers and footers.\\
% Parameters:\\
% (1) The user command that was used (like \cs{fancyhead}). This is used
% for errors/warnings.\\
% (2) \texttt{h} (for \cs{fancyhead}), \texttt{f} (for \cs{fancyfoot}),
% or \texttt{\{\}} (for \cs{fancyhf}).\\
% (3) The optional parameter that was given to these commands (default \texttt{[]}).\\
% (4) The required parameter that was given to these commands.\\
%   The header and footer fields are stored in command sequences with
%   names of the form: \cs{f@nch@}\meta{x}\meta{y}\meta{z} with \meta{x} from
%   \texttt{[eo]}, \meta{y} from \texttt{[lcr]} and \meta{z} from \texttt{[hf]}.
%
%    \begin{macrocode}
\long\def\f@nch@fancyhf#1#2[#3]#4{%
  \def\temp@c{}%
  \f@nch@forc\tmpf@ra{#3}%
  {\expandafter\f@nch@ifin\tmpf@ra{eolcrhf,EOLCRHF}%
    {}{\edef\temp@c{\temp@c\tmpf@ra}}}%
  \ifx\@empty\temp@c\else \PackageError{fancyhdr}{Illegal char `\temp@c' in
    \string#1 argument: [#3]}{}%
  \fi \f@nch@for\temp@c{#3}%
  {\f@nch@default\f@nch@@eo{eo}\temp@c
    \f@nch@fancyhf@Echeck{#1}%
    \f@nch@default\f@nch@@lcr{lcr}\temp@c
    \f@nch@default\f@nch@@hf{hf}{#2\temp@c}%
    \f@nch@forc\f@nch@eo\f@nch@@eo
        {\f@nch@forc\f@nch@lcr\f@nch@@lcr
          {\f@nch@forc\f@nch@hf\f@nch@@hf
            {\expandafter\f@nch@def\csname
              f@nch@\f@nch@eo\f@nch@lcr\f@nch@hf\endcsname {#4}}}}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[internal]{\f@nch@fancyhfoffs}
% \changes{fancyhdr v4.0.2}{2021/03/27}{Change the offset length variables
% \cs{f@nch@O@xyz} to \cs{f@nch@offset@xyz}}
% \changes{fancyhdr v4.1}{2022/09/06}{Implement \texttt{twoside} option.}
% This macro interprets the parameters for the header and footer
% offsets.\\
% Parameters:\\
% (1) The user command that was used (like \cs{fancyheadoffset}). This is used
% for errors/warnings.\\
% (2) \texttt{h} (for \cs{fancyheadoffset}), \texttt{f} (for \cs{fancyfootoffset}),
% or \texttt{\{\}} (for \cs{fancyhfoffset}).\\
% (3) The optional parameter that was given to these commands (default \texttt{[]}).\\
% (4) The required parameter that was given to these commands.\\
%   The header and footer offsets are stored in command sequences with
%   names of the form: \cs{f@nch@offset@}\meta{x}\meta{y}\meta{z} with \meta{x} from
%   \texttt{[eo]}, \meta{y} from \texttt{[lr]} and \meta{z} from \texttt{[hf]}.
%
%    \begin{macrocode}
\def\f@nch@fancyhfoffs#1#2[#3]#4{%
  \def\temp@c{}%
  \f@nch@forc\tmpf@ra{#3}%
  {\expandafter\f@nch@ifin\tmpf@ra{eolrhf,EOLRHF}%
    {}{\edef\temp@c{\temp@c\tmpf@ra}}}%
  \ifx\@empty\temp@c\else \PackageError{fancyhdr}{Illegal char `\temp@c' in
    \string#1 argument: [#3]}{}%
  \fi \f@nch@for\temp@c{#3}%
  {\f@nch@default\f@nch@@eo{eo}\temp@c
    \f@nch@fancyhf@Echeck{#1}%
    \f@nch@default\f@nch@@lcr{lr}\temp@c
    \f@nch@default\f@nch@@hf{hf}{#2\temp@c}%
    \f@nch@forc\f@nch@eo\f@nch@@eo
        {\f@nch@forc\f@nch@lcr\f@nch@@lcr
          {\f@nch@forc\f@nch@hf\f@nch@@hf
            {\expandafter\setlength\csname
              f@nch@offset@\f@nch@eo\f@nch@lcr\f@nch@hf\endcsname {#4}}}}}%
  \f@nch@setoffs}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\fancyheadwidth, \fancyfootwidth, \fancyhfwidth}
%   The commands for field widths.
% \changes{fancyhdr v5.0}{2025/01/01}{New commands \cs{fancyheadwidth},
% \cs{fancyfootwidth} and \cs{fancyhfwidth}.}
% Pick up the parameters,
% and supply an 'h' (\cs{fancyheadwidth}) or 'f' (\cs{fancyfootwidth}).
%    \begin{macrocode}
\newcommand{\fancyheadwidth}[2][]{\f@nch@fancyhfwidth\fancyheadwidth h[#1]{#2}}%
\newcommand{\fancyfootwidth}[2][]{\f@nch@fancyhfwidth\fancyfootwidth f[#1]{#2}}%
\newcommand{\fancyhfwidth}[2][]{\f@nch@fancyhfwidth\fancyhfwidth {}[#1]{#2}}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[internal]{\f@nch@fancyhfwidth}
% This macro interprets the parameters for the header and footer field
% widths.\\
% Parameters:\\
% (1) The user command that was used (like \cs{fancyheadwidth}). This is used
% for errors/warnings.\\
% (2) \texttt{h} (for \cs{fancyheadwidth}), \texttt{f} (for \cs{fancyfootwidth}),
% or \texttt{\{\}} (for \cs{fancyhfwidth}).\\
% (3) The optional parameter that was given to these commands (default \texttt{[]}).\\
% (4) The required parameter that was given to these commands.\\
%   The header and footer field widths are stored in command sequences with
%   names of the form: \cs{f@nch@width@}\meta{x}\meta{y}\meta{z} with \meta{x} from
%   \texttt{[eo]}, \meta{y} from \texttt{[lcr]} and \meta{z} from \texttt{[hf]}.
%
% First we assign the \meta{width} argument to a temporary length
% variable, to check if it is a legal \meta{length}. Then we store the
% \meta{width} in variables for all the specified places.
%
%    \begin{macrocode}
\def\f@nch@fancyhfwidth#1#2[#3]#4{%
  \setlength\@tempdima{#4}%
  \def\temp@c{}%
  \f@nch@forc\tmpf@ra{#3}%
  {\expandafter\f@nch@ifin\tmpf@ra{eolcrhf,EOLCRHF}%
    {}{\edef\temp@c{\temp@c\tmpf@ra}}}%
  \ifx\@empty\temp@c\else \PackageError{fancyhdr}{Illegal char `\temp@c' in
    \string#1 argument: [#3]}{}%
  \fi \f@nch@for\temp@c{#3}%
  {\f@nch@default\f@nch@@eo{eo}\temp@c
    \f@nch@fancyhf@Echeck{#1}%
    \f@nch@default\f@nch@@lcr{lcr}\temp@c
    \f@nch@default\f@nch@@hf{hf}{#2\temp@c}%
    \f@nch@forc\f@nch@eo\f@nch@@eo
        {\f@nch@forc\f@nch@lcr\f@nch@@lcr
          {\f@nch@forc\f@nch@hf\f@nch@@hf
            {\expandafter\def\csname
              f@nch@width@\f@nch@eo\f@nch@lcr\f@nch@hf\endcsname {#4}}}}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[internal]{\f@nch@width@elh}
% \begin{macro}[internal]{\f@nch@width@ech}
% \begin{macro}[internal]{\f@nch@width@erh}
% \begin{macro}[internal]{\f@nch@width@olh}
% \begin{macro}[internal]{\f@nch@width@och}
% \begin{macro}[internal]{\f@nch@width@orh}
% \begin{macro}[internal]{\f@nch@width@elf}
% \begin{macro}[internal]{\f@nch@width@ecf}
% \begin{macro}[internal]{\f@nch@width@erf}
% \begin{macro}[internal]{\f@nch@width@olf}
% \begin{macro}[internal]{\f@nch@width@ocf}
% \begin{macro}[internal]{\f@nch@width@orf}
% Length parameters for the widths. These are stored as macros. They are
% calculated as lengths when the header/footer is built.
%
%    \begin{macrocode}
\def\f@nch@width@elh{\headwidth}
\def\f@nch@width@ech{\headwidth}
\def\f@nch@width@erh{\headwidth}
\def\f@nch@width@olh{\headwidth}
\def\f@nch@width@och{\headwidth}
\def\f@nch@width@orh{\headwidth}
\def\f@nch@width@elf{\headwidth}
\def\f@nch@width@ecf{\headwidth}
\def\f@nch@width@erf{\headwidth}
\def\f@nch@width@olf{\headwidth}
\def\f@nch@width@ocf{\headwidth}
\def\f@nch@width@orf{\headwidth}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\lhead}
% \changes{fancyhdr v3.9}{2016/10/12}{Let \cs{newcommand} do the
% handling of the optional parameter.}
% \begin{macro}{\chead}
% \changes{fancyhdr v3.9}{2016/10/12}{Let \cs{newcommand} do the
% handling of the optional parameter.}
% \begin{macro}{\rhead}
% \changes{fancyhdr v3.9}{2016/10/12}{Let \cs{newcommand} do the
% handling of the optional parameter.}
% \begin{macro}{\lfoot}
% \changes{fancyhdr v3.9}{2016/10/12}{Let \cs{newcommand} do the
% handling of the optional parameter.}
% \begin{macro}{\cfoot}
% \changes{fancyhdr v3.9}{2016/10/12}{Let \cs{newcommand} do the
% handling of the optional parameter.}
% \begin{macro}{\rfoot}
% \changes{fancyhdr v3.9}{2016/10/12}{Let \cs{newcommand} do the
% handling of the optional parameter.}
%   Fancyheadings version 1 commands. These are deprecated,
%   but they continue to work for compatibility reasons. They have an
%   optional parameter that is used as the value for even pages in a
%   two-sided document. If this is not given (or if the document is not
%   two-sided) the required parameter is used for both even and odd pages.
%   Therefore the default value for the optional parameter is the
%   required parameter. It is not possible to express this directly in
%   the definition. Therefore we use a trick. Both parameters are store
%   in a macro. For example for \cs{lhead} the parameter for even pages
%   is stored in \cs{f@nch@elh}, and the one for odd pages in
%   \cs{f@nch@olh}. For the others it is similar, just replace the
%   \texttt{l} with \texttt{c} or \texttt{r}, and the \texttt{h} with
%   \texttt{f}. In the body of the macro we first store the required
%   parameter in \cs{f@nch@olh}, and we use this macro as default for
%   the optional parameter. The optional parameter is then stored in
%   \cs{f@nch@elh}. The order of the assignments is therefore important.
%
%    \begin{macrocode}
\newcommand{\lhead}[2][\f@nch@olh]%
                     {\f@nch@def\f@nch@olh{#2}\f@nch@def\f@nch@elh{#1}}
\newcommand{\chead}[2][\f@nch@och]%
                     {\f@nch@def\f@nch@och{#2}\f@nch@def\f@nch@ech{#1}}
\newcommand{\rhead}[2][\f@nch@orh]%
                     {\f@nch@def\f@nch@orh{#2}\f@nch@def\f@nch@erh{#1}}
\newcommand{\lfoot}[2][\f@nch@olf]%
                     {\f@nch@def\f@nch@olf{#2}\f@nch@def\f@nch@elf{#1}}
\newcommand{\cfoot}[2][\f@nch@ocf]%
                     {\f@nch@def\f@nch@ocf{#2}\f@nch@def\f@nch@ecf{#1}}
\newcommand{\rfoot}[2][\f@nch@orf]%
                     {\f@nch@def\f@nch@orf{#2}\f@nch@def\f@nch@erf{#1}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}[internal]{\f@nch@headwidth}
% \changes{fancyhdr v4.0.2}{2021/03/27}{Change the offset length variables
% \cs{f@nch@O@xyz} to \cs{f@nch@offset@xyz}}
% Length parameter to be used for \cs{headwidth}. We use this rather
% than defining \cs{headwidth} as a length paramater directly to protect
% ourself to someone saying: \verb+\let\headwidth\textwidth+.
%    \begin{macrocode}
\newlength{\f@nch@headwidth} \let\headwidth\f@nch@headwidth
%    \end{macrocode}
% \end{macro}
% \begin{macro}[internal]{\f@nch@offset@elh}
% \begin{macro}[internal]{\f@nch@offset@erh}
% \begin{macro}[internal]{\f@nch@offset@olh}
% \begin{macro}[internal]{\f@nch@offset@orh}
% \begin{macro}[internal]{\f@nch@offset@elf}
% \begin{macro}[internal]{\f@nch@offset@erf}
% \begin{macro}[internal]{\f@nch@offset@olf}
% \begin{macro}[internal]{\f@nch@offset@orf}
% Length parameters for the offsets.
%    \begin{macrocode}
\newlength{\f@nch@offset@elh}
\newlength{\f@nch@offset@erh}
\newlength{\f@nch@offset@olh}
\newlength{\f@nch@offset@orh}
\newlength{\f@nch@offset@elf}
\newlength{\f@nch@offset@erf}
\newlength{\f@nch@offset@olf}
\newlength{\f@nch@offset@orf}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\headrulewidth}
% \begin{macro}{\footrulewidth}
%    \begin{macrocode}
\newcommand{\headrulewidth}{0.4pt}
\newcommand{\footrulewidth}{0pt}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\headruleskip}
%  Don't define \cs{headruleskip} if it is already defined.
% \changes{fancyhdr v4.0}{2019/03/22}{Parameter \cs{headruleskip}.}
%    \begin{macrocode}
\f@nch@ifundefined{headruleskip}%
      {\newcommand{\headruleskip}{0pt}}{}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\footruleskip}
%  Memoir also defines \cs{footruleskip}.
%  Don't define \cs{footruleskip} if it is already defined.
%    \begin{macrocode}
\f@nch@ifundefined{footruleskip}%
      {\newcommand{\footruleskip}{.3\normalbaselineskip}}{}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\plainheadrulewidth}
% \begin{macro}{\plainfootrulewidth}
%   Fancyplain stuff shouldn't be used anymore (rather
%   \texttt{\cs{fancypagestyle}\{plain\}} should be used), but we keep it for
%   compatibility reasons.
%
%    \begin{macrocode}
\newcommand{\plainheadrulewidth}{0pt}
\newcommand{\plainfootrulewidth}{0pt}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[internal]{\if@fancyplain}
% Boolean for the implementation of \cs{fancyplain}
%    \begin{macrocode}
\newif\if@fancyplain \@fancyplainfalse
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\fancyplain}
% Deprecated macro
%    \begin{macrocode}
\def\fancyplain#1#2{\if@fancyplain#1\else#2\fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\headwidth}
%   Initialise \cs{headwidth} with a magic constant.
%    \begin{macrocode}
\headwidth=-123456789sp
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[internal]{\f@nch@raggedleft}
% \begin{macro}[internal]{\f@nch@raggedright}
% \begin{macro}[internal]{\f@nch@centering}
% \begin{macro}[internal]{\f@nch@everypar}
% \changes{fancyhdr v4.3}{2024/07/17}{Changed \cs{f@nch@everypar}. If the
%   LaTeX kernel has  expl3, use \cs{tex\_everypar:D}, and reset \cs{par},
%   \cs{@@par} and \cs{endgraf} to their original \TeX{} definitions, so that
%   no paragraph hooks will intrude in \pkg{fancyhdr} code.}
% \changes{fancyhdr v4.3.1}{2024/07/23}{Also reset \cs{everypar} to its
%   original \TeX{} value \cs{tex\_everypar:D} in \cs{f@nch@resetpar},
%   otherwise environments based on \cs{trivlist}
%   will not work properly in \pkg{fancyhdr} headers and footers.}
% Save the standard definitions of \cs{raggedleft}, \cs{raggedright},
% \cs{centering} and \cs{everypar} so that we can reset them when we are
% typesetting the headers and footers. Some packages change these to
% incompatible values.\\
% We also disable paragraph hooks, so that no paragraph hooks will
% intrude in \pkg{fancyhdr} code. \textbf{NOTE: This is a hack, and
% should be replaced by cleaner code as soon as the \LaTeX{} kernel
% provides the necessary commands.}
% \changes{fancyhdr v4.5}{2024/11/29}{We use a less fragile way to
% disable paragraph hooks, thereby partially reverting the solution in
% version v4.3 and v4.3.1. This is less intruding in the hook system,
% and especially it doesn't affect kernel hooks, only user provided
% ones. We check the kernel version to see if it support paragraph hooks.}
%    \begin{macrocode}
\let\f@nch@raggedleft\raggedleft
\let\f@nch@raggedright\raggedright
\let\f@nch@centering\centering
\let\f@nch@everypar\everypar
\ifdefined\ExplSyntaxOn
  \ExplSyntaxOn
  \providecommand\IfFormatAtLeastTF{\@ifl@t@r\fmtversion}
  \IfFormatAtLeastTF{2021-06-01}{
%    \end{macrocode}
% \begin{macro}{\f@nch@clear@par@hook:n}
% Clear a (paragraph) hook locally.
% We don't need to restore as this will be done by the \TeX{} grouping.
%    \begin{macrocode}
    \cs_new:Npn \f@nch@clear@par@hook:n #1{
      \tl_clear:c {__hook~#1}
    }
    \newcommand\f@nch@resetpar{
      \f@nch@everypar{}
      \f@nch@clear@par@hook:n{para/before}
      \f@nch@clear@par@hook:n{para/begin}
      \f@nch@clear@par@hook:n{para/end}
      \f@nch@clear@par@hook:n{para/after}
    }
  }{
  \newcommand\f@nch@resetpar{
    \f@nch@everypar{}
  }
}
  \ExplSyntaxOff
\else
  \newcommand\f@nch@resetpar{%
    \f@nch@everypar{}%
  }
\fi
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}[internal]{\f@nch@noUppercase}
%   We want \cs{nouppercase} to work with the various evolutionary stages of
%   \cs{MakeUppercase}. The current version (2022/11/09) accepts an optional
%   argument with a language specification. Therefore we define a dummy macro
%   \cs{f@nch@noUppercase} which copies its mandatory agument, as a
%   replacement for \cs{MakeUppercase} while \cs{nouppercase} is active.
%    \begin{macrocode}
\newcommand\f@nch@noUppercase[2][]{#2}
%    \end{macrocode}
%
% \begin{macro}[internal]{\f@nch@reset}
%   Command to reset various things in the headers: a.o. single spacing
%   (taken from setspace.sty) and the catcode of ^^M and
%   \cs{endlinechar} (so that epsf files
%   in the header work if a verbatim crosses a page boundary).
%   Also reset the catcodes that are changed in verbatim environments,
%   \cs{makeatother} and \cs{ExplSyntaxOn}. It also
%   defines a \cs{nouppercase} command that disables \cs{uppercase} and
%   \cs{Makeuppercase}. It can only be used in the headers and footers. Set
%   \cs{hsize} to \cs{headwidth} (this helps for multicol); reset
%   \bsbs, \cs{raggedleft}, \cs{raggedright} and \cs{centering} to
%   their default values (for tabu), and \cs{everypar} to empty.\\
%   The font is reset to \cs{normalfont}. Actually this is done in the
%   \LaTeX{} output routine, so we don't have to do it here.
%   \changes{fancyhdr v4.1}{2022/11/09}{Change \cs{nouppercase} to work with
%     new definition of \cs{MakeUppercase}.}
%   \changes{fancyhdr v4.2}{2024/04/19}{Reset catcodes to their default
%   values in order to facilitate \cs{input} in headers/footers when \texttt{verbatim}
%   is active. (Issue \# 8 \url{https://github.com/pietvo/fancyhdr/issues/8}.)}
%   \changes{fancyhdr v4.3}{2024/05/08}{Remove pre-NFSS stuff}
%   \changes{fancyhdr v4.3}{2024/07/17}{Replace \cs{f@nch@everypar} by \cs{f@nch@resetpar}.}
%    \begin{macrocode}
\def\f@nch@reset{\f@nch@resetpar\restorecr\endlinechar=13
  \catcode`\\=0\catcode`\{=1\catcode`\}=2\catcode`\$=3\catcode`\&=4
  \catcode`\#=6\catcode`\^=7\catcode`\_=8\catcode`\ =10\catcode`\@=11
  \catcode`\:=11\catcode`\~=13\catcode`\%=14
  \catcode0=15 %NULL
  \catcode9=10 %TAB
  \let\\\@normalcr \let\raggedleft\f@nch@raggedleft
  \let\raggedright\f@nch@raggedright \let\centering\f@nch@centering
  \def\baselinestretch{1}%
  \hsize=\headwidth
  \def\nouppercase##1{{%
      \let\uppercase\relax\let\MakeUppercase\f@nch@noUppercase
      \expandafter\let\csname MakeUppercase \endcsname\relax
      \expandafter\def\csname MakeUppercase\space\space\space\endcsname
                                                   [####1]####2{####2}%
      ##1}}%
  \f@nch@ifundefined{@normalsize} {\normalsize} % for ucthesis.cls
   {\@normalsize}%
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\fancycenter}
% \changes{fancyhdr v4.0}{2019/03/15}{Macro \cs{fancycenter} added}
% \cs{fancycenter}\oarg{dist}\oarg{stretch}\marg{left-field}\marg{center-field}\marg{right-field}
%    \begin{macrocode}
\newcommand*{\fancycenter}[1][1em]{%
  \@ifnextchar[{\f@nch@center{#1}}{\f@nch@center{#1}[3]}%
}
\def\f@nch@center#1[#2]#3#4#5{%
%    \end{macrocode}
% At first, we execute the case when the \meta{center-field} is empty\footnote{%
% This code is reused from the \PKGindex{nccfancyhdr}\pkg{nccfancyhdr} package by Alexander I. Rozhenko}:
%    \begin{macrocode}
  \def\@tempa{#4}\ifx\@tempa\@empty
    \hbox to\linewidth{\color@begingroup{#3}\hfil {#5}\color@endgroup}%
  \else
%    \end{macrocode}
% All we need to do is to calculate skips inserted before and after
% \meta{center-field}. We will calculate them in the \verb|\@tempskipa|
% and \verb|\@tempskipb| registers. At first:
% \begin{quote}
% \verb|\@tempdima:=|\meta{dist};\\
% \verb|\@tempdimb:=|\meta{dist}\verb|*|\meta{stretch};\\
% \verb|\@tempdimc:=|\meta{dist}\verb|*|\meta{stretch}\verb|-|\meta{dist};\\
% \verb|\@tempskipa:=\@tempskipb:=\@tempdimb + 1fil - \@tempdimc|;
% \end{quote}
%    \begin{macrocode}
    \setlength\@tempdima{#1}%
    \setlength{\@tempdimb}{#2\@tempdima}%
    \@tempdimc \@tempdimb \advance\@tempdimc -\@tempdima
    \setlength\@tempskipa{\@tempdimb \@plus 1fil \@minus \@tempdimc}%
    \@tempskipb\@tempskipa
%    \end{macrocode}
% At this point, the \cs{@tempskipa} and \cs{@tempskipb} registers
% have the natural size \meta{dist}\verb|*|\meta{stretch},
% unlimited stretchability, and the minimum size \meta{dist}.
% Now we decrease the minimum size of \cs{@tempskipa} to zero if
% the \meta{left-field} is empty:
%    \begin{macrocode}
    \def\@tempa{#3}\ifx\@tempa\@empty
      \addtolength\@tempskipa{\z@ \@minus \@tempdima}%
    \fi
%    \end{macrocode}
% Do the same things with the \cs{@tempskipb} register if the
% \meta{right-field} is empty:
%    \begin{macrocode}
    \def\@tempa{#5}\ifx\@tempa\@empty % empty right
      \addtolength\@tempskipb{\z@ \@minus \@tempdima}%
    \fi
%    \end{macrocode}
% Finally, we correct the left and right glues taking into
% account the difference between lengths of \meta{left-field}
% and \meta{right-field}. We calculate which mark is shorter
% and increase the natural size of the corresponding register
% by the difference between their lengths.
%    \begin{macrocode}
    \settowidth{\@tempdimb}{#3}%
    \settowidth{\@tempdimc}{#5}%
    \ifdim\@tempdimb>\@tempdimc
      \advance\@tempdimb -\@tempdimc
      \addtolength\@tempskipb{\@tempdimb \@minus \@tempdimb}%
    \else
      \advance\@tempdimc -\@tempdimb
      \addtolength\@tempskipa{\@tempdimc \@minus \@tempdimc}%
    \fi
%    \end{macrocode}
% The \cs{@tempskipa} and \cs{@tempskipb} have been calculated.
% Put everything in the box.
%    \begin{macrocode}
    \hbox to\linewidth{\color@begingroup{#3}\hskip \@tempskipa
                      {#4}\hskip \@tempskipb {#5}\color@endgroup}%
  \fi
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\fancyheadinit}
% This macro can be used to define initialisation code that will be run
% before the construction of the header. It can for example set the
% color or the font, or change \cs{headrulewidth} or \cs{headruleskip}.
% It cannot make global changes, just changes for the header.
% \begin{macro}[internal]{\f@nch@headinit}
%   Storage for the header initialisation code.
%    \begin{macrocode}
\newcommand{\f@nch@headinit}{}
\newcommand{\fancyheadinit}[1]{%
  \def\f@nch@headinit{#1}%
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\fancyfootinit}
% This macro can be used to define initialisation code that will be run
% before the construction of the footer. It can for example set the
% color or the font, or change \cs{footrulewidth} or \cs{footruleskip}.
% It cannot make global changes, just changes for the footer.
% \begin{macro}[internal]{\f@nch@footinit}
%   Storage for the footer initialisation code.
%    \begin{macrocode}
\newcommand{\f@nch@footinit}{}
\newcommand{\fancyfootinit}[1]{%
  \def\f@nch@footinit{#1}%
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\fancyhfinit}
% This macro sets both the header and the footer initialisation codes to
% the same value.
%    \begin{macrocode}
\newcommand{\fancyhfinit}[1]{%
  \def\f@nch@headinit{#1}%
  \def\f@nch@footinit{#1}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{hookdecl}{fancyhdr/before, fancyhdr/after, fancyhdr/head/begin,
%               fancyhdr/head/end, fancyhdr/foot/begin, fancyhdr/foot/end}
% \changes{fancyhdr v4.5}{2024/12/05}{Hooks added}
% Here we define the \pkg{fancyhdr} hooks. It will be conditional on
% the presence of hook support in the \LaTeX{} kernel.
%    \begin{macrocode}
\ifdefined\NewMirroredHookPair
  \NewMirroredHookPair{fancyhdr/before}{fancyhdr/after}
  \NewMirroredHookPair{fancyhdr/head/begin}{fancyhdr/head/end}
  \NewMirroredHookPair{fancyhdr/foot/begin}{fancyhdr/foot/end}
\fi
%    \end{macrocode}
% \end{hookdecl}
%
% \begin{macro}[internal]{\f@nch@height}
% \changes{fancyhdr v5.0}{2021/02/20}{Length variable \cs{f@nch@height} added}
% Length variable to store heigth of header/footer for use in \cs{fancyhdrsettoheight}
%    \begin{macrocode}
\newlength\f@nch@height
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[internal]{\f@nch@footalignment}
% Length variable to store alignment length of \cs{fancyfootalign}
%    \begin{macrocode}
\newlength\f@nch@footalignment
%    \end{macrocode}
% \end{macro}
% \begin{macro}[internal]{\iff@nch@footalign}
% Boolean variable to store if a \meta{length} parameter was given to \cs{fancyfootalign}
%    \begin{macrocode}
\newif\iff@nch@footalign\f@nch@footalignfalse
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\fancyfootalign}
% \changes{fancyhdr v5.0}{2024/05/13}{Macro \cs{fancyfootalign},
% \cs{iff@nch@footalign} and length variable \cs{f@nch@footalignment} added}
% This macro sets the distance between the bottom of the footer and the
% bottom margin. The argument can be empty, or a \meta{length}.
%    \begin{macrocode}
\newcommand{\fancyfootalign}[1]{%
  \def\temp@a{#1}%
  \ifx\temp@a\@empty
    \f@nch@footalignfalse
  \else
    \f@nch@footaligntrue
    \setlength\f@nch@footalignment{#1}%
  \fi
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\fancyhdrsettoheight}
% \changes{fancyhdr v5.0}{2021/02/20}{Macro \cs{fancyhdrsettoheight} added}
% Macro to store the height of a header/footer in a length variable.\\
% \cs{fancyhdrsettoheight}\marg{lengthvar}\marg{header/footer}\\
% The second parameter can be \texttt{oddhead}, \texttt{evenhead},
% \texttt{oddfoot}, or \texttt{evenfoot}.
%    \begin{macrocode}
\newcommand\fancyhdrsettoheight[2]{%
  \expandafter\ifx\csname f@nch@#2\endcsname\fancyhdrsettoheight
    \else\PackageError{fancyhdr}{Unknown parameter #2 in \string\fancyhdrsettoheight}{}\fi
  \setbox\@tempboxa\hbox{{\f@nch@checkfalse\csname @#2\endcsname}}%
  \setlength{#1}\f@nch@height
  \setbox\@tempboxa\box\voidb@x
}
%    \end{macrocode}
% Define commands that specify the valid arguments for the second parameter.
%    \begin{macrocode}
\let\f@nch@oddhead\fancyhdrsettoheight
\let\f@nch@evenhead\fancyhdrsettoheight
\let\f@nch@oddfoot\fancyhdrsettoheight
\let\f@nch@evenfoot\fancyhdrsettoheight
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[internal]{\f@nch@vbox}
% Make a \cs{vbox} with the header or footer. Check whether there is
% enough space and give a warning if not.
%   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.\\
%   Parameter 1 is \cs{headheight} or \cs{footskip}, respectively.\\
%   Parameter 2 is the contents of the box.
%
% \changes{fancyhdr v3.10}{2019/01/25}{Don't use \cs{global}\cs{setlength}.}
% \changes{fancyhdr v3.10}{2019/01/26}{Use \cs{newcommand} instead of \cs{def}.}
% \changes{fancyhdr v4.0}{2019/03/15}{Don't adjust the
% \cs{headheight}/\cs{footskip}, except when the \texttt{compatV3} option is given.}
% \changes{fancyhdr v4.0}{2019/03/15}{Don't check when the \texttt{nocheck} option is given.}
% \changes{fancyhdr v4.0.2}{2021/02/11}{Shorten one sentence in the warning message.}
% \changes{fancyhdr v4.0.2}{2021/02/11}{If the option \texttt{[nocheck]}
% is given, just keep quiet and don't change the
% \cs{headheight}/\cs{footskip} even if the \texttt{[compatV3]} option
% is given.}
% \changes{fancyhdr v5.0}{2021/02/20}{Set \cs{f@nch@height} to height of box}
%    \begin{macrocode}
\newcommand\f@nch@vbox[2]{%
  \setbox0\vbox{#2}%
  \global\f@nch@height=\ht0
  \ifdim\ht0>#1\relax
%    \end{macrocode}
% This is the part where the the header/footer is too tall for the vertical space.
% If the \texttt{[nocheck]} package option is not given, we give a
% warning message.
%    \begin{macrocode}
    \iff@nch@check
      \dimen0=#1\advance\dimen0-\ht0
      \PackageWarning{fancyhdr}{%
        \string#1 is too small (\the#1): \MessageBreak
        Make it at least \the\ht0, for example:\MessageBreak
        \string\setlength{\string#1}{\the\ht0}%
%    \end{macrocode}
% If the \texttt{[compatV3]} option was given (and not
% \texttt{[nocheck]}), we will also change the
% \cs{headheight}/\cs{footskip} globally below, and announce this in the warning message.
%    \begin{macrocode}
        \iff@nch@compatViii .\MessageBreak
        We now make it that large for the rest of the document.\MessageBreak
        This may cause the page layout to be inconsistent, however
        \fi
        \ifx#1\headheight .\MessageBreak
          You might also make \topmargin smaller:\MessageBreak
          \string\addtolength{\string\topmargin}{\the\dimen0}%
        \fi
        \@gobble
      }%
%    \end{macrocode}
% Here we do the actual global changing of the \cs{headheight}/\cs{footskip}.
%    \begin{macrocode}
      \iff@nch@compatViii
        \dimen0=#1\relax
        \global#1=\ht0\relax
        \ht0=\dimen0 %
      \else
        \ht0=#1\relax
      \fi
%    \end{macrocode}
% However, if the \texttt{[nocheck]} options is given, we just make the
% height of the header/footer equal to the reserved space, so that no
% warning about ``Overfull vbox'' will be given. So we pretend that it
% fits, and it is the users's responsibility to make sure no unwanted
% effects take place.
%    \begin{macrocode}
    \else
      \ht0=#1\relax
    \fi
  \fi
  \box0}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[internal]{\f@nch@head}
% \changes{fancyhdr v4.0}{2019/03/22}{Parameter \cs{headruleskip}.}
% \changes{fancyhdr v4.0}{2019/03/25}{\cs{fancyheadinit} initialisation
% code added and \cs{f@nch@reset} moved up.}
% \changes{fancyhdr v4.0.2}{2022/05/09}{Added
%   \cs{leavevmode}\cs{ignorespaces} to each  header/footer field.
%   The \cs{leavevmode} prevents a bug when a field starts with a
%   \cs{color} command. The \cs{ignorespaces} skips initial spaces in
%   the parameter, as is usual in a \cs{parbox}, for backwards compatibility.}
% \changes{fancyhdr v4.5}{2024/12/05}{Hooks added}
% \changes{fancyhdr v5.0}{2025/01/01}{Support for \cs{fancyheadwidth},
% \cs{fancyfootwidth} and \cs{fancyhfwidth}.}
%   Put together a header (\cs{f@nch@head}) or footer (\cs{f@nch@foot})
%   given the left, center and right text and their widths,
%   fillers at left and right and a rule. The \cs{xlap} commands put
%   the text into an hbox of zero size, so overlapping text does not
%   generate an errormessage.\\ These macros have 8 parameters:\\
%   1. LEFTSIDE BEARING.  This determines at which side the header will stick
%   out. When \cs{fancyhfoffset} is used this calculates \cs{headwidth},
%   otherwise it is \cs{hss} or \cs{relax} (after expansion). \\
%   2. \cs{f@nch@olh}, \cs{f@nch@elh}, \cs{f@nch@olf} or \cs{f@nch@elf}.
%   This is the left component. \\
%   3. \cs{f@nch@och}, \cs{f@nch@ech}, \cs{f@nch@ocf} or \cs{f@nch@ecf}.
%   This is the center component. \\
%   4. \cs{f@nch@orh}, \cs{f@nch@erh}, \cs{f@nch@orf} or \cs{f@nch@erf}.
%   This is the right component. \\
%   5. RIGHTSIDE BEARING. This is always \cs{relax} or \cs{hss}
%   (after expansion).\\
%   6-8. The specified widths for the left, center and right fields.
%
% Before constructing the header or footer, the environment is reset to
% a known state, the appropriate hooks (\texttt{fancyhdr/before} and
% \TTindex{fancyhdr/before}%
% \TTindex{fancyhdr/head/begin}\TTindex{fancyhdr/foot/begin}%
% \texttt{fancyhdr/head/begin} or \texttt{fancyhdr/foot/begin}) are
% run, and then the corresponding initialisation code as given in
% \cs{fancyheadinit} or \cs{fancyfootinit}, respectively, is run.\\
% After constructing the header or footer, the hooks for the end
% (\texttt{fancyhdr/head/end} or \texttt{fancyhdr/foot/end} and
% \TTindex{fancyhdr/head/end}\TTindex{fancyhdr/foot/end}%
% \TTindex{fancyhdr/after}%
% \texttt{fancyhdr/after}) are run.
%    \begin{macrocode}
\newcommand\f@nch@head[8]{%
  \f@nch@reset
  \ifdefined\UseHook\UseHook{fancyhdr/before}\UseHook{fancyhdr/head/begin}\fi
  \f@nch@headinit\relax
  #1%
  \hbox to\headwidth{%
    \f@nch@vbox\headheight{%
      \f@nch@hfbox{#2}{#3}{#4}{b}{#6}{#7}{#8}%
      \vskip\headruleskip\relax
      \headrule
    }%
  }%
  #5%
  \ifdefined\UseHook\UseHook{fancyhdr/head/end}\UseHook{fancyhdr/after}\fi
}
%    \end{macrocode}
%
% \begin{macro}[internal]{\f@nch@foot}
% \changes{fancyhdr v3.10}{2019/01/26}{Put \cs{footrule} in a \cs{vbox}
% to accommodate for flexible footrules.}
% \changes{fancyhdr v3.10}{2019/01/28}{Use \cs{unvbox} on the footrule \cs{vbox}
% to preserve vertical spacing.}
% \changes{fancyhdr v3.10}{2019/01/28}{Move \cs{footruleskip} outside of the \cs{footrule}
% definition.}
% \changes{fancyhdr v4.0}{2019/03/25}{\cs{fancyfootinit} initialisation
% code added and \cs{f@nch@reset} moved up.}
% \changes{fancyhdr v4.0.2}{2022/05/09}{Added
%   \cs{leavevmode}\cs{ignorespaces} to each  header/footer field.
%   The \cs{leavevmode} prevents a bug when a field starts with a
%   \cs{color} command. The \cs{ignorespaces} skips initial spaces in
%   the parameter, as is usual in a \cs{parbox}, for backwards compatibility.}
% \changes{fancyhdr v4.5}{2024/12/05}{Hooks added}
% \changes{fancyhdr v5.0}{2025/01/01}{Support for \cs{fancyheadwidth},
% \cs{fancyfootwidth} and \cs{fancyhfwidth}.}
% We put the \cs{footrule} in a \cs{vbox} to accommodate for flexible
% footrules (e.g., using \cs{hrulefill}), so that the \cs{headwidth} will
% be used as the line width. But to preserve the vertical spacing we
% then \cs{unvbox} this box.
%
%    \begin{macrocode}
\newcommand\f@nch@foot[8]{%
  \f@nch@reset
  \ifdefined\UseHook\UseHook{fancyhdr/before}\UseHook{fancyhdr/foot/begin}\fi
  \f@nch@footinit\relax
  #1%
  \hbox to\headwidth{%
    \f@nch@vbox\footskip{%
      \setbox0=\vbox{\footrule}\unvbox0
      \vskip\footruleskip
      \f@nch@hfbox{#2}{#3}{#4}{t}{#6}{#7}{#8}%
%    \end{macrocode}
% \changes{fancyhdr v5.0}{2024/05/13}{\cs{vskip}\meta{length}
%  added if \cs{fancyfootalign}\protect\marg{length} was given.}
%    Add vertical space if \cs{fancyfootalign}\marg{length} has been given.
%    \begin{macrocode}
    \iff@nch@footalign \vskip\f@nch@footalignment \fi
    }%
  }%
  #5%
  \ifdefined\UseHook\UseHook{fancyhdr/foot/end}\UseHook{fancyhdr/after}\fi
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[internal]{\f@nch@widthL, \f@nch@widthC, \f@nch@widthR}
% Length variables to store the field widths during construction of the
% header/footer.
%    \begin{macrocode}
\newlength\f@nch@widthL
\newlength\f@nch@widthC
\newlength\f@nch@widthR
%    \end{macrocode}
%
% \end{macro}
% \begin{macro}[internal]{\f@nch@hfbox}
% This macro constructs the box with the header or footer. It has 7 parameters:\\
% 1. Left field\\
% 2. Center field\\
% 3. Right field\\
% 4. Vertical alignment: \texttt{b} for the header or \texttt{t} for the footer\\
% 5-7. Widths for the left, center and right fields.
%
% Algorithm:\\
% First we store the field widths in length variables.\\
% If the sum of the field widths > \cs{headwidth}: the center field is
% centered in the header/footer, and the left and right fields are put
% in an \cs{\meta{x}lap} to prevent error messages about overlapping.\\
% Otherwise, if there is no overlap between the fields, also put the
% center field centered in the header/footer. This is done by the macro
% \cs{f@nch@hfbox@center}\\
% Otherwise (there is enough space, but centering would cause overlap):\\
% put the center field centered between the left and right field, i.e.,
% with equal gaps on both sides. This is done by the macro
% \cs{f@nch@hfbox@fit}.
%
%    \begin{macrocode}
\newcommand\f@nch@hfbox[7]{%
  \setlength\f@nch@widthL{#5}%
  \setlength\f@nch@widthC{#6}%
  \setlength\f@nch@widthR{#7}%
  \let\@tempa\f@nch@hfbox@center
  \ifdim \dimexpr \f@nch@widthL+\f@nch@widthC+\f@nch@widthR>\headwidth
  \else
    \ifdim \dimexpr \f@nch@widthL+0.5\f@nch@widthC>0.5\headwidth
      \let \@tempa\f@nch@hfbox@fit
    \fi
    \ifdim \dimexpr \f@nch@widthR+0.5\f@nch@widthC>0.5\headwidth
      \let \@tempa\f@nch@hfbox@fit
    \fi
  \fi
  \@tempa{#1}{#2}{#3}{#4}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[internal]{\f@nch@hfbox@center}
% This macro constructs the box with the header or footer. This is the
% version that centers the center field in the total header/footer.
% It has 4 parameters:\\
% 1. Left field\\
% 2. Center field\\
% 3. Right field\\
% 4. Vertical alignment: \texttt{b} for the header or \texttt{t} for the footer\\
%    \begin{macrocode}
\newcommand\f@nch@hfbox@center[4]{%
  \hbox to \headwidth{%
    \rlap{\parbox[#4]{\f@nch@widthL}{\raggedright\leavevmode\ignorespaces#1}}%
    \hfill
    \parbox[#4]{\f@nch@widthC}{\centering\leavevmode\ignorespaces#2}%
    \hfill
    \llap{\parbox[#4]{\f@nch@widthR}{\raggedleft\leavevmode\ignorespaces#3}}%
  }%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}[internal]{\f@nch@hfbox@fit}
% This macro constructs the box with the header or footer. This is the
% version that centers the center field between the left and right fields.
% It has 4 parameters:\\
% 1. Left field\\
% 2. Center field\\
% 3. Right field\\
% 4. Vertical alignment: \texttt{b} for the header or \texttt{t} for the footer\\
%    \begin{macrocode}
\newcommand\f@nch@hfbox@fit[4]{%
  \hbox to \headwidth{%
    \parbox[#4]{\f@nch@widthL}{\raggedright\leavevmode\ignorespaces#1}%
    \hfill
    \parbox[#4]{\f@nch@widthC}{\centering\leavevmode\ignorespaces#2}%
    \hfill
    \parbox[#4]{\f@nch@widthR}{\raggedleft\leavevmode\ignorespaces#3}%
  }%
}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[internal]{\MakeUppercase}
%   Define \MakeUppercase for old \LaTeX{}en. Note: we used \cs{def} rather
%   than \cs{let}, so that \verb+\let\uppercase\relax+ (from the version 1
%   documentation) will still work.
%
%    \begin{macrocode}
\f@nch@ifundefined{MakeUppercase}{\def\MakeUppercase{\uppercase}}{}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[internal]{\@chapapp}
% Define \cs{@chapapp} for classes that don't have it, e.g., amsbook
%    \begin{macrocode}
\f@nch@ifundefined{@chapapp}{\let\@chapapp\chaptername}{}%
%    \end{macrocode}
% \end{macro}
% \begin{macro}[internal]{\f@nch@initialise}
% \changes{fancyhdr v4.0}{2019/03/21}{Put all the initialisation code in
% \cs{f@nch@initialise}}
% This macro initialises the headers and footers and \cs{chaptermark}
% and/or \Cmd{[sub]sectionmark} for page style \texttt{fancy}
%    \begin{macrocode}
\def\f@nch@initialise{%
%    \end{macrocode}
%
% \begin{macro}{\chaptermark}
% \begin{macro}{\sectionmark}
% \begin{macro}{\subsectionmark}
% Standard definitions for \cs{chaptermark}, \cs{sectionmark} and \cs{subsectionmark}.
%
%    \begin{macrocode}
  \f@nch@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}}}%
   }%
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\headrule}
%    \begin{macrocode}
  \def\headrule{{\if@fancyplain\let\headrulewidth\plainheadrulewidth\fi
      \hrule\@height\headrulewidth\@width\headwidth
      \vskip-\headrulewidth}}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\footrule}
% \changes{fancyhdr v3.10}{2019/01/28}{Move \cs{footruleskip} outside of the \cs{footrule}
% definition and remove useless \cs{vskip} at the top.}
%    \begin{macrocode}
  \def\footrule{{\if@fancyplain\let\footrulewidth\plainfootrulewidth\fi
      \hrule\@width\headwidth\@height\footrulewidth}}%
%    \end{macrocode}
% \end{macro}
%
% Default values for \cs{headrulewidth}, \cs{footrulewidth},
% \cs{headruleskip} and \cs{footruleskip}.
%    \begin{macrocode}
  \def\headrulewidth{0.4pt}%
  \def\footrulewidth{0pt}%
  \def\headruleskip{0pt}%
  \def\footruleskip{0.3\normalbaselineskip}%
%    \end{macrocode}
% Initialisation of the head and foot text.
%
% The default values still contain \cs{fancyplain} for compatibility:
% lefthead empty on ``plain'' pages, \cs{rightmark} on even, \cs{leftmark} on odd pages;
% evenhead empty on ``plain'' pages, \cs{leftmark} on even, \cs{rightmark} on odd pages.
%    \begin{macrocode}
  \fancyhf{}%
  \if@twoside
    \fancyhead[el,or]{\fancyplain{}{\slshape\rightmark}}%
    \fancyhead[er,ol]{\fancyplain{}{\slshape\leftmark}}%
  \else
    \fancyhead[l]{\fancyplain{}{\slshape\rightmark}}%
    \fancyhead[r]{\fancyplain{}{\slshape\leftmark}}%
  \fi
  \fancyfoot[c]{\rmfamily\thepage}% page number
}
%    \end{macrocode}
% Call the initialisation
%    \begin{macrocode}
\f@nch@initialise
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[internal]{\ps@f@nch@fancyproto}
% \cs{ps@f@nch@fancyproto} is the
% initial value for page style \texttt{fancy}. The real page style
% is \cs{ps@f@nch@fancycore} but \cs{ps@f@nch@fancyproto} for the first use
% of \Cmd{pagestyle\{fancy\}} or any of its derivatives. It
% initialises \cs{headwidth}, and then resets itself to
% \cs{ps@f@nch@fancycore}. For backwards compatibility it still
% contains \cs{@fancyplainfalse}.
% The reason we have \cs{ps@f@nch@fancyproto} is so that page style
% \texttt{fancy} can be redefined.
% \changes{fancyhdr v4.0}{2019/03/21}{Reorganise \cs{ps@fancy}}
%    \begin{macrocode}
\def\ps@f@nch@fancyproto{%
%    \end{macrocode}
% Initialise \cs{headwidth} if the user didn't. If \cs{headwidth} ${}< 0$,
% then the user did not initialise it, or they just added something to
% it in the expectation that
% it was initialised to \cs{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 \verb+\setlength{\headwidth}{1.2\textwidth}+.
% The documentation advises to change \cs{headwidth} after the first call to
% \verb+\pagestyle{fancy}+. This code is just to catch the most common cases were
% that is not the case.
%    \begin{macrocode}
  \ifdim\headwidth<0sp
    \global\advance\headwidth123456789sp\global\advance\headwidth\textwidth
  \fi
%    \end{macrocode}
% Now we reset \cs{ps@f@nch@fancyproto} to \cs{ps@f@nch@fancycore} with \cs{@fancyplainfalse}
% and call that version.
%    \begin{macrocode}
  \gdef\ps@f@nch@fancyproto{\@fancyplainfalse\ps@f@nch@fancycore}%
  \@fancyplainfalse\ps@f@nch@fancycore
}%
%    \end{macrocode}
% Let the system know this is a \pkg{fancyhdr} page style.
%    \begin{macrocode}
\@namedef{f@nch@ps@f@nch@fancyproto-is-fancyhdr}{}
%    \end{macrocode}
% \end{macro}
% \begin{macro}[internal]{\ps@fancy}
% Define \cs{ps@fancy} just to call \cs{ps@f@nch@fancyproto}.
%    \begin{macrocode}
\def\ps@fancy{\ps@f@nch@fancyproto}
\@namedef{f@nch@ps@fancy-is-fancyhdr}{}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[internal]{\ps@fancyplain}
% The page style \texttt{fancyplain} (deprecated).
% After initializing by calling \cs{ps@f@nch@fancyproto} it sets page style
% \texttt{plain} to our version \cs{ps@plain@fancy}, which just sets
% \cs{@fancyplaintrue} and then calls the page style fancy implementation.
%
%    \begin{macrocode}
\def\ps@fancyplain{\ps@f@nch@fancyproto \let\ps@plain\ps@plain@fancy}
\def\ps@plain@fancy{\@fancyplaintrue\ps@f@nch@fancycore}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[internal]{\f@nch@ps@empty}
% Save the definition of \cs{ps@empty} (page style \texttt{empty}).
% \changes{fancyhdr v4.0}{2019/03/21}{Rename \cs{ps@@empty} to \cs{f@nch@ps@empty}}
%
%    \begin{macrocode}
\let\f@nch@ps@empty\ps@empty
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[internal]{\ps@f@nch@fancycore}
% \changes{fancyhdr v4.0}{2019/03/21}{Rename \cs{ps@@fancy} to \cs{ps@f@nch@fancycore}}
% \changes{fancyhdr v4.1}{2022/09/06}{Implement \texttt{twoside} option.}
% \changes{fancyhdr v5.0}{2025/01/01}{Support for \cs{fancyheadwidth},
% \cs{fancyfootwidth} and \cs{fancyhfwidth}.}
% The actual implementation of page style \texttt{fancy}. For amsbook/amsart, which do
% strange things with \cs{topskip}, we start with \cs{f@nch@ps@empty}. We
% construct the even and odd headers and footers from all the parts that
% we have collected.
%    \begin{macrocode}
\def\ps@f@nch@fancycore{%
  \f@nch@ps@empty
  \def\@mkboth{\protect\markboth}%
  \def\f@nch@oddhead{\f@nch@head\f@nch@Oolh\f@nch@olh\f@nch@och\f@nch@orh\f@nch@Oorh
                     \f@nch@width@olh\f@nch@width@och\f@nch@width@orh}%
  \def\@oddhead{%
    \iff@nch@twoside
      \ifodd\c@page
        \f@nch@oddhead
      \else
        \@evenhead
      \fi
    \else
      \f@nch@oddhead
    \fi
  }
  \def\f@nch@oddfoot{\f@nch@foot\f@nch@Oolf\f@nch@olf\f@nch@ocf\f@nch@orf\f@nch@Oorf
                     \f@nch@width@olf\f@nch@width@ocf\f@nch@width@orf}%
  \def\@oddfoot{%
    \iff@nch@twoside
      \ifodd\c@page
        \f@nch@oddfoot
      \else
        \@evenfoot
      \fi
    \else
      \f@nch@oddfoot
    \fi
  }
  \def\@evenhead{\f@nch@head\f@nch@Oelh\f@nch@elh\f@nch@ech\f@nch@erh\f@nch@Oerh
                 \f@nch@width@elh\f@nch@width@ech\f@nch@width@erh}%
  \def\@evenfoot{\f@nch@foot\f@nch@Oelf\f@nch@elf\f@nch@ecf\f@nch@erf\f@nch@Oerf
                 \f@nch@width@elf\f@nch@width@ecf\f@nch@width@erf}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[internal]{\f@nch@Oolh}
% \begin{macro}[internal]{\f@nch@Oorh}
% \begin{macro}[internal]{\f@nch@Oelh}
% \begin{macro}[internal]{\f@nch@Oerh}
% \begin{macro}[internal]{\f@nch@Oolf}
% \begin{macro}[internal]{\f@nch@Oorf}
% \begin{macro}[internal]{\f@nch@Oelf}
% \begin{macro}[internal]{\f@nch@Oerf}
%   Default definitions for compatibility mode: These cause the
%   header/footer to take the defined \cs{headwidth} as its width and if
%   required to shift it in the direction of the marginpar area.
%
%    \begin{macrocode}
\def\f@nch@Oolh{\if@reversemargin\hss\else\relax\fi}
\def\f@nch@Oorh{\if@reversemargin\relax\else\hss\fi}
\let\f@nch@Oelh\f@nch@Oorh
\let\f@nch@Oerh\f@nch@Oolh
\let\f@nch@Oolf\f@nch@Oolh
\let\f@nch@Oorf\f@nch@Oorh
\let\f@nch@Oelf\f@nch@Oelh
\let\f@nch@Oerf\f@nch@Oerh
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}[internal]{\f@nch@offsolh}
% \begin{macro}[internal]{\f@nch@offselh}
% \changes{fancyhdr v5.0}{2021/03/27}{Change the offset length variables
% \cs{f@nch@O@xyz} to \cs{f@nch@offset@xyz}}
%   New definitions for the use of \cs{fancyhfoffset},
%   \cs{fancyheadoffset}, \cs{fancyfootoffset}.  These calculate the
%   \cs{headwidth} from \cs{textwidth} and the specified offsets.\\
%   First for the header.
%
%    \begin{macrocode}
\def\f@nch@offsolh{\headwidth=\textwidth\advance\headwidth\f@nch@offset@olh
                   \advance\headwidth\f@nch@offset@orh\hskip-\f@nch@offset@olh}
\def\f@nch@offselh{\headwidth=\textwidth\advance\headwidth\f@nch@offset@elh
                   \advance\headwidth\f@nch@offset@erh\hskip-\f@nch@offset@elh}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[internal]{\f@nch@offsolf}
% \begin{macro}[internal]{\f@nch@offself}
% The same for the footer.
%
%    \begin{macrocode}
\def\f@nch@offsolf{\headwidth=\textwidth\advance\headwidth\f@nch@offset@olf
                   \advance\headwidth\f@nch@offset@orf\hskip-\f@nch@offset@olf}
\def\f@nch@offself{\headwidth=\textwidth\advance\headwidth\f@nch@offset@elf
                   \advance\headwidth\f@nch@offset@erf\hskip-\f@nch@offset@elf}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[internal]{\f@nch@setoffs}
% \changes{fancyhdr v5.0}{2021/03/27}{Use \cs{def} instead of \cs{let}
% to make it easier to pick up these macros for \cs{fancypagestyle*}}
% Set the offset parts to be used in the construction of the headers and
% footers. Depending on \cs{f@nch@gbl} it will be done globally (for
% page style \texttt{fancy}) in \texttt{compatV3} mode)
% or locally (for \cs{fancypagestyle}).
% The macros \Cmd{f@nch@Oxyz} tell what should be done at the various
% ends of the headers/footers. They are done with \cs{def} rather than
% \cs{let} so they are easier to pick up for \cs{fancypagestyle*}.
%
% Just in case \verb+\let\headwidth\textwidth+ was used, we reset
% \cs{headwidth} to the length parameter that it should be.
%    \begin{macrocode}
\def\f@nch@setoffs{%
  \f@nch@gbl\let\headwidth\f@nch@headwidth
  \f@nch@gbl\def\f@nch@Oolh{\f@nch@offsolh}%
  \f@nch@gbl\def\f@nch@Oelh{\f@nch@offselh}%
  \f@nch@gbl\def\f@nch@Oorh{\hss}%
  \f@nch@gbl\def\f@nch@Oerh{\hss}%
  \f@nch@gbl\def\f@nch@Oolf{\f@nch@offsolf}%
  \f@nch@gbl\def\f@nch@Oelf{\f@nch@offself}%
  \f@nch@gbl\def\f@nch@Oorf{\hss}%
  \f@nch@gbl\def\f@nch@Oerf{\hss}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[internal]{\iff@nch@footnote}
% \begin{macro}[internal]{\@makecol}
% \changes{fancyhdr v5.0}{2021/03/27}{Change the internal variables
% \cs{topfloat} and \cs{botfloat} to localised ones \cs{f@nch@topfloat}
% and \cs{f@nch@botfloat}.}
%   Redefine \cs{@makecol} so that we can capture if there are
%   top/bottom floats, footnotes or if we are on a float page. Because
%   of a clash with the \PKGindex{footmisc}\pkg{footmisc} package we do this at
%   \verb+\begin{document}+.\\
%   We need a boolean \cs{iff@nch@footnote} to capture if there was a footnote.
%
%    \begin{macrocode}
\newif\iff@nch@footnote
\AtBeginDocument{%
  \let\latex@makecol\@makecol
  \def\@makecol{\ifvoid\footins\f@nch@footnotefalse\else\f@nch@footnotetrue\fi
    \let\f@nch@topfloat\@toplist\let\f@nch@botfloat\@botlist\latex@makecol}%
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\iftopfloat}
% \begin{macro}{\ifbotfloat}
% \begin{macro}{\iffloatpage}
% \begin{macro}{\iffootnote}
% These can be used in a header/footer field to make them conditional on
% the presence of floats and/or footnotes.
% \changes{fancyhdr v5.0}{2024/05/08}{Replace \cs{empty} with \cs{@empty}}
%    \begin{macrocode}
\newcommand\iftopfloat[2]{\ifx\f@nch@topfloat\@empty #2\else #1\fi}%
\newcommand\ifbotfloat[2]{\ifx\f@nch@botfloat\@empty #2\else #1\fi}%
\newcommand\iffloatpage[2]{\if@fcolmade #1\else #2\fi}%
\newcommand\iffootnote[2]{\iff@nch@footnote #1\else #2\fi}%
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \changes{fancyhdr v5.0}{2021/02/16}{Define \cs{newtoks}\cs{@temptokenb}}
% \begin{macro}[internal]{\@temptokenb}
% A token register to collect information for \cs{fancypagestyle*}. The
% definition is conditional on the non-existence of it.
%    \begin{macrocode}
\ifx\@temptokenb\undefined \csname newtoks\endcsname\@temptokenb\fi
%    \end{macrocode}
% \end{macro}
%
% \changes{fancyhdr v5.0}{2021/02/17}{Define \cs{iff@nch@pagestyle@star}
% to record if \cs{fancypagestyle*} is used.}
% \begin{macro}[internal]{\iff@nch@pagestyle@star}
% A conditional to record if \cs{fancypagestyle*} is used.
%    \begin{macrocode}
\newif\iff@nch@pagestyle@star
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\fancypagestyle}
% Define a new page style. With \texttt{*} define a ``closed'' page
% style, otherwise an ``open'' one.
% \changes{fancyhdr v5.0}{2021/02/17}{Add \cs{fancypagestyle*}}
%    \begin{macrocode}
\newcommand\fancypagestyle{%
  \@ifstar{\f@nch@pagestyle@startrue\f@nch@pagestyle}%
          {\f@nch@pagestyle@starfalse\f@nch@pagestyle}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}[internal]{\f@nch@pagestyle}
% Internal macro for \cs{fancypagestyle}. The optional second argument is the base page style.
% It defaults to \cs{ps@f@nch@fancyproto}.
% \changes{fancyhdr v4.0}{2019/03/21}{Added optional base style argument.}
%    \begin{macrocode}
\newcommand\f@nch@pagestyle[1]{%
  \@ifnextchar[{\f@nch@@pagestyle{#1}}{\f@nch@@pagestyle{#1}[f@nch@fancyproto]}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}[internal]{\f@nch@@pagestyle}
% The actual code for \cs{fancypagestyle}. Build the page style body.
% Declare it as a fancyhdr-based page style.
% \changes{fancyhdr v4.0}{2019/09/05}{Make the definition of \cs{f@nch@pagestyle} \cs{long}.}
% \changes{fancyhdr v5.0}{2021/02/17}{Add \cs{fancypagestyle*}}
%    \begin{macrocode}
\long\def\f@nch@@pagestyle#1[#2]#3{%
  \f@nch@ifundefined{ps@#2}{%
    \PackageError{fancyhdr}{\string\fancypagestyle: Unknown base page style `#2'}{}%
  }{%
    \f@nch@ifundefined{f@nch@ps@#2-is-fancyhdr}{%
      \PackageError{fancyhdr}{\string\fancypagestyle: Base page style `#2' is not fancyhdr-based}{}%
    }%
    {%
%    \end{macrocode}
% First put necessary definitions in \cs{@temptokenb}, if required
% (\cs{fancypagestyle*}) by calling \cs{f@nch@pagestyle@setup}.
% Then define the page style by expanding \cs{the}\cs{@temptokenb} and
% adding the base style and our definitions.
%    \begin{macrocode}
      \f@nch@pagestyle@setup
      \def\temp@b{\@namedef{ps@#1}}%
      \expandafter\temp@b\expandafter{\the\@temptokenb
          \let\f@nch@gbl\relax\@nameuse{ps@#2}#3\relax}%
      \@namedef{f@nch@ps@#1-is-fancyhdr}{}%
    }%
  }%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[internal]{\f@nch@pagestyle@setup}
% Internal macro for \cs{fancypagestyle}. Setup \cs{@temptokenb}:\\
% For \cs{fancypagestyle*} collect relevant macro definitions in \cs{@temptokenb}.\\
% For \cs{fancypagestyle} make \cs{@temptokenb} empty.
% \changes{fancyhdr v5.0}{2021/02/17}{Add \cs{f@nch@pagestyle@setup}
% macro for \cs{fancypagestyle*}}
% \changes{fancyhdr v5.0}{2021/03/27}{Also save the offset length variables
% \cs{f@nch@offset@xyz} with \cs{setlength}}
% \changes{fancyhdr v5.0}{2022/01/24}{Replace \cs{myname} with \cs{temp@a}}
%    \begin{macrocode}
\newcommand\f@nch@pagestyle@setup{%
  \iff@nch@pagestyle@star
%    \end{macrocode}
% For \cs{fancypagestyle*}, first save value of \cs{iff@nch@check}
% (the \texttt{[nocheck]} option).
%    \begin{macrocode}
    \iff@nch@check\@temptokenb={\f@nch@checktrue}\else\@temptokenb={\f@nch@checkfalse}\fi
%    \end{macrocode}
% Save values of all relevant macros (26 in total):\\
% headers and footers (12), header and footer widths (12),
% header and footer offsets (8), header and footer inits (2),
% \cs{headrule} and \cs{footrule} and \texttt{...width} (4)
%    \begin{macrocode}
    \@tfor\temp@a:=
      \f@nch@olh\f@nch@och\f@nch@orh\f@nch@elh\f@nch@ech\f@nch@erh
      \f@nch@olf\f@nch@ocf\f@nch@orf\f@nch@elf\f@nch@ecf\f@nch@erf
      \f@nch@width@elh\f@nch@width@ech\f@nch@width@erh\f@nch@width@olh
      \f@nch@width@och\f@nch@width@orh\f@nch@width@elf\f@nch@width@ecf
      \f@nch@width@erf\f@nch@width@olf\f@nch@width@ocf\f@nch@width@orf
      \f@nch@Oolh\f@nch@Oorh\f@nch@Oelh\f@nch@Oerh
      \f@nch@Oolf\f@nch@Oorf\f@nch@Oelf\f@nch@Oerf
      \f@nch@headinit\f@nch@footinit
      \headrule\headrulewidth\footrule\footrulewidth
    \do {%
%    \end{macrocode}
% First get the body of the macro.
% Next put it in a \cs{def}\meta{\cs{macro}}\texttt{\{}\meta{body of \cs{macro}}\texttt{\}}.
%    \begin{macrocode}
      \toks@=\expandafter\expandafter\expandafter{\temp@a}%
      \toks@=\expandafter\expandafter\expandafter{%
        \expandafter\expandafter\expandafter\def
        \expandafter\expandafter\temp@a\expandafter{\the\toks@}}%
%    \end{macrocode}
% Set up a macro to append \cs{toks@} to \cs{@temptokenb} and then execute it.
%    \begin{macrocode}
      \edef\temp@b{\@temptokenb={\the\@temptokenb\the\toks@}}%
      \temp@b
    }%
%    \end{macrocode}
% Now pick up the offset length variables in a similar way, but with
% \cs{setlength} rather than \cs{def}.
%    \begin{macrocode}
    \@tfor\temp@a:=
      \f@nch@offset@olh\f@nch@offset@orh\f@nch@offset@elh\f@nch@offset@erh
      \f@nch@offset@olf\f@nch@offset@orf\f@nch@offset@elf\f@nch@offset@erf
    \do {%
      \toks@=\expandafter\expandafter\expandafter{\expandafter\the\temp@a}%
      \toks@=\expandafter\expandafter\expandafter{%
        \expandafter\expandafter\expandafter\setlength
        \expandafter\expandafter\temp@a\expandafter{\the\toks@}}%
%    \end{macrocode}
% Set up a macro to append \cs{toks@} to \cs{@temptokenb} and then execute it.
%    \begin{macrocode}
      \edef\temp@b{\@temptokenb={\the\@temptokenb\the\toks@}}%
      \temp@b
    }%
  \else
%    \end{macrocode}
% For \cs{fancypagestyle} without \texttt{*}, set \cs{@temptokenb} empty.
%    \begin{macrocode}
    \@temptokenb={}%
  \fi
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\fancypagestyleassign}
% \changes{fancyhdr v5.0}{2024/12/25}{Macro \cs{fancypagestyleassign} added}
% \begin{syntax}
%   \cs{fancypagestyleassign}\marg{ps1}\marg{ps2}
% \end{syntax}
% Assigns page style \meta{ps2} to \meta{ps1}. This causes
% \meta{ps1} to be an exact copy of \meta{ps2}, but completely
% independent of \meta{ps2}. We do the equivalent of a \cs{let}
% command, like \cs{let}\cs{ps@\meta{ps1}}\cs{ps@\meta{ps2}}.
%    \begin{macrocode}
\newcommand\fancypagestyleassign[2]{%
  \f@nch@ifundefined{ps@#2}{%
    \PackageError{fancyhdr}{\string\fancypagestyleassign: Unknown page style `#2'}{}%
    }{%
     \expandafter\let
       \csname ps@#1\expandafter\endcsname
       \csname ps@#2\endcsname
%    \end{macrocode}
%    If \meta{ps2} is not fancyhdr-based, \meta{ps1} will also be
%    fancyhdr-based, otherwise it will not. If \meta{ps2} is not
%    fancyhdr-based, but \meta{ps1} is, give a warning that it is
%    overwritten, which could give unexpected problems.
%    \begin{macrocode}
     \f@nch@ifundefined{f@nch@ps@#2-is-fancyhdr}{%
       \f@nch@ifundefined{f@nch@ps@#1-is-fancyhdr}{}{%
         \PackageNote{fancyhdr}{%
           \string\fancypagestyleassign: You overwrite the\MessageBreak
           fancyhdr-based page style `#1' with the\MessageBreak
           non-fancyhdr-based page style `#2'.\MessageBreak
           This could cause unexpected problems if you\MessageBreak
           use `#1' as the base style in \string\fancypagestyle}}%
       \expandafter\let\csname f@nch@ps@#1-is-fancyhdr\endcsname\@undefined
     }{%
       \@namedef{f@nch@ps@#1-is-fancyhdr}{}%
     }%
   }%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[internal]{\ps@fancydefault}
% \changes{fancyhdr v4.0}{2019/03/21}{Added \cs{ps@fancydefault}}
% \changes{fancyhdr v5.0}{2021/04/11}{Defined \cs{ps@fancydefault} with
% \cs{fancypagestyle*}}
% This is page style \texttt{fancydefault}. It is in fact page style
% \texttt{fancy} with all the defaults embedded, including the relevant
% definitions of \cs{chaptermark} and \Cmd{[sub]sectionmark}. This is in
% contrast with page style \texttt{fancy} that gets all its settings
% from the environment. It is defined with \cs{fancypagestyle*}.
%    \begin{macrocode}
\fancypagestyle*{fancydefault}{\f@nch@initialise}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\fancyhdrbox}
% \changes{fancyhdr v5.0}{2024/12/16}{Macro \cs{fancyhdrbox} added}
% \cs{fancyhdrbox}\oarg{alignment}\oarg{width}\marg{lines separated by \cmd{\\}}\\[1ex]
% This command creates a \cs{halign} inside a vertical box (\cs{vbox} or \cs{vtop}).
%
% We need some variables, but these don't have to be declared. They are
% characterised by \texttt{@@} in their name.
% \begin{description}[leftmargin=4cm]
%     \item[\cs{f@nchdrbox@@v}] --     vertical alignment (\texttt{T},
%                                      \texttt{t}, \texttt{c}, \texttt{b}, \texttt{B})
%     \item[\cs{f@nchdrbox@@h}] --     horizontal alignment (\texttt{l}, \texttt{c}, \texttt{r})
%     \item[\cs{f@nchdrbox@@pre}] --   code to be inserted before the
%                                      first row, a `topstrut' or \Cmd{vspace\{0pt\}}
%     \item[\cs{f@nchdrbox@@postx}] -- code to be executed at the end of the
%                                      last row, possibly a `botstrut'
%     \item[\cs{f@nchdrbox@@posty}] -- code to be executed after the
%                                      \cs{halign}, possibly a \Cmd{vspace\{0pt\}}
%     \item[\cs{f@nchdrbox@@crstrut}] -- a `strut' to be inserted at
%                                        each \cmd{\\} in the \cs{halign}
%     \item[\cs{f@nchdrbox@@halignto}] -- This is either empty, if no
%                                        \meta{width} argument is given,
%                                        or `\texttt{to~\meta{width}}' if
%                                        it is given.
% \end{description}
% A `strut' is a \TeX{} construct to keep the baselines of the lines in
% a text on a fixed distance. It normally is an invisible rule of \texttt{width}
% \texttt{0pt}, \texttt{height} \texttt{0.7\cs{baselineskip}} and \texttt{depth}
% \texttt{0.3\cs{baselineskip}}. Therefore the struts are
% dependent of the font of the text. But for the \cs{fancyhdrbox} alignments
% \texttt{T} and \texttt{B} we need special `topstrut' (which only has
% the \texttt{height}) part, or a `botstrut' (which only has the
% \texttt{depth}) part. For example with the \texttt{T} alignment, there
% should be no strut on the first line, because then we don't want the extra
% space above this line. We use instead a \cmd{vspace\{0pt\}}.
% But we need the \texttt{depth} part
% because we want the extra space below the line. Similar for the
% \texttt{B} alignment, but then in the opposite direction.
% \begin{macro}[internal]{\f@nchdrbox@topstrut, \f@nchdrbox@botstrut}
%    \begin{macrocode}
\def\f@nchdrbox@topstrut{\vrule height\ht\strutbox width\z@}
\def\f@nchdrbox@botstrut{\vrule depth\dp\strutbox width\z@}
%    \end{macrocode}
% \end{macro}
% \begin{macro}[internal]{\f@nchdrbox@nostrut}
% At each \cmd{\\} \cs{f@nchdrbox@@crstrut} will be inserted. It will be a normal \cs{strut}, except
% in the first row when the alignment is \texttt{T}; then it will be a
% `botstrut', and moreover, we will insert a \Cmd{vspace\{0pt\}} at the
% top of the box. The macro \cs{f@nchdrbox@nostrut} will set this up.
% The assignment to \cs{f@nchdrbox@@crstrut} will be local to the
% \cs{halign} cell, so after the \cmd{\\} it will be reset to the default.
%    \begin{macrocode}
\def\f@nchdrbox@nostrut{\noalign{\vspace{0pt}}\let\f@nchdrbox@@crstrut\f@nchdrbox@botstrut}
%    \end{macrocode}
% \end{macro}
% ^^A\begin{macro}{\fancyhdrbox}
%   Now we start the only user command in the part: \cs{fancyhdrbox}.
%   The code is run in a group so that changes to variables are local.
%   This is necessary in case we use nested \cs{fancyhdrbox}es.
%
%   First we set the variables \cs{f@nchdrbox@@pre},
%   \cs{f@nchdrbox@topstrut}, \cs{f@nchdrbox@@posty}, and
%   \cs{f@nchdrbox@@crstrut} to their default values. Then we test if
%   the second optional argument (\meta{width}) was given, and if so,
%   record this in \cs{f@nchdrbox@@halignto}. We put the \meta{width}
%   value in a length variable with \cs{setlength} so that we can
%   support \pkg{calc}-style values.
%
%   And then we check if the first optional argument \meta{alignment} is
%   empty. In that case we use \texttt{cl} instead.
%    \begin{macrocode}
\NewDocumentCommand{\fancyhdrbox}{ O{cl} o m }{%
\begingroup
  \let\f@nchdrbox@@pre\f@nchdrbox@topstrut
  \let\f@nchdrbox@@postx\f@nchdrbox@botstrut
  \let\f@nchdrbox@@posty\relax
  \let\f@nchdrbox@@crstrut\strut
  \IfNoValueTF{#2}%
    {\let\f@nchdrbox@@halignto\@empty}%
    {\setlength\@tempdima{#2}%
      \def\f@nchdrbox@@halignto{to\@tempdima}}%
  \def\@tempa{#1}%
  \ifx\@tempa\@empty
    \f@nchdrbox@align cl\@nil{#3}%
  \else
    \f@nchdrbox@align #1\@nil{#3}%
  \fi
\endgroup
}
%    \end{macrocode}
% ^^A\end{macro}
%
% \begin{macro}[internal]{\f@nchdrbox@cr, \@f@nchdrbox@xcr,
%              \@f@nchdrbox@argc, \@f@nchdrbox@xargc, \@f@nchdrbox@yargc}
% This is the definition for \cmd{\\} inside \cs{fancyhdrbox},
% \cmd{\\*} does nothing special here, but we acccept it anyway.
% The code is mostly copied from the \env{tabular} code from the
% \LaTeX{} kernel, but simplified, and the names of the macros are
% changed so that we don't rely on internals in the \LaTeX{} kernel that
% may change. The trick with the \verb|\ifnum0=`|
% allows to get unbalanced braces in a macro.
%    \begin{macrocode}
\protected\def\f@nchdrbox@cr{%
  {\ifnum0=`}\fi\@ifstar\@f@nchdrbox@xcr\@f@nchdrbox@xcr}

\def\@f@nchdrbox@xcr{%
  \unskip\f@nchdrbox@@crstrut
  \@ifnextchar[\@f@nchdrbox@argc{\ifnum0=`{\fi}\cr}%
}

\def\@f@nchdrbox@argc[#1]{%
  \ifnum0=`{\fi}%
    \ifdim #1>\z@
      \unskip\@f@nchdrbox@xargc{#1}%
    \else
      \@f@nchdrbox@yargc{#1}%
    \fi}

\def\@f@nchdrbox@xargc#1{\@tempdima #1\advance\@tempdima \dp \strutbox
   \vrule \@height\z@ \@depth\@tempdima \@width\z@ \cr}

\def\@f@nchdrbox@yargc#1{\cr\noalign{\setlength\@tempdima{#1}\vskip\@tempdima}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}[internal]{\f@nchdrbox@T, \f@nchdrbox@t, \f@nchdrbox@c, \f@nchdrbox@b, \f@nchdrbox@B}
%   Processing for the vertical alignment options \texttt{T},
%   \texttt{t}, \texttt{c}, \texttt{b}, \texttt{B}.
%
%   \begin{description}
%   \item[\texttt{T}] set \cs{f@nchdrbox@@pre} to `nostrut' and execute
%                     the \texttt{t} code
%   \item[\texttt{t}] set vertical alignment to \texttt{t} and
%                     horizontal to \texttt{l}
%   \item[\texttt{c}] set both vertical and horizontal alignment to \texttt{c}
%   \item[\texttt{b}] set vertical alignment to \texttt{b} and
%                     horizontal to \texttt{l}
%   \item[\texttt{B}] set \cs{f@nchdrbox@@postx} to do nothing and
%                     \cs{f@nchdrbox@@posty} to \Cmd{vspace\{0pt\}} and
%                     execute the \texttt{b} code
%   \end{description}
%   The horizontal alignments are defaults, they may be changed by
%   processing the horizontal argument, if present.
%    \begin{macrocode}
\def\f@nchdrbox@T{\let\f@nchdrbox@@pre\f@nchdrbox@nostrut
                  \f@nchdrbox@t}
\def\f@nchdrbox@t{\def\f@nchdrbox@@v{t}\def\f@nchdrbox@@h{l}}
\def\f@nchdrbox@c{\def\f@nchdrbox@@v{c}\def\f@nchdrbox@@h{c}}
\def\f@nchdrbox@b{\def\f@nchdrbox@@v{b}\def\f@nchdrbox@@h{l}}
\def\f@nchdrbox@B{\let\f@nchdrbox@@postx\relax
                  \def\f@nchdrbox@@posty{\vspace{0pt}}%
                  \f@nchdrbox@b}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[internal]{\f@nchdrbox@align}
% \cs{f@nchdrbox@align}\marg{vert}\marg{hor}\texttt{\cs{@nil}}\marg{lines}\\[1ex]
% The internal processing for the \cs{halign} in a vertical box.\\
% ALGORITHM \cs{f@nchdrbox@align}:\\
% (v = vertical position; h = horizontal position)\\
% \textbf{IF} \texttt{\#1} in \texttt{\{T,t,c,b,B\}} \\
% \textbf{THEN} v := \texttt{\#1}; h := (\textbf{if}  \texttt{\#1} == \texttt{c} \textbf{then} \texttt{c} \textbf{else} \texttt{l} \textbf{fi}) (coded in \cs{f@nchdrbox@\meta{\#1}})\\
% \hspace*{1em}  (The h value is the default in case  \texttt{\#2} is empty)\\
% \hspace*{1em}  \textbf{if}  \texttt{\#2} is not empty \textbf{then} h :=  \texttt{\#2} \textbf{fi}\\
% \textbf{ELSE} (\texttt{\#1} not in \texttt{\{T,t,c,b,B\}} -- it must be
% a horizontal alignment)\\
% \hspace*{1em}  v := \texttt{c}\\
% \hspace*{1em}  h := \texttt{\#1}\\
% \textbf{FI}\\
% Set the \cs{halign} in a \cs{vtop} (for \texttt{T/t} alignment) or
% \cs{vbox} (for others). This box is put into \cs{box0} because we have
% to change it for the vertical alignment \texttt{c}. For the others it
% isn't necesary, but it just makes the code easier to do it anyway.
% We also insert the \cs{f@nchdrbox@@pre},
% \cs{f@nchdrbox@@postx} and \cs{f@nchdrbox@@posty} variables in the
% proper places. The rest of the code is roughly based on the
% \env{tabular} code in the \LaTeX{} kernel.
%    \begin{macrocode}
\long\def\f@nchdrbox@align#1#2\@nil#3{%
  \f@nch@ifin{#1}{TtcbB}{%
    \@nameuse{f@nchdrbox@#1}%
    \def\@tempa{#2}%
    \ifx\@tempa\@empty\else \def\f@nchdrbox@@h{#2}\fi
  }%
  {\def\f@nchdrbox@@v{c}\def\f@nchdrbox@@h{#1}}%
  \let\\\f@nchdrbox@cr
  \setbox0=\if \f@nchdrbox@@v t\vtop
  \else \vbox
  \fi
  {%
     \ialign \f@nchdrbox@@halignto
     \bgroup \relax
     {\if \f@nchdrbox@@h l\hskip 1sp\else \hfil \fi
       \ignorespaces ##\unskip
       \if\f@nchdrbox@@h r\else \hfil \fi
     }%
     \tabskip\z@skip \cr
     \f@nchdrbox@@pre
     #3\unskip \f@nchdrbox@@postx
     \crcr
     \egroup
     \f@nchdrbox@@posty
  }%
%    \end{macrocode}
% If the vertical alignment is \texttt{c}, we calculate the
% total height + depth of the resulting \cs{box0} and then set
% the depth and height of this box each to half of this value. This way
% the box will be vertically centered. We don't use \cs{vcenter} for
% this, because it centers the box on the \emph{math axis}, which
% doesn't make sense here, and sometimes gives a different vertical
% positioning (not exactly centered).
%    \begin{macrocode}
  \if\f@nchdrbox@@v c\@tempdima=\ht0\advance\@tempdima\dp0%
    \ht0=0.5\@tempdima\dp0=0.5\@tempdima\fi
%    \end{macrocode}
% Finally we put the box in horizontal mode in the running text.
%    \begin{macrocode}
  \leavevmode \box0
}
%    \end{macrocode}
% \end{macro}
%
% \end{macro}
%
% \smallskip
% \changes{fancyhdr v4.1}{202/09/16}{Support for class \pkg{newlfm}.}
% The (really outdated) document class \pkg{newlfm} uses some internal
% \pkg{fancyhdr} commands that have gotten new names. So here we check if
% that class is loaded and then we redefine the affected \pkg{newlfm} macros.
% We have to do some of the redefinitions in \cs{AtBeginDocument}, as
% \pkg{fancyhdr} is loaded before the affected macros are defined. Also
% the macro \cs{@zfancyhead} is only called once, with wrong (outdated)
% parameters, so instead of changing the call of the macro, we substitute the
% right parameters inline.
%    \begin{macrocode}
\@ifclassloaded{newlfm}
{
  \let\ps@@empty\f@nch@ps@empty
  \AtBeginDocument{%
    \renewcommand{\@zfancyhead}[5]{\relax\hbox to\headwidth{\f@nch@reset
      \@zfancyvbox\headheight{\hbox
        {\rlap{\parbox[b]{\headwidth}{\raggedright\f@nch@olh}}\hfill
          \parbox[b]{\headwidth}{\centering\f@nch@olh}\hfill
          \llap{\parbox[b]{\headwidth}{\raggedleft\f@nch@orh}}}%
        \zheadrule}}\relax}%
  }
}
{}
%    \end{macrocode}
%</fancyhdr>
%
% \section{\pkg{extramarks.sty}}
% \setlength\parindent{0pt}
%<*extramarks>
%
% This package gives you extra marks, that you can define, set and use
% in your page headers and footers. It is based on the new \LaTeX{}
% marks mechanism as introduced in the 2022/06/01 \LaTeX{} release. If
% your \LaTeX{} implementation is older it will fallback to
% \pkg{extramarks} version 4.
%
% \changes{extramarks v5.0}{2024/05/14}{New implementation with
% independent marks, and fallback option to earlier version 4.}
% Provide a rollback to earlier version
%    \begin{macrocode}
\providecommand\DeclareRelease[3]{}
\providecommand\DeclareCurrentRelease[2]{}
\DeclareRelease{v4}{2024/11/30}{extramarks-v4.sty}
\DeclareCurrentRelease{}{2025/01/01}
\@ifundefined{NewMarkClass}
             {\PackageWarningNoLine{extramarks}{%
**************************************************\MessageBreak
Your LaTeX installation is too old for extramarks version 5.\MessageBreak
We will fallback to extramarks version 4 now.\MessageBreak
Please note that some commands will not be available,\MessageBreak
and that the functionality may be slightly different.\MessageBreak
You are advised to update your LaTeX installation.\MessageBreak
**************************************************}
             \RequirePackage{extramarks-v4}\endinput}{}
%    \end{macrocode}
% \changes{extramarks v5.0}{2024/11/30}{Check if \pkg{extramarks}
% version 5 is not used with a too old version of \pkg{multicol}.}
% We also do a sanity check for the package \PKGindex{multicol}\pkg{multicol}. If it is
% too old it will not work correctly with the new \pkg{extramarks}. In that
% case \pkg{extramarks-v4} should be used instead. So in that case
% we give a warning and then load package \pkg{extramarks-v4}.
% First \cs{extramarks} must be made undefined, otherwise loading
% \pkg{extramarks-v4} will give an error.
%    \begin{macrocode}
\AtBeginDocument{%
  \@ifpackageloaded{multicol}%
    {\IfPackageAtLeastF{multicol}{2024-11-21}{%
      \PackageWarningNoLine{extramarks}{%
        You are using package `extramarks' with a version\MessageBreak
        of `multicol' that is too old. The new version\MessageBreak
        of `multicol' will be released on June 1, 2025.\MessageBreak
        We will fallback to extramarks version 4 now.}%
      \let\extramarks\undefined
      \RequirePackage{extramarks-v4}
      }%
    }%
}
%    \end{macrocode}
%
% \changes{extramarks v1.99e}{2000/10/11}{Added a few \% marks to get rid
% of unwanted spaces, and \cs{endinput}. \\
% Added LPPL license clause.}
% \changes{extramarks v2.0beta}{2002/03/12}{Adapted for the new
% implementation of marks in \LaTeX{} to solve bug latex/3203. \\
% Added symmetric commands \cs{firstrightmark}, \cs{lastleftmark}, \cs{firstleftxmark},
% \cs{firstrightxmark}, \cs{lastrightxmark}, \cs{lastleftxmark}, \cs{topleftxmark} and
% \cs{toprightxmark}.}
% \changes{extramarks v2.0x=fancyhdr v 2.0}{2004/03/03}{version 2.0 Release.}
% \changes{extramarks v2.1}{2016/08/27}{Added a \cs{ProvidesPackage} line.\\
% Updated contact information.}
% \changes{extramarks v3.9}{2016/10/12}{Unify version number with \pkg{fancyhdr.sty}.}
%
% \begin{macro}[internal]{\@mkboth}
% Initialization of \cs{@mkboth}, so that it will pick up changes to
% \cs{markboth}
% \changes{extramarks v4.0.3}{2022/05/18}{Initialize definition of
% \cs{@mkboth} to \cs{def}\Cmd{@mkboth\{\cs{protect}\cs{markboth}\}}
% if it wasn't equal to \cs{@gobbletwo} so that it will pick up
% changes to \cs{markboth}}
%    \begin{macrocode}
\ifx\@mkboth\@gobbletwo\else\def\@mkboth{\protect\markboth}\fi
%    \end{macrocode}
% \end{macro}
%
% \begin{variable}{extramarks-left, extramarks-right}
% \begin{syntax}
% \texttt{extramarks-left}\\
% \texttt{extramarks-right}
% \end{syntax}
%   We need two mark classes. We call them \texttt{extramarks-left} and \texttt{extramarks-right}.
%    \begin{macrocode}
\NewMarkClass{extramarks-left}
\NewMarkClass{extramarks-right}
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}{\extramarks}
% This command is used to define the extra marks.
%    \begin{macrocode}
\newcommand\extramarks[2]{%
  \InsertMark{extramarks-left}{#1}%
  \InsertMark{extramarks-right}{#2}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\extramarksleft,\extramarksright}
% These commands can be used to set the two marks independently. These
% are only available in \pkg{extramarks} version 4.5 or later.
%    \begin{macrocode}
\newcommand\extramarksleft[1]{%
  \InsertMark{extramarks-left}{#1}}
\newcommand\extramarksright[1]{%
  \InsertMark{extramarks-right}{#1}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\firstleftmark}
% \begin{macro}{\lastrightmark}
% \begin{macro}{\firstrightmark}
% \begin{macro}{\lastleftmark}
% The new marks to be used in the headers and/or footers (based on the standard marks info).
%    \begin{macrocode}
\newcommand\firstleftmark{\FirstMark{2e-left}}
\newcommand\lastrightmark{\LastMark{2e-right}}
%    \end{macrocode}
% We first define the following commands with \cs{newcommand} to detect
% possible name clashes; then we redefine them with \cs{let}.
%    \begin{macrocode}
\newcommand\firstrightmark{\rightmark}
\let\firstrightmark \rightmark
\newcommand\lastleftmark{\leftmark}
\let\lastleftmark \leftmark
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\firstleftxmark}
% \begin{macro}{\firstrightxmark}
% \begin{macro}{\topleftxmark}
% \begin{macro}{\toprightxmark}
% \begin{macro}{\lastleftxmark}
% \begin{macro}{\lastrightxmark}
% \begin{macro}{\firstxmark}
% \begin{macro}{\lastxmark}
% \begin{macro}{\topxmark}
% The new extra marks.
%    \begin{macrocode}
\newcommand\firstleftxmark{\FirstMark{extramarks-left}}
\newcommand\lastrightxmark{\LastMark{extramarks-right}}
\newcommand\firstrightxmark{\FirstMark{extramarks-right}}
\newcommand\topleftxmark{\TopMark{extramarks-left}}
\newcommand\toprightxmark{\TopMark{extramarks-right}}
\newcommand\lastleftxmark{\LastMark{extramarks-left}}
%    \end{macrocode}
% We first define the following commands with \cs{newcommand} to detect
% possible name clashes; then we redefine them with \cs{let}.
%    \begin{macrocode}
\newcommand\firstxmark{\firstleftxmark}
\let\firstxmark\firstleftxmark
\newcommand\lastxmark{\lastrightxmark}
\let\lastxmark\lastrightxmark
\newcommand\topxmark{\topleftxmark}
\let\topxmark\topleftxmark
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
%</extramarks>
%
% \section{\pkg{extramarks-v4.sty}}
%
%<*extramarks-v4>
%
% \changes{extramarks v1.99e}{2000/10/11}{Added a few \% marks to get rid
% of unwanted spaces, and \cs{endinput}. \\
% Added LPPL license clause.}
% \changes{extramarks v2.0beta}{2002/03/12}{Adapted for the new
% implementation of marks in \LaTeX{} to solve bug latex/3203. \\
% Added symmetric commands \cs{firstrightmark}, \cs{lastleftmark}, \cs{firstleftxmark},
% \cs{firstrightxmark}, \cs{lastrightxmark}, \cs{lastleftxmark}, \cs{topleftxmark} and
% \cs{toprightxmark}.}
% \changes{extramarks v2.0x=fancyhdr v 2.0}{2004/03/03}{version 2.0 Release.}
% \changes{extramarks v2.1}{2016/08/27}{Added a \cs{ProvidesPackage} line.\\
% Updated contact information.}
% \changes{extramarks v3.9}{2016/10/12}{Unify version number with \pkg{fancyhdr.sty}.}
%
% \changes{extramarks v3.9a}{2017/06/30}{Restore \cs{newtoks}\cs{@temptokenb}}
% \changes{extramarks v5.0}{2021/02/16}{Make \cs{newtoks}\cs{@temptokenb} conditional}
% \begin{variable}[internal]{\@temptokenb}
% A token register to store some marks information
%    \begin{macrocode}
\ifx\@temptokenb\undefined \csname newtoks\endcsname\@temptokenb\fi
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}[internal]{\unrestored@protected@xdef}
% Define this macro just in case it isn't defined (should be part of \LaTeX).
%    \begin{macrocode}
\providecommand\unrestored@protected@xdef{%
  \let\protect\@unexpandable@protect \xdef}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[internal]{\markboth}
% Our own definition of \cs{markboth}, mainly because \cs{@markboth}
% gets more parameters.
% \changes{extramarks v4.4}{2024/11/20}{Add setting the new style marks
% for \cs{leftmark} (\texttt{2e-left}) and \cs{rightmark}
% (\texttt{2e-right} and \texttt{2e-right-nonempty}). We do this only if
% the new marks are defined in the \LaTeX{} kernel.}
% First the definition for modern \LaTeX{} distributions.
%    \begin{macrocode}
\ifdefined\ExplSyntaxOn
\ExplSyntaxOn
\DeclareRobustCommand*\markboth[2]{%
  \begingroup
    \let\label\relax \let\index\relax \let\glossary\relax
    \expandafter\@markboth\@themark{#1}{#2}%
    \@temptokena \expandafter{\@themark}%
    \ifdefined\mark_insert:nn
    % 3 new lines to set the new marks
      \mark_insert:nn{2e-left}{#1}
      \mark_insert:nn{2e-right}{#2}
      \tl_if_empty:nF{#2}{ \mark_insert:nn{2e-right-nonempty}{#2} }
    \fi
    \mark{\the\@temptokena}%
  \endgroup
  \if@nobreak\ifvmode\nobreak\fi\fi}
\ExplSyntaxOff
%    \end{macrocode}
% If we are with a pre-\LaTeX3 kernel, we use the definition from an
% older version of \pkg{extramarks}.
%    \begin{macrocode}
\else
\def\markboth#1#2{%
  \begingroup
  \let\label\relax \let\index\relax \let\glossary\relax
  \expandafter\@markboth\@themark{#1}{#2}%
  \@temptokena \expandafter{\@themark}%
  \mark{\the\@temptokena}%
  \endgroup
  \if@nobreak\ifvmode\nobreak\fi\fi}
\fi
%    \end{macrocode}
% \end{macro}
% \begin{macro}[internal]{\@mkboth}
% Initialization of \cs{@mkboth}, so that it will pick up changes to
% \cs{markboth}
% \changes{extramarks v4.0.3}{2022/05/18}{Initialize definition of
% \cs{@mkboth} to \cs{def}\Cmd{@mkboth\{\cs{protect}\cs{markboth}\}}
% if it wasn't equal to \cs{@gobbletwo} so that it will pick up
% changes to \cs{markboth}}
%    \begin{macrocode}
\ifx\@mkboth\@gobbletwo\else\def\@mkboth{\protect\markboth}\fi
%    \end{macrocode}
% \end{macro}
% \begin{macro}[internal]{\markright}
%   We use the standard definition of \cs{markright}. No use to duplicate here.
% \end{macro}
%
% \begin{macro}[internal]{\@markboth}
%   Note: put \texttt{\#3\#4} in toks register.
%    \begin{macrocode}
\def\@markboth#1#2#3#4#5#6{\@temptokena{{#3}{#4}}%
  \unrestored@protected@xdef\@themark{{#5}{#6}\the\@temptokena}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[internal]{\@markright}
%   Note: put \texttt{\#1} and \texttt{\#3\#4} in toks registers.
%   Maybe I can get rid of the extra \cs{@temptokenb} by doing the expansion
%   of \texttt{\#5} to a temp separately. But then, nowadays registers
%   are plenty.
%    \begin{macrocode}
\def\@markright#1#2#3#4#5{\@temptokena{#1}\@temptokenb{{#3}{#4}}%
  \unrestored@protected@xdef\@themark{{\the\@temptokena}{#5}\the\@temptokenb}}
%    \end{macrocode}
% \end{macro}

% \begin{macro}[internal]{\@leftmark}
% \begin{macro}[internal]{\@rightmark}
% Internal macros to get the standard marks.
%    \begin{macrocode}
\def\@leftmark#1#2#3#4{#1}
\def\@rightmark#1#2#3#4{#2}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \changes{extramarks-v4 v4.5}{2024/11/28}{Don't redefine \cs{leftmark}
% and \cs{rightmark} in \LaTeX{} kernel 2025-06-01 and later.}
%
% \begin{macro}[internal]{\leftmark}
% \begin{macro}[internal]{\rightmark}
% \begin{macro}{\firstleftmark}
% \begin{macro}{\lastrightmark}
% \begin{macro}{\firstrightmark}
% \begin{macro}{\lastleftmark}
% The standard marks + the new ones (based on the standard marks info).
% We provide \cs{IfFormatAtLeastTF} in case we have a rather old \LaTeX{}
% format (in which case the test will always be false).
% If the \LaTeX{} format is 2025-06-01 or later, \cs{leftmark} and
% \cs{rightmark} have definitions based upon the new marks, so we should
% not redefine these even in the \pkg{extramarks-v4} mode.
%    \begin{macrocode}
\providecommand\IfFormatAtLeastTF{\@ifl@t@r\fmtversion}
\IfFormatAtLeastTF{2025-06-01}{}{%
  \def\leftmark{\expandafter\@leftmark
      \botmark\@empty\@empty\@empty\@empty}
    \def\rightmark{\expandafter\@rightmark
      \firstmark\@empty\@empty\@empty\@empty}
}
\def\firstleftmark{\expandafter\@leftmark
      \firstmark\@empty\@empty\@empty\@empty}
\def\lastrightmark{\expandafter\@rightmark
      \botmark\@empty\@empty\@empty\@empty}
\let\firstrightmark \rightmark
\let\lastleftmark \leftmark
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}

% \begin{macro}[internal]{\@themark}
% This is where the marks information is stored.
%    \begin{macrocode}
\def\@themark{{}{}{}{}}
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\extramarks}
% This command is used to define the extra marks.
%    \begin{macrocode}
\newcommand\extramarks[2]{%
  \begingroup
  \let\label\relax \let\index\relax \let\glossary\relax
  \expandafter\@markextra\@themark{#1}{#2}%
  \@temptokena \expandafter{\@themark}%
  \mark{\the\@temptokena}%
  \endgroup
  \if@nobreak\ifvmode\nobreak\fi\fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[internal]{\@markextra}
% Internal macro to store the extra marks in the marks storage.\\
%   Note: Put \texttt{\#1\#2} in toks register.
%    \begin{macrocode}
\def\@markextra#1#2#3#4#5#6{\@temptokena {{#1}{#2}}%
  \unrestored@protected@xdef\@themark{\the\@temptokena{#5}{#6}}}
%    \end{macrocode}
% \end{macro}
% \changes{extramarks-v4 v4.5}{2024/11/30}{Add commands
% \cs{extramarksleft} and \cs{extramarksright} for compatibility with
% \pkg{extramarks} version 5.}
% \begin{macro}{\extramarksleft}
% This command is used to define the left extra mark.
% As this is not independent from the other marks, it is not perfect.
%    \begin{macrocode}
\def\extramarksleft#1{%
  \begingroup
  \let\label\relax \let\index\relax \let\glossary\relax
  \expandafter\@markextraleft\@themark{#1}%
  \@temptokena \expandafter{\@themark}%
  \mark{\the\@temptokena}%
  \endgroup
  \if@nobreak\ifvmode\nobreak\fi\fi}
%    \end{macrocode}
% \end{macro}
% \begin{macro}[internal]{\@extramarksleft}
% Internal macro to store the left mark in the marks storage.\\
%   Note: Put \texttt{\#1\#2} and \texttt{\#4}in toks registers.
%    \begin{macrocode}
\def\@markextraleft#1#2#3#4#5{\@temptokena {{#1}{#2}}%
  \@temptokenb {{#4}}%
  \unrestored@protected@xdef\@themark{\the\@temptokena{#5}\the\@temptokenb}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\extramarksright}
% This command is used to define the right extra mark.
% As this is not independent from the other marks, it is not perfect.
%    \begin{macrocode}
\def\extramarksright#1{%
  \begingroup
  \let\label\relax \let\index\relax \let\glossary\relax
  \expandafter\@markextraright\@themark{#1}%
  \@temptokena \expandafter{\@themark}%
  \mark{\the\@temptokena}%
  \endgroup
  \if@nobreak\ifvmode\nobreak\fi\fi}
%    \end{macrocode}
% \end{macro}
% \begin{macro}[internal]{\@extramarksright}
% Internal macro to store the right mark in the marks storage.\\
%   Note: Put \texttt{\#1\#2\#3} in toks register.
%    \begin{macrocode}
\def\@markextraright#1#2#3#4#5{\@temptokena {{#1}{#2}{#3}}%
  \unrestored@protected@xdef\@themark{\the\@temptokena{#5}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\firstleftxmark}
% \begin{macro}{\firstrightxmark}
% \begin{macro}{\topleftxmark}
% \begin{macro}{\toprightxmark}
% \begin{macro}{\lastleftxmark}
% \begin{macro}{\lastrightxmark}
% \begin{macro}{\firstxmark}
% \begin{macro}{\lastxmark}
% \begin{macro}{\topxmark}
% The new extra marks.
%    \begin{macrocode}
\def\firstleftxmark{\expandafter\@leftxmark
      \firstmark\@empty\@empty\@empty\@empty}
\def\firstrightxmark{\expandafter\@rightxmark
      \firstmark\@empty\@empty\@empty\@empty}
\def\topleftxmark{\expandafter\@leftxmark
      \topmark\@empty\@empty\@empty\@empty}
\def\toprightxmark{\expandafter\@rightxmark
      \topmark\@empty\@empty\@empty\@empty}
\def\lastleftxmark{\expandafter\@leftxmark
      \botmark\@empty\@empty\@empty\@empty}
\def\lastrightxmark{\expandafter\@rightxmark
      \botmark\@empty\@empty\@empty\@empty}
\let\firstxmark\firstleftxmark
\let\lastxmark\lastrightxmark
\let\topxmark\topleftxmark
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}[internal]{\@tleftxmark}
% \begin{macro}[internal]{\@rightxmark}
% Internal macros to extract the extra marks out of the marks storage.
%    \begin{macrocode}
\def\@leftxmark#1#2#3#4{#3}
\def\@rightxmark#1#2#3#4{#4}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%</extramarks-v4>
%
% \section{\pkg{fancyheadings.sty}}
%
% Fancyheadings.sty was the original style file (as they were called
% then) to implement fancy headers and footers in \LaTeX. This was in the time
% when MSDOS was stil quite a dominant ``Operating System''. It had a
% nasty property (amongst others): filenames consisted of at most 8
% characters + a 3 character extension. This meant that the name
% '\pkg{fancyheadings.sty}' was internally truncated in MSDOS to
% '\pkg{fancyhea.sty}', although it was perfectly OK to say
% '\pkg{fancyheadings}' in \LaTeX. However, some people started to write also
% '\pkg{fancyhea}' in \LaTeX{} documents, which made them unportable to for
% example Unix systems, unless there a copy or link was made to
% '\pkg{fancyhea.sty}'. I found this so annoying that I decided to rename the
% package to '\pkg{fancyhdr.sty}'. This package has evolved to a version that
% is incompatible with the original '\pkg{fancyheadings}'. \pkg{Fancyheadings}
% should no longer be used, therefore this package is provided that
% issues a clear warning and then switches to \pkg{fancyhdr}.
%
%
%<*fancyheadings>
%    \begin{macrocode}
\PackageWarningNoLine{fancyheadings}{%
  Please stop using fancyheadings!\MessageBreak
  Use fancyhdr instead.\MessageBreak
  We will call fancyhdr with the very same\MessageBreak
  options you passed to fancyheadings.\MessageBreak
  \MessageBreak
  fancyhdr is 99 percent compatible with\MessageBreak
  fancyheadings. The only incompatibility is\MessageBreak
  that \protect\headrulewidth\space and \protect\footrulewidth\space
     and\MessageBreak
  their \protect\plain... versions are no longer length\MessageBreak
  parameters, but normal macros (to be changed\MessageBreak
  with \protect\renewcommand\space rather than \protect\setlength).}
\RequirePackage{fancyhdr}
%    \end{macrocode}
%</fancyheadings>
% \end{implementation}
%
% \Finale
\endinput
