% \iffalse meta-comment
%
% mempatch.dtx
% Author: Peter Wilson (Herries Press) herries dot press at earthlink dot net
% Copyright 2001 --- 2007 Peter R. Wilson
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either
% version 1.3 of this license or (at your option) any 
% later version.
% The latest version of the license is in
%    http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of
% LaTeX version 2003/06/01 or later.
%
% This work has the LPPL maintenance status "author-maintained".
%
% This work consists of the files listed in the README file.
%
% 
% \fi
% \CheckSum{6253}
%
% \def\dtxfile{\texttt{mempatch.dtx}}
% \def\fileversion{v1.0} \def\filedate{2003/10/04}
% \def\fileversion{v1.1} \def\filedate{2003/10/07}
% \def\fileversion{v1.2} \def\filedate{2003/11/16}
% \def\fileversion{v2.0a} \def\filedate{2004/02/19}
% \def\fileversion{v2.1} \def\filedate{2004/03/01}
% \def\fileversion{v2.3} \def\filedate{2004/03/28}
% \def\fileversion{v2.4} \def\filedate{2004/04/26}
% \def\fileversion{v2.5} \def\filedate{2004/04/27}
% \def\fileversion{v2.6} \def\filedate{2004/04/30}
% \def\fileversion{v2.7} \def\filedate{2004/05/10}
% \def\fileversion{v2.8} \def\filedate{2004/05/13}
% \def\fileversion{v3.0} \def\filedate{2004/12/14}
% \def\fileversion{v3.1} \def\filedate{2004/12/19}
% \def\fileversion{v3.2} \def\filedate{2005/01/01}
% \def\fileversion{v3.3} \def\filedate{2005/01/17}
% \def\fileversion{v3.4} \def\filedate{2005/01/18}
% \def\fileversion{v3.5} \def\filedate{2005/02/01}
% \def\fileversion{v3.6} \def\filedate{2005/02/07}
% \def\fileversion{v3.7} \def\filedate{2005/02/26}
% \def\fileversion{v3.8} \def\filedate{2005/03/06}
% \def\fileversion{v3.9} \def\filedate{2005/03/23}
% \def\fileversion{v3.10} \def\filedate{2005/05/16}
% \def\fileversion{v3.11} \def\filedate{2005/05/23}
% \def\fileversion{v3.12} \def\filedate{2005/06/06}
% \def\fileversion{v4.1} \def\filedate{2005/10/06}
% \def\fileversion{v4.1h} \def\filedate{2005/11/15}
% \def\fileversion{v4.2} \def\filedate{2005/11/21}
% \def\fileversion{v4.3} \def\filedate{2005/12/13}
% \def\fileversion{v4.4} \def\filedate{2006/01/21}
% \def\fileversion{v4.4a} \def\filedate{2006/01/23}
% \def\fileversion{v4.5} \def\filedate{2006/08/08}
% \def\fileversion{v4.6} \def\filedate{2006/12/11}
% \def\fileversion{v4.7} \def\filedate{2006/12/23}
% \def\fileversion{v4.8} \def\filedate{2007/01/22}
%
% \title{The LaTeX \Lpack{memoir} class for configurable book 
%        typesetting: Code patches\thanks{This
%        file (\dtxfile) has version number \fileversion, last revised
%        \filedate.}}
%
% \author{%
% Peter Wilson\thanks{\texttt{herries dot press at earthlink dot net}}\\
% The Herries Press
% }
% \date{\filedate}
% \maketitle
%
%  ^^A \MakeShortVerb{\=}
%
% \begin{abstract}
% The \Lpack{memoir} class is designed for typesetting
% general books such as novels, biographies, histories, 
% and so on, although as it supports all the functionality of the 
% standard \Lpack{book} class it can also be used for technical writing.
% It provides more functions than the standard class as well as 
% presenting a more friendly interface for the book designer.
% It can also simulate the typesetting style of the standard \Lpack{article}
% class.
%
%    Because of its newness not all packages work well with \Lpack{memoir}.
% Also, as more users beat on it, subtle flaws are discovered in \Lpack{memoir}
% itself. Patching code is supplied to resolve known problems.
%
%    Code is also provided for new functionality that will later be 
% folded into the class code.
% \end{abstract}
%
% \tableofcontents
% \listoftables
%
% \StopEventually{}
%
% 
%
% \section{Introduction}
%
% This document provides the commented source for packages that patch
% the LaTeX \Lpack{memoir} class~\cite{MEMOIR}. 
% This class is designed for typesetting
% general books such as novels, biographies, histories, 
% and so on. It has all the functionality of the standard \Lpack{book}
% class and, as well as providing some extra functions, also provides
% a more friendly interface for the document designer. As it can encompass
% everything that the \Lpack{book} class provides it may also be used
% for technical writing.
%
%
%    Section~\ref{sec:docstrip} describes some
% administrative elements and code for general use later in the 
% specification.
% The macros forming the package files are defined in section~\ref{sec:hyper}
% and in section~\ref{sec:class} onwards.
% 
% This manual is typeset according to the conventions of the
% \LaTeX{} \textsc{docstrip} utility which enables the automatic
% extraction of the \LaTeX{} macro source files~\cite{GOOSSENS94}.
%
%
% \section{A driver for this document} \label{sec:docstrip}
%
% The next series of code contains the documentation driver file for
% \LaTeX, i.e., the file that will produce the documentation you are
% currently reading. This will be extracted from this file by the 
% \textsc{docstrip} program.
%
%    \begin{macrocode}
%<*driver>
\documentclass[twoside]{ltxdoc}
\usepackage{url}
%    \end{macrocode}
%
%    We do want an index, using linenumbers, but not update information.
%    \begin{macrocode}
\makeatletter
  \@mparswitchfalse
\makeatother
\EnableCrossrefs
\CodelineIndex
%% \RecordChanges
%    \end{macrocode}
% Don't use the default Short Verb.
%    \begin{macrocode}
\AtBeginDocument{\DeleteShortVerb{\|}}
%    \end{macrocode}
% We had better have page headings to aid navigation, but I don't
% like Uppercased titles.
%    \begin{macrocode}
\renewcommand{\MakeUppercase}[1]{#1}
\pagestyle{headings}
%    \end{macrocode}
%    We may use many \file{docstrip} modules so we set the
%    \texttt{StandardModuleDepth} counter to 1.
%    \begin{macrocode}
\setcounter{StandardModuleDepth}{1}
%    \end{macrocode}
%    Some commonly used abbreviations
%    \begin{macrocode}
\newcommand*{\Lopt}[1]{\textsf {#1}}            % typeset an option
\newcommand*{\file}[1]{\texttt {#1}}            % typeset a file
\newcommand*{\Lcount}[1]{\textsl {\small#1}}    % typeset a counter
\newcommand*{\pstyle}[1]{\textsl {#1}}          % typeset a pagestyle
\newcommand*{\Lenv}[1]{\texttt {#1}}            % typeset an environment
\newcommand*{\Lpack}[1]{\textsf {#1}}           % typeset a package
\newcommand*{\ctt}{\textsc{ctt}}
\newcommand{\Lclass}[1]{\textsf{#1}}
\newcommand{\ledmac}{ledmac}
\newcommand{\Ledmac}{\Lpack{\ledmac}}
\newcommand{\edmac}{\texttt{EDMAC}}
\newcommand{\tabmac}{\texttt{TABMAC}}
\newcommand{\PWcomment}[1]{}
\newenvironment{PW}{\itshape}{}
\renewenvironment{PW}{\sffamily}{}
\newcommand{\texbook}{\textit{TeXbook}}
\newcommand{\thetexbook}{\textit{The TeXbook}}
\newcommand{\egstart}{}
\newcommand{\egmid}{}
\newcommand{\egend}{}
\renewcommand{\egstart}{%
   \par
   \begingroup
   \centering
   \begin{minipage}{0.45\textwidth}}
\renewcommand{\egmid}{%
   \end{minipage}\hfill\begin{minipage}{0.45\textwidth}}
\renewcommand{\egend}{%
   \end{minipage}\par\endgroup}
%    \end{macrocode}
%    We want the full details printed.
%    \begin{macrocode}
\begin{document}
\DocInput{mempatch.dtx}
\PrintIndex
%% \PrintChanges
\end{document}
%</driver>
%    \end{macrocode}
%
% 
% \DoNotIndex{\',\.,\@M,\@@input,\@addtoreset,\@arabic,\@badmath}
% \DoNotIndex{\@centercr,\@cite}\
% \DoNotIndex{\@dotsep,\@empty,\@gobble,\@gobbletwo,\@ignoretrue}
% \DoNotIndex{\@input,\@ixpt,\@m}
% \DoNotIndex{\@minus,\@ne,\@nil,\@nomath,\@plus,\@set@topoint}
% \DoNotIndex{\@tempboxa,\@tempcnta,\@tempdima,\@tempdimb}
% \DoNotIndex{\@tempswafalse,\@tempswatrue,\@viipt,\@viiipt,\@vipt}
% \DoNotIndex{\@vpt,\@warning,\@xiipt,\@xipt,\@xivpt,\@xpt,\@xviipt}
% \DoNotIndex{\@xxpt,\@xxvpt,\\,\ ,\addpenalty,\addtolength,\addvspace}
% \DoNotIndex{\advance,\Alph,\alph}
% \DoNotIndex{\arabic,\ast,\begin,\begingroup,\bfseries,\bgroup,\box}
% \DoNotIndex{\bullet}
% \DoNotIndex{\cdot,\cite,\CodelineIndex,\cr,\day,\DeclareOption}
% \DoNotIndex{\def,\DisableCrossrefs,\divide,\DocInput,\documentclass}
% \DoNotIndex{\DoNotIndex,\egroup,\ifdim,\else,\fi,\em,\endtrivlist}
% \DoNotIndex{\EnableCrossrefs,\end,\endgroup}
% \DoNotIndex{\endlist,\everycr,\everypar,\ExecuteOptions,\expandafter}
% \DoNotIndex{\fbox}
% \DoNotIndex{\filedate,\filename,\fileversion,\fontsize,\framebox,\gdef}
% \DoNotIndex{\global,\halign,\hbox,\hfil,\hfill,\hrule}
% \DoNotIndex{\hsize,\hskip,\hspace,\hss,\if@tempswa,\ifcase,\or,\fi,\fi}
% \DoNotIndex{\ifhmode,\ifvmode,\ifnum,\iftrue,\ifx,\fi,\fi,\fi,\fi,\fi}
% \DoNotIndex{\input}
% \DoNotIndex{\jobname,\kern,\leavevmode,\let}
% \DoNotIndex{\list,\llap,\long,\m@ne,\m@th}
% \DoNotIndex{\month,\newcommand,\newcounter,\newenvironment}
% \DoNotIndex{\NeedsTeXFormat,\newdimen}
% \DoNotIndex{\newlength,\newpage,\nobreak,\noindent,\null,\number}
% \DoNotIndex{\numberline,\OldMakeindex,\OnlyDescription,\p@}
% \DoNotIndex{\par,\paragraph,\paragraphmark,\parfillskip}
% \DoNotIndex{\penalty,\PrintChanges,\PrintIndex,\ProcessOptions}
% \DoNotIndex{\protect,\ProvidesClass}
% \DoNotIndex{\refstepcounter,\relax,\renewcommand,\reset@font}
% \DoNotIndex{\rightskip,\rlap,\rmfamily,\roman}
% \DoNotIndex{\roman,\secdef,\selectfont,\setbox,\setcounter,\setlength}
% \DoNotIndex{\settowidth,\sfcode,\skip,\sloppy,\slshape,\space}
% \DoNotIndex{\symbol,\the,\trivlist,\typeout,\tw@,\undefined,\uppercase}
% \DoNotIndex{\usecounter,\usefont,\usepackage,\vfil,\vfill,\viiipt}
% \DoNotIndex{\viipt,\vipt,\vskip,\vspace}
% \DoNotIndex{\wd,\xiipt,\year,\z@}
% \DoNotIndex{\@namedef,\@nameuse,\csname,\endcsname}
%
%
%
% \section{Patches for hyperref and friends} \label{sec:hyper}
%
% The \Lpack{hyperref} package~\cite{HYPERREF} is a marvel of macro coding but it does
% seem to have a habit of not working too well with classes or packages
% that contain anything out of the
% ordinary.
%
%    \begin{macrocode}
%<*hyper>
%    \end{macrocode}
%
%
% The \Lpack{memhfixc} package provides \Lpack{hyperref} related temporary 
% fixes and extensions for version v1.618 of the \Lpack{memoir} class.
% \begin{itemize}
% \item  hyperref fix for part and chapter entries in the ToC (v1.0)
%  \item Other fixes as and when
% \end{itemize}
% Only use this package in conjunction with the hyperref package. 
% Call the package without options after the hyperref package as:
% \begin{verbatim}
% \documentclass[...]{memoir}
% ...
% \usepackage[...]{hyperref}
% \usepackage{memhfixc}
% \end{verbatim}
%
%    \begin{macrocode}
%%
%% The memhfixc package provides hyperref related temporary 
%% fixes and extensions for version v1.3a of the memoir class.
%%     o hyperref fix for part and chapter entries in the ToC (v1.0)
%%     o Other fixes as and when
%% 
%% Only use this package in conjunction with the hyperref package. 
%% Call the package without options after the hyperref package as:
%% \documentclass[...]{memoir}
%% ...
%% \usepackage[...]{hyperref}
%% \usepackage{memhfixc}
%%
%% With thanks to Heiko Oberdiek, if you use hyperref dated 2006/11/15
%% or later, memhfixc will be automatically loaded after hyperref.
%%
%% Version 1.9  2006/11/22
%% Version 1.8  2006/01/21
%% Version 1.7  2005/11/15
%% Version 1.6  2004/05/13
%% Version 1.5  2003/10/14
%% Version 1.4  2003/09/26
%% Version 1.3  2003/02/20
%% Version 1.2  2003/02/02
%% Version 1.1  2003/01/22
%% Version 1.0  2002/10/22
%%
%    \end{macrocode}
% 
%
%    \begin{macrocode}
\ProvidesPackage{memhfixc}[2006/11/22 v1.9 nameref/hyperref package fixes for memoir class]
%    \end{macrocode}
%
% \begin{macro}{\M@hfixcfinish}
% The \Lpack{memhfixc} package is only useful with the memoir class
%    \begin{macrocode}
\let\M@hfixcfinish\relax
\@ifclassloaded{memoir}{}%
                       {\let\M@hfixcfinish\endinput
                        \typeout{No memoir class: memhfixc does nothing}}
\M@hfixcfinish

%    \end{macrocode}
% \end{macro}
%
% \subsection{Patches for nameref}
%
% \begin{macro}{\M@sect}
% \begin{macro}{\NR@sectm@m}
% The \Lpack{nameref} package~\cite{NAMEREF} fiddles with \cs{@sect} (which memoir replaces
%  by \cs{M@sect}). Note that the \Lpack{hyperref} package may call 
% \Lpack{nameref} via:
%  \verb?\AtBeginDocument{\usepackage{nameref}}?
%    \begin{macrocode}
\AtBeginDocument{%
  \@ifpackageloaded{nameref}{% nameref loaded
    \@ifundefined{NR@sectm@m}{% haven't fiddled with \M@sect
      \typeout{Redoing nameref's sectioning}
      \let\NR@sectm@m\M@sect
      \def\M@sect#1#2#3#4#5#6[#7][#8]#9{%
        \setcounter{section@level}{#2}%
        \def\@currentlabelname{#7}%
        \NR@sectm@m{#1}{#2}{#3}{#4}{#5}{#6}[{#7}][{#8}]{\Sectionformat{#9}{#2}}}%
    }%
    {% \NR@sectm@m has already been defined
    }
  }%
  {% no nameref
  }
} % end of \AtBeginDocument

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\label}
% \begin{macro}{\MNR@label}
% The \Lpack{nameref} package replaces \cs{label}, which (which memoir has added to).
%  Make the memoir addition again.
%
%    \begin{macrocode}
\AtBeginDocument{%
  \@ifpackageloaded{nameref}{% nameref loaded
    \@ifundefined{MNR@label}{% haven't fiddled with \label
      \typeout{Redoing nameref's label}
      \let\MNR@label\label
      \def\label#1{\@bsphack\begingroup
        \protected@edef\@currentlabel{\protect\M@TitleReference
          {\@currentlabel}{\M@currentTitle}}%
        \MNR@label{#1}%
        \endgroup \@esphack}%
      \let\MNR@old@caption\@caption
      \long\def\@caption#1[#2]#3{%
        \MNR@old@caption{#1}[{#2}]{#3}%
        \def\@currentlabelname{#2}%
        \M@gettitle{#2}%
        }%
    }%
    {% \MNR@label has already been defined
    }
  }%
  {% no nameref
  }
} % end of \AtBeginDocument

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% We are done unless the \Lpack{hyperref} package has been loaded
%    \begin{macrocode}
%% We are done unless the hyperref package has been loaded
\@ifpackageloaded{hyperref}{}{\let\M@hfixcfinish\endinput}
\M@hfixcfinish

%    \end{macrocode}
%
% \subsection{Patches for hyperref}
%
% \begin{macro}{\H@old@sbook}
% \begin{macro}{\@sbook}
%  Possibly this will provide some hyperref bookmarks support for
% the \cs{book*} document division (see page
% 206 in the \textit{Hypertext marks in LaTeX} manual that I processed
% on 2002/10/23 (cunningly it was internally dateless). It is based on
% hyperref's code for \cs{@spart}, which uses \cs{H@old@spart} to hold
% its original definition.
%    \begin{macrocode}
\let\H@old@sbook\@sbook
\def\@sbook#1{%
  \H@old@sbook{#1}%
  \Hy@GlobalStepCount\Hy@linkcounter
  \xdef\@currentHref{book*.\the\Hy@linkcounter}%
  \Hy@raisedlink{\hyper@anchorstart{\@currentHref}\hyper@anchorend}%
}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\Hy@captionsenglish}
% \begin{macro}{\HyLang@english}
% Not sure what these do. I do know I have one version of \Lpack{hyperref}
% that defines \cs{Hy@captionsenglish} and a later one that
% defines \cs{HyLang@english} instead.
%    \begin{macrocode}
\@ifundefined{Hy@captionsenglish}{}{%
  \g@addto@macro{\Hy@captionsenglish}{\def\bookautorefname{Book}}}
\@ifundefined{HyLang@english}{}{%
  \g@addto@macro{\HyLang@english}{\def\bookautorefname{Book}}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\Hy@captionsgerman}
% \begin{macro}{\HyLang@german}
%    \begin{macrocode}
\@ifundefined{Hy@captionsgerman}{}{%
  \g@addto@macro{\Hy@captionsgerman}{\def\bookautorefname{Buch}}}
\@ifundefined{HyLang@german}{}{%
  \g@addto@macro{\HyLang@german}{\def\bookautorefname{Buch}}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\Hy@captionsportuges}
% \begin{macro}{\HyLang@portuges}
%    \begin{macrocode}
\@ifundefined{Hy@captionsportuges}{}{%
  \g@addto@macro{\Hy@captionsportuges}{\def\bookautorefname{Livro}}}
\@ifundefined{HyLang@portuges}{}{%
  \g@addto@macro{\HyLang@portuges}{\def\bookautorefname{Livro}}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\Hy@captionsspanish}
% \begin{macro}{\HyLang@spanish}
%    \begin{macrocode}
\@ifundefined{Hy@captionsspanish}{}{%
  \g@addto@macro{\Hy@captionsspanish}{\def\bookautorefname{Libro}}}
\@ifundefined{HyLang@spanish}{}{%
  \g@addto@macro{\HyLang@spanish}{\def\bookautorefname{Libro}}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\Hy@captionsafrikaans}
% \begin{macro}{\HyLang@afrikaans}
%    \begin{macrocode}
\@ifundefined{Hy@captionsafrikaans}{}{%
  \g@addto@macro{\Hy@captionsafrikaans}{\def\bookautorefname{Boek}}}
\@ifundefined{HyLang@afrikaans}{}{%
  \g@addto@macro{\HyLang@afrikaans}{\def\bookautorefname{Boek}}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\bookautorefname}
%    \begin{macrocode}
\providecommand{\bookautorefname}{Book}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@writetorep}
% Change \Lpack{hyperref}'s \cs{@@writetorep} as memoir uses \cs{partnumberline} and 
% \cs{chapternumberline} as well as the normal \cs{numberline}
% \changes{v1.9}{2006/11/22}{\cs{@@writetorep} now in hyperref 2006/11/15}
%    \begin{macrocode}
\@ifpackagelater{hyperref}{2006/11/15}{%
}{%
\def\@@writetorep#1#2#3#4#5{%
  \begingroup
    \def\Hy@tempa{#5}%
    \ifx\Hy@tempa\Hy@bookmarkstype
      \edef\Hy@level{#4}%
      \ifx\Hy@levelcheck Y%
        \@tempcnta\Hy@level\relax
        \advance\@tempcnta by -1
        \ifnum\Hy@currentbookmarklevel<\@tempcnta
          \advance\@tempcnta by -\Hy@currentbookmarklevel\relax
          \advance\@tempcnta by 1
          \Hy@Warning{%
            Difference (\the\@tempcnta) between bookmark levels is %
            greater \MessageBreak than one, level fixed%
          }%
          \@tempcnta\Hy@currentbookmarklevel
          \advance\@tempcnta by 1
          \edef\Hy@level{\the\@tempcnta}%
        \fi
      \else
        \global\let\Hy@levelcheck Y%
      \fi
      \global\let\Hy@currentbookmarklevel\Hy@level
      \@tempcnta\Hy@level\relax
      \expandafter\xdef\csname Parent\Hy@level\endcsname{#3}%
      \advance\@tempcnta by -1
      \edef\Hy@tempa{#3}
      \edef\Hy@tempb{\csname Parent\the\@tempcnta\endcsname}%
      \ifx\Hy@tempa\Hy@tempb
        \Hy@Warning{%
          The anchor of a bookmark and its parent's must not%
          \MessageBreak be the same. Added a new anchor%
        }%
        \phantomsection
      \fi
      \ifHy@bookmarksnumbered
        \let\numberline\Hy@numberline
        \let\booknumberline\Hy@numberline     % <- added
        \let\partnumberline\Hy@numberline     % <- added
        \let\chapternumberline\Hy@numberline  % <- added
      \else
        \let\numberline\@gobble
        \let\booknumberline\@gobble           % <- added
        \let\partnumberline\@gobble           % <- added
        \let\chapternumberline\@gobble        % <- added
      \fi
      \pdfstringdef\Hy@tempa{#2}%
      \protected@write\@outlinefile{}{%
        \protect\BOOKMARK
          [\Hy@level][\@bookmarkopenstatus{\Hy@level}]{#3}%
          {\Hy@tempa}{\Hy@tempb}%
      }%
    \fi
  \endgroup}
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\M@sect}
% \Lpack{hyperref} fix for memoir's redefinition of \cs{@sect}
%    \begin{macrocode}
\@ifundefined{H@old@sectm@m}{% haven't fiddled with \M@sect
  \let\H@old@sectm@m\M@sect
  \def\M@sect#1#2#3#4#5#6[#7][#8]#9{%
    \ifnum #2>\c@secnumdepth
      \Hy@GlobalStepCount\Hy@linkcounter
      \xdef\@currentHref{section*.\the\Hy@linkcounter}%
    \fi
    \H@old@sectm@m{#1}{#2}{#3}{#4}{#5}{#6}[{#7}][{#8}]{#9}%
    \ifnum #2>\c@secnumdepth
     \Hy@raisedlink{\hyper@anchorstart{\@currentHref}\hyper@anchorend}%
    \fi}%
  }%
  {% already fiddled \M@sect 
  }

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@resets@pp}
% \Lpack{hyperref} fix for memoir's \texttt{appendices} environment
%      (discovered by Lars Hoemke, \url{hoemke@nikocity.de},
%       private email 20 Feb 2003)
%    \begin{macrocode}
\let\MH@old@resets@pp\@resets@pp
\renewcommand*{\@resets@pp}{%
  \MH@old@resets@pp
  \def\theHchapter{\Alph{chapter}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@resets@ppsub}
% \Lpack{hyperref} fix for memoir's \texttt{subappendices} environment
%      (Problem reported by Ignasi Furio, \url{ignasi.furio@uib.es},
%       private email 13 May 2004)
% \changes{v1.6}{2004/05/13}{Added \cs{@resets@ppsub} to memhfixc}
%    \begin{macrocode}
\let\MH@old@resets@ppsub\@resets@ppsub
\renewcommand*{\@resets@ppsub}{%
  \MH@old@resets@ppsub
  \def\theHsection{\theHchapter.\Alph{section}}}
\def\endsubappendices{\def\theHsection{\theHchapter.\arabic{section}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\cftaddtitleline}
% \begin{macro}{\cftaddnumtitleline}
% \Lpack{hyperref} fix for memoir's add to contents macros
%      (discovered by Martin Siegumfeldt, \ctt{} 2003/09/25
%       \textit{`Combinig [sic] Tocloft and hyperref'}
%    \begin{macrocode}
\renewcommand{\cftaddtitleline}[4]{%
  \addtocontents{#1}{\protect\contentsline{#2}{#3}{#4}{\@currentHref}}}
\renewcommand{\cftaddnumtitleline}[5]{\addtocontents{#1}{%
  \protect\contentsline{#2}{\protect\numberline{#3}#4}{#5}{\@currentHref}}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\caption}
% Yet another little jolly (fixed in version 1.5). \Lpack{hyperref}'s
% \verb?\def? of \verb?\caption? cunningly messes up \Lpack{memoir}'s,
% thereby screwing up the labels for subcaptions.
%    \begin{macrocode}
\let\m@moldhypcaption\caption
\renewcommand{\caption}{\donemaincaptiontrue\m@moldhypcaption}

%    \end{macrocode}
% \end{macro}
%
%    More tweaks are needed for continued captions and sidecaptions.
% The need for these discovered by 
% \v{Z}arko F \v{C}u\v{c}ej\footnote{\texttt{zarko.cucej@uni-mb.si}}
% (for \cs{contcaption}) and by 
% J{\o}rgen Larsen\footnote{\texttt{jl@ruc.dk}} (for \cs{sidecaption}).
% \changes{v1.7}{2005/11/15}{Added tweaks for \cs{contcaption},
%                \cs{sidecaption} and \cs{sidecontcaption}}
%
% \begin{macro}{\c@memhycontfloat}
% \begin{macro}{\thememhycontfloat}
% \begin{macro}{\theHmemhycontfloat}
% We need an extra marker for continued captions.
% \changes{v1.7}{2005/11/15}{Added extra counter for continued captions}
%    \begin{macrocode}
\newcounter{memhycontfloat}
\renewcommand*{\thememhycontfloat}{\arabic{memhycontfloat}}
\renewcommand{\theHmemhycontfloat}{\arabic{memhycontfloat}}
\setcounter{memhycontfloat}{0}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\contcaption}
% Tweak the \cs{contcaption} macro.
%    \begin{macrocode}
\renewcommand{\contcaption}{%
  \refstepcounter{memhycontfloat}%
  \addtocounter{\@captype}{\m@ne}\H@refstepcounter{\@captype}%
  \@contcaption\@captype}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\endsidecaption}
% Tweak the \texttt{sidecaption} environment. I don't know why it 
% works but it took a lot of time trying all sorts of combinations
% of commands to get it to do so (if it really does).
%    \begin{macrocode}
\def\endsidecaption{%
  \m@mscapend@fbox
  \H@refstepcounter{\@captype}%
  \hyper@makecurrent{\@captype}%
  \m@mscaplabel
  \begin{lrbox}{\m@mscap@capbox}%
    \begin{minipage}[c]{\sidecapwidth}%
      \sidecapstyle
      \@caption\@captype[\m@mscap@fortoc]{\m@mscap@forcap}%
    \end{minipage}%
  \end{lrbox}%
  \m@mscapopboxes}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sidecontcaption}
% \begin{macro}{\endsidecontcaption}
% Tweaks for the \texttt{sidecontcaption} environment. Again it was 
% a miss, miss, \ldots hit method to get something that appears 
% to work.
%    \begin{macrocode}
\renewcommand*{\sidecontcaption}{%
  \refstepcounter{memhycontfloat}%
  \@sidecontcaption}
\def\endsidecontcaption{%
  \m@mscapend@fbox
  \addtocounter{\@captype}{\m@ne}\H@refstepcounter{\@captype}%
  \hyper@makecurrent{memhycontfloat}%
  \m@mscaplabel
  \begin{lrbox}{\m@mscap@capbox}%
    \begin{minipage}[c]{\sidecapwidth}%
      \sidecapstyle
      \@contcaption\@captype{\m@mscap@forcap}%
    \end{minipage}%
  \end{lrbox}%
  \m@mscapopboxes}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% 
%
% The end of the \Lpack{hyperref} related patches.
%
%    \begin{macrocode}
%</hyper>
%    \end{macrocode}
%
%
% \section{Patches for the class code}     \label{sec:class}
%
% At this date, 2005/09/25, with the simultaneous release of memoir v1.618 
% and the patch file, by definition there are no patches.
%
%    Subsequently code patches were found to be required.
%
% \subsection{Identification} \label{sec:id}
%
% \changes{v2.0}{2004/01/31}{No code in mempatch.sty}
% \changes{v2.1}{2004/02/12}{Code now required in mempatch.sty}
% \changes{v2.1a}{2004/02/19}{Extra pagestyle code for handling \cs{linespread}}
% \changes{v2.4}{2004/04/26}{Removed extraneous spaces}
% \changes{v2.4+}{2004/04/26}{Stopped noting general changes}
% 
%    \begin{macrocode}
%<*patch>
%    \end{macrocode}
%
% ^^A \typeout{mempatch.sty 2002/12/12 v1.1 Patches for memoir class v1.3a}
% ^^A \typeout{mempatch.sty 2003/01/06 v1.2 Patches for memoir class v1.3a}
% ^^A \typeout{mempatch.sty 2003/01/22 v1.3 Patches for memoir class v1.3a}
% ^^A \typeout{mempatch.sty 2003/02/13 v1.4 Patches for memoir class v1.3a}
% ^^A \typeout{mempatch.sty 2003/04/26 v1.5 Patches for memoir class v1.3a}
% ^^A \typeout{mempatch.sty 2003/06/28 v1.6 Patches for memoir class v1.3a}
% ^^A \typeout{mempatch.sty 2003/07/23 v1.7 Patches for memoir class v1.3a}
% ^^A \typeout{mempatch.sty 2003/09/16 v1.8 Patches for memoir class v1.3a}
% ^^A \typeout{mempatch.sty 2003/10/06 v1.8a Patches for memoir class v1.3a}
% ^^A \typeout{mempatch.sty 2003/11/16 v1.9 Patches for memoir class v1.3a}
% ^^A \typeout{mempatch.sty 2004/01/31 v2.0 Patches for memoir class v1.6}
% ^^A \typeout{mempatch.sty 2004/02/12 v2.1 Patches for memoir class v1.6}
% ^^A \typeout{mempatch.sty 2004/02/19 v2.1a Patches for memoir class v1.6}
% ^^A \typeout{mempatch.sty 2004/03/01 v2.2 Patches for memoir class v1.6}
% ^^A \typeout{mempatch.sty 2004/03/28 v2.3 Patches for memoir class v1.61}
% ^^A \typeout{mempatch.sty 2004/04/26 v2.3 Patches for memoir class v1.61}
% ^^A \typeout{mempatch.sty 2004/04/27 v2.3a Patches for memoir class v1.61}
% ^^A \typeout{mempatch.sty 2004/04/30 v2.3b Patches for memoir class v1.61}
% ^^A \typeout{mempatch.sty 2004/05/10 v2.3c Patches for memoir class v1.61}
% ^^A \typeout{mempatch.sty 2004/05/14 v2.3d Patches for memoir class v1.61}
% ^^A \typeout{mempatch.sty 2004/12/14 v3.0 Patches for memoir class v1.61}
% ^^A \typeout{mempatch.sty 2004/12/19 v3.1 Patches for memoir class v1.61}
% ^^A \typeout{mempatch.sty 2005/01/01 v3.2 Patches for memoir class v1.61}
% ^^A \typeout{mempatch.sty 2005/01/17 v3.3 Patches for memoir class v1.61}
% ^^A \typeout{mempatch.sty 2005/01/18 v3.4 Patches for memoir class v1.61}
% ^^A \typeout{mempatch.sty 2005/02/01 v3.5 Patches for memoir class v1.61}
% ^^A \typeout{mempatch.sty 2005/02/07 v3.6 Patches for memoir class v1.61}
% ^^A \typeout{mempatch.sty 2005/02/26 v3.7 Patches for memoir class v1.61}
% ^^A \typeout{mempatch.sty 2005/03/06 v3.8 Patches for memoir class v1.61}
% ^^A \typeout{mempatch.sty 2005/03/23 v3.9 Patches for memoir class v1.61}
% ^^A \typeout{mempatch.sty 2005/05/16 v3.10 Patches for memoir class v1.61}
% ^^A \typeout{mempatch.sty 2005/05/23 v3.11 Patches for memoir class v1.61}
% ^^A \typeout{mempatch.sty 2005/06/06 v3.12 Patches for memoir class v1.61}
% ^^A \typeout{mempatch.sty 2005/09/25 v4.0 Patches for memoir class v1.618}
% ^^A \ProvidesFile{mempatch.sty}[2005/10/06 v4.1 Patches for memoir class v1.618]
% ^^A \ProvidesFile{mempatch.sty}[2005/11/21 v4.2 Patches for memoir class v1.618]
% ^^A \ProvidesFile{mempatch.sty}[2005/12/13 v4.3 Patches for memoir class v1.618]
% ^^A \ProvidesFile{mempatch.sty}[2006/01/21 v4.4 Patches for memoir class v1.618]
% ^^A \ProvidesFile{mempatch.sty}[2006/01/23 v4.4a Patches for memoir class v1.618]
% ^^A \ProvidesFile{mempatch.sty}[2006/07/10 v4.5 Patches for memoir class v1.618]
% ^^A \ProvidesFile{mempatch.sty}[2006/08/08 v4.5 Patches for memoir class v1.618]
% ^^A \ProvidesFile{mempatch.sty}[2006/12/11 v4.6 Patches for memoir class v1.618]
% ^^A \ProvidesFile{mempatch.sty}[2006/12/23 v4.7 Patches for memoir class v1.618]
%    \begin{macrocode}
\ProvidesFile{mempatch.sty}[2007/01/22 v4.8 Patches for memoir class v1.618]


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%   Version 4.0 was released simultaneously with version 1.618 of memoir.
%%% By definition there were no patches.
%%% Most of the subsequent additions are noted in the Addendum.
%%%
%%%   Version 4.3 introduces the \reparticle macro which is not in the 
%%% Addendum.
%%%
%%%   Version 4.4 introduces several additional commands, in particular 
%%% the \book command provides a document division above Part. See the 
%%% code (towards the end) for details of the new commands.
%%%
%%%   Version 4.5 introduces commands for switching trim marks off and on
%%% and a \DisemulatePackage macro to negate a prior \EmulatedPackage.
%%% It also provides the functions of the pagenote package for end notes.
%%% See the code (near the end) for details.
%%%
%%%   Version 4.6 fixes all known bugs and provides means of increasing
%%% the interlinear and interparagraph spacing, adds some minor new commands,
%%% and provides about a dozen more built-in chapterstyles.
%%% See the code (near the end) for details.
%%%
%%%   Version 4.7 fixes tiny bugs introduced in version 4.6
%%%
%%%   Version 4.8 fixes some more little bugs.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%    \end{macrocode}
%
%
% \section{Mempatch version 4.1}
%
%    \begin{macrocode}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Version 4.1 (2005/10/03 and later)

%    \end{macrocode}
%
% \begin{macro}{\@ssect}
% Gabriel Guernik on 2005/10/03 discovered that I had made a boo-boo with \cs{memsecstarinfo}
% in \cs{@ssect} by calling it with 3 arguments when it only has two.
% \changes{v4.1}{2005/10/03}{Fixed problem with \cs{memsecstarinfo} in 
%                \cs{@ssect}}
%    \begin{macrocode}
%%%% Use correct number of arguments for \memsecstarinfo (2 not 3).
\def\@ssect#1#2#3#4#5{%
  \M@gettitle{#5}%
  \memsecstarinfo{\m@msecn@mame}{#5}%
  \@mem@old@ssect{#1}{#2}{#3}{#4}{#5}}

%    \end{macrocode}
% \end{macro}
%
% \subsection{Side captions}
% \changes{v4.1}{2005/10/03}{Added all the code for side captions}
%
%    \begin{macrocode}
%%%%%%%%%%%%
%%%%%%%%%%%% Side captions
%%%%%%%%%%%%

%    \end{macrocode}
%
% \begin{macro}{\m@mscap@capbox}
% \begin{macro}{\m@mscap@fbox}
% We need two save boxes, one to hold the caption and the other
% for the float material.
%    \begin{macrocode}
\newsavebox{\m@mscap@capbox}
\newsavebox{\m@mscap@fbox}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\sidecapsep}
% \begin{macro}{\sidecapwidth}
% \cs{sidecapsep} is the space between the text and the caption, which
% is set in a box \cs{sidecapwidth} wide. These are initialized to
% the \cs{marginpar...} values.
%    \begin{macrocode}
\newdimen\sidecapsep
  \sidecapsep=\marginparsep
\newdimen\sidecapwidth
  \sidecapwidth=\marginparwidth

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\m@m@tempdima}
% \begin{macro}{\m@mscapraise}
% Lengths for internal use. \cs{m@m@tempdima} is meant for general
% temporary use.
%    \begin{macrocode}
\newdimen\m@m@tempdima
\newdimen\m@mscapraise

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\sidecapraise}
% Length to make (small) adjustments to the position of the caption wrt the float.
%    \begin{macrocode}
\newdimen\sidecapraise
  \sidecapraise \z@

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\setsidecappos}
% \begin{macro}{\m@mscappos}
% \cs{setsidecappos}\marg{pos}, where \meta{pos} is one of \texttt{t},
% \texttt{c}, or \texttt{b}, sets the vertical position of the caption
% in relation to the float and the result is saved as \cs{m@mscappos}.
% The default is \texttt{c}.
%    \begin{macrocode}
\newcommand*{\setsidecappos}[1]{%
  \def\m@mscappos{#1}\def\@tempb{t}%
  \ifx\@tempb\m@mscappos
  \else
    \def\@tempb{b}%
    \ifx\@tempb\m@mscappos
    \else
      \def\@tempb{c}%
      \ifx\@tempb\m@mscappos
      \else
        \@memerror{Argument to \string\setsidecappos\space is not t or c or b.
                   \MessageBreak Set to c}{\@ehc}%
        \def\m@mscappos{c}%
      \fi
    \fi
  \fi}     
\setsidecappos{c}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\sidecapmargin}
% \begin{macro}{\m@mscapmarg}
% \cs{sidecapmargin}\marg{margin}, where \meta{marg} is one of
% \texttt{left}, \texttt{right}, \texttt{inner}, or \texttt{outer}, 
% controls the margin where the caption will be put. The result
% is saved as \cs{m@mscapmarg} as a number. The default
% is \texttt{left}.
%    \begin{macrocode}
\newcommand{\sidecapmargin}[1]{%
  \def\@tempa{#1}\def\@tempb{left}%
    \ifx\@tempb\@tempa
      \def\m@mscapmarg{0}%   left
    \else
      \def\@tempb{right}%
      \ifx\@tempb\@tempa
        \def\m@mscapmarg{1}%  right
      \else
        \def\@tempb{outer}%
        \ifx\@tempb\@tempa
          \def\m@mscapmarg{2}%  outer
        \else
          \def\@tempb{inner}%
          \ifx\@tempb\@tempa
            \def\m@mscapmarg{3}%  inner
          \else
            \@memerror{Unrecognized argument for \string\sidecapmargin}%
                      {\@ehc}%
            \def\m@mscapmarg{-1}% error
          \fi
        \fi
      \fi
    \fi}
\sidecapmargin{left}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\ifscapmargleft}
% \begin{macro}{\scapmarglefttrue}
% \begin{macro}{\scapmargleftfalse}
%  \cs{ifscapmargleft} is TRUE the caption should be in the left hand margin,
% otherwise in the right hand margin.
%    \begin{macrocode}
\newif\ifscapmargleft

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\sidecapfloatwidth}
% \begin{macro}{\m@mscapmainwidth}
% \cs{sidecapfloatwidth} is the width of the box holding the float. 
% Note that this is a macro,
% not a length, so must be changed using \cs{renewcommand*}.
% The default is \cs{linewidth}. Later, \cs{m@mscapmainwidth} will be set
% to the current length specification from \cs{sidecapfloatwidth}.
%    \begin{macrocode}
\def\sidecapfloatwidth{\linewidth}
\newdimen\m@mscapmainwidth

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\m@mscaplkern}
% \begin{macro}{\setm@mscaplkern}
% \cs{setm@mscaplkern} is a utility macro to calculate the kern 
% (\cs{m@mscaplkern}) required when the caption is in the left margin.
%    \begin{macrocode}
\newdimen\m@mscaplkern
\newcommand*{\setm@mscaplkern}{%
  \m@mscaplkern=\sidecapwidth
  \advance\m@mscaplkern \sidecapsep
  \advance\m@mscaplkern \m@mscapmainwidth}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\sidecapstyle}
% \cs{sidecapstyle} is called just before the caption is set. It can
% be redefined to set different caption style parameters. The default
% is raggedleft for left margin captions and raggedright for right
% margin captions.
%    \begin{macrocode}
\newcommand*{\sidecapstyle}{%
%%%  \captionnamefont{\bfseries}%
  \ifscapmargleft
    \captionstyle{\raggedleft}%
  \else
    \captionstyle{\raggedright}%
  \fi}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sidecaption}
% The whole shebang is in the \texttt{sidecaption} environment but
% it is more convenient to specify this via macros rather than 
% directly as an environment.
%
% \cs{sidecaption}\oarg{fortoc}\marg{title}\oarg{label} is what it
% looks like to the user, but internally further macros handle
% all the arguments.
%    \begin{macrocode}
\newcommand*{\sidecaption}{%
  \@ifnextchar [{\@sidecaption}{\@sidecaption[]}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@sidecaption}
% \cs{@sidecaption}\oarg{fortoc}\marg{title} grabs the first
% two arguments.
%    \begin{macrocode}
\def\@sidecaption[#1]#2{%
  \@ifnextchar [{\@@sidecaption{#1}{#2}}{\@@sidecaption{#1}{#2}[]}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@sidecaption}
% \cs{@@sidecaption}\marg{fortoc}\marg{title}\oarg{label} is the
% last macro in the chain and handles all three arguments.
% This does all the work for \verb?\begin{sidecaption}?
% \begin{macro}{\m@mscap@fortoc}
% \begin{macro}{\m@mscap@forcap}
% \begin{macro}{\m@mscaplabel}
% First, save all the arguments as macros.
%    \begin{macrocode}
\def\@@sidecaption#1#2[#3]{%
  \ifx\@empty#1\@empty
    \def\m@mscap@fortoc{#2}%
  \else
    \def\m@mscap@fortoc{#1}%
  \fi
  \def\m@mscap@forcap{#2}%
  \ifx\@empty#3\@empty
    \def\m@mscaplabel{}%
  \else
    \def\m@mscaplabel{\@bsphack\label{#3}\@esphack}%
  \fi
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% Set the float width, calculate the left margin kern, and start
% a \texttt{minipage} to hold the float, saving it in box
% \cs{m@mscap@fbox}.
%    \begin{macrocode}
  \m@mscapstart@fbox}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@mscapstart@fbox}
% \begin{macro}{\m@mscapend@fbox}
% \cs{m@mscapstart@fbox} is the macro that actually sets the float width, 
% calculates the left margin kern, and starts the float's \texttt{minipage}.
% The macro \cs{m@mscapend@fbox} ends the box.
%    \begin{macrocode}
\newcommand*{\m@mscapstart@fbox}{%
  \setlength{\m@mscapmainwidth}{\sidecapfloatwidth}%
  \setm@mscaplkern
  \begin{lrbox}{\m@mscap@fbox}%
    \begin{minipage}[c]{\m@mscapmainwidth}}
\newcommand*{\m@mscapend@fbox}{%
    \end{minipage}%
  \end{lrbox}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\endsidecaption}
% \cs{endsidecaption} does the work for \verb?\end{sidecaption}?.
%
% Finish the float minipage, then increment the caption counter and
% call \cs{label} via \cs{m@mscaplabel}.
%    \begin{macrocode}
\def\endsidecaption{%
  \m@mscapend@fbox
  \refstepcounter\@captype
  \m@mscaplabel
%    \end{macrocode}
% Set the caption inside a minipage, saving it in box \cs{m@mscap@capbox}.
%    \begin{macrocode}
  \begin{lrbox}{\m@mscap@capbox}%
    \begin{minipage}[c]{\sidecapwidth}%
      \sidecapstyle
      \@caption\@captype[\m@mscap@fortoc]{\m@mscap@forcap}
    \end{minipage}%
  \end{lrbox}%
%    \end{macrocode}
% Output the float and caption.
%    \begin{macrocode}
  \m@mscapopboxes}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@mscapopboxes}
% Having determined how high the caption box must be raised with
% respect to the float box, output the boxes. 
%    \begin{macrocode}
\newcommand*{\m@mscapopboxes}{%
  \m@mcalcscapraise
%    \end{macrocode}
% Set the float (from box \cs{m@mscap@fbox}) then the caption (from
% box \cs{m@mscap@capbox}) kerning it to the left or right as
% appropriate.
%    \begin{macrocode}
  \usebox{\m@mscap@fbox}\m@mscapcheckside
  \ifscapmargleft%
    \rlap{\kern-\m@mscaplkern
          \raisebox{\m@mscapraise}{\usebox{\m@mscap@capbox}}}%
  \else%
    \rlap{\kern\sidecapsep 
          \raisebox{\m@mscapraise}{\usebox{\m@mscap@capbox}}}%
  \fi
%    \end{macrocode}
% Finally, make \cs{m@mscapthisside} a no-op.
%    \begin{macrocode}
  \gdef\m@mscapthisside{}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@mcalcscapraise}
% Calculate the amount the caption might have to be raised wrt the
% float. This depends on the position: \\
% \texttt{t:} raise by the difference in heights \\
% \texttt{c:} shouldn't have to do anything as the minipages are meant
%             to center aligned \\
% \texttt{b:} lower by the difference in depths \\
%
%    However, experiments showed that a little bit of tweaking might
% help. The final adjustment, \cs{sidecapraise} is controlled by
% the user.
%    \begin{macrocode}
\newcommand*{\m@mcalcscapraise}{%
  \def\@tempb{t}%
  \ifx\m@mscappos\@tempb
    \settoheight{\m@m@tempdima}{\strut\usebox{\m@mscap@capbox}}%
    \settoheight{\m@mscapraise}{\usebox{\m@mscap@fbox}}%
    \advance\m@mscapraise -\m@m@tempdima
    \advance\m@mscapraise  0.5ex
  \else
    \def\@tempb{b}%
    \ifx\m@mscappos\@tempb
      \settodepth{\m@m@tempdima}{\usebox{\m@mscap@fbox}}%
      \settodepth{\m@mscapraise}{\strut\usebox{\m@mscap@capbox}}%
      \advance\m@mscapraise -\m@m@tempdima
    \else
      \m@mscapraise=\z@
      \advance\m@mscapraise 0.25ex
    \fi
  \fi
  \advance\m@mscapraise  \sidecapraise}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@mscapcheckside}
% This macro determines whether the caption should be set in the
% left or right margin. In twocolumn documents the caption for a single
% column float is always set in the adjacent margin. Starred floats
% are treated as regular floats as in a onecolumn document.
%    \begin{macrocode}
\newcommand*{\m@mscapcheckside}{%
  \if@twocolumn
    \ifdim\hsize=\textwidth%  float*
      \m@mscapcheckregside
    \else
      \if@firstcolumn
        \scapmarglefttrue
      \else
        \scapmargleftfalse
      \fi
    \fi
  \else
    \m@mscapcheckregside
  \fi
%    \end{macrocode}
% Finally apply any user's override.
%    \begin{macrocode}
  \m@mscapthisside}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@mscapcheckregside}
% This performs the margin calculation for onecolumn documents.
% \changes{v4.1}{2005/11/21}{Fixed missing t in \cs{marglefttrue}, courtesy Jorgen Larsen}
%    \begin{macrocode}
\newcommand*{\m@mscapcheckregside}{%
    \if@twoside
      \checkoddpage
      \ifnum\m@mscapmarg<\@ne%      % left
        \scapmarglefttrue
      \else
        \ifnum\m@mscapmarg=\@ne%    % right
          \scapmargleftfalse
        \else  
          \ifnum\m@mscapmarg=\tw@%  % outer
            \scapmarglefttrue
            \ifoddpage
              \scapmargleftfalse
            \fi
          \else%                  % inner
            \scapmargleftfalse
            \ifoddpage
              \scapmarglefttrue
            \fi
          \fi
        \fi
      \fi
    \else%     oneside
      \scapmarglefttrue
      \ifnum\m@mscapmarg>\@ne
        \ifnum\m@mscapmarg<\thr@@
          \scapmargleftfalse
        \fi
      \fi
    \fi}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\overridescapmargin}
% \begin{macro}{\m@mscapthisside}
% User macro to override the calculated caption margin. Call as either:\\
% \verb?\overridescapmargin{left}? or \verb?\overridescapmargin{right}?  \\
%    \begin{macrocode}
\newcommand*{\overridescapmargin}[1]{%
  \def\@tempb{#1}\def\@tempa{left}%
  \ifx\@tempa\@tempb
    \def\m@mscapthisside{\scapmarglefttrue}%
  \else
    \def\@tempa{right}%
    \ifx\@tempa\@tempb
      \def\m@mscapthisside{\scapmargleftfalse}%
    \else
      \@memerror{Argument to \string\overridescapmargin\space neither 
                 left nor right}{\@ehc}%
      \def\m@mscapthisside{}%
    \fi
  \fi}
\newcommand*{\m@mscapthisside}{}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%    Now for the other kinds of captions.
%
% \begin{macro}{\sidecontcaption}
% \cs{sidecontcaption}\marg{title}\oarg{label} is for
% a continuation sidecaption.
%    \begin{macrocode}
\newcommand*{\sidecontcaption}{%
  \@sidecontcaption}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@sidecontcaption}
% \cs{@sidecontcaption}\marg{title} grabs the first
% argument.
%    \begin{macrocode}
\def\@sidecontcaption#1{%
  \@ifnextchar [{\@@sidecontcaption{#1}}{\@@sidecontcaption{#1}[]}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@sidecontcaption}
% \cs{@@sidecontcaption}\marg{title}\oarg{label} is the
% last macro in the chain and handles all two arguments.
% This does all the work for \verb?\begin{sidecontcaption}?
%    \begin{macrocode}
\def\@@sidecontcaption#1[#2]{%
  \def\m@mscap@forcap{#1}%
  \ifx\@empty#2\@empty
    \def\m@mscaplabel{}%
  \else
    \def\m@mscaplabel{\@bsphack\label{#2}\@esphack}%
  \fi
  \m@mscapstart@fbox}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\endsidecontcaption}
% \cs{endsidecontcaption} does the work for \verb?\end{sidecontcaption}?.
%
%    \begin{macrocode}
\def\endsidecontcaption{%
  \m@mscapend@fbox
  \addtocounter{\@captype}{\m@ne}\refstepcounter\@captype
  \m@mscaplabel
  \begin{lrbox}{\m@mscap@capbox}%
    \begin{minipage}[c]{\sidecapwidth}%
      \sidecapstyle
      \@contcaption\@captype{\m@mscap@forcap}
    \end{minipage}%
  \end{lrbox}%
  \m@mscapopboxes}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\sidenamedlegend}
% \begin{macro}{\@sidenamedlegend}
% \cs{sidenamedlegend}\oarg{fortoc}\marg{title} is for
% a namedlegend sidecaption.
%    \begin{macrocode}
\newcommand*{\sidenamedlegend}{%
  \@ifnextchar [{\@sidenamedlegend}{\@sidenamedlegend[]}}
\def\@sidenamedlegend[#1]#2{%
  \@@sidenamedlegend{#1}{#2}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@sidenamedlegend}
% \cs{@@sidenamedlegend}\marg{fortoc}\marg{title} is the
% last macro in the chain and handles all arguments.
% This does all the work for \verb?\begin{sidenamedlegend}?
%    \begin{macrocode}
\def\@@sidenamedlegend#1#2{%
  \ifx\@empty#1\@empty
    \def\m@mscap@fortoc{#2}%
  \else
    \def\m@mscap@fortoc{#1}%
  \fi
  \def\m@mscap@forcap{#2}%
  \def\m@mscaplabel{}%
  \m@mscapstart@fbox}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\endsidenamedlegend}
% \cs{endsidenamedlegend} does the work for \verb?\end{sidenamedlegend}?.
%    \begin{macrocode}
\def\endsidenamedlegend{%
  \m@mscapend@fbox
  \begin{lrbox}{\m@mscap@capbox}%
    \begin{minipage}[c]{\sidecapwidth}%
      \sidecapstyle
      \@legend\@captype[\m@mscap@fortoc]{\m@mscap@forcap}
    \end{minipage}%
  \end{lrbox}%
  \m@mscapopboxes}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\sidelegend}
% \cs{sidelegend}\marg{title} is for
% a legend sidecaption.
%    \begin{macrocode}
\newcommand*{\sidelegend}{%
  \@@sidelegend}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@sidelegend}
% \cs{@@sidelegend}\marg{title} is the
% last macro in the chain and handles all arguments.
% This does all the work for \verb?\begin{sidelegend}?
%    \begin{macrocode}
\def\@@sidelegend#1{%
  \def\m@mscap@forcap{#1}%
  \m@mscapstart@fbox}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\endsidelegend}
% \cs{endsidelegend} does the work for \verb?\end{sidelegend}?.
%    \begin{macrocode}
\def\endsidelegend{%
  \m@mscapend@fbox
  \begin{lrbox}{\m@mscap@capbox}%
    \begin{minipage}[c]{\sidecapwidth}%
      \sidecapstyle
      \legend{\m@mscap@forcap}
    \end{minipage}%
  \end{lrbox}%
  \m@mscapopboxes}

%    \end{macrocode}
% \end{macro}
%
% \section{Mempatch version 4.2}
%
%    \begin{macrocode}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Version 4.2 (2005/11/21 and later)

%    \end{macrocode}
%
% \subsection{AtBegin/End...}
%
%    Code, based on a hint from Morten H{\o}gholm\footnote{CTT posting
% 2005/11/08, \textit{Re: Checking for packages froma class... 
% revisited}} that the \Lpack{scrlfile} package from 
% \Lpack{koma-script} includes commands such as \\
% \cs{AfterFile}\marg{file}\marg{code} \\
% plus before and after classes and packages.
%
%    \begin{macrocode}
%%%%%%%%%%%%
%%%%%%%%%%%% Extensions to \AtBeginDocument
%%%%%%%%%%%%

%    \end{macrocode}
%
%    Extend the \cs{InputIfFileExists} macro to call hooks before and
% the actual inputting. The kernel version (as of 2005/11/21) is:
% \begin{verbatim}
% \newcommand{\InputIfFileExists}[2]{%
%  \IfFileExists{#1}%
%    {\#2\@addtofilelist{#1}\@@input \@filef@und}}
% \end{verbatim}
%
% \begin{macro}{\InputIfFileExists}
% Effectively all file input is handled by this macro. Extend it
% by adding pre- and post- input hooks.
%    \begin{macrocode}
\renewcommand{\InputIfFileExists}[2]{%
  \IfFileExists{#1}%
    {#2\@addtofilelist{#1}\m@matbeginf{#1}%
     \@@input \@filef@und
     \m@matendf{#1}%
     \killm@matf{#1}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@matbeginf}
% \begin{macro}{\m@matendf}
% \cs{m@matbeginf}\marg{file} calls macro \cs{file-m@mfb} if it 
% is defined. Similarly \cs{m@matendf}\marg{file} calls
% \cs{file-m@mfe} if it is defined.
%    \begin{macrocode}
\newcommand{\m@matbeginf}[1]{\@ifundefined{#1-m@mfb}{}%
  {\@nameuse{#1-m@mfb}}}
\newcommand{\m@matendf}[1]{\@ifundefined{#1-m@mfe}{}%
  {\@nameuse{#1-m@mfe}}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\killm@matf}
% \cs{killm@matf}\marg{file} undefines the \cs{file-m@mfb} and
% \cs{file-m@mfe} macros.
%    \begin{macrocode}
\newcommand*{\killm@matf}[1]{%
  \@namelet{#1-m@mfb}\relax
  \@namelet{#1-m@mfe}\relax}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\AtBeginFile}
% \begin{macro}{\AtEndFile}
% \cs{AtBeginFile}\marg{file}\marg{code} inserts \meta{code}
% just before \meta{file} is input. Similarly \cs{AtEndFile} inserts
% just after input.
%    \begin{macrocode}
\newcommand{\AtBeginFile}[2]{\@ifundefined{#1-m@mfb}%
  {\@namedef{#1-m@mfb}{#2}}%
  {\expandafter\addtodef\csname #1-m@mfb\endcsname{}{#2}}}
\newcommand{\AtEndFile}[2]{\@ifundefined{#1-m@mfe}%
  {\@namedef{#1-m@mfe}{#2}}%
  {\expandafter\addtodef\csname #1-m@mfe\endcsname{}{#2}}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\AtBeginPackage}
% \begin{macro}{\AtEndPackage}
% \begin{macro}{\RequireAtEndPackage}
% \cs{AtBeginPackage}\marg{pack}\marg{code} inserts \meta{code} 
% just before the \meta{pack} package is input, and \cs{AtEndPackage}
% is the equivalent for after input. \cs{RequireAtEndPackage} inserts
% \meta{code} either at the end of \meta{pack}, or immediately if
% \meta{pack} has already been input.
%    \begin{macrocode}
\newcommand{\AtBeginPackage}[2]{%
  \AtBeginFile{#1.\@pkgextension}{#2}}
\newcommand{\AtEndPackage}[2]{%
  \AtEndFile{#1.\@pkgextension}{#2}}
\newcommand{\RequireAtEndPackage}[2]{%
  \@ifpackageloaded{#1}{#2}%
  {\AtEndFile{#1.\@pkgextension}{#2}}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\AtBeginClass}
% \begin{macro}{\AtEndClass}
% \begin{macro}{\RequireAtEndClass}
% \cs{AtBeginClass}\marg{class}\marg{code} and friends are the class
% equivalents of \cs{AtBeginPackage} and friends.
%    \begin{macrocode}
\newcommand{\AtBeginClass}[2]{%
  \AtBeginFile{#1.\@clsextension}{#2}}
\newcommand{\AtEndClass}[2]{%
  \AtEndFile{#1.\@clsextension}{#2}}
\newcommand{\RequireAtEndClass}[2]{%
  \@ifclassloaded{#1}{#2}%
  {\AtEndFile{#1.\@clsextension}{#2}}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \section{Mempatch version 4.3}
%
%    \begin{macrocode}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Version 4.3 (2005/12/13 and later)

%    \end{macrocode}
%
% \subsection{reparticle chapter style}
%
% The \verb?reparticle? chapterstyle replicates the appearance
% of a \cs{section} in an \Lpack{article} class.
%
%    \begin{macrocode}
%%% Replicate the appearance of a \section in the article class
\makechapterstyle{reparticle}{%
  \setlength{\beforechapskip}{3.5ex \@plus 1ex \@minus .2ex}%
  \renewcommand*{\chapterheadstart}{\vspace{\beforechapskip}}%
  \setlength{\afterchapskip}{2.3ex \@plus .2ex}%
  \renewcommand*{\printchaptername}{}%
  \renewcommand*{\chapternamenum}{}%
  \renewcommand*{\chaptitlefont}{\normalfont\Large\bfseries}%
  \renewcommand*{\chapnumfont}{\chaptitlefont}%
  \renewcommand*{\printchapternum}{\@hangfrom{\chapnumfont \thechapter\quad}}%
  \renewcommand*{\afterchapternum}{}}

%    \end{macrocode}
%
% \begin{macro}{\reparticle}
% Call this to get division heads to replicate the \Lpack{article} class.
% For example: \\
% \verb?\ifartopt \reparticle \fi?
%    \begin{macrocode}
%%% \reparticle redefines the higher level division heads fonts and spacing 
%%% to replicate the article class (but \chapter must still be used).
%%% Use, for example like: \ifartopt \reparticle \fi
\newcommand*{\reparticle}{%
  \chapterstyle{reparticle}%
  \setsecheadstyle{\large\bfseries\raggedright}%
  \setsubsecheadstyle{\normalsize\bfseries\raggedright}%
}

%    \end{macrocode}
% \end{macro}
%
% \section{Mempatch version 4.4}
%
%    \begin{macrocode}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Version 4.4 (2006/01/06 and later)

%    \end{macrocode}
%
% \subsection{Captiontitlefinal}
%
% \begin{macro}{\captiontitlefinal}
% \begin{macro}{\@makecaption}
% \cs{captiontitlefinal}\marg{stuff} puts \meta{stuff} immediately at the
% end of the caption's title text, but it will not appear the the LoF/LoT/etc.
% The code was supplied by Frederic 
% Connes\footnote{\texttt{frederic@connes.org}} in an email to me on 
% 2005/12/28.
% \changes{v4.4a}{2006/01/23}{Fixed \cs{@tewmpboxa} typo in \cs{@makecaption}}
%    \begin{macrocode}
%%% \captiontitlefinal{<stuff>} will put <stuff> immediately at the
%%% end of a \caption's title text and it will not appear in the
%%% LoF/LoT/etc. For example: \captiontitlefinal{.}
%%% (Code supplied by Frederic Connes)
\newcommand*{\captiontitlefinal}[1]{\def\@contfinal{#1}}
\captiontitlefinal{}

\renewcommand{\@makecaption}[2]{\let\@memtempa\relax
  \ifdim\prevdepth>-99\p@ \vskip\abovecaptionskip
  \else \def\@memtempa{\vbox to\topskip{}}\fi
  \let\@contfnote\footnote \renewcommand{\footnote}[2][]{}%
  \let\@contfmark\footnotemark \renewcommand{\footnotemark}[1][]{}%
  \sbox\@tempboxa{\@contnfont #1\@contdelim \@conttfont #2\@contfinal}%
  \let\footnote\@contfnote
  \let\footnotemark\@contfmark
  \ifdim\wd\@tempboxa<\linewidth \centering\fi
  \if@contcw
    \centering
    \parbox{\@contcwidth}{%
    \ifdim\wd\@tempboxa<\@contcwidth \centering\fi
  \fi
  \if@conthang
    \sbox\@tempboxa{\@contnfont #1\@contdelim}%
    \@contpre%
    {\@contcstyle\hangindent=\wd\@tempboxa
     \noindent\box\@tempboxa\@memtempa \@conttfont #2\@contfinal\par}%
  \else
    \if@contindent
      \@contpre%
      {\@contnfont #1\@contdelim}\@memtempa
      {\@contcstyle\hangindent=\@contindw
                   \hangafter=\@ne\@conttfont #2\@contfinal\par}% <- v1.4
    \else
      \@contpre%
      {\@contnfont #1\@contdelim}\@memtempa
      {\ifdim\wd\@tempboxa<\linewidth
         \@contcshortstyle\else \@contcstyle\fi%    <- v1.4
       \@conttfont #2\@contfinal\par}%
    \fi
  \fi
  \@contpost
  \if@contcw
    \par
    }%    end of the parbox
  \fi
  \vskip\belowcaptionskip}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{xindy and theindex}
%
% \begin{macro}{\ifm@mxindy}
% \begin{macro}{\m@mxindytrue}
% \begin{macro}{\m@mxindyfalse}
% \begin{macro}{\xindyindex}
% \begin{macro}{\@@wrspindexhyp}
% Use \cs{xindyindex} when you are going to use the xindy program rather
% than makeindex. hyperrefed entries won't work with xindy.
% The code was supplied by Frederic 
% Connes\footnote{\texttt{frederic@connes.org}} in an email to me on 
% 2006/01/08.
%    \begin{macrocode}
%%% Use the declaration \xindyindex if you will be using the xindy program
%%% instead of MakeIndex to process index entries. As yet, xindy will not
%%% hyperref any entries.
%%% (Code supplied by Frederic Connes)
\newif\ifm@mxindy
\m@mxindyfalse
\newcommand*{\xindyindex}{\m@mxindytrue}
\def\@@wrspindexhyp#1|#2|#3\\{%
  \ifshowindexmark\@showidx{#1}\fi
  \ifx\\#2\\%
    \protected@write\@auxout{}%
      {\string\@@wrindexm@m{\@idxfile}%
        \ifm@mxindy{#1}\else{#1|hyperspindexpage(\thepage)}\fi
      {\@nameuse{the\@sptheidx}}}%
  \else
    \def\Hy@temp@A{#2}%
    \ifx\Hy@temp@A\HyInd@ParenLeft
      \protected@write\@auxout{}%
        {\string\@@wrindexm@m{\@idxfile}%
         \ifm@mxindy{#1|#2}\else{#1|#2hyperspindexpage(\thepage)}\fi
      {\@nameuse{the\@sptheidx}}}%
    \else
      \protected@write\@auxout{}%
        {\string\@@wrindexm@m{\@idxfile}{#1|#2}%
      {\@nameuse{the\@sptheidx}}}%
    \fi
  \fi
  \endgroup
  \@esphack}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{environment}{theindex}
% Problem noted by Erik Quaeghebeur\footnote{\texttt{equaeghe@nospammail.net}}
% in a CTT posting \textit{memoir, openleft and \cs{clear*page} commands} on
% 2006/01/04 that an index did not start on a verso page when the 
% \textsf{openleft} option is in effect.
% On 2006/12/06 Lars Madsen asked for an `indexpagestyle'. It's easier
% to add it here than under version 4.6.
%    \begin{macrocode}
\renewenvironment{theindex}{%
  \clearforchapter%             <- added
  \if@twocolumn
    \@restonecolfalse
  \else
    \@restonecoltrue
  \fi
  \ifonecolindex
    \onecolumn
    \chapter*{\indexname}
    \preindexhook
  \else
    \setlength{\columnseprule}{\indexrule}%
    \setlength{\columnsep}{\indexcolsep}%
    \twocolumn[\@makeschapterhead{\indexname}
               \preindexhook]%
  \fi
  \indexmark
  \ifnoindexintoc\else
    \phantomsection
    \addcontentsline{toc}{chapter}{\indexname}
  \fi
%%%  \thispagestyle{chapter}\parindent\z@ % <- changed in v4.6
  \thispagestyle{indextitlepagestyle}\parindent\z@
  \parskip\z@ \@plus .3\p@\relax
  \let\item\@idxitem}%
  {\if@restonecol\onecolumn\else\twocolumn\fi}

%    \end{macrocode}
% \end{environment}
%
% \subsection{Verse numbering}
%
% \begin{macro}{\verselinebreak}
% Aaron Rendahl (\texttt{arendahl@stat.umn.edu}) provided the \cs{ignorespaces}
% fix for an extra indent when using \verb?\\>[0pt]? to end a verse line.
% \changes{v4.4}{2006/01/14}{Added \cs{ignorespaces} to \cs{verselinebreak}}
%    \begin{macrocode}
%%% \ignorespaces fix by Aaron Rendahl to remove extraneous space 
%%% with \\>[0pt] ending a verse line
\renewcommand*{\verselinebreak}[1][\z@]{\newline\hspace*{#1}\ignorespaces}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\verselinenumbersright}
% \begin{macro}{\verselinenumbersleft}
% \begin{macro}{\@vslnumright}
% \begin{macro}{\@vslnumleft}
% \begin{macro}{\@vscentercr}
% New declarations for setting verse line numbers at the right (default)
% or at the left.
% \changes{v4.4}{2006/01/15}{Added code for switching verse line numbers left/right}
%     \begin{macrocode}
%%% Use the \verselinenumbersleft declaration to set verse line numbers
%%% at the left. To later return to the default of setting them at the right
%%% use the \verselinenumbersright declaration.
\newcommand*{\verselinenumbersright}{\def\@vstypelinenum{\@vslnumright}}
\newcommand*{\verselinenumbersleft}{\def\@vstypelinenum{\@vslnumleft}}
\verselinenumbersright

\newcommand*{\@vslnumright}{%
  \hfill\rlap{\kern\rightskip\kern\rightmargin%
              \vlvnumfont\getthelinenumber{poemline}}}
\newcommand*{\@vslnumleft}{%
  \hfill\rlap{\kern-\textwidth\kern-\rightskip%
              \vlvnumfont\getthelinenumber{poemline}}}
\renewcommand*{\@vscentercr}{%
  \ifhmode \unskip\else \@nolnerr\fi
  \@vstypelinenum%
  \@vsifgt{\verselinebreak}{%
    \incr@vsline
    \par\@ifstar{\nobreak\@vsxcentercr}{%
      \@vsifbang{\@ifnextchar[ {\@vsicentercr}{}}{\@vsxcentercr}}}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsection{Wrapped verbatims}
%
% \begin{macro}{\raggedwrap}
% \begin{macro}{\wrappingon}
% Paul (\texttt{paulaugust2003@yahoo.com}) found that wrapped verbatims in
% a list environment (e.g. \texttt{itemize}) were not indented although
% regular verbatims were.
%
% \cs{raggedwrap} is a variation on \cs{raggedright} (\cs{leftskip} is set 
% to \cs{@totalleftmargin} instead of 0pt). Hopefully this fix will not 
% mess up any existing wrapped verbatims.
% \changes{v4.4}{2006/01/16}{Fixed surprise in wrapped verbatims}
%    \begin{macrocode}
%%% Fix for wrapped verbatims in list environments.
\newcommand*{\raggedwrap}{%
  \@rightskip\@flushglue
  \rightskip\@rightskip
  \leftskip\@totalleftmargin
  \parindent\ragrparindent}
\renewcommand*{\wrappingon}{%
  \def\@xobeysp{~\discretionary{\verbatimbreakchar}%
    {\kern\verbatimindent}{}}%
  \def\wrapright{\raggedwrap}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Page after a Part heading}
% 
% \begin{macro}{\partblankpage}
% \begin{macro}{\nopartblankpage}
% \begin{macro}{\ifm@mnopartnewpage}
% \begin{macro}{\m@mnopartnewpagetrue}
% \begin{macro}{\m@mnopartnewpagefalse}
% \begin{macro}{\@endpart}
%  Code to prevent a \cs{part} outputting a blank page. Provided by
% Frederic Connes.
% \changes{v4.4}{2006/01/15}{Added controls for \cs{part} blank pages}
%    \begin{macrocode}
%%% Use the \nopartblankpage declaration if you don't want a blank
%%% page after a Part title page. The default is set by \partblankpage.
\newif\ifm@mnopartnewpage
\newcommand*{\partblankpage}{\m@mnopartnewpagefalse}
\newcommand*{\nopartblankpage}{\m@mnopartnewpagetrue}

\renewcommand*{\@endpart}{\afterpartskip
  \ifm@mnopartnewpage
  \else
    \if@twoside
      \if@openright
        \null
        \thispagestyle{afterpart}%
        \newpage
      \fi
    \fi
  \fi
  \if@tempswa
    \twocolumn
  \fi}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\partnumberline}
% Let's make sure that there is a space between the part number and 
% title in the ToC.
% \changes{v4.4}{2006/01/16}{Added space after part number in ToC}
%    \begin{macrocode}
\renewcommand{\partnumberline}[1]{%
  \hb@xt@\@tempdima{%
    \cftpartname\@cftbsnum #1\@cftasnum\hfil}\@cftasnumb\space}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ps@afterpart}
% \begin{macro}{\ps@book}
% \begin{macro}{\ps@afterbook}
% Some new page styles for the page after a Part page, and a Book and 
% its following page.
% \begin{macrocode}
\aliaspagestyle{afterpart}{empty}
\aliaspagestyle{book}{empty}
\aliaspagestyle{afterbook}{empty}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsection{Book document division}
%
%    Frederic Connes tells me that in French typography there is often
% a sectional division above Part. He called this \cs{superpart} but
% I think that \cs{book} more closely resembles the English usage.
% He provided code for implementing his \cs{superpart}. (I have just
% checked and the Chicago Manual of Style does have an example of
% a Book, Part, Chapter, Subsection hierarchy).
%
% \begin{macro}{\book}
% \begin{macro}{\book*}
% Here we go with a \cs{book} division.
% \changes{v4.4}{2006/01/15}{Added a \cs{book} division with all that that entails}
%    \begin{macrocode}
%%% The \book (and \book*) document division is one level higher than
%%% a \cs{part}. It has the same kind of controls over its appearance in
%%% the body of the document and in the ToC as \part has.
\newcommand*{\book}{%
  \@setupbook
  \secdef\@book\@sbook}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\bookmark}
% \begin{macro}{\c@book}
% \begin{macro}{\thebook}
% \begin{macro}{\theHbook}
% \begin{macro}{\toclevel@book}
% Start of the setting up. \cs{theHbook} and \cs{toclevel@book} are for 
% \Lpack{hyperref}
%    \begin{macrocode}
\newcommand*{\bookmark}[1]{}
\newcounter{book} \setcounter{book}{0}
\renewcommand*{\thebook}{\Roman{book}}
\newcommand*{\theHbook}{\arabic{book}}
\newcommand*{\toclevel@book}{-2}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\beforebookskip}
% \begin{macro}{\midbookskip}
% \begin{macro}{\afterbookskip}
%    \begin{macrocode}
\newcommand*{\beforebookskip}{\null\vfil}
\newcommand*{\midbookskip}{\par \vskip 2\baselineskip}
\newcommand*{\afterbookskip}{\vfil\newpage}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@setupbook}
% Start off the book page.
%    \begin{macrocode}
\newcommand*{\@setupbook}{%
  \if@openright
    \cleardoublepage
  \else
    \clearpage
  \fi
  \thispagestyle{book}%
  \if@twocolumn
    \@tempswatrue
  \else
    \@tempswafalse
  \fi
  \beforebookskip}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\bookname}
% \begin{macro}{\booknamefont}
% \begin{macro}{\booknumfont}
% \begin{macro}{\booktitlefont}
% Specify the typesetting styles.
%    \begin{macrocode}
\newcommand*{\bookname}{Book}
\newcommand*{\booknamefont}{\normalfont\huge\bfseries}
\newcommand*{\booknumfont}{\normalfont\huge\bfseries}
\newcommand*{\booktitlefont}{\normalfont\Huge\bfseries}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\printbookname}
% \begin{macro}{\booknamenum}
% \begin{macro}{\printbooknum}
% \begin{macro}{\printbooktitle}
%    \begin{macrocode}
\newcommand*{\printbookname}{\booknamefont \bookname}
\newcommand*{\booknamenum}{\space}
\newcommand*{\printbooknum}{\booknumfont \thebook}
\newcommand{\printbooktitle}[1]{\booktitlefont #1}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\membookinfo}
% \begin{macro}{\membookstarinfo}
%    \begin{macrocode}
\newcommand*{\membookinfo}[3]{}
\newcommand*{\membookstarinfo}[1]{}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@book}
% \begin{macro}{\@sbook}
%    \begin{macrocode}
\long\def\@book[#1]#2{%
  \M@gettitle{#1}%
  \phantomsection
  \ifnum\c@secnumdepth > -3\relax
    \refstepcounter{book}%
    \addcontentsline{toc}{book}%
      {\protect\booknumberline{\thebook}#1}%
    \membookinfo{\thebook}{#1}{#2}%
  \else
    \addcontentsline{toc}{book}{#1}%
    \membookinfo{}{#1}{#2}%
  \fi
  \bookmark{#1}%
  {\centering
   \interlinepenalty \@M
   \normalfont
   \ifnum \c@secnumdepth >-3\relax
     \printbookname \booknamenum \printbooknum
     \midbookskip
   \fi
   \printbooktitle{#2}\par}%
  \@endbook}

\def\@sbook#1{%
  \M@gettitle{#1}%
  \phantomsection
  \membookstarinfo{#1}%
  {\centering
   \interlinepenalty \@M
   \normalfont
   \printbooktitle{#1}\par}%
  \@endbook}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\bookblankpage}
% \begin{macro}{\nobookblankpage}
% \begin{macro}{\ifm@mnobooknewpage}
% \begin{macro}{\m@mnobooknewpagetrue}
% \begin{macro}{\m@mnobooknewpagefalse}
% \begin{macro}{\@endbook}
%  Code to prevent a \cs{book} outputting a blank page. Provided by
% Frederic Connes.
% \changes{v4.4}{2006/01/15}{Added controls for \cs{book} blank pages}
%    \begin{macrocode}
%%% Use the \nobookblankpage declaration if you don't want a blank
%%% page after a Book title page. The default is set by \bookblankpage.
\newif\ifm@mnobooknewpage
\newcommand*{\bookblankpage}{\m@mnobooknewpagefalse}
\newcommand*{\nobookblankpage}{\m@mnobooknewpagetrue}

\newcommand*{\@endbook}{\afterbookskip
  \ifm@mnobooknewpage
  \else
    \if@twoside
      \if@openright
        \null
        \thispagestyle{afterbook}%
        \newpage
      \fi
    \fi
  \fi
  \if@tempswa
    \twocolumn
  \fi}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\cftbookname}
% \begin{macro}{\l@book}
% \begin{macro}{\booknumberline}
% Now for the ToC entry.
%    \begin{macrocode}
\newcommand*{\cftbookname}{}
\newcommand*{\l@book}[2]{%
  \ifnum\c@tocdepth >-3\relax
    \addpenalty{-\@highpenalty}%
    \addvspace{\cftbeforebookskip}%
    \begingroup
      {\leftskip \cftbookindent\relax
       \rightskip \@tocrmarg
       \parfillskip -\rightskip
       \parindent \cftbookindent\relax\@afterindenttrue
       \interlinepenalty\@M
       \leavevmode
       \settowidth{\@tempdima}{\cftbookfont\cftbookname}%
       \addtolength{\@tempdima}{\cftbooknumwidth}%
       \let\@cftbsnum \cftbookpresnum
       \let\@cftasnum \cftbookaftersnum
       \let\@cftasnumb \cftbookaftersnumb
       \advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
       {\cftbookfont #1}%
       \cftbookfillnum{#2}}
      \nobreak
      \global\@nobreaktrue
      \everypar{\global\@nobreakfalse\everypar{}}%
    \endgroup
  \fi}
\newcommand{\booknumberline}[1]{%
  \hb@xt@\@tempdima{%
    \cftbookname\@cftbsnum #1\@cftasnum\hfil}\@cftasnumb\space}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\cftbeforebookskip}
% \begin{macro}{\cftbookindent}
% \begin{macro}{\cftbooknumwidth}
% \begin{macro}{\cftbookfont}
% \begin{macro}{\cftbookpresnum}
% \begin{macro}{\cftbookaftersnum}
% \begin{macro}{\cftbookaftersnumb}
% \begin{macro}{\cftbookleader}
% \begin{macro}{\cftbookdotsep}
% \begin{macro}{\cftbookpagefont}
% \begin{macro}{\cftbookafterpnum}
% \begin{macro}{\cftbookfillnum}
%    \begin{macrocode}
%  ToC layout parameters.
\newlength{\cftbeforebookskip}
  \setlength{\cftbeforebookskip}{2.25em \@plus\p@}
\newdimen\cftbookindent
  \setlength{\cftbookindent}{0em}
\newdimen\cftbooknumwidth
  \setlength{\cftbooknumwidth}{1.5em}
\newcommand*{\cftbookfont}{\large\bfseries}
\newcommand*{\cftbookpresnum}{}
\newcommand*{\cftbookaftersnum}{}
\newcommand*{\cftbookaftersnumb}{}
\newcommand*{\cftbookleader}{%
  \large\bfseries\cftdotfill{\cftbookdotsep}}
\newcommand*{\cftbookdotsep}{\cftnodots}
\newcommand*{\cftbookpagefont}{\large\bfseries}
\newcommand{\cftbookafterpnum}{}
\newcommand{\cftbookfillnum}[1]{%
  {\cftbookleader}%
  {\hb@xt@\@pnumwidth{\hss {\cftbookpagefont #1}}}%
  \cftbookafterpnum\par}

%    \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}{\@setclcnt}
% Add the book to \cs{@setclcnt}
%    \begin{macrocode}
\renewcommand*{\@setclcnt}[2]{%
  \@tempswafalse
  \nametest{#1}{none}%
  \ifsamename
    \setcounter{#2}{-10}%
    \@tempswatrue
  \fi
  \nametest{#1}{book}%
  \ifsamename
    \setcounter{#2}{-2}%
    \@tempswatrue
  \fi
  \nametest{#1}{part}%
  \ifsamename
    \setcounter{#2}{-1}%
    \@tempswatrue
  \fi
  \nametest{#1}{chapter}%
  \ifsamename
    \setcounter{#2}{0}%
    \@tempswatrue
  \fi
  \nametest{#1}{section}%
  \ifsamename
    \setcounter{#2}{1}%
    \@tempswatrue
  \fi
  \nametest{#1}{subsection}%
  \ifsamename
    \setcounter{#2}{2}%
    \@tempswatrue
  \fi
  \nametest{#1}{subsubsection}%
  \ifsamename
    \setcounter{#2}{3}%
    \@tempswatrue
  \fi
  \nametest{#1}{paragraph}%
  \ifsamename
    \setcounter{#2}{4}%
    \@tempswatrue
  \fi
  \nametest{#1}{subparagraph}%
  \ifsamename
    \setcounter{#2}{5}%
    \@tempswatrue
  \fi
  \nametest{#1}{all}%
  \ifsamename
    \setcounter{#2}{50}%
    \@tempswatrue
  \fi
  \if@tempswa\else
    \@memerror{%
      Unknown document division name (#1)
    }{%
     I'll ignore it.
     Type \space <return> and I'll continue.\MessageBreak
     If you haven't mistyped the name then use
     \protect\setcounter\space instead.}%
  \fi}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\settocdepth}
% Add \cs{book} to ToC depth setting.
%    \begin{macrocode}
\renewcommand*{\settocdepth}[1]{%
  \@tempswafalse
  \nametest{#1}{none}%
  \ifsamename
    \addtocontents{toc}{\changetocdepth{-10}}%
    \@tempswatrue
  \fi
  \nametest{#1}{book}%
  \ifsamename
    \addtocontents{toc}{\changetocdepth{-2}}%
    \@tempswatrue
  \fi
  \nametest{#1}{part}%
  \ifsamename
    \addtocontents{toc}{\changetocdepth{-1}}%
    \@tempswatrue
  \fi
  \nametest{#1}{chapter}%
  \ifsamename
    \addtocontents{toc}{\changetocdepth{0}}%
    \@tempswatrue
  \fi
  \nametest{#1}{section}%
  \ifsamename
    \addtocontents{toc}{\changetocdepth{1}}%
    \@tempswatrue
  \fi
  \nametest{#1}{subsection}%
  \ifsamename
    \addtocontents{toc}{\changetocdepth{2}}%
    \@tempswatrue
  \fi
  \nametest{#1}{subsubsection}%
  \ifsamename
    \addtocontents{toc}{\changetocdepth{3}}%
    \@tempswatrue
  \fi
  \nametest{#1}{paragraph}%
  \ifsamename
    \addtocontents{toc}{\changetocdepth{4}}%
    \@tempswatrue
  \fi
  \nametest{#1}{subparagraph}%
  \ifsamename
    \addtocontents{toc}{\changetocdepth{5}}%
    \@tempswatrue
  \fi
  \nametest{#1}{all}%
  \ifsamename
    \addtocontents{toc}{\changetocdepth{50}}%
    \@tempswatrue
  \fi
  \if@tempswa
%    \end{macrocode}
% The next bit is from Heiko Oberdiek (CTT {\itshape Re: Memoir, \verb?\settocdepth?
% and pdflatex \verb?=>? problem with PDF bookmarks}, 2006/07/21) as 
% \Lpack{hyperref} needs \verb?\tocdepth? set in the body not just in the ToC.
%    \begin{macrocode}
    \@ifundefined{toclevel@#1}{%
      @memwarn{Unknown toclevel for #1}%
    }{%
      \setcounter{tocdepth}{\@nameuse{toclevel@#1}}%
    }
  \else
    \@memerror{%
      Unknown document division name (#1)
    }{%
     I'll ignore it.
     Type \space <return> and I'll continue.}%
  \fi}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\toclevel@none}
% \begin{macro}{\toclevel@all}
% Couple of extras for hypperef to cater for \texttt{all} and \texttt{none}
% as `division levels'.
%    \begin{macrocode}
\newcommand*{\toclevel@none}{-10}
\newcommand*{\toclevel@all}{50}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \section{Mempatch version 4.5}
%
%    \begin{macrocode}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Version 4.5 (2006/02/11 and later)

%    \end{macrocode}
%
% \subsection{Odds and ends}
%
% \begin{macro}{\DisemulatePackage}
% Undoes an \cs{EmulatedPackage}
%    \begin{macrocode}
%%% \DisemulatePackage{pack} undoes a previous \EmulatedPackage{pack}.
%%% For example \DisemulatePackage{index} if you need to use the index package.
\providecommand*{\DisemulatePackage}[1]{%
  \@namelet{ver@#1.\@pkgextension}\relax}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@nameedef}
% Add a missing \verb?%? (noticed by Lars Madsen).
%    \begin{macrocode}
\renewcommand{\@nameedef}[1]{%
  \expandafter\protected@edef\csname #1\endcsname}


%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@smemmain}
% Change the timing of \cs{clear[double]page}. Problem noted by David Wilson
% \url{David.Wilson@murdoch.edu.au} in email dated 2006/01/29. It now matches 
% the other \cs{...matter} macros.
%    \begin{macrocode}
%%% change timing of \@mainmattertrue in \@smemmain
\renewcommand*{\@smemmain}{%
  \ifartopt
    \clearpage
  \else
    \cleardoublepage
    \counterwithin{figure}{chapter}%
    \counterwithin{table}{chapter}%
  \fi
  \@mainmattertrue
  \setcounter{secnumdepth}{\value{maxsecnumdepth}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@footnotetext}
% This was fixed in v1.618, except that I unfixed it later in the class file.
% This really should be done in a new class release, but it's not time for
% that yet.
%    \begin{macrocode}
\renewcommand{\@footnotetext}[1]{%
  \reset@font\m@mold@footnotetext{#1}\m@mmf@prepare}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ifm@mpn@new@chap}
% \begin{macro}{\m@mpn@new@chapfalse}
% \begin{macro}{\m@mpn@new@chaptrue}
% \begin{macro}{\ifm@mpn@new@schap}
% \begin{macro}{\m@mpn@new@schapfalse}
% \begin{macro}{\m@mpn@new@schaptrue}
% These are for supporting the page/end notes, flagging that a 
% \verb?\chapter(*)? has been called.
%    \begin{macrocode}
\newif\ifm@mpn@new@chap
  \m@mpn@new@chapfalse
\newif\ifm@mpn@new@schap
  \m@mpn@new@schapfalse

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@chapter}
% \begin{macro}{\f@rbdy}
% Fix from Lars Madsen for mistiming \verb?\chaptermark? in articles.
% Also adding \verb?\m@m@new@chap? flag, and setting \verb?\f@rbdy?
% to the chapter title for the body of the text.
%    \begin{macrocode}
%%% Fix for mistiming of \chaptermark with article option
\def\@chapter[#1]#2{%
  \m@mpn@new@chaptrue
  \def\f@rbdy{#2}%
  \ifx\ch@pt@c\@empty % no optional args
    \def\f@rtoc{#2}%
    \def\f@rhdr{#2}%
  \else                  % at least one opt arg
    \let\f@rtoc\ch@pt@c
    \ifx\@empty#1\@empty
      \let\f@rhdr\ch@pt@c
    \else
      \def\f@rhdr{#1}%
    \fi
  \fi
  \m@m@Andfalse
  \ifnum \c@secnumdepth >\m@ne
    \if@mainmatter
      \m@m@Andtrue
    \fi
  \fi
  \ifm@m@And
    \refstepcounter{chapter}%
  \fi
  \ifartopt
    \@makechapterhead{#2}%
    \@afterheading
    \chaptermark{\f@rhdr}%  
  \else
    \chaptermark{\f@rhdr}%  
    \insertchapterspace
    \if@twocolumn
      \@topnewpage[\@makechapterhead{#2}]%
    \else
      \@makechapterhead{#2}%
    \fi
    \@afterheading
  \fi
  \ifm@m@And
    \ifanappendix
      \addcontentsline{toc}{appendix}{%
        \protect\chapternumberline{\thechapter}\f@rtoc}%
      \memappchapinfo{\thechapter}{\f@rtoc}{\f@rhdr}{#2}%
    \else
      \addcontentsline{toc}{chapter}{%
        \protect\chapternumberline{\thechapter}\f@rtoc}%
      \memchapinfo{\thechapter}{\f@rtoc}{\f@rhdr}{#2}%
    \fi
  \else
    \addcontentsline{toc}{chapter}{\f@rtoc}%
    \ifanappendix
      \memappchapinfo{}{\f@rtoc}{\f@rhdr}{#2}%
    \else
      \memchapinfo{}{\f@rtoc}{\f@rhdr}{#2}%
    \fi
  \fi
  \ifheadnameref\M@gettitle{\f@rhdr}\else\M@gettitle{\f@rtoc}\fi}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@schapter}
% \begin{macro}{\@m@mschapter}
% Make the same page/end note extensions to \verb?\@schapter?.
%    \begin{macrocode}
\renewcommand{\@schapter}[1]{%
  \m@mpn@new@schaptrue
  \def\f@rbdy{#1}%
  \ifartopt
    \@makeschapterhead{#1}%
  \else
    \if@twocolumn
      \@topnewpage[\@makeschapterhead{#1}]
    \else
      \@makeschapterhead{#1}%
    \fi
  \fi   
  \@afterheading}
\renewcommand{\@m@mschapter}[2][\@empty]{%
  \@schapter{#2}%
  \ifx \@empty#1
    \def\f@rhdr{#2}%
  \else
    \def\f@rhdr{#1}%
    \setcounter{secnumdepth}{-10}%
    \chaptermark{#1}%
    \setcounter{secnumdepth}{\value{maxsecnumdepth}}%
  \fi
  \ifanappendix
    \memappchapstarinfo{\f@rhdr}{#2}%
  \else
    \memchapstarinfo{\f@rhdr}{#2}%
  \fi
  \ifheadnameref\M@gettitle{\f@rhdr}\else\M@gettitle{#2}\fi}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% 
%
% \subsection{Switch trims on/off}
%
% \begin{macro}{\showtrimson}
% \begin{macro}{\showtrimsoff}
% \begin{macro}{\mem@shipii}
% Switch trimming marks off and on. Requested by a James Hunt on CTT, 
% \emph{Are crop marks needed on every page?}, February 2006.
%    \begin{macrocode}
%%%% Use \showtrimsoff and \showtrimson to switch trim marks off and on.
%%%% If the showtrims option has not been used, these do nothing.
\newcommand*{\showtrimsoff}{\showtrimsfalse}
\newcommand*{\showtrimson}{\showtrimstrue}
\renewcommand*{\mem@shipii}{%
  \ifvoid\@cclv
    \mem@oldshipout\box\@cclv
  \else
    \ifshowtrims
      \mem@oldshipout\vbox{\trimmarks\unvbox\@cclv}%
    \else
      \mem@oldshipout\box\@cclv
    \fi
  \fi}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsection{Sidepars}
%
% \begin{macro}{\@sidepar}
% Sidepars have a nasty habit of moving up or down depending on whether 
% characters in the sidepar and the main text line have ascenders and/or 
% descenders. The length \verb?\sideparvshift? was provided to
% enable adjustments. This code from Dan Luecking 
% (CTT \textit{Re: sidepars drift up a point}, 2006/04/11) does a much
% better job,
% and the default for \verb?\sideparvshift? is now 0pt.
%    \begin{macrocode}
%%%% Fix to stop \sidepars moving up or down
\long\def\@sidepar[#1]#2{\@bsphack\strut\vadjust{%
  \checkoddpage
  \ifsideparswitch
    \ifreversesidepar
      \ifoddpage
        \oddpagefalse
      \else
        \oddpagetrue
      \fi
    \fi
  \else
    \oddpagetrue
    \ifreversesidepar
      \oddpagefalse
     \fi
  \fi
  \rlap{\kern-\parindent
    \if@twocolumn
      \if@firstcolumn%              put at left
        \kern -\marginparsep \kern -\marginparwidth
      \else%                        put at right
        \kern \columnwidth \kern \marginparsep
      \fi
    \else
      \ifoddpage%                   put at right
        \kern \textwidth \kern \marginparsep
      \else%                        put at left
        \kern -\marginparsep \kern -\marginparwidth
      \fi
    \fi
    \setbox0=\vtop to 0pt{%
      \begin{minipage}[t]{\marginparwidth}%
        \ifoddpage #2\else #1\fi%
      \end{minipage}%
      \vss}%
    \vtop to 0pt{\kern\sideparvshift%  default should be 0pt
      \kern-\dp\strutbox 
      \kern-\ht0 
      \box0 \vss}}}%
  \@esphack}
\setlength{\sideparvshift}{0pt}

%    \end{macrocode}
% \end{macro}
%
% \subsection{Anonymous divisions}
%
% \begin{macro}{\@fbreak}
% \begin{macro}{\@sfbreak}
% The \verb?\fancybreak? macros start a paragraph and so should end it as well,
% as noted by Dan Luecking (CTT \textit{Re: Appendix Package - adding 
% semicolon ``:'' after appendix letter in ToC}  2006/04/20).
% 
%    \begin{macrocode}
%%%% Ensure that fancybreaks close the paragraph they start
\renewcommand{\@fbreak}[1]{\par
  \penalty -100
  \noindent\parbox{\linewidth}{\centering #1}%
  \par
%  \penalty -20
  \@afterindentfalse
  \@afterheading}
\renewcommand{\@sfbreak}[1]{\par
  \penalty -100
  \noindent\parbox{\linewidth}{\centering #1}%
  \par
%  \penalty -20
  \@afterindenttrue
  \@afterheading}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Vertically center text}
%
% \begin{environment}{vplace}
% An environment which centers its body vertically. This is more robust
% than that proposed in the manual. The optional argument, which is a 
% (decimal) number, increases ($>1.0$) or decreases ($<1.0$) the space
% above the body with respect to that below.
%    \begin{macrocode}
%%%% \begin{vplace}[<num>] vertically centered stuff \end{vplace}
%%%% <num> adjusts above space wrt to below space
\newenvironment{vplace}[1][1]{%
  \par\vspace*{\stretch{#1}}%
}{%
  \vspace*{\stretch{1}}%
  \par}

%    \end{macrocode}
% \end{environment}
%
% \subsection{End notes}
%
%     This is from the \Lpack{pagenote} package, with minor amendments.
%
%    \begin{macrocode}
%%%%
%%%% Include the functions of the pagenote package
%%%%

%    \end{macrocode}
%
% \begin{macro}{\ifm@mpnpageopt}
% \begin{macro}{\ifm@mpncontopt}
% We need two flags for the options.
%    \begin{macrocode}
\newif\ifm@mpnpageopt
  \m@mpnpageoptfalse
\newif\ifm@mpncontopt
  \m@mpncontoptfalse
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\notepageref}
% \begin{macro}{\continuousnotenums}
% Let the user change the default options.
%    \begin{macrocode}
\newcommand*{\notepageref}{\m@mpnpageopttrue}
\newcommand*{\continuousnotenums}{%
  \counterwithout{pagenote}{chapter}
  \renewcommand{\thepagenote}{\arabic{pagenote}}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\c@pagenote}
% \begin{macro}{\thepagenote}
% We need a counter for the notes. Just how it is defined depends on the 
% \Lopt{continuous} option.
%    \begin{macrocode}
%\ifm@mpncontopt
%  \newcounter{pagenote}
%\else
  \newcounter{pagenote}[chapter]
%\fi
\renewcommand{\thepagenote}{\arabic{pagenote}}
\setcounter{pagenote}{0}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\ifmempagenotes}
% Need to check if notes are required.
%    \begin{macrocode}
\newif\ifmempagenotes
  \mempagenotesfalse

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\makepagenote}
% This sets up the note file. At the end it emasculates itself so it can only
% be used once.
%    \begin{macrocode}
\newcommand*{\makepagenote}{%
  \newwrite\@notefile
  \immediate\openout\@notefile=\jobname.ent
  \mempagenotestrue
%    \end{macrocode}
% \begin{macro}{\pagenote}
% Make sure that this has a useful definition.
%    \begin{macrocode}
  \def\pagenote{\@bsphack\begingroup
    \@sanitize
    \m@m@wrpnote}%
%    \end{macrocode}
% \end{macro}
% \begin{macrocode}
  \typeout{Writing note file \jobname.ent}%
  \let\makepagenote\@empty}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\immediate@protected@write}
% We might have to do some immediate writes. This is an immediate
% version of the kernel \verb?\protected@write?.
%    \begin{macrocode}
\newcommand{\immediate@protected@write}[3]{%
  \begingroup
  #2%
  \let\protect\@unexpandable@protect
  \edef\reserved@a{\immediate\write#1{#3}}%
  \reserved@a
  \endgroup
  \if@nobreak\ifvmode\nobreak\fi\fi}

%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\m@m@pnwrite}
% If the \Lopt{page} option is used we cannot use an immediate write
% because the page number is only known in the output routine.
%    \begin{macrocode}
\ifm@mpnpageopt
  \let\m@m@pnwrite\protected@write
\else
  \let\m@m@pnwrite\immediate@protected@write
\fi

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\pnchap}
% \begin{macro}{\pnschap}
% Redefine these for different subheadings in the notes list.
%    \begin{macrocode}
\newcommand*{\pnchap}{\f@rtoc}
\newcommand*{\pnschap}{\f@rbdy}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\m@m@wrpnote}
% This writes the note information to the note file. It first increments the
% note counter and calls \verb?\notenumintext? to handle its appearance in 
% the body text.
%    \begin{macrocode}
\newcommand{\m@m@wrpnote}[2][]{%
  \refstepcounter{pagenote}%
  \notenumintext{\thepagenote}%
%    \end{macrocode}
% Check if this is the first note in a division, and if so indicate this 
% in the file.
%    \begin{macrocode}
  \ifm@mpn@new@chap
    \global\m@mpn@new@chapfalse
    \addtonotes{\string\pagenotesubhead{\@chapapp}{\thechapter}{\pnchap}}%
  \fi
  \ifm@mpn@new@schap
    \global\m@mpn@new@schapfalse
    \addtonotes{\string\pagenotesubhead{\@chapapp}{}{\pnschap}}%
  \fi
%    \end{macrocode}
% Finally, write the entry.
%    \begin{macrocode}
  \m@m@pnwrite\@notefile{}
    {\string\noteentry{\thepagenote}{#1}{#2}{\thepage}}%
  \endgroup
  \@esphack}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\pagenote}
% The user command to generate a note. It is given substance 
% by \verb?\makepagenote?.
%    \begin{macrocode}
\def\pagenote{\@bsphack\begingroup \@sanitize\m@m@pagenote}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\m@m@pagenote}
%    \begin{macrocode}
\newcommand{\m@m@pagenote}[2][]{\endgroup\@esphack}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\pagetofootnote}
% \begin{macro}{\foottopagenote}
% \begin{macro}{\memsavefootnote}
% \begin{macro}{\memsavepagenote}
%  Let the user change pagenotes to footnotes, or vice-versa. In either
% case the optional argument is ignored.
%    \begin{macrocode}
\newcommand*{\pagetofootnote}{%
  \let\memsavepagenote\pagenote
  \renewcommand{\pagenote}[2][]{\footnote{##2}}}
\newcommand*{\foottopagenote}{%
  \let\memsavefootnote\footnote
  \renewcommand*{\footnote}[2][]{\pagenote{##2}}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\addtonotes}
% \verb?\addtonotes?\marg{text} puts \meta{text} into the notes file.
%    \begin{macrocode}
\newcommand{\addtonotes}[1]{%
  \ifmempagenotes
 \IfFileExists{\jobname.ent}{\m@m@pnwrite\@notefile{}{#1}}{\mempnofilewarn}%
\fi}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\notenumintext}
% \begin{macro}{\notenuminnotes}
% \verb?\notenumintext?\marg{notenum} typesets \meta{notenum} (in the body text).
%    \begin{macrocode}
\newcommand{\notenumintext}[1]{%
  \textsuperscript{#1}}
%    \end{macrocode}
% \verb?\notenuminnotes?\marg{notenum} typesets \meta{notenum} (as part of the note). 
%    \begin{macrocode}
\newcommand{\notenuminnotes}[1]{%
  {\normalfont #1.}\space}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\noteentry}
% \verb?\noteentry?\marg{notenum}\marg{id}\marg{pagenum}\marg{text}
% typesets a note.
%    \begin{macrocode}
\newcommand{\noteentry}[4]{%
  \prenoteinnotes 
  \noteidinnotes{#1}{#2}\pageinnotes{#4}\noteinnotes{#3}%
  \postnoteinnotes}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\idtextinnotes}
% \verb?\idtextinnotes?\marg{id text} typesets the note's \meta{id text}.
%    \begin{macrocode}
\newcommand{\idtextinnotes}[1]{%
  [#1]\space}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\noteidinnotes}
% \verb?\noteidinnotes?\marg{notenum}\marg{id} is used to typeset the note 
% identification (in the note listing). It is set so that it typesets the 
% \meta{id} if it is not empty, otherwise it sets the \meta{notenum}.
%    \begin{macrocode}
\newcommand{\noteidinnotes}[2]{%
  \@ifmtarg{#2}{%
    \notenuminnotes{#1}}{\idtextinnotes{#2}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\pageinnotes}
% \begin{macro}{\printpageinnotes}
% \verb?\pageinnotes?\meta{pagenum} calls 
% \verb?\printpageinnotes?\marg{pagenum} to typeset the originating 
% page number (in the note), but only if the \verb?\notepageref? declaration
% has been used (the page number is not trustworthy unless the
% \verb?\notepageref? has been used).
%    \begin{macrocode}
\newcommand{\pageinnotes}[1]{%
  \ifm@mpnpageopt \printpageinnotes{#1}\fi}
\newcommand*{\printpageinnotes}[1]{%
  (\pagerefname\ #1)\space}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\noteinnotes}
% \verb?\noteinnotes?\marg{text} is used to typeset the note's text 
% (in the note list).
%    \begin{macrocode}
\newcommand{\noteinnotes}[1]{#1}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\prenoteinnotes}
% \begin{macro}{\postnoteinnotes}
% These are called immediately before and after the note information is 
% typeset.
%    \begin{macrocode}
\newcommand{\prenoteinnotes}{\par\noindent}
\newcommand{\postnoteinnotes}{\par}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\notesname}
% \begin{macro}{\notedivision}
% Heading for note list.
%    \begin{macrocode}
\providecommand*{\notesname}{Notes}
\newcommand*{\notedivision}{\chapter{\notesname}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\printnotes}
% \begin{macro}{\printpagenotes*}
% User commands to print the note file.
%    \begin{macrocode}
\newcommand*{\printpagenotes}{\@ifstar{\@sprintpagenotes}{\@printpagenotes}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\mempnofilewarn}
% Warning when the notes file does not exist.
%    \begin{macrocode}
\newcommand*{\mempnofilewarn}{%
  \ClassWarning{memoir}{There is no .ent file}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@sprintpagenotes}
% Macro implementing \verb?\printpagenotes*?.
%    \begin{macrocode}
\newcommand*{\@sprintpagenotes}{%
  \ifmempagenotes
  \notedivision
\IfFileExists{\jobname.ent}{%
  \immediate\closeout\@notefile
  \input{\jobname.ent}%
  \immediate\openout\@notefile=\jobname.ent%
  }{%
  \mempnofilewarn
}%
\fi}
  
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@printpagenotes}
% Macro implementing \verb?\printpagenotes?.
%    \begin{macrocode}
\newcommand*{\@printpagenotes}{%
  \ifmempagenotes
    \notedivision
    \IfFileExists{\jobname.ent}{%
      \immediate\closeout\@notefile
      \input{\jobname.ent}%
     }{%
       \mempnofilewarn
     }
  \fi}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\pagenotesubhead}
% The section heading before each set of notes. \\
% \verb?\pagenotesubhead?\marg{chaptername}\marg{number}\marg{title}
%    \begin{macrocode}
\newcommand*{\pagenotesubhead}[3]{%
  \section*{#1 #2 #3}}

%    \end{macrocode}
% \end{macro}
%
% Prevent loading of the \Lpack{pagenote} package.
%    \begin{macrocode}
\EmulatedPackage{pagenote}

%    \end{macrocode}
%
% \subsection{ToC, LoF, etc lists}
%
% Lars Madsen suggested that macros like \verb?\cftfigurename? be added for use
% in the LoF, and so on.
%
% \begin{macro}{\@cftn@me}
% \begin{macro}{\numberline}
%    \begin{macrocode}
\newcommand*{\@cftn@me}{}
\renewcommand*{\numberline}[1]{%
  \hb@xt@\@tempdima{\@cftn@me\@cftbsnum #1\@cftasnum\hfil}\@cftasnumb}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\newlistentry}
% Extend \cs{newlistentry} to add \cs{cftXname}.
% \changes{v4.8}{2007/01/22}{Added missing backslash in \cs{newlistentry}}
%    \begin{macrocode}
\renewcommand*{\newlistentry}[4][\@empty]{%
  \@ifundefined{c@#2}{%  check & set the counter
    \ifx \@empty#1\relax
      \newcounter{#2}%  % added the backslash 2007/01/22 per J{\o}rgen Larsen
    \else
      \newcounter{#2}[#1]%
      \expandafter\edef\csname the#2\endcsname{%
    \expandafter\noexpand\csname the#1\endcsname.\noexpand\arabic{#2}}%
    \fi}{}
  \setcounter{#2}{0}
  \@namedef{l@#2}##1##2{%
    \ifnum \@nameuse{c@#3depth} > #4\relax
      \vskip \@nameuse{cftbefore#2skip}%
      {\leftskip \@nameuse{cft#2indent}\relax
       \rightskip \@tocrmarg
       \parfillskip -\rightskip
       \parindent \@nameuse{cft#2indent}\relax\@afterindenttrue
       \interlinepenalty\@M
       \leavevmode
       \settowidth{\@tempdima}{\@nameuse{cft#2font}\@nameuse{cft#2name}}%
       \addtolength{\@tempdima}{\@nameuse{cft#2numwidth}}%
\expandafter\let\expandafter\@cftbsnum\csname cft#2presnum\endcsname
\expandafter\let\expandafter\@cftasnum\csname cft#2aftersnum\endcsname
\expandafter\let\expandafter\@cftasnumb\csname cft#2aftersnumb\endcsname
\expandafter\let\expandafter\@cftn@me\csname cft#2name\endcsname
       \advance\leftskip\@tempdima \null\nobreak\hskip -\leftskip
       {\@nameuse{cft#2font}##1}\nobreak
       \@nameuse{cft#2fillnum}{##2}}
     \fi
  }% end of \l@#2
  \expandafter\newlength\csname cftbefore#2skip\endcsname
    \setlength{\@nameuse{cftbefore#2skip}}{\z@ \@plus .2\p@}
  \expandafter\newlength\csname cft#2indent\endcsname
  \expandafter\newlength\csname cft#2numwidth\endcsname
  \ifcase #4\relax%         0 (level 1)
    \setlength{\@nameuse{cft#2indent}}{0em}
    \setlength{\@nameuse{cft#2numwidth}}{2.3em}
  \or%                      1 (level 2)
    \setlength{\@nameuse{cft#2indent}}{2.3em}
    \setlength{\@nameuse{cft#2numwidth}}{3.2em}
  \or%                      2 (level 3)
    \setlength{\@nameuse{cft#2indent}}{5.5em}
    \setlength{\@nameuse{cft#2numwidth}}{4.1em}
  \or%                      3 (level 4)
    \setlength{\@nameuse{cft#2indent}}{8.5em}
    \setlength{\@nameuse{cft#2numwidth}}{5.0em}
  \else%                    anything else
    \setlength{\@nameuse{cft#2indent}}{10.5em}
    \setlength{\@nameuse{cft#2numwidth}}{6.0em}
  \fi
  \@namedef{cft#2font}{\normalfont}
  \@namedef{cft#2name}{}
  \@namedef{cft#2presnum}{}  
  \@namedef{cft#2aftersnum}{}  
  \@namedef{cft#2aftersnumb}{}  
  \@namedef{cft#2dotsep}{\cftdotsep}
  \@namedef{cft#2leader}{\normalfont\cftdotfill{\@nameuse{cft#2dotsep}}}
  \@namedef{cft#2pagefont}{\normalfont}
  \@namedef{cft#2afterpnum}{}
  \@namedef{cft#2toclevel@#2}{#4}
  \@namedef{cft#2fillnum}##1{%
    {\@nameuse{cft#2leader}}\nobreak
    \hb@xt@\@pnumwidth{%
      \hfil\@nameuse{cft#2pagefont}##1}\@nameuse{cft#2afterpnum}\par}
}% end of \newlistentry

%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
\let\cftbeforesectionskip\relax
\let\cftsectionindent\relax
\let\cftsectionnumwidth\relax
\newlistentry[chapter]{section}{toc}{0}
  \cftsetindents{section}{1.5em}{2.3em}
\let\cftbeforesubsectionskip\relax
\let\cftsubsectionindent\relax
\let\cftsubsectionnumwidth\relax
\newlistentry[section]{subsection}{toc}{1}
  \cftsetindents{subsection}{3.8em}{3.2em}
\let\cftbeforesubsubsectionskip\relax
\let\cftsubsubsectionindent\relax
\let\cftsubsubsectionnumwidth\relax
\newlistentry[subsection]{subsubsection}{toc}{2}
  \cftsetindents{subsubsection}{7.0em}{4.1em}
\let\cftbeforeparagraphskip\relax
\let\cftparagraphindent\relax
\let\cftparagraphnumwidth\relax
\newlistentry[subsubsection]{paragraph}{toc}{3}
  \cftsetindents{paragraph}{10.0em}{5.0em}
\let\cftbeforesubparagraphskip\relax
\let\cftsubparagraphindent\relax
\let\cftsubparagraphnumwidth\relax
\newlistentry[paragraph]{subparagraph}{toc}{4}
  \cftsetindents{subparagraph}{12.0em}{6.0em}

%    \end{macrocode}
%
%    \begin{macrocode}
\let\cftbeforefigureskip\relax
\let\cftfigureindent\relax
\let\cftfigurenumwidth\relax
\newlistentry[chapter]{figure}{lof}{0}
  \cftsetindents{figure}{0em}{2.3em}
\let\cftbeforetableskip\relax
\let\cfttableindent\relax
\let\cfttablenumwidth\relax
\newlistentry[chapter]{table}{lot}{0}
  \cftsetindents{table}{0em}{2.3em}

%    \end{macrocode}
%
% \subsection{Page layout}
%
% Morten H{\o}gholm suggested (2006/07/26) that instead of using TeX syntax I should use
% \verb?\setlength? when setting up the page layout so that the \Lpack{calc}
% package can be used, e.g. \\
% \verb?\settypeblocksize{41\baselineskip+\topskip}{310pt}{*}?
%
%
% \begin{macro}{\setrectanglesize}
%  The macro \cs{setrectanglesize}\marg{H}\marg{W}\marg{r}
% calculates the height and width of a rectangle given any two out of
% the three arguments. An unvalued argument is denoted by \texttt{*}.
%
% Table~\ref{tab:setrect} shows the argument combinations and the result
% for each combination.
% \begin{table}
% \centering
% \caption{Arguments and results for \cs{setrectanglesize}}\label{tab:setrect}
% \begin{tabular}{ccc|l} \hline
%  H & W & r & Result  \\ \hline
%  * & W & r & $H = rW$  \\{}
%  * & W & * & $H = W$   \\{}
%  * & * & r & ambiguous   \\{}
%  * & * & * & ambiguous   \\{}
%  H & W & r & $H, W$  \\
%  H & W & * & $H, W$   \\
%  H & * & r & $W = rH$  \\
%  H & * & * & $W = H$   \\
%  \hline
% \end{tabular}
% \end{table}
%
% The
% calculated height and width are stored in \cs{@tempdima} and \cs{@tempdimb}
% respectively. Both lengths are set to zero if there is an error.
%    \begin{macrocode}
\renewcommand*{\setrectanglesize}[3]{%
  \nametest{#1}{*}%
  \ifsamename                           % H = *
    \nametest{#2}{*}%
    \ifsamename                         % W = *
      \@memerror{%
        The combination of argument values is ambiguous.\MessageBreak
        The lengths will be set to zero}{\@ehd}
      \setlength{\@tempdima}{0pt}%
      \setlength{\@tempdimb}{0pt}%
    \else                               % W
      \nametest{#3}{*}%
      \ifsamename                       % r = *
        \setlength{\@tempdimb}{#2}%
        \setlength{\@tempdima}{\@tempdimb}%
      \else                             % r
        \setlength{\@tempdimb}{#2}
        \setlength{\@tempdima}{#3\@tempdimb}
      \fi
    \fi
  \else                                 % H
    \nametest{#2}{*}%
    \ifsamename                         % W = *
      \nametest{#3}{*}%
      \ifsamename                       % r = *
        \setlength{\@tempdima}{#1}%
        \setlength{\@tempdimb}{\@tempdima}%
      \else                             % r
        \setlength{\@tempdima}{#1}%
        \setlength{\@tempdimb}{#3\@tempdima}%
      \fi
    \else                               % W
      \setlength{\@tempdima}{#1}%
      \setlength{\@tempdimb}{#2}%
    \fi
  \fi
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\setfillsize}
% Consider a set of 4 lengths, $T$, $L$, $C$, and $R$, such that
% $T = L + C + R$, where $T$ is a fixed size and normally $C$ is also fixed.
% Also $L$ and $R$ may be specified independently of each other or as a
% ratio (i.e., $L = rR$ or $R = rL$).
%  The macro 
% \cs{setfillsize}\marg{T}\marg{C}\marg{L}\marg{R}\marg{r}
% maintains these constraints among the variables, where an unvalued 
% argument is denoted by \texttt{*}. 
%
% Table~\ref{tab:setfill} shows the argument combinations and the result
% for each combination.
% \begin{table}
% \centering
% \caption{Arguments and results for \cs{setfillsize}}\label{tab:setfill}
% \begin{tabular}{cccc|ll} \hline
%  C & L & R & r & Result                     &   \\ \hline
%  * & * & R & r & $L = rR$,                  & $C = T - L - R$ \\{}
%  * & * & R & * & $L = R$,                   & $C = T - L - R$ \\{}
%  * & * & * & r & ambigous                   & \\{}
%  * & * & * & * & ambiguous                  & \\{}
%  * & L & R & r & $L$, $R$,                  & $C = T - L - R$ \\{}
%  * & L & R & * & $L$, $R$,                  & $C = T - L - R$ \\{}
%  * & L & * & r & $R = rL$,                  & $C = T - L - R$ \\{}
%  * & L & * & * & $R = L$,                   & $C = T - L - R$ \\{}
%  C & * & R & r & $L = T - C - R$            & $C$ \\
%  C & * & R & * & $L = T - C - R$,           & $C$ \\
%  C & * & * & r & $L + R = T - C$, $R = rL$, & $C$ \\
%  C & * & * & * & $L + R = T - C$, $R = L$,  & $C$ \\
%  C & L & R & r & ambiguous                  & $C$ \\
%  C & L & R & * & ambiguous                  & $C$ \\ 
%  C & L & * & r & $R = T - C - L$,           & $C$ \\
%  C & L & * & * & $R = T - C - L$,           & $C$ \\
%  \hline
% \end{tabular}
% \end{table}
%
%
% The
% calculated values of \meta{C}, \meta{L} and \meta{R} are stored in 
% \cs{@tempdimc}, \cs{@tempdima} and \cs{@tempdimb} respectively. 
% If there is an error the lengths are set to zero.
%    \begin{macrocode}
\renewcommand*{\setfillsize}[5]{%
  \nametest{#2}{*}%
  \ifsamename                                % C = *
    \nametest{#3}{*}%
    \ifsamename                              % L = *
      \nametest{#4}{*}%
      \ifsamename                            % R = *
        \@memerror{%
          The combination of argument values is ambiguous.\MessageBreak
          The lengths will be set to zero}{\@ehd}
        \setlength{\@tempdima}{0pt}%
        \setlength{\@tempdimb}{0pt}%
        \setlength{\@tempdimc}{0pt}%
      \else                                  % R 
        \nametest{#5}{*}%
        \ifsamename                          % r = *
          \setlength{\@tempdimb}{#4}%
          \setlength{\@tempdima}{\@tempdimb}%
          \setlength{\@tempdimc}{#1}%
          \advance\@tempdimc -\@tempdima
          \advance\@tempdimc -\@tempdimb
        \else                                % r 
          \setlength{\@tempdimb}{#4}%
          \setlength{\@tempdima}{#5\@tempdimb}%
          \setlength{\@tempdimc}{#1}%
          \advance\@tempdimc -\@tempdima
          \advance\@tempdimc -\@tempdimb
        \fi
      \fi
    \else                                    % L 
      \nametest{#4}{*}%
      \ifsamename                            % R = *
        \nametest{#5}{*}%
        \ifsamename                          % r = *
          \setlength{\@tempdima}{#3}%
          \setlength{\@tempdimb}{\@tempdima}%
          \setlength{\@tempdimc}{#1}%
          \advance\@tempdimc -\@tempdima
          \advance\@tempdimc -\@tempdimb
        \else                                % r 
          \setlength{\@tempdima}{#3}%
          \setlength{\@tempdimb}{#5\@tempdima}%
          \setlength{\@tempdimc}{#1}%
          \advance\@tempdimc -\@tempdima
          \advance\@tempdimc -\@tempdimb
        \fi
      \else                                  % R
        \setlength{\@tempdima}{#3}%
        \setlength{\@tempdimb}{#4}%
        \setlength{\@tempdimc}{#1}%
        \advance\@tempdimc -\@tempdima
        \advance\@tempdimc -\@tempdimb
      \fi
    \fi
  \else                                      % C is valued
    \nametest{#3}{*}%
    \ifsamename                              % L = *
      \nametest{#4}{*}%
      \ifsamename                            % R = *
        \nametest{#5}{*}%
        \ifsamename                          % r = *
          \setlength{\@tempdimc}{#2}%
          \setlength{\@tempdima}{#1}%
          \advance\@tempdima -\@tempdimc
          \@tempdima = 0.5\@tempdima
          \@tempdimb = \@tempdima
        \else                                % r (CODE PERHAPS FIXED)
          \setlength{\@tempdimc}{#2}%
          \setlength{\@tempdimb}{#1}%        % T
          \advance\@tempdimb -\@tempdimc     % T - C
          \@tempdima = 1000sp
          \@tempdima = #5\@tempdima          % 1000r sp
          \advance\@tempdima by 1000sp       % 1000(1+r)sp
          \@tempcnta = \@tempdima            % 1000(1+r)
          \@tempdima = \@tempdimb            % T - C
          \divide\@tempdima by \@tempcnta    % (T-C)/1000(1+r) pts
          \@tempdima = 1000\@tempdima        % (T-C)/(1+r)  pts = L
          \advance\@tempdimb by -\@tempdima  % = R
        \fi
      \else                                  % R 
        \setlength{\@tempdimc}{#2}%
        \setlength{\@tempdimb}{#4}%
        \setlength{\@tempdima}{#1}%
        \advance\@tempdima -\@tempdimc
        \advance\@tempdima -\@tempdimb
      \fi
    \else                                    % L 
      \nametest{#4}{*}%
      \ifsamename                            % R = *
        \setlength{\@tempdimc}{#2}%
        \setlength{\@tempdima}{#3}%
        \setlength{\@tempdimb}{#1}%
        \advance\@tempdimb -\@tempdimc
        \advance\@tempdimb -\@tempdima
      \else                                  % R
        \@memerror{%
          The combination of argument values is ambiguous.\MessageBreak
          The lengths will be set to zero}{\@ehd}%
        \setlength{\@tempdima}{0pt}%
        \setlength{\@tempdimb}{0pt}%
        \setlength{\@tempdimc}{#2}%
      \fi
    \fi
  \fi}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@mclassicht}
% The classic adjustment of the \cs{textheight} to get an integral number 
% of lines (given an integral number of baselineskips returns a height
% giving one more line in the block).
%    \begin{macrocode}
\newcommand*{\m@mclassicht}{%
  \setlength{\@tempdima}{\textheight}%
  \divide\@tempdima \baselineskip
  \@tempcnta=\@tempdima
  \setlength{\textheight}{\@tempcnta\baselineskip}%
  \addtolength{\textheight}{\topskip}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@mlinesht}
% The adjustment of the \cs{textheight} to get an integral number 
% of lines (given an inegral number of baselineskips returns a height
% giving that number of lines).
%    \begin{macrocode}
\newcommand*{\m@mlinesht}{%
  \setlength{\@tempdima}{\textheight}%
  \advance\@tempdima -\baselineskip
  \divide\@tempdima \baselineskip
  \@tempcnta=\@tempdima
  \setlength{\textheight}{\@tempcnta\baselineskip}%
  \addtolength{\textheight}{\topskip}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@mnearestht}
% The adjustment of the \cs{textheight} to get an integral number 
% of lines with the calculated height being as the closest to the given 
% height. Algorithm supplied by Lars Madsen and Morten H{\o}lmgren on
% 2006/07/27.
%    \begin{macrocode}
\newcommand*{\m@mnearestht}{%
  \setlength{\@tempdima}{\textheight}%
  \advance\@tempdima -\topskip
  \advance\@tempdima 0.5\baselineskip
  \divide\@tempdima \baselineskip
  \@tempcnta=\@tempdima
  \setlength{\textheight}{\@tempcnta\baselineskip}%
  \addtolength{\textheight}{\topskip}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\checkthelayout}
% \cs{checkthelayout}\oarg{text} is the user level macro for
% checking the layout. The \meta{text} argument controls which
% algorithm should be used to calculate the \cs{textheight}.
%
%    \begin{macrocode}
\renewcommand*{\checkthelayout}[1][classic]{%
%    \end{macrocode}
% First check the dimensions are not negative.
%    \begin{macrocode}
  \@memnegtest{\trimedge}
  \@memnegtest{\trimtop}
  \@memznegtest{\stockwidth}
  \@memznegtest{\paperwidth}
  \@memznegtest{\textwidth}
  \@memznegtest{\spinemargin}
  \@memznegtest{\foremargin}
  \@memznegtest{\marginparsep}
  \@memznegtest{\marginparwidth}
  \@memznegtest{\stockheight}
  \@memznegtest{\paperheight}
  \@memznegtest{\textheight}
  \@memznegtest{\uppermargin}
  \@memznegtest{\lowermargin}
  \@memznegtest{\headheight}
  \@memznegtest{\headsep}
  \@memznegtest{\footskip}
%    \end{macrocode}
%
%    Carry on regardless. We may need to adjust the \cs{textheight} 
% to get an integral number of lines.
%    \begin{macrocode}
  \nametest{#1}{classic}%
  \ifsamename
    \m@mclassicht
  \else
    \nametest{#1}{lines}%
    \ifsamename
      \m@mlinesht
    \else
      \nametest{#1}{nearest}%
      \ifsamename
        \m@mnearestht
      \else
        \nametest{#1}{fixed}
        \ifsamename
        \else%                   not classic, lines, nearest, or fixed
          \@memerror{Optional argument is not one of:\MessageBreak
                     classic, fixed, lines, or nearest. \MessageBreak
                     I will assume the default}%
                    {\@ehc}%
        \fi
      \fi
    \fi
  \fi
  \setulmargins{\uppermargin}{*}{*}
%    \end{macrocode}
%
% Check that all the sums add up correctly, or
% at least to within a small (\cs{@tempdimb}) error.
%    \begin{macrocode}
  \@tempdimb = -1pt  
  \@tempdima=\stockwidth
  \advance\@tempdima -\trimedge
  \advance\@tempdima -\paperwidth
  \ifdim\@tempdima<\@tempdimb
    \@tempdima = -\@tempdima
    \@memerror{\protect\paperwidth\space and/or 
                        \protect\trimedge\space 
                        are too large for \protect\stockwidth\space
                        by \the\@tempdima}%
                       {\@ehd}
  \fi
  \@tempdima = \paperwidth
  \advance\@tempdima -\foremargin
  \advance\@tempdima -\textwidth
  \advance\@tempdima -\spinemargin
  \ifdim\@tempdima<\@tempdimb
    \@tempdima = -\@tempdima
    \@memerror{\protect\spinemargin\space and/or 
                        \protect\textwidth\space and/or
                        \protect\foremargin\space 
                        are too large for \protect\paperwidth\space
                        by \the\@tempdima}%
                       {\@ehd}
  \fi
  \@tempdima = \stockheight
  \advance\@tempdima -\trimtop
  \advance\@tempdima -\paperheight
  \ifdim\@tempdima<\@tempdimb
    \@tempdima = -\@tempdima
    \@memerror{\protect\paperheight\space and/or 
                        \protect\trimtop\space 
                        are too large for \protect\stockheight\space
                        by \the\@tempdima}%
                       {\@ehd}
  \fi
  \@tempdima = \paperheight
  \advance\@tempdima -\uppermargin
  \advance\@tempdima -\textheight
  \advance\@tempdima -\lowermargin
  \ifdim\@tempdima<\@tempdimb
    \@tempdima = -\@tempdima
    \@memerror{\protect\uppermargin\space and/or 
                        \protect\textheight\space and/or 
                        \protect\lowermargin\space 
                        are too large for \protect\paperheight\space
                        by \the\@tempdima}%
                       {\@ehd}
  \fi
  \@tempdima = \uppermargin
  \advance\@tempdima -\headheight
  \advance\@tempdima -\headsep
  \ifdim\@tempdima<\@tempdimb
    \@tempdima = -\@tempdima
    \@memerror{\protect\headheight\space and/or 
                        \protect\headsep\space 
                        are too large for \protect\uppermargin\space
                        by \the\@tempdima}%
                       {\@ehd}
  \fi
  \@tempdima = \lowermargin
  \advance\@tempdima -\footskip
  \ifdim\@tempdima<\z@
    \@tempdima = -\@tempdima
    \@memerror{\protect\footskip
                        is too large for \protect\lowermargin\space
                        by \the\@tempdima}%
                       {\@ehd}
  \fi
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\checkandfixthelayout}
% This macro checks and fixes the layout, and reports the result. It takes the
% same optional argument as \cs{checkthelayout}.
%    \begin{macrocode}
\renewcommand*{\checkandfixthelayout}[1][classic]{%
  \checkthelayout[#1]%
  \fixthelayout
  \typeoutlayout}

%    \end{macrocode}
% \end{macro}
%
% \subsection{Overlong lines}
%
% LaTeX provides \cs{fussy} and \cs{sloppy} to control the amount of slack
% in a line while trying to make justified lines. Their definitions are:
% \begin{verbatim}
% \def\fussy{%
%   \emergencystretch\z@
%   \tolerance 200%
%   \hfuzz .1\p@
%   \vfuzz\hfuzz}
% \def\sloppy{%
%   \tolerance 9999%
%   \emergencystretch 3em%
%   \hfuzz .5\p@
%   \vfuzz\hfuzz}
% \end{verbatim}
%
% \begin{macro}{\midsloppy}
% Somewhere between \cs{fussy} and \cs{sloppy}.
%    \begin{macrocode}
\newcommand*{\midsloppy}{%
  \tolerance 5000%
  \hbadness 4000%
  \emergencystretch 1.5em%
  \hfuzz .1\p@
  \vfuzz\hfuzz}
%    \end{macrocode}
% \end{macro}
%
% \begin{environment}{midsloppypar}
% A paragraph form of \cs{midsloppy}; equivalent to \verb?\par \midsloppy ... \par}?
%    \begin{macrocode}
\newenvironment{midsloppypar}{\par\midsloppy}{\par}

%    \end{macrocode}
% \end{environment}
%
% \subsection{Lists}
%
% \begin{macro}{\everylistparindent}
% \begin{macro}{\list}
% The kernel sets \cs{listparindent} to zero within a \cs{list}, where it
% can be overridden in \cs{lits}'s second argument. Here it is set to
% \cs{everyparlistindent} by default, which in turn is 0pt.
%    \begin{macrocode}
\newdimen\everylistparindent
  \everylistparindent \z@
\renewcommand*{\list}[2]{%
  \ifnum \@listdepth >5\relax
    \@toodeep
  \else
    \global\advance\@listdepth\@ne
  \fi
  \rightmargin\z@
  \listparindent\everylistparindent
  \itemindent\z@
  \csname @list\romannumeral\the\@listdepth\endcsname
  \def\@itemlabel{#1}%
  \let\makelabel\@mklab
  \@nmbrlistfalse
  #2\relax
  \@trivlist
  \parskip\parsep
  \parindent\listparindent
  \advance\linewidth -\rightmargin
  \advance\linewidth -\leftmargin
  \advance\@totalleftmargin \leftmargin
  \parshape \@ne \@totalleftmargin \linewidth
  \ignorespaces}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\defaultlists}
%  This is a simplification of memoir's original, and will apply to any font
% size.
%    \begin{macrocode}
\renewcommand*{\defaultlists}{%
  \setlength{\partopsep}{0.2\onelineskip \@plus 0.1\onelineskip 
                                         \@minus 0.1\onelineskip}%
  \parsepi = 0.3333\onelineskip \@plus 0.1667\onelineskip \@minus \p@
  \itemsepi = \parsepi
  \topsepi = 0.6667\onelineskip \@plus 0.3333\onelineskip 
                                \@minus 0.2\onelineskip
  \parsepii = 0.1667\onelineskip \@plus \p@ \@minus \p@
  \topsepii = \parsepi
  \topsepiii = \parsepii
  \everylistparindent \z@}
\defaultlists

%    \end{macrocode}
% \end{macro}
%
% These are the original versions of \cs{firmlist} and \cs{tightlist}.
% \begin{verbatim}
% \newcommand*{\firmlist}{%
%   \setlength{\itemsep}{0.5\itemsep}\setlength{\parskip}{0.5\parskip}}
%
% \newcommand*{\tightlist}{%
%   \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
% \end{verbatim}
%
% \begin{macro}{\firmlists}
% \begin{macro}{\firmlists*}
% \begin{macro}{\m@msfirmlists}
% \begin{macro}{\m@mfirmlists}
% These give approximately half the vertical spacing of the default lists, 
% with all spaces equal. The starred version allows slightly less space
% before and after the list when it is preceded by a blank line.
%
%    \begin{macrocode}
\newcommand*{\firmlists}{%
  \@ifstar{\m@msfirmlists}{\m@mfirmlists}}

\newcommand*{\m@msfirmlists}{
  \setlength{\partopsep}{\z@ \@plus \p@ \@minus \p@}%
  \parsepi = 0.1667\onelineskip \@plus 0.0833\onelineskip \@minus \p@
  \itemsepi = \parsepi
  \topsepi = \parsepi
  \parsepii = 0.0833\onelineskip \@plus \p@ \@minus \p@
  \topsepii = \parsepi
  \topsepiii = \parsepii
  \everylistparindent\z@}

\newcommand*{\m@mfirmlists}{
  \setlength{\partopsep}{0.1\onelineskip \@plus 0.05\onelineskip 
                                         \@minus 0.05\onelineskip}%
  \parsepi = 0.1667\onelineskip \@plus 0.0833\onelineskip \@minus \p@
  \itemsepi = \parsepi
  \topsepi = \parsepi
  \parsepii = 0.0833\onelineskip \@plus \p@ \@minus \p@
  \topsepii = \parsepi
  \topsepiii = \parsepii
  \everylistparindent\z@}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\tightlists}
% \begin{macro}{\tightlists*}
% \begin{macro}{\m@mstightlists}
% \begin{macro}{\m@mtightlists}
% These give zero vertical spacing of the default lists.
% The starred version allows slightly no extra space
% before and after the list when it is preceded by a blank line,
% whereas the unstarred version puts half a \cs{onelineskip}
% before and after.
%
%    \begin{macrocode}
\renewcommand*{\tightlists}{%
  \@ifstar{\m@mstightlists}{\m@mtightlists}}

\newcommand*{\m@mstightlists}{%
  \setlength{\partopsep}{\z@ \@plus \p@ \@minus \p@}%
  \parsepi = \z@ \@plus \p@ \@minus \p@
  \itemsepi = \parsepi
  \topsepi = \z@ \@plus \p@ \@minus \p@
  \parsepii = \z@ \@plus \p@ \@minus \p@
  \topsepii = \parsepi
  \topsepiii = \parsepii
  \everylistparindent\parindent}  

\newcommand*{\m@mtightlists}{%
  \setlength{\partopsep}{0.5\onelineskip \@plus \p@ \@minus \p@}%
  \parsepi = \z@ \@plus \p@ \@minus \p@
  \itemsepi = \parsepi
  \topsepi = \z@ \@plus \p@ \@minus \p@
  \parsepii = \z@ \@plus \p@ \@minus \p@
  \topsepii = \parsepi
  \topsepiii = \parsepii
  \everylistparindent\parindent}  

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
%
% \section{Mempatch version 4.6}
%
%    \begin{macrocode}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Version 4.6 (2006/11/22 and later)

%    \end{macrocode}
%
%
% \subsection{Double spacing}
%
% This is an embedding of the code from the \Lpack{setspace} package, with
% names changed.
%
%    \begin{macrocode}
\EmulatedPackage{setspace}

%    \end{macrocode}
%
% \begin{macro}{\setSpacing}
% \cs{setSpacing}\marg{num} effectively increases the \cs{baselineskip} to \meta{num}*\cs{baselineskip}
%    \begin{macrocode}
\newcommand{\setSpacing}[1]{%
  \def\baselinestretch{#1}%
  \@currsize}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\setSingleSpace}
% \cs{setSingleSpace}\marg{num} effectively increases the \cs{baselineskip} for single
% spacing to \meta{num}*\cs{baselineskip} (\meta{num} should be close to 1.0).
%    \begin{macrocode}
\newcommand*{\setSingleSpace}[1]{%
  \def\m@m@singlespace{#1}}
\setSingleSpace{1}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\SingleSpacing}
% \cs{SingleSpacing} starts single spacing.
%    \begin{macrocode}
%%% start single spacing
\newcommand*{\SingleSpacing}{%
  \setSpacing{\m@m@singlespace}%
  \vskip\baselineskip% correction for coming into single spacing
}
\SingleSpacing

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\OnehalfSpacing}
% \cs{OnehalfSpacing} starts `one and a half' spacing, which to most thesis nitpickers
% will look like double spacing.
%    \begin{macrocode}
%%% start `one and a half spacing', the elegant version of double spacing.
\newcommand*{\OnehalfSpacing}{
  \setSpacing{1.25}% default (10pt)
  \ifcase \@ptsize \relax   % 10pt
    \setSpacing{1.25}%
  \or%  11pt
    \setSpacing{1.213}%
  \or%  12pt
    \setSpacing{1.241}%
  \or\or% 14pt
    \setSpacing{1.20}%
  \or\or\or% 17pt
    \setSpacing{1.16}%
  \or\or% 9pt
    \setSpacing{1.35}%
  \fi}
    
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\DoubleSpacing}
% \cs{DoubleSpacing} starts double spacing, which to most thesis nitpickers
% will look far too spaced out.
%    \begin{macrocode}
%%% start double spacing, which looks terrible.
\newcommand*{\DoubleSpacing}{
  \setSpacing{1.667}% default (10pt)
  \ifcase \@ptsize \relax   % 10pt
    \setSpacing{1.667}%
  \or%  11pt
    \setSpacing{1.618}%
  \or%  12pt
    \setSpacing{1.655}%
  \or\or% 14pt
    \setSpacing{1.60}%
  \or\or\or% 17pt
    \setSpacing{1.545}%
  \or\or% 9pt
    \setSpacing{1.8}%
  \fi}
    
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@setsize}
% Modify the kernel command.
%    \begin{macrocode}
\renewcommand*{\@setsize}[4]{%
  \@nomath#1%
  \let\@currsize#1%
  \baselineskip #2%
  \baselineskip \baselinestretch\baselineskip
  \parskip \baselinestretch\parskip
  \setbox\strutbox \hbox{%
    \vrule height.7\baselineskip
           depth .3\baselineskip
           width \z@}%
  \skip\footins \baselinestretch\skip\footins
  \normalbaselineskip\baselineskip#3#4}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{environment}{SingleSpace}
% Environment form of \cs{SingleSpacing}.
%    \begin{macrocode}
%%% Environment form of \SingleSpacing.
\newenvironment{SingleSpace}{%
  \vskip\baselineskip
  \setSpacing{\m@m@singlespace}%
  \vskip -\baselineskip
}{\par}

%    \end{macrocode}
% \end{environment}
%
% \begin{environment}{SingleSpace*}
% Don't use this; it's only here to match the \Lpack{setspace} package. 
%    \begin{macrocode}
\newenvironment{SingleSpace*}{%
%  \vskip\baselineskip
  \setSpacing{\m@m@singlespace}%
  \vskip 0.5\baselineskip
}{\vskip -0.5\baselineskip}

%    \end{macrocode}
% \end{environment}
%
% \begin{macro}{\m@mrestore@spacing}
%    \begin{macrocode}
\newcommand*{\m@mrestore@spacing}{%
  \par
  \vskip \parskip
  \vskip \baselineskip
  \endgroup
  \vskip -\parskip
  \vskip -\baselineskip}

%    \end{macrocode}
% \end{macro}
%
% \begin{environment}{Spacing}
%    \begin{macrocode}
%%% \begin{Spacing}{num} baselineskip is increased to num*baselineskip.
\newenvironment{Spacing}[1]{%
  \par
  \begingroup
    \setSpacing{#1}}{\m@mrestore@spacing}

%    \end{macrocode}
% \end{environment}
%
% \begin{environment}{OnehalfSpace}
%    \begin{macrocode}
%%% Environment form of \OnehalfSpacing
\newenvironment{OnehalfSpace}{%
  \begingroup
    \OnehalfSpacing}{\m@mrestore@spacing}

%    \end{macrocode}
% \end{environment}
%
% \begin{environment}{DoubleSpace}
%    \begin{macrocode}
%%% Environment form of \DoubleSpacing
\newenvironment{DoubleSpace}{%
  \begingroup
    \DoubleSpacing}{\m@mrestore@spacing}

%    \end{macrocode}
% \end{environment}
%
%  Deal with spacing around displays.
%
% \begin{macro}{\memdskipstretch}
% \begin{macro}{\setDisplayskipStretch}
% \begin{macro}{\noDisplayskipStretch}
%    \begin{macrocode}
%%% \setDisplayskipStretch{num} changes space around displays by the factor 
%%% (1+num).
%%% \noDisplayskipStretch keeps the regular space around displays.
\newcommand*{\memdskipstretch}{0.0}
\newcommand*{\setDisplayskipStretch}[1]{%
  \renewcommand*{\memdskipstretch}{#1}}
\newcommand*{\noDisplayskipStretch}{\setDisplayskipStretch{0.0}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\memdskips}
% Macro added to \cs{everydisplay}, changing the settings of displayskips
%    \begin{macrocode}
\newcommand*{\memdskips}{%
  \advance\abovedisplayskip \memdskipstretch\abovedisplayskip
  \advance\belowdisplayskip \memdskipstretch\belowdisplayskip
  \advance\abovedisplayshortskip \memdskipstretch\abovedisplayshortskip
  \advance\belowdisplayshortskip \memdskipstretch\belowdisplayshortskip
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\everydisplay}
%    \begin{macrocode}
\everydisplay\expandafter{%
  \the\everydisplay
  \memdskips}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@xfloat}
% Reset \cs{baselinestretch} in floats.
%    \begin{macrocode}
\let\m@m@xfloat\@xfloat
\def\@xfloat #1[#2]{%
  \m@m@xfloat #1[#2]%
  \def\baselinestretch{\m@m@singlespace}%
  \normalsize}

%    \end{macrocode}
% \end{macro}
%
%  The extra spacing does not add space before and after a minipage.
% The solution, hinted at by Donald Arseneau, is to create a new
% environment. I came up with a solution which DA then much improved upon
% (CTT \textit{setspace and minipages}, 2006/11/28). 
% This is like minipage from an author's view, except that it is 
% always top positioned and acts like a paragraph.
% \begin{macro}{\memPD}
%    \begin{macrocode}
\newdimen\memPD
%    \end{macrocode}
% \end{macro}
% \begin{environment}{vminipage}
% The user view, just as for \texttt{minipage}, but ensures the \texttt{t}
% position and as a new paragraph.
%    \begin{macrocode}
%%% vminipage is like minipage but with better fore and aft spacing.
\newenvironment{vminipage}{%
  \par
  \@ifnextchar[%]
    \@ivminipage
    {\@iiiminipage t\relax[s]}
}{%
  \par\global\memPD=\prevdepth
  \endminipage
  \par
  \kern-\memPD%     no pagebreak allowed here
  \hbox{\vrule depth \memPD width \z@}}

%    \end{macrocode}
% \end{environment}
%
% \begin{macro}{\@ivminipage}
% Deal with first optional argument to \texttt{vminipage}.
%    \begin{macrocode}
 \def\@ivminipage[#1]{%
  \@ifnextchar[%]
    {\@iiminipage{t}}{\@iiiminipage{t}\relax[s]}}
%    \end{macrocode}
% \end{macro}
%
%
%
% We want single spacing in footnotes. Memoir has redone much of the footnote
% stuff, so we have to modify the modified versions.
%
% \begin{macro}{\@footnotetext}
% \begin{macro}{\@mpfootnotetext}
% \begin{macro}{\m@mold@footnotetext}
% \begin{macro}{\m@mold@mpfootnotetext}
% Single spacing in footnotes.
%    \begin{macrocode}
%%% Ensure single spacing in footnotes
%%%

\renewcommand{\@footnotetext}[1]{%
  \insert\footins{%
  \def\baselinestretch{\m@m@singlespace}
  \reset@font\foottextfont
  \@preamfntext
  \hsize\columnwidth
  \protected@edef\@currentlabel{%
    \csname p@footnote\endcsname\@thefnmark
  }%
  \color@begingroup
    \@makefntext{%
      \rule\z@\footnotesep\ignorespaces{\foottextfont #1}%
      \@finalstrut\strutbox}%
  \color@endgroup}\m@mmf@prepare}

\renewcommand{\@mpfootnotetext}[1]{%
  \global\setbox\@mpfootins\vbox{%
  \unvbox \@mpfootins
  \def\baselinestretch{\m@m@singlespace}
  \reset@font\foottextfont
  \hsize\columnwidth
  \@parboxrestore
  \protected@edef\@currentlabel{%
    \csname p@mpfootnote\endcsname\@thefnmark
  }%
  \color@begingroup
    \@makefntext{%
      \rule\z@\footnotesep\ignorespaces{\foottextfont #1}
      \@finalstrut\strutbox}%
  \color@endgroup}\m@mmf@prepare}

\let\m@mold@footnotetext\@footnotetext
\let\m@mold@mpfootnotetext\@mpfootnotetext

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\plainfootnotes}
%    \begin{macrocode}
\renewcommand{\plainfootnotes}{%
  \let\@footnotetext\m@mold@footnotetext
  \let\@mpfootnotetext\m@mold@mpfootnotetext}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@make@footnotetext}
% \begin{macro}{\m@make@mpfootnotetext}
%    \begin{macrocode}
\renewcommand{\m@make@footnotetext}[1]{%
  \@namelongdef{@footnotetext#1}##1{%
  \insert\@nameuse{footins#1}{%
  \def\baselinestretch{\m@m@singlespace}
  \reset@font\@nameuse{foottextfont#1}%
  \@preamfntext
  \hsize\columnwidth
  \protected@edef\@currentlabel{%
    \csname p@footnote#1\endcsname\@nameuse{@thefnmark#1}%
  }%
  \color@begingroup
    \@nameuse{@makefntext#1}{%
      \rule\z@\footnotesep\ignorespaces{\@nameuse{foottextfont#1} ##1}%
      \@finalstrut\strutbox}%
  \color@endgroup}\m@mmf@prepare}}

\renewcommand{\m@make@mpfootnotetext}[1]{%
  \@namelongdef{@mpfootnotetext#1}##1{%
  \global\setbox\@nameuse{@mpfootins#1}\vbox{%
  \unvbox \@nameuse{@mpfootins#1}%
  \def\baselinestretch{\m@m@singlespace}
  \reset@font\@nameuse{foottextfont#1}%
  \hsize\columnwidth
  \@parboxrestore
  \protected@edef\@currentlabel{%
    \csname p@mpfootnote#1\endcsname\@nameuse{@thefnmark#1}%
  }%
  \color@begingroup
    \@nameuse{@makefntext#1}{%
      \rule\z@\footnotesep\ignorespaces{\@nameuse{foottextfont#1} ##1}
      \@finalstrut\strutbox}%
  \color@endgroup}\m@mmf@prepare}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@twocolfootnotetext}
% \begin{macro}{\@mptwocolfootnotetext}
%    \begin{macrocode}
\renewcommand{\@twocolfootnotetext}[1]{%
  \insert\footinsv@r{%
  \def\baselinestretch{\m@m@singlespace}
  \reset@font\foottextfont
  \@preamfntext
  \protected@edef\@currentlabel{%
    \csname p@footnote\endcsname\@thefnmark
  }%
  \color@begingroup
    \@twocolfootfmt{#1}%
  \color@endgroup}\m@mmf@prepare}

\renewcommand{\@mptwocolfootnotetext}[1]{%
  \global\setbox\@mpfootinsv@r\vbox{%
  \unvbox \@mpfootinsv@r
  \def\baselinestretch{\m@m@singlespace}
  \reset@font\foottextfont
  \hsize\columnwidth
  \@parboxrestore
  \protected@edef\@currentlabel{%
    \csname p@mpfootnote\endcsname\@thefnmark
  }%
  \color@begingroup
    \@twocolfootfmt{#1}%
  \color@endgroup}\m@mmf@prepare}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\m@maketwocolfootnotetext}
% \begin{macro}{\m@make@mptwocolfootnotetext}
%    \begin{macrocode}
\renewcommand{\m@make@twocolfootnotetext}[1]{%
  \@namelongdef{@twocolfootnotetext#1}##1{%
  \insert\@nameuse{footins#1}{%
  \def\baselinestretch{\m@m@singlespace}
  \reset@font\@nameuse{foottextfont#1}%
  \@preamfntext
  \protected@edef\@currentlabel{%
    \csname p@footnote#1\endcsname\@nameuse{@thefnmark#1}%
  }%
  \color@begingroup
    \@nameuse{@twocolfootfmt#1}{##1}
  \color@endgroup}\m@mmf@prepare}}

\renewcommand{\m@make@mptwocolfootnotetext}[1]{%
  \@namelongdef{@mptwocolfootnotetext#1}##1{%
  \global\setbox\@nameuse{@mpfootins#1}\vbox{%
  \unvbox \@nameuse{@mpfootins#1}%
  \def\baselinestretch{\m@m@singlespace}
  \reset@font\@nameuse{foottextfont#1}%
  \hsize\columnwidth
  \@parboxrestore
  \protected@edef\@currentlabel{%
    \csname p@mpfootnote#1\endcsname\@nameuse{@thefnmark#1}%
  }%
  \color@begingroup
    \@nameuse{@twocolfootfmt#1}{##1}%
  \color@endgroup}\m@mmf@prepare}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@threecolfootnotetext}
% \begin{macro}{\@mpthreecolfootnotetext}
%    \begin{macrocode}
\renewcommand{\@threecolfootnotetext}[1]{%
  \insert\footinsv@r{%
  \def\baselinestretch{\m@m@singlespace}
  \reset@font\foottextfont
  \@preamfntext
  \protected@edef\@currentlabel{%
    \csname p@footnote\endcsname\@thefnmark
  }%
  \color@begingroup
    \@threecolfootfmt{#1}%
  \color@endgroup}\m@mmf@prepare}

\renewcommand{\@mpthreecolfootnotetext}[1]{%
  \global\setbox\@mpfootinsv@r\vbox{%
  \unvbox \@mpfootinsv@r
  \def\baselinestretch{\m@m@singlespace}
  \reset@font\foottextfont
  \hsize\columnwidth
  \@parboxrestore
  \protected@edef\@currentlabel{%
    \csname p@mpfootnote\endcsname\@thefnmark
  }%
  \color@begingroup
    \@threecolfootfmt{#1}%
  \color@endgroup}\m@mmf@prepare}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\m@makethreecolfootnotetext}
% \begin{macro}{\m@make@mpthreecolfootnotetext}
%    \begin{macrocode}
\renewcommand{\m@make@threecolfootnotetext}[1]{%
  \@namelongdef{@threecolfootnotetext#1}##1{%
  \insert\@nameuse{footins#1}{%
  \def\baselinestretch{\m@m@singlespace}
  \reset@font\@nameuse{foottextfont#1}%
  \@preamfntext
  \protected@edef\@currentlabel{%
    \csname p@footnote#1\endcsname\@nameuse{@thefnmark#1}%
  }%
  \color@begingroup
    \@nameuse{@threecolfootfmt#1}{##1}
  \color@endgroup}\m@mmf@prepare}}

\renewcommand{\m@make@mpthreecolfootnotetext}[1]{%
  \@namelongdef{@mpthreecolfootnotetext#1}##1{%
  \global\setbox\@nameuse{@mpfootins#1}\vbox{%
  \unvbox \@nameuse{@mpfootins#1}%
  \def\baselinestretch{\m@m@singlespace}
  \reset@font\@nameuse{foottextfont#1}%
  \hsize\columnwidth
  \@parboxrestore
  \protected@edef\@currentlabel{%
    \csname p@mpfootnote#1\endcsname\@nameuse{@thefnmark#1}%
  }%
  \color@begingroup
    \@nameuse{@threecolfootfmt#1}{##1}%
  \color@endgroup}\m@mmf@prepare}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@parafootnotetext}
% \begin{macro}{\@mpparafootnotetext}
%    \begin{macrocode}
\renewcommand{\@parafootnotetext}[1]{%
  \insert\footinsv@r{%
  \def\baselinestretch{\m@m@singlespace}
  \reset@font\foottextfont
  \@preamfntext
  \protected@edef\@currentlabel{%
    \csname p@footnote\endcsname\@thefnmark
  }%
  \setbox0=\vbox{\hsize=\maxdimen
  \color@begingroup
    \noindent \@parafootfmt{#1}%
  \color@endgroup}\m@mungebox}%
  \m@mmf@prepare}

\renewcommand{\@mpparafootnotetext}[1]{%
  \global\setbox\@mpfootinsv@r\vbox{%
  \unvbox \@mpfootinsv@r
  \def\baselinestretch{\m@m@singlespace}
  \reset@font\foottextfont
  \hsize\columnwidth
  \@parboxrestore
  \protected@edef\@currentlabel{%
    \csname p@mpfootnote\endcsname\@thefnmark
  }%
  \setbox0=\vbox{\hsize=\maxdimen
  \color@begingroup
    \noindent \@parafootfmt{#1}%
  \color@endgroup}\m@mungebox}%
  \m@mmf@prepare}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\m@makeparafootnotetext}
% \begin{macro}{\m@make@mpparafootnotetext}
%    \begin{macrocode}
\renewcommand{\m@make@parafootnotetext}[1]{%
  \@namelongdef{@parafootnotetext#1}##1{%
  \insert\@nameuse{footins#1}{%
  \def\baselinestretch{\m@m@singlespace}
  \reset@font\@nameuse{foottextfont#1}%
  \@preamfntext
  \protected@edef\@currentlabel{%
    \csname p@footnote#1\endcsname\@nameuse{@thefnmark#1}%
  }%
  \setbox0=\vbox{\hsize=\maxdimen
  \color@begingroup
    \noindent \@nameuse{@parafootfmt#1}{##1}
  \color@endgroup}\m@mungebox}\m@mmf@prepare}}

\renewcommand{\m@make@mpparafootnotetext}[1]{%
  \@namelongdef{@mpparafootnotetext#1}##1{%
  \global\setbox\@nameuse{@mpfootins#1}\vbox{%
  \unvbox \@nameuse{@mpfootins#1}%
  \def\baselinestretch{\m@m@singlespace}
  \reset@font\@nameuse{foottextfont#1}%
  \hsize\columnwidth
  \@parboxrestore
  \protected@edef\@currentlabel{%
    \csname p@mpfootnote#1\endcsname\@nameuse{@thefnmark#1}%
  }%
  \setbox0=\vbox{\hsize=\maxdimen
  \color@begingroup
    \noindent \@nameuse{@parafootfmt#1}{##1}%
  \color@endgroup}\m@mungebox}\m@mmf@prepare}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \subsection{Abnormal parskips}
%
% Non-zero, positive \cs{parskip}, which is not to be encouraged. Code
% based on Robin Fairbairns \Lpack{parskip} package and the NTG classes.
%
% Prevent loading of the \Lpack{parskip} package.
%    \begin{macrocode}
\EmulatedPackage{parskip}

%    \end{macrocode}
%
% \begin{macro}{\ifm@mnzpskip}
% \begin{macro}{\m@mnzpskiptrue}
% \begin{macro}{\m@mnzpskipfalse}
% \cs{ifm@mnzpskip} is TRUE if \cs{parskip} set to antything but 0pt.
%    \begin{macrocode}
\newif\ifm@mnzpskip
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\traditionalparskip}
% \begin{macro}{\m@mabparskip}
% \begin{macro}{\abnormalparskip}
% \begin{macro}{\nonzeroparskip}
%    \begin{macrocode}
%%% \traditionalparskip set \parskip to 0pt.
\newcommand*{\traditionalparskip}{%
  \parskip \z@
  \m@mnzpskipfalse}
\newskip\m@mabparskip
%%% \abnormalparskip{length} sets \parskip to length.
\newcommand*{\abnormalparskip}[1]{%
  \setlength{\parskip}{#1}\m@mabparskip=#1\relax
  \m@mnzpskiptrue}
%%% \nonzeroparskip sets \parskip to a non-zero value that might be 
%%% not too bad (any non-zero \parskip is not good).
\newcommand*{\nonzeroparskip}{\abnormalparskip{%
  0.5\baselineskip 
  \@plus .1\baselineskip \@minus .1\baselineskip% NTG
%%  0.5/baselineskip \@plus 2pt% RF
}}
\traditionalparskip

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\defaultlists}
% \begin{macro}{\tightlists}
% Change these to cater for abnormal \cs{parskip}s
%    \begin{macrocode}
%%% Change lists to cater for non-zero \parskip.
\newlength{\itemsepii}
\newlength{\itemsepiii}
\newlength{\partopsepiii}
%%%\newskip\parsepiii
\let\m@mold@defaultlists\defaultlists
\renewcommand*{\defaultlists}{%
  \m@mold@defaultlists
  \itemsepii\parsepii
  \itemsepiii\topsepiii
  \partopsepiii \p@ \@plus\z@ \@minus\p@
  \ifm@mnzpskip
    \partopsep \p@ \@plus\z@ \@minus\p@
    \topsepi\z@
    \parsepi\parskip
    \itemsepi\z@
    \topsepii\z@
    \parsepii\parskip
    \itemsepii\z@
    \topsepiii\z@
%%    \parsepiii\parskip
    \itemsepiii\z@
  \fi}

\let\m@mold@tightlists\tightlists
\renewcommand*{\tightlists}{%
  \m@mold@tightlists
  \ifm@mnzpskip
    \partopsep \p@ \@plus\z@ \@minus\p@
    \topsepi\z@
    \parsepi\parskip
    \itemsepi\z@
    \topsepii\z@
    \parsepii\parskip
    \itemsepii\z@
    \topsepiii\z@
%%    \parsepiii\parskip
    \itemsepiii\z@
    \partopsepiii\partopsep
  \fi}

\let\@listI\@listi
\defaultlists
\@listi

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\@listii}
% \begin{macro}{\@listiii}
%    \begin{macrocode}
\renewcommand*{\@listii}{%
  \leftmargin\leftmarginii
  \labelwidth\leftmarginii
  \advance\labelwidth-\labelsep
  \topsep\topsepii
  \parsep\parsepii
  \itemsep\itemsepii}
\renewcommand*{\@listiii}{%
  \leftmargin\leftmarginiii
  \labelwidth\leftmarginiii
  \advance\labelwidth-\labelsep
  \topsep\topsepiii
  \parsep\z@
  \itemsep\itemsepiii
  \partopsep\partopsepiii}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
%
%
% \begin{macro}{\verbatim}
% From NTG, where it is a \cs{def}
%    \begin{macrocode}
\providecommand*{\verbatim}{%
  \topsep=-0.5\parskip
  \@verbatim
  \frenchspacing\@vobeyspaces \@xverbatim}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@minpagerestore}
% From NTG
%    \begin{macrocode}
\providecommand*{\@minipagerestore}{%
  \parskip=.5\baselineskip \@plus .1\baselineskip \@minus .1\baselineskip}
%    \end{macrocode}
% But there are two conflicting earlier definitions which I'll now combine.
%    \begin{macrocode}
\renewcommand*{\@minipagerestore}{%
  \let\@verbfootnotetext\@verbmpfootnotetext%  for \verbfootnotein a minipage
  \m@mdoextrafeetmini%                         footnotes in minipages
  \ifm@mnzpskip \parskip=\m@mabparskip\fi}

%    \end{macrocode}
% \end{macro}
%
% Use zero \cs{parskip} in ToC listing. Already done in memoir.
%
%
%
%
% \subsection{Side captions}
%
% \begin{macro}{\setsidecaps}
% Ivars Finvers (\texttt{ifinvers@ucalgary.ca}) noted (2006/09/26)
% that the settings for
% \cs{sidecapwidth} and \cs{sidecapsep} didn't change if the marginal note
% settings were changed via \cs{checkandfixthelayout}. These settings get 
% checked at setting time, not at \cs{checkand...}.
%    \begin{macrocode}
%%% \setsidecaps{<sep>}{<width>} sets sidecaption lengths
\newcommand*{\setsidecaps}[2]{%
  \setlength{\sidecapsep}{#1}\@memznegtest{\sidecapsep}%
  \setlength{\sidecapwidth}{#2}\@memznegtest{\sidecapwidth}}

%    \end{macrocode}
% \end{macro}
%
% \subsection{Sidebars}
%
% Donald Arseneau came up with some improvements to sidebars. The impetus
% for this came from a CTT thread, \textit{whitespace after my command}, 
% 2006/11/30 and earlier. In email to me on 2006/11/30 he said:
% \begin{quotation}
%    For \Lclass{memoir} \cs{sidebar}, it seems the rules are expanding
% to fill the space for the sidebar, so here is my suggested change
% to \cs{sidecontents}.
%
%    It occurs to me that a separate \cs{sidetsep} (or \cs{sidetopsep})
% would be more valuable than having an explicit 1\cs{onelineskip}.
%
%   I grouped common code into two macros, and altered \cs{sidebarform} too.
% \end{quotation}
%
% Since then we have been going back and forth, with the result that
% practically all the complex code is Donald's.
%
% \begin{macro}{\m@sideb@left}
% \begin{macro}{\m@sideb@right}
% Some common code for use later.
%    \begin{macrocode}
\newcommand*{\m@sideb@left}{%
  \@tempdimc \sidebarwidth
  \advance\@tempdimc\sidebarhsep
  \kern-\@tempdimc}
\newcommand*{\m@sideb@right}{%
  \@tempdimc \columnwidth%   or \hsize
  \advance\@tempdimc\sidebarhsep
  \kern\@tempdimc}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\sidebartopsep}
% New length for separation at the top of a sidebar.
%    \begin{macrocode}
\newlength{\sidebartopsep}
  \setlength{\sidebartopsep}{0pt}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sidebarfont}
%    \begin{macrocode}
\renewcommand*{\sidebarfont}{}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\setsidebarheight}
% The macro \cs{setsidebarheight}\marg{height} sets the total height
% of sidebars on a page to \meta{height}. This is an interface for
% controlling \verb?\dimen\sideins? properly, taking account of \cs{topskip}
% and the sidebar font size (and therefore should be invoked \emph{after}
% declaring \cs{sidebarfont}). 
%
% Note that \cs{sidebartopsep} is \emph{not} included as part of the
% allocated height.
%    \begin{macrocode}
\renewcommand*{\setsidebarheight}[1]{%
  \setlength{\dimen\sideins}{#1}%
  \advance\dimen\sideins-\topskip
  \advance\dimen\sideins\ht\strutbox}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\setsidebars}
% Macro to set the main sidebar parameters.
%    \begin{macrocode}
%%% \setsidebars{hsep}{width}{vsep}{topsep}{font}{height}
%%% A * argument means leave the setting as is.
\newcommand*{\setsidebars}[6]{%
  \nametest{#1}{*}\ifsamename\else
    \setlength{\sidebarhsep}{#1}\@memznegtest{\sidebarhsep}%
  \fi
  \nametest{#2}{*}\ifsamename\else
    \setlength{\sidebarwidth}{#2}\@memznegtest{\sidebarwidth}%
  \fi
  \nametest{#3}{*}\ifsamename\else
    \setlength{\sidebarvsep}{#3}\@memnegtest{\sidebarvsep}%
  \fi
  \nametest{#4}{*}\ifsamename\else
    \setlength{\sidebartopsep}{#4}%
  \fi
  \nametest{#5}{*}\ifsamename\else
    \def\sidebarfont{#5}%
  \fi
  \nametest{#6}{*}\ifsamename\else
    \setsidebarheight{#6}%
    \ifdim\dimen\sideins>\z@\else
      \@memerror{\protect\sidebarheight\space is zero or negative}{\@ehd}%
    \fi
  \fi}
  \setsidebars{\marginparsep}%          sidebarhsep
              {\marginparwidth}%        sidebarwidth
              {\onelineskip}%           sidebarvsep
              {0pt}%                    sidebartopsep
              {\normalsize\normalfont}% sidebarfont
              {\textheight}%            sidebarheight

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sidecontents}
%    \begin{macrocode}
\renewcommand{\sidecontents}{\hbox to \z@{%
  \if@twocolumn%                 %% put outside nearest column
    \if@firstcolumn%             %% move to left
      \m@sideb@left
    \else%                       %% move to right
      \m@sideb@right
    \fi
  \else%                         %% put into foremargin?
    \ifsidebaroneside%           %% move to right
      \m@sideb@right
    \else%                       %% pick the margin
      \ifcase\m@msidebar@margin%    0 to left
        \m@sideb@left
      \or%                          1 to right
        \m@sideb@right
      \or%                          2 to outer
        \ifodd\c@page%           %% move to right
          \m@sideb@right
        \else%                   %% move to left
          \m@sideb@left
        \fi
      \or%                          3 to inner
        \ifodd\c@page%           %% move to left
          \m@sideb@left
        \else                    %% move to right
          \m@sideb@right
        \fi
      \fi
    \fi
  \fi
%    \end{macrocode}
% DA and I have gone to and fro on the next bit of code trying to get
% the top alignment correct. DA has the last word.
% \begin{verbatim}
%  \vtop to0pt{%
%    \vskip\sidebartopsep
%    \unvbox\sideins \vss}%
% \end{verbatim}
%    \begin{macrocode}
%  \vbox to0pt{%
%    \vskip\onelineskip \vskip-\topskip
%    \vskip\sidebartopsep
%    \unvbox\sideins \vss}%
% \end{verbatim}
%    \begin{macrocode}
  \vtop to0pt{%
    \normalsize\normalfont\sidebarfont % select font so we know the strut size
    \vskip\topskip \vskip-\ht\strutbox
    \vskip\sidebartopsep % extra vertical shift
    \unvbox\sideins \vss}%
  \hss
}}
    
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sidebar}
% DA says that having stacked insertions position properly is difficult.
% Here he uses the size of the strut to regulate both the height and the
% depth of each insertion (much as for footnotes). The following is his
% code and commentary.
%    \begin{macrocode}
\renewcommand{\sidebar}[1]{%
  \insert\sideins{%
%    \end{macrocode}
% Begin the insertion with settings. The height of the strut box
% (dependant on the font) will determine the top alignment both initially
% and after a page break. The setting for maximum depth does not control
% anything; see the code further on instead.
%    \begin{macrocode}
    \hsize\sidebarwidth
    \@parboxrestore
    \sidebarform \normalsize\normalfont\sidebarfont
    \splittopskip=\ht\strutbox
    \splitmaxdepth=\dp\strutbox % doesn't do anything useful
%    \end{macrocode}
% Allow a free split at the top (so this whole insertion moves
% to the next page if it does not fit).
%    \begin{macrocode}
    \allowbreak
%    \end{macrocode}
% Control the (vertical) positioning of non-split entries. Footnotes (and
% previous versions of \cs{sidebar}) us a strut at the beginning of the text,
% but we will allow a baseline-skip to perform the same function. This
% method also introduces a \cs{parskip} when the paragraph begins, so we
% counteract that. (The problem with an initial strut is that it messes'
% up entries that begin with vertical space.)
%    \begin{macrocode}
    \prevdepth=\dp\strutbox    % supersedes a "top-strut"
    \vskip-\parskip
%    \end{macrocode}
% Now the user's sidebar entry:
%    \begin{macrocode}
    #1%
%    \end{macrocode}
% If the entry ended still in a paragraph, take the chance to insert a
% final strut; then end the paragraph.
%    \begin{macrocode}
    \ifvmode\else
      \unskip\@finalstrut\strutbox
    \fi\par
%    \end{macrocode}
% Explicitly force the effect of \cs{maxdepth} (\cs{splitmaxdepth}),
% but using the depth of the strut in the r\^{o}le of \cs{maxdepth}.
%    \begin{macrocode}
    \ifdim\prevdepth>\dp\strutbox \prevdepth=\dp\strutbox \fi
%    \end{macrocode}
% Now control how adjacent entries abut (whether or not a final strut got 
% inserted) and how an entry fits at the bottom of the page. Use \cs{vskip}s
% to move from the text bottom to where a strut would bottom out,
% but insert an allowed breakpoint at the baseline position
% (so that the entry baseline may match the page's baseline). Finally
% insert the extra separation between entries.
%    \begin{macrocode}
    \ifdim\prevdepth>99\p@
      \nobreak
      \vskip-\prevdepth
      \allowbreak
      \vskip\dp\strutbox
    \fi
    \vskip\sidebarvsep}}

%    \end{macrocode}
% \end{macro}
%
%
% \subsection{Typeout the layout}
%
%   Update this for sidecaptions and sidebars.
%
% \begin{macro}{\typeoutlayout}
% Include the sidecap and sidebar settings.
%    \begin{macrocode}
\renewcommand*{\typeoutlayout}{%
  \typeout{}
  \typeout{******************************************************}
  \typeout{Stock height and width: 
                 \the\stockheight\space by \the\stockwidth}
  \typeout{Top and edge trims: 
                 \the\trimtop\space and \the\trimedge}
  \typeout{Page height and width: 
                 \the\paperheight\space by \the\paperwidth}
  \typeout{Text height and width: 
                 \the\textheight\space by \the\textwidth}
  \typeout{Spine and edge margins: 
                 \the\spinemargin\space and \the\foremargin}
  \typeout{Upper and lower margins: 
                 \the\uppermargin\space and \the\lowermargin}
  \typeout{Headheight and headsep: 
                 \the\headheight\space and \the\headsep}
  \typeout{Footskip: 
                 \the\footskip}
  \typeout{Columnsep and columnseprule: 
                 \the\columnsep\space and \the\columnseprule}
  \typeout{Marginparsep and marginparwidth: 
                 \the\marginparsep\space and \the\marginparwidth}
  \typeout{Sidecapsep and sidecapwidth: 
                 \the\sidecapsep\space and \the\sidecapwidth}
  \typeout{Sidebarhsep and sidebarwidth:
                 \the\sidebarhsep\space and \the\sidebarwidth}
  \typeout{Sidebarvsep and sidebartopsep:
                 \the\sidebarvsep\space and \the\sidebartopsep}
  \typeout{Sidebarheight:
                 \the\dimen\sideins}         
  \typeout{******************************************************}
  \typeout{}}

%    \end{macrocode}
% \end{macro}
%
%
% \subsection{Footnotes}
%
% Per Starb\"{a}ck found (CTT \textit{feetbelowfloat in memoir}, 2006/11/24)
% that this didn't effect anything in \cs{raggedbottom}, contrary to the
% claim in the manual.
%
% \begin{macro}{\setfootnoterule}
% \cs{setfootnoterule}\oarg{vfill}\marg{uplift}\marg{width}\marg{thickness}
% defines the \cs{footnoterule}.
%    \begin{macrocode}
%%% user command to set the \footnoterule
%%% \setfootnoterule[<fill>]{<uplift>}{<width>}{<thickness>}
\newcommand*{\setfootnoterule}[4][]{%
  \def\footnoterule{\kern -#2\relax #1\relax
    \hrule width #3\relax
    \kern #2\kern-#4}}
%%%% memoir's default setting is:
\setfootnoterule{3pt}{0.4\columnwidth}{\normalrulethickness}
%%%% to force footnotes to the bottom after a \raggedbottom
%%%% \setfootnoterule[\vfill]{3pt}{0.4\columnwidth}{\normalrulethickness}

%    \end{macrocode}
% \end{macro}
%
% \subsection{Widows and sloppybottom}
%
%    There was a discussion \textit{widow handling?} on CTT in 
% May 2006. \texttt{ivowel@gmail.com} wrote
% \begin{quote}
% in experimenting with raggedbottom, widowpenalty, and clubpenalty,
% I think that I have not found a solution that strikes me as 
% particularly desirable. I think what I would really like is that
% widows (i.e., left-over single lines that begin on the following
% page) are resolved not by pushing one extra line from the same
% paragraph also onto the next page, but by stretching the
% \cs{textheight} to allow this one extra at the bottom of the
% same page.
% \end{quote}
% Donald Arseneau, as he so often does, came up with a solution he
% termed \cs{sloppybottom}. Here is a generalised version.
%
% \begin{macro}{\sloppybottom}
% Allow an extra line on a page to save a widow.
%    \begin{macrocode}
%%% \sloppybottom allows an extra line on a page to save a widow.
%%% You must increase the \topskip (by 60\% is reasonable) and this
%%% will push the text lower on the page. Run \checkandfixthelayout
%%% after the change. For example:
%%% \setlength{\topskip}{1.6\topskip}
%%% \checkandfixthelayout
%%% \sloppybottom
%%% ...
\newcommand*{\sloppybottom}{%
  \def\@textbottom{\vskip \z@ \@plus.0001fil \@minus .95\topskip}%
  \topskip=1\topskip \@plus 0.625\topskip \@minus .95\topskip
  \def\@texttop{\vskip \z@ \@plus -0.625\topskip \@minus -0.95\topskip}}

%    \end{macrocode}
% \end{macro}
%
% \subsection{Quark marks}
%
% William Adams (2006/08/28) supplied the following code to use trim marks 
% along the style of Quark Xpress. He also supplied a basis for 
% \cs{printime} but I have used one from \textit{TeX for the Impatient} 
% as it saves some counters.
% \begin{macro}{\m@mcalchm}
% Calculate the hours and minutes from \cs{time}.
%    \begin{macrocode}
%%% \m@mcalchm calculates the time of day. (Code basis from TeX for the Impatient)
\newcommand*{\m@mcalchm}{%
  \count0 = \time \divide \count0 by 60\relax
  \count2 = \count0\relax%    the hour
  \count4 = \time \multiply\count0 by 60\relax
  \advance\count4 by -\count0\relax% the minute
  \ifnum\count4<10 \toks1 = {0}%  make a leading zero
  \else \toks1 = {}%
  \fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\hmpunct}
% \begin{macro}{\amname}
% \begin{macro}{\pmname}
% User format controls for \cs{printtime}
%    \begin{macrocode}
%%% punctuation, am and pm for \printtime
\newcommand*{\hmpunct}{:}% hours minutes separator
\newcommand*{\amname}{am}% ante meridiem
\newcommand*{\pmname}{pm}% post meridiem

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\printtime}
% \begin{macro}{\printtime*}
% Print the time of day as 24 hour clock or 12 hour clock
%    \begin{macrocode}
%%% \printtime   prints time per 24 hour clock
%%% \printtime*  prints time per 12 hour clock
\newcommand*{\printtime}{%
  \@ifstar{\m@msprtime}{\m@mprtime}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\m@mprtime}
% \begin{macro}{\m@msprtime}
% These implement time printing: \cs{m@mprtime} as 24 hour clock and
% \cs{m@msprtime} as 12 hour clock
%    \begin{macrocode}
\newcommand*{\m@mprtime}{\begingroup
  \m@mcalchm
  \number\count2\hmpunct\the\toks1 \number\count4
  \endgroup}
\newcommand*{\m@msprtime}{\begingroup
  \m@mcalchm
  \def\@mpm{\pmname}%
  \ifnum\count2<1\relax% early in the morning
    \count2=12\relax
    \ifnum\count4>0\relax% not midnight
      \def\@mpm{\amname}%
    \fi
  \else
    \ifnum\time<721\relax% noon or earlier
      \def\@mpm{\amname}%
    \else
      \ifnum\time>779\relax% 1300 hrs or later
        \advance\count2 by -12\relax
      \fi
    \fi
  \fi
  \number\count2\hmpunct\the\toks1 \number\count4\ \@mpm
  \endgroup}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\registrationColour}
% \begin{macro}{\quarkmarks}
%    \begin{macrocode}
%%% \quarkmarks generates trim marks in the style of Quark Xpress
%%% (Code supplied by William Adams)
%%%
\newcommand*{\registrationColour}[1]{#1}
\newcommand*{\quarkmarks}{%
\renewcommand*{\tmarktl}{\registrationColour{%
  \begin{picture}(0,0)
    \setlength{\unitlength}{1bp}\thicklines
    \put(-36,0){\line(1,0){24}}
    \put(0,12){\line(0,1){24}}
    \put(3,27){\ttfamily\fontsize{8bp}{10bp}\selectfont\jobname\ \
      \today\ \ \printtime\ \ Page \thepage}
  \end{picture}}}
\renewcommand*{\tmarktm}{\registrationColour{%
  \begin{picture}(0,0)
    \setlength{\unitlength}{1bp}\thicklines
    \put(-24,24){\line(1,0){48}}
    \put(0,12){\line(0,1){24}}
    \put(0,24){\oval(12,12)}
  \end{picture}}}
\renewcommand*{\tmarktr}{\registrationColour{%
  \begin{picture}(0,0)
    \setlength{\unitlength}{1bp}\thicklines
    \put(12,0){\line(1,0){24}}
    \put(0,12){\line(0,1){24}}
  \end{picture}}}
\renewcommand*{\tmarkmr}{\registrationColour{%
  \begin{picture}(0,0)
    \setlength{\unitlength}{1bp}\thicklines
    \put(12,0){\line(1,0){24}}
    \put(24,-24){\line(0,1){48}}
    \put(24,0){\oval(12,12)}
  \end{picture}}}
\renewcommand*{\tmarkbr}{\registrationColour{%
  \begin{picture}(0,0)
    \setlength{\unitlength}{1bp}\thicklines
    \put(12,0){\line(1,0){24}}
    \put(0,-36){\line(0,1){24}}
  \end{picture}}}
\renewcommand*{\tmarkbm}{\registrationColour{%
  \begin{picture}(0,0)
    \setlength{\unitlength}{1bp}\thicklines
    \put(-24,-24){\line(1,0){48}}
    \put(0,-36){\line(0,1){24}}
    \put(0,-24){\oval(12,12)}
  \end{picture}}}
\renewcommand*{\tmarkbl}{\registrationColour{%
  \begin{picture}(0,0)
    \setlength{\unitlength}{1bp}\thicklines
    \put(-36,0){\line(1,0){24}}
    \put(0,-36){\line(0,1){24}}
  \end{picture}}}
\renewcommand*{\tmarkml}{\registrationColour{%
  \begin{picture}(0,0)
    \setlength{\unitlength}{1bp}\thicklines
    \put(-36,0){\line(1,0){24}}
    \put(-24,-24){\line(0,1){48}}
    \put(-24,0){\oval(12,12)}
  \end{picture}}}
\renewcommand*{\trimmarks}{%
%%  \special{papersize=\the\stockwidth,\the\stockheight}
  {%
  \vbox to \z@{\vskip-1in \vskip\trimtop % top of logical page
    \hb@xt@\z@{\hskip-1in
      \ifodd\c@page
        \hskip\stockwidth \hskip-\trimedge \hskip-\paperwidth
      \else
        \if@twoside
          \hskip\trimedge % left of logical page
        \else
          \hskip\stockwidth \hsip-\trimedge \hskip-\paperwidth
        \fi
      \fi
      \vbox to \paperheight{%
        \let\protect\relax %      <- v1.4 addition
        \hb@xt@\paperwidth{\tmarktl\hfil\tmarktm\hfil\tmarktr}%
        \vfil
        \hb@xt@\paperwidth{\tmarkml\hfil\tmarkmr}%
        \vfil
        \hb@xt@\paperwidth{\tmarkbl\hfil\tmarkbm\hfil\tmarkbr}}%
      \hss}%
    \vss}}%
}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \subsection{Pagestyles}
%
% Lars Madsen (2006/11/03) noted that there were some missing \verb?%?
% at the end of some lines in \cs{makepagestyle} that would cause
% extraneous spaces in something like: \\
% \verb?X\pagestyle{plain}X? \\
% though I can't think that many would do this. In any case here is the
% entire revised section from \texttt{memoir.dtx} \ldots
%
%
% \begin{macro}{\makepagestyle}
%  At last we can put everything together for defining a new pagestyle,
% via the macro \cs{makepagestyle}\marg{style} which initially sets up 
% a new pagestyle \meta{style} corresponding the the \LaTeX{} \texttt{empty}
% pagestyle. The prior macros can then be used to make modifications
% to the style.
% \changes{v1.0}{2001/11/15}{Now 2 stage process for defining \cs{@evenhead}
%                            and friends}
% \changes{v1.61}{2004/03/14}{Added \cs{m@mhe@dreset} at several points in
%                             \cs{makepagestyle} (from patch 2.1)}
%    \begin{macrocode}
%%% Remove extraneous spaces from pagestyle code
%%%
\renewcommand*{\makepagestyle}[1]{%
%    \end{macrocode}
% First define the macro \cs{ps@style}, which in turn defines the macros
% \cs{@evenhead}, \cs{@oddhead}, \cs{@evenfoot} and \cs{@oddfoot}.
%    \begin{macrocode}
  \@namedef{ps@#1}{%
    \@namedef{#1@evenhead}{%
%    \end{macrocode}
% The code for the definition of \cs{@evenhead} and friends is based on code
% from Piet van Oostrum's \Lpack{fancyhdr} package~\cite{FANCYHDR}. The three
% parts of the header are put into parboxes, with fills between them, and
% the whole lot is put into a box the width of the header. Fillers are put
% before and after the main box which control the header position.
%
% First the left filler which is either \cs{relax} or \cs{hss}, and then
% start the main box.
%    \begin{macrocode}
      \@nameuse{#1evenhpl}\hb@xt@\@nameuse{#1runwidth}{\m@mhe@dreset%
        \vbox{\hbox{%
%    \end{macrocode}
% The left part of the header.
%    \begin{macrocode}
        \rlap{\parbox[b]{\@nameuse{#1runwidth}}{%
          \raggedright\@nameuse{#1eheadl}\strut}}\hfill
%    \end{macrocode}
% The center part of the header.
%    \begin{macrocode}
              \parbox[b]{\@nameuse{#1runwidth}}{%
          \centering\@nameuse{#1eheadc}\strut}\hfill
%    \end{macrocode}
% The right part of the header.
%    \begin{macrocode}
        \llap{\parbox[b]{\@nameuse{#1runwidth}}{%
          \raggedleft\@nameuse{#1eheadr}\strut}}}%
%    \end{macrocode}
% Finally, the header rule and finish with the right filler, which
% is either \cs{relax} or \cs{hss}.
%    \begin{macrocode}
        \@nameuse{#1headrule}}}\@nameuse{#1evenhpr}}%
%    \end{macrocode}
% The code for the \cs{@oddhead}, \cs{@evenfoot} and \cs{@oddfoot} follows
% a similar pattern. Here is \cs{@oddhead}.
%    \begin{macrocode}
    \@namedef{#1@oddhead}{%
      \@nameuse{#1oddhpl}\hb@xt@\@nameuse{#1runwidth}{\m@mhe@dreset%
        \vbox{\hbox{%
        \rlap{\parbox[b]{\@nameuse{#1runwidth}}{%
          \raggedright\@nameuse{#1oheadl}\strut}}\hfill
              \parbox[b]{\@nameuse{#1runwidth}}{%
          \centering\@nameuse{#1oheadc}\strut}\hfill
        \llap{\parbox[b]{\@nameuse{#1runwidth}}{%
          \raggedleft\@nameuse{#1oheadr}\strut}}}%
        \@nameuse{#1headrule}}}\@nameuse{#1oddhpr}}%
%    \end{macrocode}
% And \cs{@evenfoot}. For the footers the rules come \emph{before} any foot
% entries.
%    \begin{macrocode}
    \@namedef{#1@evenfoot}{%
      \@nameuse{#1evenfpl}\hb@xt@\@nameuse{#1runwidth}{\m@mhe@dreset%
        \vbox{\@nameuse{#1footrule}\hbox{%
        \rlap{\parbox[b]{\@nameuse{#1runwidth}}{%
          \raggedright\@nameuse{#1efootl}\strut}}\hfill
              \parbox[b]{\@nameuse{#1runwidth}}{%
          \centering\@nameuse{#1efootc}\strut}\hfill
        \llap{\parbox[b]{\@nameuse{#1runwidth}}{%
          \raggedleft\@nameuse{#1efootr}\strut}}}%
        }}\@nameuse{#1evenfpr}}%
%    \end{macrocode}
% Lastly the \cs{@oddfoot}.
%    \begin{macrocode}
    \@namedef{#1@oddfoot}{%
      \@nameuse{#1oddfpl}\hb@xt@\@nameuse{#1runwidth}{\m@mhe@dreset%
        \vbox{\@nameuse{#1footrule}\hbox{%
        \rlap{\parbox[b]{\@nameuse{#1runwidth}}{%
          \raggedright\@nameuse{#1ofootl}\strut}}\hfill
              \parbox[b]{\@nameuse{#1runwidth}}{%
          \centering\@nameuse{#1ofootc}\strut}\hfill
        \llap{\parbox[b]{\@nameuse{#1runwidth}}{%
          \raggedleft\@nameuse{#1ofootr}\strut}}}%
        }}\@nameuse{#1oddfpr}}%
%    \end{macrocode}
% Now we define \cs{@evenhead} etc., in terms of \verb?#1@evenhead?.
%    \begin{macrocode}
    \def\@evenhead{\@nameuse{#1@evenhead}}%
    \def\@oddhead{\@nameuse{#1@oddhead}}%
    \def\@evenfoot{\@nameuse{#1@evenfoot}}%
    \def\@oddfoot{\@nameuse{#1@oddfoot}}%
%    \end{macrocode}
% To finish off the definition of \cs{ps@style}, add in a hook which can be
% defined so that it adds additional code, if required.
%    \begin{macrocode}
    \@nameuse{#1pshook}%
  }%
%    \end{macrocode}
% The final part of setting up the new pagestyle is defining all the macros
% called by \cs{ps@style}, and giving them default values. Make the headers
% and footers empty.
%    \begin{macrocode}
  \makeevenhead{#1}{}{}{}%
  \makeoddhead{#1}{}{}{}%
  \makeevenfoot{#1}{}{}{}%
  \makeoddfoot{#1}{}{}{}%
%    \end{macrocode}
% Make the headers/footers the same width as the \cs{textwidth}, center
% them, and ensure
% that the rules have zero height so that they will be invisible.
%    \begin{macrocode}
  \makerunningwidth{#1}{\textwidth}%
  \makeheadposition{#1}{}{}{}{}%
  \makeheadrule{#1}{\textwidth}{0pt}%
  \makefootrule{#1}{\textwidth}{\footruleheight}{\footruleskip}%
%    \end{macrocode}
% Finally, there is no addtional code needed, so make the hook empty, and
% we are done.
%    \begin{macrocode}
  \makepsmarks{#1}{}%
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\aliaspagestyle}
% The command \cs{aliaspagestyle}\marg{aliasl}\marg{original}
% defines the \meta{alias} pagestyle to be an alias for the
% \meta{original} pagestyle.
%    \begin{macrocode}
\renewcommand*{\aliaspagestyle}[2]{%
  \@namedef{ps@#1}{\@nameuse{ps@#2}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\copypagestyle}
% The command \cs{copypagestyle}\marg{new}\marg{original}
% defines the \meta{new} pagestyle to be a copy of the
% \meta{original} pagestyle.
%
% It first makes the \meta{new} (empty) pagestyle then defines the internals
% in terms of the \meta{original} internals.
% \changes{v1.2}{2002/08/10}{Added \cs{copypagestyle}}
%    \begin{macrocode}
\renewcommand*{\copypagestyle}[2]{%
  \makepagestyle{#1}%
%    \end{macrocode}
% Do the headers and footers.
%    \begin{macrocode}
  \makeevenhead{#1}{\@nameuse{#2eheadl}}%
               {\@nameuse{#2eheadc}}{\@nameuse{#2eheadr}}%
  \makeoddhead{#1}{\@nameuse{#2oheadl}}%
               {\@nameuse{#2oheadc}}{\@nameuse{#2oheadr}}%
  \makeevenfoot{#1}{\@nameuse{#2efootl}}%
               {\@nameuse{#2efootc}}{\@nameuse{#2efootr}}%
  \makeoddfoot{#1}{\@nameuse{#2ofootl}}%
              {\@nameuse{#2ofootc}}{\@nameuse{#2ofootr}}%
%    \end{macrocode}
% Set the width.
%    \begin{macrocode}
  \makerunningwidth{#1}{\@nameuse{#2runwidth}}%
%    \end{macrocode}
% Specify the \cs{headposition}.
%    \begin{macrocode}
  \@namedef{#1evenhpl}{\@nameuse{#2evenhpl}}%
  \@namedef{#1oddhpl}{\@nameuse{#2oddhpl}}%
  \@namedef{#1evenfpl}{\@nameuse{#2evenfpl}}%
  \@namedef{#1oddfpl}{\@nameuse{#2oddfpl}}%
%    \end{macrocode}
% Specify the head and foot rules.
%    \begin{macrocode}
  \@namedef{#1headrule}{\@nameuse{#2headrule}}%
  \@namedef{#1footrule}{\@nameuse{#2footrule}}%
%    \end{macrocode}
% And pick up the hook.
%    \begin{macrocode}
  \makepsmarks{#1}{\@nameuse{#2pshook}}%
}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\ifonlyfloats}
% \cs{ifonlyfloats}\marg{yes}\marg{no} processes \meta{yes} if the macro is
% called on a page consisting only of floats, otherwise \meta{no} is 
% processed. \cs{if@fcolmade} is specified in \file{ltoutput.dtx}.
% \changes{v1.0}{2001/11/14}{Added \cs{ifonlyfloats}}
%    \begin{macrocode}
\renewcommand*{\ifonlyfloats}[2]{\if@fcolmade #1\else #2\fi}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\mergepagefloatstyle}
% \cs{mergepagefloatstyle}\marg{style}\marg{textstyle}\marg{floatstyle}
% makes a new page style \meta{style} that is \meta{textstyle} on normal
% pages but uses \meta{floatstyle} on float-only pages. Both \meta{textstyle}
% and \meta{floatstyle} must have been previously defined.
% \changes{v1.0}{2001/11/15}{Added \cs{mergepagefloatstyle}}
%    \begin{macrocode}
\renewcommand*{\mergepagefloatstyle}[3]{%
%    \end{macrocode}
% Make sure that the two styles have been called, otherwise we get some
% undefined errors.
%    \begin{macrocode}
  \@nameuse{ps@#3}\@nameuse{ps@#2}%
%    \end{macrocode}
% Specify the pagestyle's headers and footers.
%    \begin{macrocode}
  \@namedef{ps@#1}{%
  \def\@evenhead{\ifonlyfloats{\@nameuse{#3@evenhead}}%
                {\@nameuse{#2@evenhead}}}%
  \def\@oddhead{\ifonlyfloats{\@nameuse{#3@oddhead}}%
               {\@nameuse{#2@oddhead}}}%
  \def\@evenfoot{\ifonlyfloats{\@nameuse{#3@evenfoot}}%
                {\@nameuse{#2@evenfoot}}}%
  \def\@oddfoot{\ifonlyfloats{\@nameuse{#3@oddfoot}}%
               {\@nameuse{#2@oddfoot}}}%
%    \end{macrocode}
% Set the hook to the \meta{textstyle} on the assumption that that is
% more complex that required for a float page.
%    \begin{macrocode}
  \@namedef{#1pshook}{\@nameuse{#2pshook}}%
%    \end{macrocode}
% That's it.
%    \begin{macrocode}
}}
%    \end{macrocode}
% \end{macro}
%
%    The pagestyles \pstyle{empty} and \pstyle{plain} are defined 
%    in \file{latex.dtx}. However, I will redefine them here, just in
% case someone takes a fancy to modifying them.
%
% \begin{macro}{\ps@empty}
% The \pstyle{empty} pagestyle is simple, it's just what we get when
% we call \cs{makepagestyle}.
%    \begin{macrocode}
\makepagestyle{empty}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ps@plain}
% The \pstyle{plain} pagestyle is also simple, it just puts the
% page number at the bottom middle of the page.
% We call \cs{makepagestyle{plain}} and then adjust as required.
%    \begin{macrocode}
\makepagestyle{plain}
  \makeevenfoot{plain}{}{\thepage}{}
  \makeoddfoot{plain}{}{\thepage}{}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ps@headings}
%    The \pstyle{headings} pagestyle behaves differently for
% twosided and onesided printing. This is a rewrite of the
% standard style.
% \changes{v1.1}{2002/03/10}{Added \cs{tocmark} etc., to \cs{ps@headings}}
%    \begin{macrocode}
\if@twoside
%    \end{macrocode}
% The footer is empty and the header contains the page number and
% one of the marks.
%    \begin{macrocode}
  \makepagestyle{headings}
    \makepsmarks{headings}{%
      \let\@mkboth\markboth
      \def\chaptermark##1{%
        \markboth{\MakeUppercase{%
          \ifnum \c@secnumdepth >\m@ne
            \if@mainmatter
              \@chapapp\ \thechapter. \ %
            \fi
          \fi
          ##1}}{}}%
      \def\tocmark{\markboth{\MakeUppercase{\contentsname}}{}}%
      \def\lofmark{\markboth{\MakeUppercase{\listfigurename}}{}}%
      \def\lotmark{\markboth{\MakeUppercase{\listtablename}}{}}%
      \def\bibmark{\markboth{\MakeUppercase{\bibname}}{}}%
      \def\indexmark{\markboth{\MakeUppercase{\indexname}}{}}%
      \def\sectionmark##1{%
        \markright{\MakeUppercase{%
          \ifnum \c@secnumdepth > \z@
            \thesection. \ %
          \fi
          ##1}}}%
    }%
    \makeevenhead{headings}{\thepage}{}{\slshape\leftmark}
    \makeoddhead{headings}{\slshape\rightmark}{}{\thepage}
\else
%    \end{macrocode}
% For one sided printing even and odd pages are treated the same,
% so no need to bother with the evenhead, and
% just the \cs{rightmark} is used.     
%    \begin{macrocode}
  \makepagestyle{headings}
    \makepsmarks{headings}{%
      \let\@mkboth\markboth
      \def\chaptermark##1{%
        \markright{\MakeUppercase{%
          \ifnum \c@secnumdepth >\m@ne
            \if@mainmatter
              \@chapapp\ \thechapter. \ %
            \fi
          \fi
          ##1}}}%
      \def\tocmark{\markright{\MakeUppercase{\contentsname}}}%
      \def\lofmark{\markright{\MakeUppercase{\listfigurename}}}%
      \def\lotmark{\markright{\MakeUppercase{\listtablename}}}%
      \def\bibmark{\markright{\MakeUppercase{\bibname}}}%
      \def\indexmark{\markright{\MakeUppercase{\indexname}}}%
    }
    \makeoddhead{headings}{\slshape\rightmark}{}{\thepage}
\fi

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ps@myheadings}
%    The \pstyle{myheadings} page style is simpler because the user
% has to specify the contents using \cs{markboth} and \cs{markright}
% commands. This is the definition used in the standard classes.
% \changes{v1.1}{2002/03/10}{Added \cs{tocmark} etc., to \cs{ps@myheadings}}
%    \begin{verbatim}}
% \newcommand{\ps@myheadings}{%
%   \let\@oddfoot\@empty\let\@evenfoot\@empty
%   \def\@evenhead{\thepage\hfil{\slshape\leftmark}}%
%   \def\@oddhead{{\slshape\rightmark}\hfil\thepage}%
%   \let\@mkboth\@gobbletwo
%   \let\chaptermark\@gobble
%   \let\sectionmark\@gobble
% }
% \end{verbatim}
% Translating that into our terms we get:
% \changes{v1.0}{2001/11/15}{Changed the code for creating the myheadings pagestyle}
% \changes{v1.4}{2003/11/22}{Further change to myheadings pagestyle code}
%    \begin{macrocode}
\makepagestyle{myheadings}
  \makepsmarks{myheadings}{%
    \let\@mkboth\@gobbletwo
    \let\chaptermark\@gobble
    \let\sectionmark\@gobble
    \def\tocmark{}%
    \def\lofmark{}%
    \def\lotmark{}%
    \def\bibmark{}%
    \def\indexmark{}%
  }
  \makeevenhead{myheadings}{\thepage}{}{\slshape\leftmark}
  \makeoddhead{myheadings}{\slshape\rightmark}{}{\thepage}

%    \end{macrocode}
% (Kai von Fintel (\texttt{fintel@mit.edu}) emailed me on 2003/02/24
% saying that the original \verb?\let\tocmark\@gobble? in the myheadings
% pagestyle did nasty things
% to the ToC, etc. Now using \verb?\def\tocmark{}?.)
% \end{macro}
%
% \begin{macro}{\ps@chapter}
% \begin{macro}{\ps@part}
% \begin{macro}{\ps@cleared}
%  The standard classes use the \pstyle{plain} pagestyle for the first page
% of a chapter. This class uses the \pstyle{chapter} instead, which is
% aliased to \pstyle{plain}. Similarly for parts. 
% Further, \cs{cleardoublepage} uses whatever
% pagestyle is in effect for the empty verso page. I find that this
% looks odd if the header contains a chapter name. This class uses
% the \pstyle{cleared} pagestyle in this case. I have aliased this
% to \pstyle{empty}; the \pstyle{plain} would be another reasonable choice.
%    \begin{macrocode}
\aliaspagestyle{chapter}{plain}
\aliaspagestyle{part}{plain}
\aliaspagestyle{cleared}{empty}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\cleardoublepage}
% A slight adjustment to the kernel definition to set a pagestyle.
%    \begin{macrocode}
\def\cleardoublepage{\clearpage\if@twoside \ifodd\c@page\else
  \hbox{}\thispagestyle{cleared}%
  \newpage\if@twocolumn\hbox{}\newpage\fi\fi\fi}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\ps@ruled}
%    Partly to show how it is done, the class provides a \pstyle{ruled} pagestyle.
% In this style the headers and footers are the same width as the textblock,
% there is a rule under the header, page numbers are set in the footers
% at the outside of the page. Even page headers have the chapter number
% and title at the left, and odd page headers have the section title
% at the right.
%
%    Start by making the (empty) \pstyle{ruled} pagestyle.
%    \begin{macrocode}
\makepagestyle{ruled}
%    \end{macrocode}
% There is no need to change the default width (which is the \cs{textwidth}),
% nor the default positions (centered),
% nor to make the footrule visible. We do, though, have to put the page
% numbers into the footers.
%    \begin{macrocode}
\makeevenfoot{ruled}{\thepage}{}{}
\makeoddfoot{ruled}{}{}{\thepage}
%    \end{macrocode}
% Make the header rule visible and equal to the \cs{textwidth}.
%    \begin{macrocode}
\makeheadrule{ruled}{\textwidth}{\normalrulethickness}
%    \end{macrocode}
%
% \begin{macro}{\@ruledmarks}
% We have to make sure that the \cs{chapter} and \cs{section} commands make
% the appropriate marks for use in the headers. We use the hook for this.
% Note that contrary to normal \LaTeX{} practice, the titles are not
% automatically upper-cased. The marks for the tocbibinds also need adjusting.
% \changes{v1.1}{2002/03/10}{Changed \cs{@ruledmarks} to cater for tocbibind}
%    \begin{macrocode}
\renewcommand*{\@ruledmarks}{%
  \let\@mkboth\markboth
  \def\chaptermark##1{%
    \markboth{%
      \ifnum \c@secnumdepth >\m@ne
        \if@mainmatter
          \thechapter. \ %
        \fi
      \fi
      ##1}{}}
  \def\sectionmark##1{\markright{##1}}%
  \def\tocmark{\markboth{\contentsname}{}}%
  \def\lofmark{\markboth{\listfigurename}{}}%
  \def\lotmark{\markboth{\listtablename}{}}%
  \def\bibmark{\markboth{\bibname}{}}%
  \def\indexmark{\markboth{\indexname}{}}%
}
\makepsmarks{ruled}{\@ruledmarks}
%    \end{macrocode}
% \end{macro}
%
% We can now define the even page header which is to have the chapter title
% at the left. As the chapter mark did no upper-casing we will print it
% using small caps, but just use the normal font for section title on
% the odd page header.
%    \begin{macrocode}
\makeevenhead{ruled}{\scshape\leftmark}{}{}
\makeoddhead{ruled}{}{}{\rightmark}

%    \end{macrocode}
% \end{macro}
% This is all that we need to do for the \pstyle{ruled} pagestyle.
%
% \begin{macro}{\ps@Ruled}
%    Also define a \pstyle{Ruled} pagestyle similar to \pstyle{ruled} except that the
% headers and footers are 10\% wider than the textblock, sticking out
% into the fore edge.
%    \begin{macrocode}
\makepagestyle{Ruled}
\makerunningwidth{Ruled}{1.1\textwidth}
\makeheadposition{Ruled}{flushright}{flushleft}{flushright}{flushleft}
\makeevenfoot{Ruled}{\thepage}{}{}
\makeoddfoot{Ruled}{}{}{\thepage}
\makeheadrule{Ruled}{1.1\textwidth}{\normalrulethickness}
\makepsmarks{Ruled}{\@ruledmarks}
\makeevenhead{Ruled}{\scshape\leftmark}{}{}
\makeoddhead{Ruled}{}{}{\rightmark}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\headwidth}
% \begin{macro}{\ps@companion}
% A \pstyle{companion} pagestyle like the one in the 
% \textit{\LaTeX{} Companion} series.
% We need the \cs{headwidth} length for this.
% \changes{v1.1}{2002/03/10}{Added \cs{tocmark} etc., to \cs{ps@companion}}
%    \begin{macrocode}
%%%%\newlength{\headwidth}  % don't use in the patch file

\makepagestyle{companion}
\setlength{\headwidth}{\textwidth}
  \addtolength{\headwidth}{\marginparsep}
  \addtolength{\headwidth}{\marginparwidth}
\makerunningwidth{companion}{\headwidth}
\makeheadrule{companion}{\headwidth}{\normalrulethickness}
\makeheadposition{companion}{flushright}{flushleft}{}{}
\makepsmarks{companion}{%
  \let\@mkboth\markboth
  \def\chaptermark##1{\markboth{##1}{##1}}    % left mark & right marks
  \def\sectionmark##1{\markright{%
    \ifnum \c@secnumdepth>\z@
      \thesection. \ %
    \fi
    ##1}}
  \def\tocmark{\markboth{\contentsname}{\contentsname}}%
  \def\lofmark{\markboth{\listfigurename}{\listfigurename}}%
  \def\lotmark{\markboth{\listtablename}{\listtablename}}%
  \def\bibmark{\markboth{\bibname}{\bibname}}%
  \def\indexmark{\markboth{\indexname}{\indexname}}%
}
\makeevenhead{companion}{\normalfont\bfseries\thepage}{}%
                        {\normalfont\bfseries\leftmark}
\makeoddhead{companion}{\normalfont\bfseries\rightmark}{}%
                       {\normalfont\bfseries\thepage}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%% \subsection{Theindex}
%
% On 2006/12/05 Lars Madsen asked for an `indextitlepagestyle' (he wanted
% to put a hyperlink bar with links to each letter startpoint into the
% header).
% \begin{macro}{\ps@indextitlepagestyle}
% Here is half of it, the other half is in the definition of \cs{theindex}.
%    \begin{macrocode}
\aliaspagestyle{indextitlepagestyle}{chapter}

%    \end{macrocode}
% \end{macro}
%
% \subsection{New Listof...}
%
% Ignacio Fern\'{a}ndez Galv\'{a}n (aka Jellby) noted a problem with
% changing chapter styles and the ToC heading 
% (ctt, \textit{Inconsistency in memoir}, 2006/11/04). Unfortunately the
% fix involves repeating much prior code. Essentially, I got the 
% setting of \cs{parskip} equal to \cs{cftparskip} in the wrong place.
% The following has been copied from \file{memoir.dtx} and appropriate
% changes made.
%
% \begin{macro}{\newlistof}
% \cs{newlistof}\marg{listofcmmd}\marg{ext}\marg{listofname}
% creates the command \cs{listofcmmd} to typeset a new List of, where the
% external file has the extension \file{.ext} and the heading title is
% \meta{listofname}. The code for this is a heavily modified part
% of the \Lpack{tocloft} package.
%
%    \begin{macrocode}
%%%%%%% Fix for inconsistent chapter styles and ToC, etc style.
\renewcommand*{\newlistof}[3]{%
%    \end{macrocode}
%
% In the following, \verb?X? stands for the value of \meta{listofcmmd} and
% \verb?Z? stands for the value of \meta{ext}.
%
% \begin{macro}{\ext@Z}
% \begin{macro}{\Zdepth}
% The file extension and listing depth, which is set to level~1.
%    \begin{macrocode}
  \@namedef{ext@#2}{#2}
  \@ifundefined{c@#2depth}{\newcounter{#2depth}}{}
  \setcounter{#2depth}{1}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\Zmark}
% The heading marks for the listing.
% \changes{v1.1}{2002/03/10}{Changed \cs{@mkboth} to \cs{markboth} in \cs{Zmark}}
%    \begin{macrocode}
  \@namedef{#2mark}{\markboth{#3}{#3}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\X}
% Typeset the listing title and entries, 
% with both a normal and starred version.
%    \begin{macrocode}
  \@namedef{#1}{\@ifstar{\@nameuse{@star#2}}{\@nameuse{@plain#2}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@starZ}
% The starred listof version
%    \begin{macrocode}
  \@namedef{@star#2}{%
    \ensureonecol
    \par
    \begingroup
%%%%%%%      \parindent\z@ \parskip\cftparskip
      \@nameuse{@#2maketitle}
      \@starttoc{#2}%
    \endgroup
    \restorefromonecol}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@plainZ}
% The unstarred listof version
%    \begin{macrocode}
  \@namedef{@plain#2}{%
    \ensureonecol
    \par
    \begingroup
%%%%%%%      \parindent\z@ \parskip\cftparskip
      \@nameuse{@#2maketitle}
      \phantomsection
      \addcontentsline{toc}{chapter}{#3}
\parskip\cftparskip
      \@starttoc{#2}%
    \endgroup
    \restorefromonecol}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@Zmaketitle}
% This macro typesets the title.
% \changes{v1.3}{2002/10/10}{Added \cs{printZnonum} to \cs{@Zmaketitle}}
%    \begin{macrocode}
  \@namedef{@#2maketitle}{%
    \@nameuse{#2headstart}
   {\parindent\z@ 
%%%%%%%%    \parskip\cftparskip
    \interlinepenalty\@M
%    \end{macrocode}
% \changes{v1.3}{2002/11/14}{Added printZnonum to \cs{newlistof}}
%    \begin{macrocode}
    \@nameuse{print#2nonum}%
    \@nameuse{print#2title}{#3}%
    \@nameuse{#2mark}%
    \thispagestyle{chapter}%
    \@nameuse{after#2title}
   }
    \@afterheading}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\Zheadstart}
% \begin{macro}{\afterZtitle}
% The macros \cs{Zheadstart} and \cs{afterZtitle}
% control what goes before and after the title. They
% default to the corresponding macros for chapters.
%    \begin{macrocode}
  \@namedef{#2headstart}{\chapterheadstart}
  \@namedef{after#2title}{\afterchaptertitle}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\printZnonum}
% This typesets something before the title and defaults
% to \cs{printchapternonum}.
% \changes{v1.3}{2002/10/10}{Added \cs{printZnonum} macro}
%    \begin{macrocode}
  \@namedef{print#2nonum}{\printchapternonum}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\printZtitle}
% The title is typeset by the macro \cs{printZtitle}\marg{title}
% which defaults to \cs{printchaptertitle}.
%    \begin{macrocode}
  \@namedef{print#2title}##1{\printchaptertitle{##1}}
%    \end{macrocode}
% \end{macro}
%
% This is the end of the definition of \cs{newlistof}.
%    \begin{macrocode}
} % end \newlistof

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@starttoc}
% A list of macro calls \cs{@starttoc} to read the appropriate file. I have
% changed the kernel definition to allow a file to be read multiple times
% by delaying killing the file until the end of the document.
% \changes{v1.3}{2002/11/14}{Changed \cs{@starttoc}}
%    \begin{macrocode}
\renewcommand*{\@starttoc}[1]{%
  \begingroup\makeatletter
    \@input{\jobname.#1}%
    \if@filesw
      \AtEndDocument{%
        \expandafter\newwrite\csname tf@#1\endcsname
        \immediate\openout \csname tf@#1\endcsname \jobname.#1\relax
      }%
    \fi
  \@nobreakfalse
  \endgroup}

%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
\newlistof{tableofcontents}{toc}{\contentsname}
\newlistof{listoffigures}{lof}{\listfigurename}
\newlistof{listofables}{lot}{\listtablename}
%%% remove subfigure support
\renewcommand*{\@cftl@subfigtab}{}

%    \end{macrocode}
%
% \subsection{Chapter styles}
%
% I have added some further chapter styles. These have either been sent to me,
% published on CTT or are my own invention.
%
% \begin{macro}{\chs@default}
% A reimplementation of the default chapter style which sets everything.
%    \begin{macrocode}
\makechapterstyle{default}{%
  \setlength{\beforechapskip}{50pt}
  \def\chapterheadstart{\vspace*{\beforechapskip}}
  \def\chapnamefont{\normalfont\huge\bfseries}
  \def\printchaptername{\chapnamefont \@chapapp}
  \def\chapternamenum{\space}
  \def\chapnumfont{\normalfont\huge\bfseries}
  \def\printchapternum{\chapnumfont \thechapter}
  \setlength{\midchapskip}{20pt}
  \def\afterchapternum{\par\nobreak\vskip \midchapskip}
  \def\printchapternonum{}
  \def\chaptitlefont{\normalfont\Huge\bfseries}
  \def\printchaptertitle##1{\chaptitlefont ##1}
  \setlength{\afterchapskip}{40pt}
  \def\afterchaptertitle{\par\nobreak\vskip \afterchapskip}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\chs@bianchi}
% Posted to CTT on 2003/12/09, \textit{New chapter style: chapter vs chapter*}
% by Stefano Bianchi.
%    \begin{macrocode}
%% Stefano Bianchi, ctt 2003/12/09 `New chapter style: chapter vs chapter*'
\makechapterstyle{bianchi}{%
  \renewcommand*{\chapnamefont}{\normalfont\Large\sffamily\itshape}
  \renewcommand*{\chapnumfont}{\normalfont\huge}
  \renewcommand*{\printchaptername}{%
    \chapnamefont\centering\@chapapp}
  \renewcommand*{\printchapternum}{\chapnumfont \textit{\thechapter}}
  \renewcommand*{\chaptitlefont}{\normalfont\Huge\sffamily}
  \renewcommand*{\printchaptertitle}[1]{%
    \hrule\vskip\onelineskip \centering \chaptitlefont\textbf{##1}\par}
  \renewcommand*{\afterchaptertitle}{\vskip\onelineskip \hrule\vskip
    \afterchapskip}
  \renewcommand*{\printchapternonum}{%
    \vphantom{\chapnumfont \textit{9}}\afterchapternum}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\chs@bringhurst}
% My bringhurst style.
%    \begin{macrocode}
\makechapterstyle{bringhurst}{%
  \renewcommand*{\chapterheadstart}{}
  \renewcommand*{\printchaptername}{}
  \renewcommand*{\chapternamenum}{}
  \renewcommand*{\printchapternum}{}
  \renewcommand*{\afterchapternum}{}
  \renewcommand*{\printchaptertitle}[1]{%
    \raggedright\Large\scshape\MakeLowercase{##1}}
  \renewcommand*{\afterchaptertitle}{%
  \vskip\onelineskip \hrule\vskip\onelineskip}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\chs@brotherton}
% An extremely simple chapterstyle created by William Adams for Mike 
% Brotherton's science fiction novel \textit{Star Dragon}, posted to CTT
% 2006/12/09, \textit{An example of a novel?}.
%    \begin{macrocode}
\makechapterstyle{brotherton}{%
  \renewcommand*{\printchapternum}{\chapnumfont 
    \ifanappendix \thechapter \else \numtoName{\c@chapter}\fi}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\chs@chappell}
% My Chappell style
%    \begin{macrocode}
\makechapterstyle{chappell}{%
  \setlength{\beforechapskip}{0pt}
  \renewcommand*{\chapnamefont}{\large\centering}
  \renewcommand*{\chapnumfont}{\large}
  \renewcommand*{\printchapternonum}{%
    \vphantom{\printchaptername \chapnumfont 1}
    \afterchapternum
    \vskip \onelineskip \vskip -\topskip}
  \renewcommand*{\chaptitlefont}{\Large\itshape}
  \renewcommand*{\printchaptertitle}[1]{%
    \hrule\vskip\onelineskip \centering\chaptitlefont ##1}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\chs@culver}
% By me in an answer to Christopher Culver on CTT, 
% \textit{"Biblical" formatting, how?} on 2004/03/29,where I called it 
% the `biblical' style.
%    \begin{macrocode}
\makechapterstyle{culver}{%
  \chapterstyle{article}%
%%%  \renewcommand*{\thechapter}{\Roman{chapter}}
  \renewcommand*{\printchapternum}{% center number/title
    \centering\chapnumfont \thechapter\space}%
  \renewcommand*{\printchapternonum}{\centering}
  \renewcommand*{\clearforchapter}{}% no new page
  \aliaspagestyle{chapter}{headings}% no special pagestyle
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\chs@dash}
% A very simple style of mine but I couldn't think of a good name for it.
%    \begin{macrocode}
\makechapterstyle{dash}{%
  \setlength{\beforechapskip}{5\onelineskip}
  \renewcommand*{\printchaptername}{}
  \renewcommand*{\chapternamenum}{}
  \renewcommand*{\chapnumfont}{\normalfont\large}
  \settoheight{\midchapskip}{\chapnumfont 1}
  \renewcommand*{\printchapternum}{\centering \chapnumfont 
    \rule[0.5\midchapskip]{1em}{0.4pt} \thechapter\ 
    \rule[0.5\midchapskip]{1em}{0.4pt}}
  \renewcommand*{\afterchapternum}{\par\nobreak\vskip 0.5\onelineskip}
  \renewcommand*{\printchapternonum}{\centering 
                 \vphantom{\chapnumfont 1}\afterchapternum}
  \renewcommand*{\chaptitlefont}{\normalfont\Large}
  \renewcommand*{\printchaptertitle}[1]{\centering \chaptitlefont ##1}
  \setlength{\afterchapskip}{2.5\onelineskip}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\chs@demo2}
% My second version of the demo chapterstyle.
%    \begin{macrocode}
\makechapterstyle{demo2}{%
  \renewcommand*{\printchaptername}{\centering}
  \renewcommand*{\printchapternum}{\chapnumfont 
     \ifanappendix \thechapter \else \numtoName{\c@chapter}\fi}
  \renewcommand*{\chaptitlefont}{\normalfont\Huge\sffamily}
  \renewcommand*{\printchaptertitle}[1]{%
    \hrule\vskip\onelineskip \raggedleft \chaptitlefont ##1}
  \renewcommand*{\afterchaptertitle}{%
    \vskip\onelineskip \hrule\vskip \afterchapskip}
  \setlength{\beforechapskip}{3\baselineskip}
  \renewcommand*{\printchapternonum}{%
    \vphantom{\chapnumfont One}
    \afterchapternum%
    \vskip\topskip}
  \setlength{\beforechapskip}{2\onelineskip}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\chs@ell}
% Another of my styles which I'll probably use for my next book.
%    \begin{macrocode}
\makechapterstyle{ell}{%
  \renewcommand*{\chapnumfont}{\normalfont\HUGE\sffamily}
  \renewcommand*{\chaptitlefont}{\normalfont\huge\sffamily}
  \settowidth{\chapindent}{\chapnumfont 111}
  \renewcommand*{\chapterheadstart}{\begingroup
    \vspace*{\beforechapskip}%
    \begin{adjustwidth}{}{-\chapindent}%
    \hrulefill
    \smash{\rule{0.4pt}{15mm}}
    \end{adjustwidth}\endgroup}
  \renewcommand*{\printchaptername}{}
  \renewcommand*{\chapternamenum}{}
  \renewcommand*{\printchapternum}{%
    \begin{adjustwidth}{}{-\chapindent}
    \hfill
    \raisebox{10mm}[0pt][0pt]{\chapnumfont \thechapter}%
                              \hspace*{1em}
    \end{adjustwidth}\vspace*{-3.0\onelineskip}}
  \renewcommand*{\printchaptertitle}[1]{%
    \vskip\onelineskip
    \raggedleft {\chaptitlefont ##1}\par\nobreak}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\chs@ger}
% Posted to CTT on 2002/04/12 \textit{Fancy Headings, Chapter Headings} by Gerardo Garcia.
% \changes{v4.7}{2006/12/23}{Renamed the `Ger' chapterstyle to `ger'}
%    \begin{macrocode}
%% Gerardo Garcia, ctt 2002/04/12, `Fancy Headings, Chapter Headings
\makechapterstyle{ger}{%
  \renewcommand*{\chapterheadstart}{\vspace*{\beforechapskip}
  \mbox{}\\\mbox{}\rule[0pt]{\textwidth}{0.4pt}\par}
  \setlength{\midchapskip}{20pt}
  \renewcommand*{\printchaptertitle}[1]{\chaptitlefont ##1
    \\\mbox{}\rule[5pt]{\textwidth}{0.4pt}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\chs@lyhne}
% Posted to CTT 2006/02/09 \textit{Glossary}, by Anders Lyhne.
% It requires the \Lpack{graphicx} package. I have modified it to remove the
% new length and adjusted the unnumbered appearance.
%    \begin{macrocode}
\makechapterstyle{lyhne}{%  needs graphicx package
  \setlength{\beforechapskip}{1.5cm}
  \setlength{\afterchapskip}{1cm}
  \setlength{\midchapskip}{2cm}
  \renewcommand*{\chapnamefont}{\normalfont\normalsize\scshape\raggedleft}
  \renewcommand*{\chaptitlefont}{\normalfont\normalsize\bfseries\sffamily\raggedleft}
  \renewcommand*{\chapternamenum}{}
  \renewcommand*{\printchapternum}{\makebox[0pt][l]{\hspace{0.2em}%
    \resizebox{!}{2ex}{\chapnamefont\bfseries\sffamily\thechapter}}}
  \renewcommand*{\afterchapternum}{\par\hspace{1.5cm}\hrule\vspace{0.2cm}}
 \renewcommand*{\printchapternonum}{\vphantom{\chapnamefont 1}\afterchapternum}
  \renewcommand*{\afterchaptertitle}{\vskip 0.2cm 
    \hrule\vskip\afterchapskip}}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\chs@madsen}
% Posted to CTT on 2003/12/09, \textit{New chapter style: chapter vs chapter*}
% by Lars Madsen. 
% This requires the \Lpack{graphicx} package,
% \changes{v4.7}{2006/12/23}{Added \cs{raggedleft} to \cs{chaptitlefont} in the madsen chapterstyle}
%    \begin{macrocode}
%% posted to ctt, December 2003
%%%% \usepackage{graphicx}
\makechapterstyle{madsen}{%
  \renewcommand*{\chapnamefont}{%
    \normalfont\Large\scshape\raggedleft}
  \renewcommand*{\chaptitlefont}{%
    \normalfont\Huge\bfseries\sffamily\raggedleft}
  \renewcommand*{\chapternamenum}{}
  \renewcommand*{\printchapternum}{%
    \makebox[0pt][l]{\hspace{0.4em}
      \resizebox{!}{4ex}{%
        \chapnamefont\bfseries\sffamily\thechapter}
    }%
  }%
  \renewcommand*{\afterchapternum}{%
    \par\hspace{1.5cm}\hrule\vskip\midchapskip}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\colorchapnum}
% \begin{macro}{\colorchaptitle}
% \begin{macro}{\chs@pedersen}
% Posted to CTT 2006/01/31, \textit{Chapter style}, by Troels Pedersen.
% It requires the \Lpack{graphicx} package and possibly the \Lpack{color}
% package. I have used a \cs{sidebar} instead of the original \cs{marginpar},
% and eliminated a new length. The original color specification, the same for
% the title and nunmber, can now be set via \cs{colorchapnum} and
% \cs{colorchaptitle}.
%    \begin{macrocode}
\newcommand*{\colorchapnum}{}
\newcommand*{\colorchaptitle}{}
\makechapterstyle{pedersen}{%
  \setlength{\beforechapskip}{-20pt}
  \setlength{\afterchapskip}{10pt}
  \renewcommand*{\chapnamefont}{\normalfont\LARGE\itshape}
  \renewcommand*{\chapnumfont}{\normalfont\HUGE\itshape\colorchapnum}
  \renewcommand*{\chaptitlefont}{\normalfont\huge\itshape\colorchaptitle}
  \renewcommand*{\afterchapternum}{}
  \renewcommand*{\printchaptername}{}
  \setlength{\midchapskip}{20mm}% was \numberheight
  \renewcommand*{\chapternamenum}{}
  \renewcommand*{\printchapternum}{%
    \sidebar{\raisebox{0pt}[0pt][0pt]{\makebox[0pt][l]{%
      \resizebox{!}{\midchapskip}{\chapnumfont\thechapter}}}}}
  \renewcommand*{\printchaptertitle}[1]{\chaptitlefont ##1}
}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\chs@southall}
% Style provided by Thomas Dye. I have modified the original to eliminate
% the use of new lengths.
%    \begin{macrocode}
%% Thomas Dye's southall chapter style
\makechapterstyle{southall}{%
  \setlength{\afterchapskip}{5\baselineskip}
  \setlength{\beforechapskip}{36pt}%    \headindent
  \setlength{\midchapskip}{\textwidth}% \rightblock
  \addtolength{\midchapskip}{-\beforechapskip}
  \renewcommand*{\chapterheadstart}{\vspace*{2\baselineskip}}
  \renewcommand*{\chaptitlefont}{\huge\rmfamily\raggedright}
  \renewcommand*{\chapnumfont}{\chaptitlefont}
  \renewcommand*{\printchaptername}{}
  \renewcommand*{\chapternamenum}{}
  \renewcommand*{\afterchapternum}{}
  \renewcommand*{\printchapternum}{%
    \begin{minipage}[t][\baselineskip][b]{\beforechapskip}
      {\vspace{0pt}\chapnumfont%%%\figureversion{lining} 
                   \thechapter}
    \end{minipage}}
  \renewcommand*{\printchaptertitle}[1]{%
    \hfill\begin{minipage}[t]{\midchapskip}
      {\vspace{0pt}\chaptitlefont ##1\par}\end{minipage}}
  \renewcommand*{\afterchaptertitle}{%
    \par\vspace{\baselineskip}%
    \hrulefill \par\nobreak\noindent \vskip \afterchapskip}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\chs@thatcher}
%  Original posted to CTT on 2006/01/18 by Scott Thatcher,
% \textit{memoir: chapter headings capitalize math symbols}. I have modified
% it to cater for multiline titles, appendices, and unnumbered chapters.
%    \begin{macrocode}
\makechapterstyle{thatcher}{%
  \renewcommand*{\chapterheadstart}{}
  \renewcommand*{\printchaptername}{%
    \centerline{\chapnumfont{\@chapapp\ \thechapter}}}
  \renewcommand*{\chapternamenum}{}
  \renewcommand*{\chapnumfont}{\normalfont\scshape\MakeLowercase}
  \renewcommand*{\printchapternum}{}
  \renewcommand*{\afterchapternum}{%
    \par\centerline{\parbox{0.5in}{\hrulefill}}\par}
  \renewcommand*{\printchapternonum}{%
    \vphantom{\chapnumfont \@chapapp 1}\par 
    \parbox{0.5in}{}\par}
  \renewcommand*{\chaptitlefont}{\normalfont\large}
  \renewcommand*{\printchaptertitle}[1]{%
    \centering \chaptitlefont\MakeUppercase{##1}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\chs@veelo}
% This, from Baastian Veelo, has been noted in the documentation for 
% quite a time.
% I have modified the original to eliminate the need for extra lengths.
%    \begin{macrocode}
%% A new chapter style, that suits well for trimmed documents.
%% We are scaling the chapter number, which most DVI viewers
%% will not display accurately. It requires the graphicx package.
%%%% \usepackage{graphicx}
\makechapterstyle{veelo}{%
   \setlength{\afterchapskip}{40pt}
  \renewcommand*{\chapterheadstart}{\vspace*{40pt}}
  \renewcommand*{\afterchapternum}{\par\nobreak\vskip 25pt}
   \renewcommand*{\chapnamefont}{\normalfont\LARGE\flushright}
   \renewcommand*{\chapnumfont}{\normalfont\HUGE}
   \renewcommand*{\chaptitlefont}{\normalfont\HUGE\bfseries\flushright}
   \renewcommand*{\printchaptername}{%
     \chapnamefont\MakeUppercase{\@chapapp}}
   \renewcommand*{\chapternamenum}{}
%   \setlength{\numberheight}{18mm}
%   \setlength{\barlength}{\paperwidth}
%   \addtolength{\barlength}{-\textwidth}
%   \addtolength{\barlength}{-\spinemargin}
  \setlength{\beforechapskip}{18mm}%  \numberheight
  \setlength{\midchapskip}{\paperwidth}% \barlength
  \addtolength{\midchapskip}{-\textwidth}
  \addtolength{\midchapskip}{-\spinemargin}
   \renewcommand*{\printchapternum}{%
     \makebox[0pt][l]{%
       \hspace{.8em}%
       \resizebox{!}{\beforechapskip}{\chapnumfont \thechapter}%
       \hspace{.8em}%
       \rule{\midchapskip}{\beforechapskip}%
     }%
   }%
   \makeoddfoot{plain}{}{}{\thepage}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\chs@verville}
% I posted the original to CTT on 2005/01/18, \textit{Headers and special
% formatting of sections}, in answer to a question by 
% Guy Verville. This version caters for unnumbered chapters.
%    \begin{macrocode}
\makechapterstyle{verville}{%
  \setlength{\beforechapskip}{0pt}
  \renewcommand*{\printchaptername}{}
  \renewcommand*{\printchapternum}{%
    \hrule \vskip 0.5\onelineskip
    \Huge \centering \thechapter.\ }
  \renewcommand*{\printchapternonum}{%
    \hrule \vskip 0.5\onelineskip
    \Huge \centering}
  \renewcommand*{\afterchapternum}{}
  \setlength{\midchapskip}{0pt}
  \renewcommand*{\printchaptertitle}[1]{%
    ##1 \par
    \vskip 0.5\onelineskip
   \hrule}}

%    \end{macrocode}
% \end{macro}
%
%
% \section{Mempatch version 4.7}
%
%    \begin{macrocode}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Version 4.7 (2006/12/23 and later)

%    \end{macrocode}
%
% \subsection{Epigraphs}
%
% Erik Quaeghebeur (CTT, 2006/12/21, \textit{[memoir:epigraph] dissapearing initial L/l
% (minimal example)}) discovered that an initial `l' was deleted when
% \texttt{flushleftright} was used for \cs{epigraphtextposition}. This was caused
% by `fil' being the last token in the definition.
%
% \begin{macro}{flushleftright}
% \changes{v4.7}{2006/12/23}{Changed \cs{flushleftright} and \cs{centerlastline}
%  so that `fil' was not the last token}
%    \begin{macrocode}
\renewcommand*{\flushleftright}{%
  \leftskip\z@ \rightskip\z@
  \parfillskip\@flushglue}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{centerlastline}
%    \begin{macrocode}
\renewcommand*{\centerlastline}{%
  \parfillskip=\z@ plus 2fil
  \rightskip=\z@ plus -1fil
  \leftskip\@flushglue}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{@epitext}
% \changes{v4.7}{2006/12/23}{Added \cs{par} to \cs{@epitext} and \cs{@episource}}
%    \begin{macrocode}
\renewcommand{\@epitext}[1]{%
  \begin{minipage}{\epigraphwidth}\begin{\textflush} #1\par
  \ifdim\epigraphrule>\z@ \@epirule \else \vspace*{1ex}\fi
  \end{\textflush}\end{minipage}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{@episource}
%    \begin{macrocode}
\renewcommand{\@episource}[1]{%
  \begin{minipage}{\epigraphwidth}\begin{\sourceflush} #1\par
  \end{\sourceflush}\end{minipage}}

%    \end{macrocode}
% \end{macro}
%
% \section{Mempatch version 4.8}
%
%    \begin{macrocode}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Version 4.8 (2007/01/22 and later)

%    \end{macrocode}
%
% \subsection{Abstracts}
%
%    There are various problems with the code for abstracts, partly because
% it was too early in the class file (it should have come \emph{after}
% the code for lists).
% \changes{v4.8}{2007/01/22}{Several changes to the code for abstracts}
%
% \begin{macro}{\abscolnamefont}
% \begin{macro}{\abscoltextfont}
% Fonts for an abstract in a two column document where the abstract 
% is part of a column. Also the default \cs{absrightmargin}.
% \changes{v4.8}{2007/01/22}{Added \cs{abscolnamefont} and \cs{abscoltextfont}}
%    \begin{macrocode}
%%%% \abscolnamefont and \abscoltextfont are the fonts when an abstract
%%%% is like a section in a two column document.
\newcommand*{\abscolnamefont}{\normalfont\Large\bfseries}
\newcommand*{\abscoltextfont}{\normalfont}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\absleftindent}
% \begin{macro}{\abs@leftindent}
% \begin{macro}{\absrightindent}
% Set these lengths \emph{after} \cs{leftmargin} and \cs{rightmargin} have 
% been set.
%    \begin{macrocode}
\absleftindent=\leftmargin
\abs@leftindent=\leftmargin
\absrightindent=\leftmargin

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\setup@bstract}
% The \cs{setup@bstract} macro set \cs{absrightindent} and it shouldn't
% have done.
% \changes{v4.8}{2007/01/22}{Deleted always setting \cs{absrightindent} in \cs{setup@bstract}}
%    \begin{macrocode}
\renewcommand*{\setup@bstract}{%
  \abs@leftindent=\absleftindent
  \if@twocolumn
    \if@bsonecol
    \else
      \abs@leftindent=\z@
      \absrightindent=\z@
      \renewcommand*{\abstractnamefont}{\abscolnamefont}
      \renewcommand*{\abstracttextfont}{\abscoltextfont}
      \renewcommand*{\absnamepos}{flushleft}
      \setlength{\abstitleskip}{-2ex}
    \fi
  \fi}

%    \end{macrocode}
% \end{macro}
%
%
% The end of the patch file
%
%    \begin{macrocode}
%</patch>
%    \end{macrocode}
%
%
% \bibliographystyle{alpha}
% \begingroup
% \raggedright
% \begin{thebibliography}{GMSN94A}
%
%
% \bibitem[GMS94]{GOOSSENS94}
% Michel Goossens, Frank Mittelbach, and Alexander Samarin.
% \newblock \emph{The LaTeX Companion}.
% \newblock Addison-Wesley Publishing Company, 1994.
%
%
% \bibitem[Rah01]{NAMEREF}
% Sebastian Rahtz.
% \newblock \emph{Section name references in LaTeX}.
% \newblock January 2001.
% \newblock (Available from CTAN in
%            \texttt{/macros/latex/contrib/hyperref})
%
% \bibitem[Rah02]{HYPERREF}
% Sebastian Rahtz.
% \newblock \emph{Hypertext marks in LaTeX}.
% \newblock March 2002.
% \newblock (Available from CTAN in
%            \texttt{/macros/latex/contrib/hyperref})
%
% \bibitem[Wil04]{MEMOIR}
% Peter Wilson.
% \newblock \emph{The LaTeX \Lpack{memoir} class for configurable 
%                 typesetting: Source code}.
% \newblock January 2004.
% \newblock (Available from CTAN in
%            \texttt{macros/latex/contrib/memoir})
%
%
% \end{thebibliography}
% \endgroup
%
% \Finale
%
\endinput

%% \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         \~}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

