% \iffalse meta-comment
%
% memoir.dtx
% Author: Peter Wilson (Herries Press) herries dot press at earthlink dot net
% Copyright 2001--2005 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{20643}
%
% \changes{v0.1}{2001/05/20}{First public alpha release}
% \changes{v0.2}{2001/06/03}{First beta release}
% \changes{v0.3}{2001/07/09}{Bug fixing}
% \changes{v0.31}{2001/07/09}{Bug fixing}
% \changes{v0.32}{2001/08/03}{Fix for old versions of amsmath package}
% \changes{v0.33}{2001/08/03}{Added controllable chapter/LoF spacing}
% \changes{v0.33}{2001/08/03}{Fixed bug in \cs{appendixpage}}
% \changes{v1.0}{2001/10/30}{First production release}
% \changes{v1.0a}{2001/12/07}{Feature fixes} ^^A never released
% \changes{v1.1}{2002/03/10}{More fixups}
% \changes{v1.1}{2002/03/10}{Replaced all the subfigure code}
% \changes{v1.1}{2002/03/28}{Added sidebars}
% \changes{v1.1}{2002/03/28}{Upgraded verse typesetting}
% \changes{v1.2}{2002/07/07}{Added 17pt option}
% \changes{v1.2}{2002/07/07}{Added extra \cs{provide} commands}
% \changes{v1.2}{2002/07/27}{Major verbatim, box and file additions}
% \changes{v1.2}{2002/07/27}{Removed extraneous space from change marks}
% \changes{v1.2}{2002/07/27}{Extra optarg in \cs{chapter} and \cs{chapter*}}
% \changes{v1.2}{2002/07/27}{Twiddled font sizes a little}
% \changes{v1.2}{2002/08/27}{Fixed 12pt bug}
% \changes{v1.3}{2002/09/27}{Minor tweaks wrt v1.2}
% \changes{v1.3}{2002/10/10}{Extra trimming styles}
% \changes{v1.3}{2002/10/10}{Major extensions for indexing}
% \changes{v1.3}{2002/11/14}{Automatically call a patch file}
% \changes{v1.3a}{2002/11/22}{Fixed nasty figure/table bug}
% \changes{v1.4}{2003/02/27}{Added patches v1.1}
% \changes{v1.4}{2003/02/27}{Added patches v1.3}
% \changes{v1.4}{2003/11/16}{Including current patches}
% \changes{v1.4}{2003/11/16}{Added patches v1.2}
% \changes{v1.4}{2003/11/22}{Added patches v1.4}
% \changes{v1.4}{2003/11/22}{Added patches v1.5}
% \changes{v1.4}{2003/11/22}{Added patches v1.6}
% \changes{v1.4}{2003/11/22}{Added patches v1.7}
% \changes{v1.4}{2003/11/30}{Added patches v1.8}
% \changes{v1.4}{2003/11/30}{Added patches v1.9}
% \changes{v1.6}{2004/01/31}{Decided to issue v1.4 as v1.6 on the way to 
%                            the golden section}
% \changes{v1.61}{2004/03/14}{Changes to clo files, and footnotes} 
% \changes{v1.61}{2004/03/21}{Removed a potential problem with the 
%                             hangul package}
% \changes{v1.618}{2005/09/02}{Folded in all v1.61 patches}
% \changes{v1.618}{2005/09/15}{Some minor extensions}
% \changes{v1.618}{2005/09/25}{Support for glossaries}
%
% \def\dtxfile{memoir.dtx}
%
% \def\fileversion{v0.1}  \def\filedate{2001/05/20}
% \def\fileversion{v0.2}  \def\filedate{2001/06/03}
% \def\fileversion{v0.3}  \def\filedate{2001/07/14}
% \def\fileversion{v0.31} \def\filedate{2001/07/24}
% \def\fileversion{v0.32} \def\filedate{2001/08/03}
% \def\fileversion{v0.33} \def\filedate{2001/08/31}
% \def\fileversion{v1.0}  \def\filedate{2001/11/17}
% \def\fileversion{v1.0a} \def\filedate{2001/12/07}
% \def\fileversion{v1.1}  \def\filedate{2002/03/28}
% \def\fileversion{v1.1a} \def\filedate{2002/04/28}
% \def\fileversion{v1.2}  \def\filedate{2002/08/10}
% \def\fileversion{v1.2}  \def\filedate{2002/08/27}
% \def\fileversion{v1.3}  \def\filedate{2002/11/14}
% \def\fileversion{v1.3a} \def\filedate{2002/11/22}
% \def\fileversion{v1.4}  \def\filedate{2003/12/18}
% \def\fileversion{v1.6}  \def\filedate{2004/01/31}
% \def\fileversion{v1.61}  \def\filedate{2004/04/04}
% \def\fileversion{v1.618}  \def\filedate{2005/09/27}
% \title{The LaTeX \Lpack{memoir} class for configurable book 
%        typesetting: Source code\thanks{This
%        file (\texttt{\dtxfile}) has version number \fileversion, last revised
%        \filedate.}}
%
% \author{%
% Peter Wilson\thanks{\texttt{herries dot press at earthlink dot net}}\\
% Herries Press}
% \date{\filedate}
% \maketitle
%
% \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.
%
%    The class was first released in mid 2001 and has been well used ever
% since.
% \end{abstract}
%
% \tableofcontents
% \listoftables
%
% \StopEventually{}
%
% 
%
% \section{Introduction}
%
% This document provides the commented source 
% for the LaTeX \Lpack{memoir} class, which 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.
%
%   The default appearance of a document typeset with this class is
% the same as if it had been typeset with the \Lpack{book} class and
% it can be made to simulate the \Lpack{article} class..
% The class, though, includes extra facilities that make it easy to
% change the appearance of such things as the page headers and footers,
% the style of chapter and other sectional headings, and the style of
% captions. It also makes it easy to both change the style of the
% Table of Contents, List of Figures, etc., as well as creating new
% kinds of `List of\ldots'. New types of floats, if needed, can be
% created very simply. Epigraphs can be put into the document in a
% variety of styles.
%
%    The class provides a variety of page, chapter and captioning
% styles that you can choose from if you don't want to create your own.
%
%    Documents can be typeset in 9pt, 10pt, 11pt, 12pt, 14pt or 17pt font
% sizes and there is a reasonably intuitive means of setting the margins
% and placement of the text on a page. There is an option to put
% trim marks on the printed pages if the stock sheets need to be trimmed
% down to the final page size. For those whose publishers like a
% manuscript to look as though it was typewritten, there is an option
% to do this (double spacing, ragged right, no hyphenation, 
% fixed width font). There is also an unsophisticated means of flagging any
% revisions to the text.
%
%    As this is a new class, by default it does not support the old 
% LaTeX v2.09
% font commands, namely the |\bf|, |\sl|, |\it| and |\sc| commands;
% it warns about using the |\em| command but does support it.
%
%
% I hope that apart from the font commands the class 
% is compatible with
% `standard' LaTeX.
%
%    Development of this class would never have been started without
% the wonderful work done by Leslie Lamport and others~\cite{CLASSES}
% from whom I have learned a great deal and borrowed much code. 
%
%    Sections~\ref{sec:docstrip} through~\ref{sec:ic} describe some
% administrative elements and code for general use later in the 
% specification.
% The macros forming the class file are defined in sections~\ref{sec:opt}
% through~\ref{sec:init}. 
% 
% 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[T1]{fontenc}
\usepackage{url}
%%\usepackage[draft=false,
%%            plainpages=false,
%%            pdfpagelabels,
%%            bookmarksnumbered,
%%            hyperindex=true
%%           ]{hyperref}  % Doesn't work with indexing of \DescribeMacro
\providecommand{\phantomsection}{} % just in case hyperref not used 
%    \end{macrocode}
%
%    We do want an index, using linenumbers, but not update information.
%    \begin{macrocode}
\EnableCrossrefs
\CodelineIndex
%% \RecordChanges
%    \end{macrocode}
% We had better have page headings to aid navigation, but I don't
% like Uppercased titles.
%    \begin{macrocode}
\makeatletter
  \@mparswitchfalse
\makeatother
\renewcommand{\MakeUppercase}[1]{#1}
\pagestyle{headings}
%    \end{macrocode}
%    We may use so many \file{docstrip} modules that 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}}                % comp.text.tex
\newenvironment{PW}{\em}{}
\newcommand*{\theTeXbook}{\textit{The \TeX book}}
%    \end{macrocode}
%    We want the full details printed.
%    \begin{macrocode}
\begin{document}
\raggedbottom
\raggedright
\DocInput{memoir.dtx}
\PrintIndex
%% \PrintChanges
\end{document}
%</driver>
%    \end{macrocode}
%
%
%   ^^A  There are some elements we do not want to appear in the index.
% \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{Identification} \label{sec:id}
%
%    The \Lpack{memoir} document class can only be used with LaTeX2e, 
% so we make
%    sure that an appropriate message is displayed when another \TeX{}
%    format is used.
%    \begin{macrocode}
%<class>\NeedsTeXFormat{LaTeX2e}
%    \end{macrocode}
%
%    Announce the name, option files and version for LaTeX2e files:
%    \begin{macrocode}
%<class>\ProvidesClass{memoir}%
%<class>              [2005/09/25 v1.618 configurable document class]
%<9pt>\ProvidesFile{mem9.clo}%
%<9pt>             [2004/03/12 v0.3 memoir class 9pt size option]
%<10pt>\ProvidesFile{mem10.clo}%
%<10pt>             [2002/07/27 v0.2 memoir class 10pt size option]
%<11pt>\ProvidesFile{mem11.clo}%
%<11pt>             [2002/07/27 v0.2 memoir class 11pt size option]
%<12pt>\ProvidesFile{mem12.clo}%
%<12pt>             [2004/03/12 v0.3 memoir class 12pt size option]
%<14pt>\ProvidesFile{mem14.clo}%
%<14pt>             [2004/03/12 v0.3 memoir class 14pt size option]
%<17pt>\ProvidesFile{mem17.clo}%
%<17pt>             [2004/03/12 v0.2 memoir class 17pt size option]
%    \end{macrocode}
%
%
% \section{Initial Code} \label{sec:ic}
%
%    \begin{macrocode}
%<*class>
%    \end{macrocode}
%
% Note (2001/08/03): Old versions of the \Lpack{amsmath} package did odd
% things with \cs{@tempa}, \cs{@tempb} and \cs{@tempc}. I have now replaced
% any use of these with \cs{@memtempa}, etc.
%
%    In this part we define a few commands that are used later on.
%
% \begin{macro}{\@ptsize}
%    This control sequence is used to store the second digit of the
%    pointsize we are typesetting in. So, normally, it's value is one
%    of 0, 1 or 2.
%    \begin{macrocode}
\newcommand{\@ptsize}{}
%    \end{macrocode}
% \end{macro}
%
% Any new lengths that depend on the point size option must be declared
% before the options are executed.
% \begin{macro}{\onelineskip}
% \begin{macro}{\lxvchars}
% \begin{macro}{\xlvchars}
% The length \cs{onelineskip} is the vertical space taken
% by a normal line of text. The lengths \cs{lxvchars} and \cs{xlvchars} are
% the approximate lengths required for typesetting lines with either 65 or 45
% characters.
%    \begin{macrocode}
\newlength{\onelineskip}
\newlength{\lxvchars}
\newlength{\xlvchars}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@memcnta}
% We need a scratch count register.
%    \begin{macrocode}
\newcount\@memcnta
%    \end{macrocode}
% \end{macro}
%
% \changes{v1.0a}{2001/11/28}{Deleted count register \cs{@memtempa}}
%
% \begin{macro}{\if@restonecol}
%    When the document has to be printed in two columns, we sometimes
%    have to temporarily switch to one column. This switch is used to
%    remember to switch back.
%    \begin{macrocode}
\newif\if@restonecol
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\if@openright}
% This is TRUE if chapters are to start on righthand (recto) pages;
% this is the default. FALSE means chapters can start on any page.
%    \begin{macrocode}
\newif\if@openright
  \@openrighttrue

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\if@openleft}
% This is TRUE if chapters are to start on lefthand (verso) pages.
%    \begin{macrocode}
\newif\if@openleft
  \@openleftfalse

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\if@mainmatter}
% This is TRUE if the main part of the document is being currently procesed;
% this is the default.
%    \begin{macrocode}
\newif\if@mainmatter
  \@mainmattertrue

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\if@memoldfont}
% This is TRUE if the \Lopt{oldfontcommands} option is used.
% \changes{v1.0a}{2002/01/29}{Added \cs{if@memoldfont}}
%    \begin{macrocode}
\newif\if@memoldfont
  \@memoldfontfalse

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@memerror}
% \begin{macro}{\@memwarn}
% Two macros to save some space when reporting errors or warnings.
% The macros take the same arguments, ignoring the first in each case,
% as |\ClassError| and |\ClassWarning|, e.g., \\
% |\@memwarn{Message}| \\
% instead of \\
% |\ClassWarning{memoir}{Message}|.
% \changes{v1.4}{2003/02/27}{Added \cs{@memerror} and \cs{@memwarn}}
%    \begin{macrocode}
\newcommand*{\@memerror}{\ClassError{memoir}}
\newcommand*{\@memwarn}{\ClassWarning{memoir}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\ifsamename}
% \begin{macro}{\nametest}
%    The macro |\nametest{|\meta{name1}|}{|\meta{name2}|}| tests whether the
% characters forming the two arguments are the same or not. If they are the
% same then |\ifsamename| is set TRUE, otherwise it is set FALSE. \meta{name1}
% and \meta{name2} must both be either macro names (including the backslash)
% or must both not be macro names. That is, you cannot do |\nametest{\cs}{cs}|.
%    \begin{macrocode}
\newif\ifsamename
\newcommand{\nametest}[2]{%
  \samenamefalse
  \begingroup
  \def\@memtempa{#1} \def\@memtempb{#2}
  \ifx \@memtempa\@memtempb
    \endgroup
    \samenametrue
  \else
    \endgroup
  \fi}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\ifm@m@And}
% \begin{macro}{\m@m@Andtrue}
% \begin{macro}{\m@m@Andfalse}
% \begin{macro}{\ifm@m@Or}
% \begin{macro}{\m@m@Ortrue}
% \begin{macro}{\m@m@Orfalse}
% \begin{macro}{\ifm@m@Xor}
% \begin{macro}{\m@m@Xortrue}
% \begin{macro}{\m@m@Xorfalse}
% These are for `if A and B', `if A or B' and `if A xor B'.
%
% For A and B:
% \begin{verbatim}
% \m@m@Andfalse
% \ifA
%   \ifB
%     \m@mAndtrue
%   \fi
% \fi
% \end{verbatim}
% For A or B:
% \begin{verbatim}
% \m@m@Ortrue
% \ifA
% \else
%   \ifB
%   \else
%     \m@m@Orfalse
%   \fi
% \fi
% \end{verbatim}
% For A xor B:
% \begin{verbatim}
% \m@m@Xortrue
% \ifA
%   \ifB
%     \m@m@Xfalse
%   \fi
% \else
%   \ifB
%   \else
%     \m@m@Xorfalse
%   \fi
% \fi
% \end{verbatim}
% \changes{v1.618}{2005/09/09}{Added \cs{ifm@m@And}, \cs{ifm@m@Or} and \cs{ifm@m@Xor}}
%    \begin{macrocode}
\newif\ifm@m@And
\newif\ifm@m@Or
\newif\ifm@m@Xor

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \changes{v0.3}{2001/07/09}{Deleted \cs{fillline} and \cs{makecommand} as
% they were not used}
%
%
% \begin{macro}{\kill@lastcounter}
% |\kill@lastcounter{|\meta{cntr}|}|\footnote{With thanks to Stefan Ulrich
% (\texttt{ulrich@cis.uni-muenchen.de}) who answered a question on this
% on \ctt, 2001/07/09. (Also answered by Heiko Oberdiek,
% \texttt{oberdiek@ruf.uni-freiburg.de}).}
%  kills the counter \meta{cntr}. This macro is an extension to the
% suggestions as it also makes the last count register reusable.
% \emph{The macro must only be used when the last allocated counter is to
% be killed.}
% For example with:\\
% |\newcounter{fred} \kill@lastcounter{fred} \newcounter{fred}| \\
% there is no error with the second |\newcounter|.
% \changes{v0.3}{2001/07/09}{Added \cs{kill@counter}}
% \changes{v1.618}{2005/09/02}{Ensured \cs{kill@counter} zeroed the counter}
%    \begin{macrocode}
\newcommand{\kill@lastcounter}[1]{%
%    \end{macrocode}
% Deallocate the last counter register. From \theTeXbook, |\count10| is
% the number of the last register. The counter should be zeroed before being
% deallocated otherwise an immediatally following \cs{newcount} may not
% be zero when allocated\footnote{Discovered and fix provided by Robert 
% Schlicht on 2005/08/31.}.
%    \begin{macrocode}
  \count\count10 \z@
  \advance\count10 \m@ne
%    \end{macrocode}
% Set the internal counter definition to |\relax|.
%    \begin{macrocode}
  \expandafter\let\csname c@#1\endcsname\relax}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@name@p@xdf}
% \begin{macro}{\@name@unresp@xdef}
% \begin{macro}{\@namelet}
% \begin{macro}{\@namelongdef}
%  Utility definition macros, along the lines of the kernel's \cs{@namedef}.
% \changes{v1.4}{2003/11/22}{Added \cs{@namelet} and friends (from patch v1.8)}
%    \begin{macrocode}
\newcommand{\@name@p@xdef}[1]{%
  \expandafter\protected@xdef\csname #1\endcsname}
\newcommand{\@name@unresp@xdef}[1]{%
  \expandafter\unrestored@protected@xdef\csname #1\endcsname}
\newcommand{\@namelet}[1]{%
  \expandafter\let\csname #1\endcsname}
\newcommand{\@namelongdef}[1]{%
  \long\expandafter\def\csname #1\endcsname}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\memjustarg}
% \begin{macro}{\memgobble}
% Utility macros \emph{that are not to be changed by any user!}
% \changes{v1.618}{2005/09/25}{Added \cs{memjustarg} and \cs{memgobble}}
%    \begin{macrocode}
\newcommand{\memjustarg}[1]{#1}
\newcommand{\memgobble}[1]{}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@memfakeusepackage}
% \cs{@memfakeusepackage}\marg{pack} fools \LaTeX{} into thinking
% that the \meta{pack.sty} package has been loaded.
% \changes{v1.4}{2003/11/22}{Added \cs{@memfakeusepackage} (from patch v1.9)}
%    \begin{macrocode}
\newcommand*{\@memfakeusepackage}[1]{%
  \@namelet{ver@#1.sty}\@empty}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\EmulatedPackage}
% \begin{macro}{\EmulatedPackageWithOptions}
%  There was some discussion on CTT\footnote{\textit{Re: breakurl + pdfetex 
% for generating .dvi}, March 2005.} about how to prevent loading of
% a package. My response was to use \cs{@memfakusepackage}. Donald Arseneau
% came up with this comprehensive solution (there was a little discussion
% about naming with my final suggestion as here). \\
% \cs{EmulatedPackage}\marg{package}\oarg{date} \\
% \cs{EmulatedPackageWithOptions}\marg{option-list}\marg{package}\oarg{date} \\
%
% \changes{v1.618}{2005/09/04}{Added \cs{EmulatedPackage} and
%          \cs{EmulatedPackageWithOptions}}
%    \begin{macrocode}
\providecommand*{\EmulatedPackage}{}
\renewcommand*{\EmulatedPackage}[1]{%
  \@ifnextchar[{\@emulated@package{#1}}%
               {\@emulated@package{#1}[]}%]
}
\providecommand*{\EmulatedPackageWithOptions}{}
\renewcommand*{\EmulatedPackageWithOptions}[2]{%
  \PassOptionsToPackage{#1}{#2}%
  \EmulatedPackage{#2}%
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@emulated@package}
% The workhorse for the user commands.
%    \begin{macrocode}
\def\@emulated@package#1[#2]{%
  \expandafter\xdef\csname ver@#1.\@pkgextension\endcsname{#2}%
  \@ifundefined{opt@#1.\@pkgextension}%
    {\@namedef{opt@#1.\@pkgextension}{}}{}%
  \wlog{Package #1 \if,\csname opt@#1.\@pkgextension\endcsname,\else
        (with options \csname opt@#1.\@pkgextension\endcsname)\fi
        emulated by \@currname.}%
}
\@onlypreamble\EmulatedPackage
\@onlypreamble\EmulatedPackageWithOptions
\@onlypreamble\@emulated@package

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\ifpdf}
% This can be used to check whether or not a document is being processed
% by \LaTeX{} or pdf\LaTeX. 
% \changes{v1.0}{2001/09/21}{Made checking of \cs{pdfouput} more robust}
% \changes{v1.618}{2005/09/03}{Prohibit loading the ifpdf package (mempatch v3.9)}
%    \begin{macrocode}
\newif\ifpdf
\ifx\pdfoutput\undefined
\else
  \ifx\pdfoutput\relax
  \else
    \ifcase\pdfoutput
    \else
      \pdftrue
    \fi
  \fi
\fi
%%\@memfakeusepackage{ifpdf}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\phantomsection}
% A command needed if the \Lpack{hyperref} package is used, for putting
% before certain |\addcontentsline| commands.
%    \begin{macrocode}
\newcommand{\phantomsection}{}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\nofiles}
% The kernel's \cs{nofiles}, which surprisingly may be called \emph{before} 
% the class is loaded, lets \cs{makeindex} to \cs{relax}. This could 
% cause problems\footnote{As reported by Heiko Oberdiek on 2005/07/09.}
% with my initial version of \cs{makeindex}.
% Here's the kernel \cs{nofiles} definition:
% \begin{verbatim}
% \def\nofiles{%
%   \@fileswfalse%  flag for suppressing \immediate \writes
%   \typeout{No auxiliary output files.^^J}%
%   \long\def\protected@write##1##2##3%
%     {\write\m@ne{}\if@nobreak\ifvmode\nobreak\fi\fi}%
%   \let\makeindex\relax
%   \let\makeglossary\relax}
% \end{verbatim}
% To get it to work for when \cs{makeindex}es optional argument is
% used I let \cs{makeindex} itself deal with \cs{nofiles} (and
% also \cs{makeglossary}).
% \changes{v1.618}{2005/09/25}{Modified the kernel's \cs{nofiles}}
%    \begin{macrocode}
 \renewcommand*{\nofiles}{%
   \@fileswfalse%  flag for suppressing \immediate \writes
   \typeout{No auxiliary output files.^^J}%
   \long\def\protected@write##1##2##3%
     {\write\m@ne{}\if@nobreak\ifvmode\nobreak\fi\fi}%
   }

%    \end{macrocode}
% \end{macro}
%
% \subsection{Extending an existing macro}
%
%
%    My usual technique for modifying the code of an existing
% macro was to use |\renewcommand|. However it slowly became apparent that
% many of such modifications merely consisted of adding some code at
% either the start or end of the existing definition. In June 2000 I 
% posed a question
% on the |comp.text.tex| newsgroup about this, and 
% Michael Downes\footnote{\texttt{epsmjd@ams.org}} and
% Heiko Oberdiek\footnote{\texttt{oberdiek@ruf.uni-freiburg.de}} 
% were kind enough to respond.
% The responses arrived almost simultaneously, both
% supplying methods for adding code at the end of a macro defined to take
% one argument. Michael Downes' response included a remark about it probably
% being too much effort to formulate a general way of doing this.
% Heiko Oberdiek almost immediately replied with such a generalisation.
%
%    The following code, which I have extracted from a package that I
% wrote but which I did not submit to CTAN, incorporates the work of both
% Michael and Heiko. I have added a bit. All errors are my responsibility.
% Michael went on the write the \Lpack{patchcmd} package which is
% a generalisation of the facilities below and so there was no need for
% my more restricted package.
% \changes{v0.33}{2001/08/31}{Replaced \cs{appendiarg} with \cs{addtodef}
%                             and friends}
%
% \begin{macro}{\wo@dmacro}
%    Heiko Oberdiek suggested\footnote{Via email on 2000/07/12.} that
% \meta{macro} should be tested for being the name of a macro, rather than
% using the normal \LaTeX{} test against it being undefined. He supplied
% the code for performing the macro name test.
%
% |\wo@dmacro| is a helper for handling the string `macro:'.
%    \begin{macrocode}
\edef\wo@dmacro{%
  \string m\string a\string c\string r\string o\string :%
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\wo@difmacro@begingroup}
% This seems to take two arguments: 
% \cs{wo@difmacro@begingroup}\marg{macro}\marg{code}, where 
% \meta{macro} should be the name of a defined macro and \meta{code}
% is code to be executed if and only if \meta{macro} \emph{is} a
% defined macro.
%    \begin{macrocode}
\def\wo@difmacro@begingroup#1{%
%    \end{macrocode}
% Start a group.
%    \begin{macrocode}
  \begingroup
%    \end{macrocode}
% Test if |#1| is a defined macro; |\wo@dparsemacro| does most of the work.
%    \begin{macrocode}
    \edef\x{%
      \noexpand\wo@dparsemacro\meaning#1\wo@dmacro\string -%
    }%
    \x\@nil{#1}%
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\wo@dparsemacro}
% This is called by |\wo@difmacro@begingroup|. If \meta{macro} is not
% a defined macro it prints a warning and closes the group begun by
% |\wo@difmacro@begingroup|. Otherwise it process the \meta{code} which
% \emph{must} close the group.
%    \begin{macrocode}
\begingroup
  \edef\x{\endgroup
    \def\noexpand\wo@dparsemacro##1\wo@dmacro##2\string -}%
\x#3\@nil#4{%
  \ifx\\#3\\%
    \endgroup
    \@memwarn{\string `\string #4\string ' is not a macro}%
    \expandafter\@gobble
  \else
    \expandafter\@firstofone
  \fi
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\addtodef}
% \begin{macro}{\addtodef*}
% \begin{macro}{\wo@daddtodef}
% The command |\addtodef{|\meta{macro}|}{|\meta{start-stuff}|}{|\meta{end-stuff}|}|
% adds stuff at the start and/or end of an argumentless  macro. 
% Initially the code for appending was a reimplementation of the
% kernel |\g@addto@macro| command (from \file{ltclass.dtx}).
% Later, at Michael Downes' suggestion, I combined
% appending and prepending stuff into a single command. 
% Actually, there is no real need to have both
% |\addtodef| and |\addtodef*| as the body of the command
% being amended has no argument,
% but both versions are provided for consistency.
%
%    The kernel |\@star@or@long| and |\l@ngrel@x| commands 
% (in \file{ltdefns.dtx}) are used to handle 
% the potential |*| after the command name.
%    \begin{macrocode}
\def\addtodef{\@star@or@long\wo@daddtodef}
%    \end{macrocode}
% The |\@star@or@long| command dealt with a possible |*| and now
% |\wo@daddtodef| does the work. It picks up the three arguments
% that the user thinks belong to |\addtodef|, namely \meta{macro},
% \meta{start-stuff} and \meta{end-stuff}.
%    \begin{macrocode}
\long\def\wo@daddtodef#1#2#3{%
%    \end{macrocode}
% Check if \meta{macro} has been defined. 
%    \begin{macrocode}
  \wo@difmacro@begingroup{#1}{%
%    \end{macrocode}
% If \meta{macro} is defined then store the tokens corresponding to the 
% body of \meta{macro} and the extra \meta{-stuff} in token registers.
%    \begin{macrocode}
    \@temptokena{#2}%
    \toks@\expandafter{#1#3}%
%    \end{macrocode}
% Do an expanded definition for |\x|, so that calling |\x|
% will |\def| a new version of \meta{macro}, whose body consists of the
% saved tokens (i.e., the orginal body plus the extra stuff). 
% As Michael Downes noted, single letter control sequences do not take any of
% \TeX's hash table space.
% |\l@ngrel@x|
% has been previously set to either |\long| or |\relax| by |\@star@or@long|,
% and so may make the new definition of \meta{macro} to be |\long|. 
%    \begin{macrocode}
    \edef\x{\endgroup 
         \l@ngrel@x\def\noexpand#1{\the\@temptokena \the\toks@}}%
%    \end{macrocode}
% Finally, call |\x| to perform the new definition.
%    \begin{macrocode}
    \x
  }%
}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\addtoiargdef}
% \begin{macro}{\addtoiargdef*}
% \begin{macro}{\wo@daddtoiargdef}
%  This adds stuff at the start and/or end of a macro that takes one argument.
% It is a modification of Michael Downes' appending 
% code\footnote{Posted to \ctt{} on 15~June 2000,\\
% URL: \url{http://www.dejanews.com/getdoc.xp?AN=635057844}.}
% which was:
% \begin{small}\begin{verbatim}
% \def\appendef#1#2{%
%   \begingroup
%     \toks@\expandafter{#1{##1}#2}%
%     \edef\x{\endgroup \def\noexpand#1####1{\the\toks@}}%
%   \x}
% \end{verbatim}
% \par\end{small}
%
%    \begin{macrocode}
\def\addtoiargdef{\@star@or@long\wo@daddtoiargdef}
\long\def\wo@daddtoiargdef#1#2#3{%
  \wo@difmacro@begingroup{#1}{%
    \@temptokena{#2}%
    \toks@\expandafter{#1{##1}#3}%
    \edef\x{\endgroup 
       \l@ngrel@x\def\noexpand#1####1{\the\@temptokena \the\toks@}}%
    \x
  }%
}

%    \end{macrocode}
%    For the record, Heiko Oberdiek produced the following 
% version\footnote{Posted to \ctt{} on 16~June 2000,\\
% URL: \url{http://www.dejanews.com/getdoc.xp?AN=635095381}.}
% which avoids the use of a token register.
% \begin{small}\begin{verbatim}
% \newcommand{\appendiargdef}[2]{%
%   \long\expandafter\def\expandafter#1\expandafter
%   ##\expandafter1\expandafter{#1{##1}#2}}
% \end{verbatim}
% \par\end{small}
%
% \end{macro}
% \end{macro}
% \end{macro}
%
% Continuing the saga, Michael came up with a pretty general solution for
% modifying a macro with any number (up to 9) of arguments which he
% sent to me on 2000/07/13; this became
% the \Lpack{patchcmd} package which is on CTAN. I'm sure that Michael 
% would have given me permission to include it here if he had not tragically
% passed away in 2003. I miss his encouragement, advice, and skills.
%
% \begin{macro}{\patchcmd}
% \cs{patchcmd}\marg{macro}\marg{start-stuff}\marg{end-stuff} inserts
% \meta{start-stuff} at the beginning of the definition of \meta{macro}
% and \meta{end-stuff} at the end of the definition. \meta{macro}
% can have 0--9 arguments and be defined by \cs{newcommand} (and associates)
% or with \cs{DeclareRobustCommand}. For macros that use \cs{futurelet}
% (e.g., those with starred forms or optional arguments) only prepending
% works --- any non-empty \meta{end-stuff} will mess things up. \cs{patchcmd}
% does not work with macros that have delimited arguments.
% \changes{v1.618}{2005/09/05}{Added \cs{patchcmd}}
%
%    \begin{macrocode}
%%%%%%%%%%%%%%%% Michael Downes' patchcmd 2000/07/31 v1.03 %%%%%%%%
\newcommand{\patchcommand}[1]{%
  \expandafter\patchcmd@a\meaning#1??->@\@nil#1%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\patchcmd@a}
%    \begin{macrocode}
\long\def\patchcmd@a#1#2#3->#4#5\@nil#6{%
  \ifx @#4\relax \patchcmdError#6#1%
    \expandafter\@gobbletwo % discard the other two arguments
  \else
    \if l#2\toks@{\patchcmd@e{}#6}% l in this position means \long
    \else \toks@{\patchcmd@e*#6}%   not \long
    \fi
    \patchcmd@b #3@#4#5 ? ? ? \@nil#6%
    \expandafter\the\expandafter\toks@
  \fi}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\patchcmd@b}
%    \begin{macrocode}
\def\patchcmd@b#1:#2@#3#4 #5#6 #7 #8\@nil#9{%
  \if \ifx @#7@\expandafter
      \ifx\csname #6\endcsname#9T\else F\fi\else F\fi T%
    \toks@\expandafter{\expandafter\patchcommand\csname #6 \endcsname}%
  \else
    \ifx @#2@% No arguments
      \toks@\expandafter{\the\toks@ 0}%
    \else
      \patchcmd@c 0#2{\string##}0%
    \fi
  \fi}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\patchcmd@c}
%    \begin{macrocode}
\def\patchcmd@c#1#2#3{%
  \if\string###2%      % yes it's a # token
    \ifodd 0#31 % and it's followed by a number
      \if 0#3\patchcmd@d#1\fi % number=0? then we're done
    \else \patchcmd@d D% # not a number: must be a delimited arg
    \fi
  \else \patchcmd@d D% not a # token: must be a delmited arg
  \fi
  \patchcmd@c#3}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\patchcmd@d}
%    \begin{macrocode}
\def\patchcmd@d#1{%
  \if D#1%
%%%    \PackageError{patchcmd}{Cannot change a macro that has
%%%      delimited arguments}\@ehd
    \@memerror{%
      Cannot change a macro that has delimited arguments}{\@ehd}
  \else
    \toks@\expandafter{\the\toks@ #1}%
  \fi
  \begingroup
  \aftergroup\@gobble
  \let\patchcmd@c\endgroup}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\patchcmd@e}
%    \begin{macrocode}
\def\patchcmd@e#1#2#3#4#5{%
  \begingroup
  \edef\@##1{%
    \@temptokena\noexpand\expandafter{%
      \noexpand#2%
        \ifnum#3>0 {####1}\ifnum#3>1 {####2}\ifnum#3>2 {####3}%
        \ifnum#3>3 {####4}\ifnum#3>4 {####5}\ifnum#3>5 {####6}%
        \ifnum#3>6 {####7}\ifnum#3>7 {####8}\ifnum#3>8 {####9}%
        \fi\fi\fi\fi\fi\fi\fi\fi\fi
      ##1%
    }%
  }
  \@{#5}%
  \edef\@##1{\endgroup
    \noexpand\renewcommand#1\noexpand#2\ifcase#3 \else [#3]\fi
    {##1\the\@temptokena}}%
  \@{#4}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\patchcmdError}
%    \begin{macrocode}
\long\def\patchcmdError#1#2{%
  \begingroup
  \toks@{Not redefinable}%
  \ifcat\relax\noexpand#1% Is it a control sequence?
    \begingroup
    \let#1=?\ifx ?\relax % Is it "\relax"?
      \endgroup % accept current value of \toks@
    \else \endgroup
      \if\ifx\relax#1u\else #2\fi u%
        \toks@{Not defined}%
      \fi
    \fi
  \fi
  \edef\@{\endgroup
%%%    \noexpand\PackageError{patchcmd}{%
%%%      \the\toks@: \string#1}\noexpand\@ehd}%
    \noexpand\@memerror{%
      \the\toks@: \string#1}\noexpand\@ehd}%
  \@}

%%%%%%%%%%%%%%%%%%%%% end of patchcmd code %%%%%%%%%%%%%%%%%%%%%%%%%%%

%    \end{macrocode}
% \end{macro}
% 
% Prevent later loading of the original \Lpack{patchcmd} package.
%    \begin{macrocode}
%%\@memfakeusepackage{patchcmd}

%    \end{macrocode}
% 
%
% \section{Declaration of Options} \label{sec:opt}
%
%
% \subsection{Setting Paper Sizes}
%
%    The variables |\paperheight| and |\paperwidth| should reflect the
%    physical paper size after trimming. 
%
%    Option \Lopt{letterpaper} will be the default.
%
% \begin{macro}{\stockheight}
% \begin{macro}{\stockwidth}
% \begin{macro}{\trimtop}
% \begin{macro}{\trimedge}
% The lengths |\stockheight| and |\stockwidth| should be the height and 
% width of the stock sheet before trimming. For example, this is the 
% physical size
% of a single sheet that might be laser-printed. The lengths |\trimtop|
% and |\trimedge| are the amount that will be trimmed off the top and fore
% edge of the physical sheet.
% For desk printer output the size of the trimmed sheet is often the
% same as the physical sheet. In other words the |\paper...| and |\stock...|
% sizes are the same and the trims are zero. This class assumes that this
% is the normal case.
%    \begin{macrocode}
\newlength{\stockheight}
\newlength{\stockwidth}
\newlength{\trimtop}
\newlength{\trimedge}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
%    Declare the stock size options.
%    \begin{macrocode}
\DeclareOption{a3paper}
   {\setlength\stockheight {420mm}%
    \setlength\stockwidth  {297mm}}
\DeclareOption{a4paper}
   {\setlength\stockheight {297mm}%
    \setlength\stockwidth  {210mm}}
\DeclareOption{a5paper}
   {\setlength\stockheight {210mm}%
    \setlength\stockwidth  {148mm}}
\DeclareOption{a6paper}
   {\setlength\stockheight {148mm}%
    \setlength\stockwidth  {105mm}}
\DeclareOption{b3paper}
   {\setlength\stockheight {500mm}%
    \setlength\stockwidth  {353mm}}
\DeclareOption{b4paper}
   {\setlength\stockheight {353mm}%
    \setlength\stockwidth  {250mm}}
\DeclareOption{b5paper}
   {\setlength\stockheight {250mm}%
    \setlength\stockwidth  {176mm}}
\DeclareOption{b6paper}
   {\setlength\stockheight {176mm}%
    \setlength\stockwidth  {125mm}}
\DeclareOption{letterpaper}
   {\setlength\stockheight {11in}%
    \setlength\stockwidth  {8.5in}}
\DeclareOption{legalpaper}
   {\setlength\stockheight {14in}%
    \setlength\stockwidth  {8.5in}}
\DeclareOption{executivepaper}
   {\setlength\stockheight {10.5in}%
    \setlength\stockwidth  {7.25in}}
\DeclareOption{ebook}
   {\setlength\stockheight {9in}%
    \setlength\stockwidth  {6in}}
%    \end{macrocode}
%
%    The \Lopt{landscape} option switches the values of the height and width,
% assuming that the dimensions were originally given for portrait 
% orientation.
%    \begin{macrocode}
\DeclareOption{landscape}
  {\setlength\@tempdima  {\stockheight}
   \setlength\stockheight{\stockwidth}
   \setlength\stockwidth {\@tempdima}}

%    \end{macrocode}
%
%
% \subsection{Choosing the type size}
%
%    The type size options are handled by defining |\@ptsize| to contain
%    the last digit of the size in question and branching on |\ifcase|
%    statements. This is done for historical reasons to stay compatible
%    with other packages that use the |\@ptsize| variable to select
%    special actions. It makes the declarations of size options less
%    than 10pt difficult. In this class \texttt{9} is used for the 
%    \Lopt{9pt} option, assuming that the class will never define a 
%    \Lopt{19pt} option.
%
%    Option \Lopt{10pt} will be the default.
% \changes{v1.2}{2002/07/07}{Added 17pt option}
%    \begin{macrocode}
\renewcommand{\@ptsize}{0}
\DeclareOption{9pt}{\renewcommand{\@ptsize}{9}}
\DeclareOption{10pt}{\renewcommand{\@ptsize}{0}}
\DeclareOption{11pt}{\renewcommand{\@ptsize}{1}}
\DeclareOption{12pt}{\renewcommand{\@ptsize}{2}}
\DeclareOption{14pt}{\renewcommand{\@ptsize}{4}}
\DeclareOption{17pt}{\renewcommand{\@ptsize}{7}}
%    \end{macrocode}
%
%
%  \subsection{Two-side or one-side printing}
%
%    For two-sided printing we use the switch |\if@twoside|. In
%    addition we have to set the |\if@mparswitch| to get any margin
%    paragraphs into the outside margin. The default is \Lopt{twoside}.
% \begin{macro}{\if@twoside}
% \begin{macro}{\if@mparswitch}
%    \begin{macrocode}
\DeclareOption{twoside}{\@twosidetrue  \@mparswitchtrue}
\DeclareOption{oneside}{\@twosidefalse \@mparswitchfalse}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
%  \subsection{Two column printing}
%
%    Two-column and one-column printing is again realized via a switch
%    which is defined in the kernel. The default is single column printing.
%
% \begin{macro}{\if@twocolumn}
%    \begin{macrocode}
\DeclareOption{onecolumn}{\@twocolumnfalse}
\DeclareOption{twocolumn}{\@twocolumntrue}
%    \end{macrocode}
% \end{macro}
%
%
%  \subsection{The \Lopt{draft} option}
%
%    If the user requests \Lopt{draft} we show any overfull boxes,
%    marginal notes are allowed, and any copyright notices are not
%    printed. For symmetry, we also define a \Lopt{final} option
%    which is the default.
%
%    The user can use the |\ifdraftdoc| flag to add additional effects: \\
% |\ifdraftdoc <additional code> \fi|
% \begin{macro}{\ifdraftdoc}
%    \begin{macrocode}
\newif\ifdraftdoc\draftdocfalse
\setlength{\overfullrule}{\z@}
\DeclareOption{final}{\setlength{\overfullrule}{\z@}
                      \draftdocfalse
                      \msdocfalse}
\DeclareOption{draft}{\setlength\overfullrule{5pt}%
                      \draftdoctrue
                      \msdocfalse}
%    \end{macrocode}
% \end{macro}
%
% \subsection{The \Lopt{ms} option}
%
% \begin{macro}{\ifmsdoc}
%    The \Lopt{ms} option makes the document look as though it was produced
% on a typewriter. We use a flag for remembering this. The user may also
% use the flag for specifying \Lopt{ms} effects.
%    \begin{macrocode}
\newif\ifmsdoc
  \msdocfalse
\DeclareOption{ms}{%
  \msdoctrue
  \draftdocfalse
  \setlength\overfullrule{\z@}
}

%    \end{macrocode}
% \end{macro}
%
% \subsection{The \Lopt{showtrims} option}
%
%    The \Lopt{showtrims} option will display crosses at the corners
% of the logical pages showing where the stock should be trimmed.
%
% \begin{macro}{\ifshowtrims}
%    \begin{macrocode}
\newif\ifshowtrims
  \showtrimsfalse
\DeclareOption{showtrims}{\showtrimstrue}

%    \end{macrocode}
% \end{macro}
%
% \subsection{The \Lopt{article} option}
%
%    The \Lopt{article} option typesets as a simulation of the
% \Lpack{article} class.
% \changes{v0.3}{2001/07/09}{Added article option}
% \begin{macro}{\ifartopt}
% |\ifartopt| is a flag (TRUE) if the \Lopt{article} option is called.
%    \begin{macrocode}
\newif\ifartopt
  \artoptfalse
\DeclareOption{article}{\artopttrue}

%    \end{macrocode}
% \end{macro}
%
% \subsection{The \Lopt{subfigure} option}
%
% The \Lopt{subfigure} option has been made a no-op since version 1.1.
% \changes{v0.3}{2001/07/09}{Added subfigure option}
% \changes{v1.1}{2002/03/10}{Effectively removed subfigure option}
%    \begin{macrocode}
\DeclareOption{subfigure}{%
  \ClassWarningNoLine{memoir}{The subfigure option is not required}}

%    \end{macrocode}
%
%
% \subsection{The openright, openleft and openany options}
%
%    The \Lopt{openright} option specifies that Chapters must begin on 
% recto pages. The \Lopt{openleft} option specifies that Chapters
% must begin on verso pages and the \Lopt{openany} option lets Chapters 
% start on any page.
% \changes{v1.0}{2001/10/24}{Added the openleft option}
%    \begin{macrocode}
\DeclareOption{openright}{\@openrighttrue}
\DeclareOption{openany}{\@openrightfalse}
\DeclareOption{openleft}{\@openlefttrue}
%    \end{macrocode}
%
% \begin{macro}{\openright}
% \begin{macro}{\openany}
% \begin{macro}{\openleft}
% Commands that can be used to change the option in the middle of the 
% document\footnote{Openleft provided to meet a request by 
% Vladimir G.~Ivanovic (\texttt{vladimir@acm.org}) in September 2001.}.
%    \begin{macrocode}
\newcommand{\openright}{\@openrighttrue\@openleftfalse%
  \gdef\clearforchapter{\cleartorecto}}
\newcommand{\openany}{\@openrightfalse\@openleftfalse%
  \gdef\clearforchapter{\clearpage}}
\newcommand{\openleft}{\@openlefttrue
  \gdef\clearforchapter{\cleartoverso}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% \subsection{Equation numbering on the left}
%
% The \Lopt{leqno} option prints equation numbers on the left. This
% is implemented via an external class option file.
%    \begin{macrocode}
\DeclareOption{leqno}{\input{leqno.clo}}
%    \end{macrocode}
%
% \subsection{Flush left math displays}
%
% The \Lopt{fleqn} option redefines the displayed math environments
% so that they are left adjusted with an indent of |\mathindent|
% from the current left margin. This
% is implemented via an external class option file.
%    \begin{macrocode}
\DeclareOption{fleqn}{\input{fleqn.clo}}
%    \end{macrocode}
%
% \subsection{Open bibliography}
%
% The \Lopt{openbib} option redefines the |thebibliography| so that each
% block starts on a new line, and succeeding lines in a block are
% indented by |\bibindent|.
% \changes{v1.0a}{2001/11/28}{Replaced \cs{AtEndOfPackage} with \cs{AtEndOfClass}}
%    \begin{macrocode}
\DeclareOption{openbib}{%
  \AtEndOfClass{%
    \renewcommand\@openbib@code{%
      \advance\leftmargin\bibindent
      \itemindent -\bibindent
      \listparindent \itemindent
      \parsep \z@
     }%
    \renewcommand\newblock{\par}}%
}

%    \end{macrocode}
%
% \subsection{Old font commands}
%
% The \Lopt{oldfontcommands} option enables commands like |\bf| and friends.
% \changes{v1.0a}{2002/01/29}{Added the oldfontcommands option}
%    \begin{macrocode}
\DeclareOption{oldfontcommands}{%
  \@memoldfonttrue}

%    \end{macrocode}
%
%
% \section{Executing Options} \label{sec:xopt}
%
%    Here we execute the default options to initialize certain
%    variables. 
%    \begin{macrocode}
\ExecuteOptions{final,letterpaper,10pt,onecolumn,openright,twoside}
%    \end{macrocode}
%
%    The |\ProcessOptions| command causes the execution of the code
%    for every option \Lopt{foo}
%    which is declared and for which the user typed
%    the \Lopt{foo} option in his
%    |\documentclass| command.  For every option \Lopt{bar} he typed,
%    which is not declared, the option is assumed to be a global option.
%    All options will be passed as document options to any
%    |\usepackage| command in the document preamble.
%    \begin{macrocode}
\ProcessOptions

%    \end{macrocode}
%
% \begin{macro}{\cleartorecto}
% A repeat of |\cleardoublepage|; clears to a recto (odd-numbered) page.
%    \begin{macrocode}
\def\cleartorecto{\clearpage\if@twoside \ifodd\c@page\else
  \hbox{}\thispagestyle{cleared}%
  \newpage\if@twocolumn\hbox{}\newpage\fi\fi\fi}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\cleartoverso}
% Clears to a verso (even-numbered) page.
%    \begin{macrocode}
\def\cleartoverso{\clearpage\if@twoside 
  \ifodd\c@page\hbox{}\thispagestyle{cleared}%
  \newpage\if@twocolumn\hbox{}\newpage\fi\fi\fi}

%    \end{macrocode}
% \end{macro}
%

%    Set |\clearforchapter| according to the options.
%    \begin{macrocode}
\if@openleft
  \openleft
\else
  \if@openright
    \openright
  \else
    \openany
  \fi
\fi

%    \end{macrocode}
%
%    Now that all the options have been executed we can load the
%    chosen class option file that contains all size dependent code.
%    \begin{macrocode}
\ifnum\@ptsize = 9\relax
  \input{mem\@ptsize.clo}
\else
  \input{mem1\@ptsize.clo}
\fi
%</class>
%    \end{macrocode}
%
%
% \section{Fonts and spaces} \label{sec:docl}
% \label{sec:maincode}
%
%  In this section we deal with most of the aspects that are related
% to font sizes, and spacing that is related to the size of the body font.
%
% \subsection{Fonts}
%
%    \LaTeX\ offers the user commands to change the size of the font,
%    relative to the `main' size. Each relative size changing command
%    |\size| executes the command
%    |\@setfontsize||\size|\meta{font-size}\meta{baselineskip} where:
%
%    \begin{description}
%    \item[\meta{font-size}] The absolute size of the font to use from
%        now on.
%
%    \item[\meta{baselineskip}] The normal value of |\baselineskip|
%        for the size of the font selected. (The actual value will be
%        |\baselinestretch| * \meta{baselineskip}.)
%    \end{description}
%
%    A number of commands, defined in the \LaTeX\ kernel, shorten the
%    following  definitions and are used throughout. They are listed in 
% Table~\ref{tab:sh}.
%
% \begin{table}
% \centering
% \caption{Shorthand font point size commands}\label{tab:sh}
% \begin{tabular}{ll@{\qquad}ll@{\qquad}ll}
%  \verb=\@vpt= & 5 & \verb=\@vipt= & 6 & \verb=\@viipt= & 7 \\
%  \verb=\@viiipt= & 8 & \verb=\@ixpt= & 9 & \verb=\@xpt= & 10 \\
%  \verb=\@xipt= & 10.95 & \verb=\@xiipt= & 12 & \verb=\@xivpt= & 14.4\\
%  \verb=\@xviipt= & 17.28 & \verb=\@xxpt= & 20.74 & \verb=\@xxvpt= & 24.88\\
%  \end{tabular}
%  \end{table}
%
% \begin{macro}{\normalsize}
% \begin{macro}{\@normalsize}
%
%    The user level command for the main size is |\normalsize|.
%    Internally \LaTeX\ uses |\@normalsize| when it refers to the
%    main size. |\@normalsize| will be defined to work like
%    |\normalsize| if the latter is redefined from its default
%    definition (that just issues an error message). Otherwise
%    |\@normalsize| simply selects a 9pt/11pt size.
%
%    The |\normalsize| macro also sets new values for\\
%    |\abovedisplayskip|, |\abovedisplayshortskip| and
%    |\belowdisplayshortskip|.
%
%    \begin{macrocode}
%<*9pt|10pt|11pt|12pt|14pt|17pt>
%    \end{macrocode}
% \changes{v0.11 (mem14.clo)}{2001/10/30}{Changed baseline skip from 17.28pt to 17.5pt}
%    \begin{macrocode}

\renewcommand{\normalsize}{%
%<*9pt>
   \@setfontsize\normalsize\@ixpt\@xpt
   \abovedisplayskip 9\p@ \@plus 2\p@ \@minus 4.5\p@
   \abovedisplayshortskip \z@ \@plus 3\p@
   \belowdisplayshortskip 5.5\p@ \@plus 2.5\p@ \@minus 3\p@
%</9pt>
%<*10pt>
   \@setfontsize\normalsize\@xpt\@xiipt
   \abovedisplayskip 10\p@ \@plus2\p@ \@minus5\p@
   \abovedisplayshortskip \z@ \@plus3\p@
   \belowdisplayshortskip 6\p@ \@plus3\p@ \@minus3\p@
%</10pt>
%<*11pt>
   \@setfontsize\normalsize\@xipt{13.6}%
   \abovedisplayskip 11\p@ \@plus3\p@ \@minus6\p@
   \abovedisplayshortskip \z@ \@plus3\p@
   \belowdisplayshortskip 6.5\p@ \@plus3.5\p@ \@minus3\p@
%</11pt>
%<*12pt>
   \@setfontsize\normalsize\@xiipt{14.5}%
   \abovedisplayskip 12\p@ \@plus3\p@ \@minus7\p@
   \abovedisplayshortskip \z@ \@plus3\p@
   \belowdisplayshortskip 6.5\p@ \@plus3.5\p@ \@minus3\p@
%</12pt>
%<*14pt>
   \@setfontsize\normalsize\@xivpt{17.5}%
   \abovedisplayskip 14\p@ \@plus3\p@ \@minus8\p@
   \abovedisplayshortskip \z@ \@plus3\p@
   \belowdisplayshortskip 7\p@ \@plus3.5\p@ \@minus3\p@
%</14pt>
%<*17pt>
   \@setfontsize\normalsize\@xviipt{22}%
   \abovedisplayskip 15\p@ \@plus4\p@ \@minus8\p@
   \abovedisplayshortskip \z@ \@plus4\p@
   \belowdisplayshortskip 8\p@ \@plus4\p@ \@minus3\p@
%</17pt>
%    \end{macrocode}
%    The |\belowdisplayskip| is always equal to the
%    |\abovedisplayskip|. The parameters of the first level list are
%    always given by |\@listI|.
%    \begin{macrocode}
   \belowdisplayskip \abovedisplayskip
   \let\@listi\@listI}
%    \end{macrocode}
%
%    We initially choose the normalsize font.
%    \begin{macrocode}
\normalsize
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\small}
%    This code is similar to that for |\normalsize|.
% (Robert Schlicht\footnote{\texttt{w.m.l@gmx.net}, via email on 2004/03/11.}
% noted that in some cases \% signs were missing after \verb?\@setfontsize?).
% \changes{v1.61}{2004/03/12}{Added missing percent signs to some uses of
%                             \cs{@setfontsize}}
%    \begin{macrocode}
\newcommand{\small}{%
%<*9pt>
    \@setfontsize\small\@viiipt{9.5}%
    \abovedisplayskip 6\p@ \@plus 2\p@ \@minus 4\p@
    \abovedisplayshortskip \z@ \@plus 2\p@
    \belowdisplayshortskip 4\p@ \@plus 2\p@ \@minus 2\p@
    \def\@listi{\leftmargin\leftmargini
                \topsep 2\p@ \@plus 2\p@ \@minus 2\p@
                \parsep 1\p@ \@plus\p@ \@minus\p@
                \itemsep \parsep
%%                \itemindent\z@
                }%
%</9pt>
%<*10pt>
   \@setfontsize\small\@ixpt{11}%
   \abovedisplayskip 8.5\p@ \@plus3\p@ \@minus4\p@
   \abovedisplayshortskip \z@ \@plus2\p@
   \belowdisplayshortskip 4\p@ \@plus2\p@ \@minus2\p@
   \def\@listi{\leftmargin\leftmargini
               \topsep 4\p@ \@plus2\p@ \@minus2\p@
               \parsep 2\p@ \@plus\p@ \@minus\p@
               \itemsep \parsep
%%               \itemindent\z@
              }%
%</10pt>
%<*11pt>
   \@setfontsize\small\@xpt\@xiipt
   \abovedisplayskip 10\p@ \@plus2\p@ \@minus5\p@
   \abovedisplayshortskip \z@ \@plus3\p@
   \belowdisplayshortskip 6\p@ \@plus3\p@ \@minus3\p@
   \def\@listi{\leftmargin\leftmargini
               \topsep 6\p@ \@plus2\p@ \@minus2\p@
               \parsep 3\p@ \@plus2\p@ \@minus\p@
               \itemsep \parsep
%%               \itemindent\z@
              }%
%</11pt>
%<*12pt>
   \@setfontsize\small\@xipt{13.6}%
   \abovedisplayskip 11\p@ \@plus3\p@ \@minus6\p@
   \abovedisplayshortskip \z@ \@plus3\p@
   \belowdisplayshortskip 6.5\p@ \@plus3.5\p@ \@minus3\p@
   \def\@listi{\leftmargin\leftmargini
               \topsep 9\p@ \@plus3\p@ \@minus5\p@
               \parsep 4.5\p@ \@plus2\p@ \@minus\p@
               \itemsep \parsep
%%               \itemindent\z@
              }%
%</12pt>
%<*14pt>
   \@setfontsize\small\@xiipt{14.5}%
   \abovedisplayskip 12\p@ \@plus3\p@ \@minus7\p@
   \abovedisplayshortskip \z@ \@plus3\p@
   \belowdisplayshortskip 6.5\p@ \@plus3.5\p@ \@minus3\p@
   \def\@listi{\leftmargin\leftmargini
               \topsep 11\p@ \@plus4\p@ \@minus6\p@
               \parsep 6\p@ \@plus3\p@ \@minus\p@
               \itemsep \parsep
%%               \itemindent\z@
              }%
%</14pt>
%<*17pt>
   \@setfontsize\small\@xivpt{17}%
   \abovedisplayskip 12\p@ \@plus3\p@ \@minus7\p@
   \abovedisplayshortskip \z@ \@plus3\p@
   \belowdisplayshortskip 7\p@ \@plus4\p@ \@minus3\p@
   \def\@listi{\leftmargin\leftmargini
               \topsep 11\p@ \@plus4\p@ \@minus6\p@
               \parsep 6\p@ \@plus3\p@ \@minus\p@
               \itemsep \parsep
%%               \itemindent\z@
              }%
%</17pt>
   \belowdisplayskip \abovedisplayskip
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\footnotesize}
%    This code is similar to that for |\normalsize|.
%    \begin{macrocode}
\newcommand{\footnotesize}{%
%<*9pt>
    \@setfontsize\footnotesize\@viipt{8}%
    \abovedisplayskip 6\p@ \@plus 2\p@ \@minus 4\p@
    \abovedisplayshortskip \z@ \@plus 2\p@
    \belowdisplayshortskip 4\p@ \@plus 2\p@ \@minus 2\p@
    \def\@listi{\leftmargin\leftmargini
                \topsep 2\p@ \@plus 2\p@ \@minus 2\p@
                \parsep 1\p@ \@plus\p@ \@minus\p@
                \itemsep \parsep
%%                \itemindent\z@
               }%
%</9pt>
%<*10pt>
   \@setfontsize\footnotesize\@viiipt{9.5}%
   \abovedisplayskip 6\p@ \@plus2\p@ \@minus4\p@
   \abovedisplayshortskip \z@ \@plus\p@
   \belowdisplayshortskip 3\p@ \@plus\p@ \@minus2\p@
   \def\@listi{\leftmargin\leftmargini
               \topsep 3\p@ \@plus\p@ \@minus\p@
               \parsep 2\p@ \@plus\p@ \@minus\p@
               \itemsep \parsep
%%               \itemindent\z@
              }%
%</10pt>
%<*11pt>
   \@setfontsize\footnotesize\@ixpt{11}%
   \abovedisplayskip 8\p@ \@plus2\p@ \@minus4\p@
   \abovedisplayshortskip \z@ \@plus\p@
   \belowdisplayshortskip 4\p@ \@plus2\p@ \@minus2\p@
   \def\@listi{\leftmargin\leftmargini
               \topsep 4\p@ \@plus2\p@ \@minus2\p@
               \parsep 2\p@ \@plus\p@ \@minus\p@
               \itemsep \parsep
%%               \itemindent\z@
              }%
%</11pt>
%<*12pt>
   \@setfontsize\footnotesize\@xpt\@xiipt
   \abovedisplayskip 10\p@ \@plus2\p@ \@minus5\p@
   \abovedisplayshortskip \z@ \@plus3\p@
   \belowdisplayshortskip 6\p@ \@plus3\p@ \@minus3\p@
   \def\@listi{\leftmargin\leftmargini
               \topsep 6\p@ \@plus2\p@ \@minus2\p@
               \parsep 3\p@ \@plus2\p@ \@minus\p@
               \itemsep \parsep
%%               \itemindent\z@
              }%
%</12pt>
%<*14pt>
   \@setfontsize\footnotesize\@xipt{13.6}%
   \abovedisplayskip 11\p@ \@plus3\p@ \@minus6\p@
   \abovedisplayshortskip \z@ \@plus3\p@
   \belowdisplayshortskip 6.5\p@ \@plus3.5\p@ \@minus3\p@
   \def\@listi{\leftmargin\leftmargini
               \topsep 6.5\p@ \@plus3.5\p@ \@minus3\p@
               \parsep 4\p@ \@plus3\p@ \@minus\p@
               \itemsep \parsep
%%               \itemindent\z@
              }%
%</14pt>
%<*17pt>
   \@setfontsize\footnotesize\@xiipt{14}%
   \abovedisplayskip 12\p@ \@plus3\p@ \@minus7\p@
   \abovedisplayshortskip \z@ \@plus3\p@
   \belowdisplayshortskip 6.5\p@ \@plus3.5\p@ \@minus3\p@
   \def\@listi{\leftmargin\leftmargini
               \topsep 6.5\p@ \@plus3.5\p@ \@minus3\p@
               \parsep 4\p@ \@plus3\p@ \@minus\p@
               \itemsep \parsep
%%               \itemindent\z@
              }%
%</17pt>
   \belowdisplayskip \abovedisplayskip
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\miniscule}
% \begin{macro}{\scriptsize}
% \begin{macro}{\tiny}
% \begin{macro}{\large}
% \begin{macro}{\Large}
% \begin{macro}{\LARGE}
% \begin{macro}{\huge}
% \begin{macro}{\Huge}
% \begin{macro}{\HUGE}
%    These are all much simpler than the previous macros, they just
%    select a new fontsize, but leave the parameters for displays and
%    lists alone. The class provides two additional sizes, \cs{miniscule}
% and \cs{HUGE}, with respect to the usual set.
% \changes{v1.2}{2002/07/27}{Added \cs{miniscule} and \cs{HUGE} font sizes}
% \changes{v1.2}{2002/07/27}{Twiddled some font sizes to get regular steps}
% \changes{v1.2a}{2002/08/27}{Fixed 12pt scriptsize bug}
%    \begin{macrocode}
%<*9pt>
\newcommand{\miniscule}{\@setfontsize\miniscule\@vpt{6}}
\newcommand{\tiny}{\@setfontsize\tiny\@vpt{6}}
\newcommand{\scriptsize}{\@setfontsize\scriptsize\@vipt{7}}
\newcommand{\large}{\@setfontsize\large\@xpt{12}}
\newcommand{\Large}{\@setfontsize\Large\@xipt{13.6}}
\newcommand{\LARGE}{\@setfontsize\LARGE\@xiipt{14.5}}
\newcommand{\huge}{\@setfontsize\huge\@xivpt{18}}
\newcommand{\Huge}{\@setfontsize\Huge\@xviipt{22}}
\newcommand{\HUGE}{\@setfontsize\HUGE\@xxpt{25}}
%</9pt>
%<*10pt>
\newcommand{\miniscule}{\@setfontsize\miniscule\@vpt{6}}
\newcommand{\tiny}{\@setfontsize\tiny\@vipt{7}}
\newcommand{\scriptsize}{\@setfontsize\scriptsize\@viipt{8}}
\newcommand{\large}{\@setfontsize\large\@xipt{13.6}}
\newcommand{\Large}{\@setfontsize\Large\@xiipt{14.5}}
\newcommand{\LARGE}{\@setfontsize\LARGE\@xivpt{18}}
\newcommand{\huge}{\@setfontsize\huge\@xviipt{22}}
\newcommand{\Huge}{\@setfontsize\Huge\@xxpt{25}}
\newcommand{\HUGE}{\@setfontsize\HUGE\@xxvpt{30}}
%</10pt>
%<*11pt>
\newcommand{\miniscule}{\@setfontsize\miniscule\@vipt{7}}
\newcommand{\tiny}{\@setfontsize\tiny\@viipt{8}}
\newcommand{\scriptsize}{\@setfontsize\scriptsize\@viiipt{9.5}}
\newcommand{\large}{\@setfontsize\large\@xiipt{14.5}}
\newcommand{\Large}{\@setfontsize\Large\@xivpt{18}}
\newcommand{\LARGE}{\@setfontsize\LARGE\@xviipt{22}}
\newcommand{\huge}{\@setfontsize\huge\@xxpt{25}}
\newcommand{\Huge}{\@setfontsize\Huge\@xxvpt{30}}
\let\HUGE=\Huge
%</11pt>
%<*12pt>
\newcommand{\miniscule}{\@setfontsize\miniscule\@viipt{8}}
\newcommand{\tiny}{\@setfontsize\tiny\@viiipt{9.5}}
\newcommand{\scriptsize}{\@setfontsize\scriptsize\@ixpt\@xpt}
\newcommand{\large}{\@setfontsize\large\@xivpt{18}}
\newcommand{\Large}{\@setfontsize\Large\@xviipt{22}}
\newcommand{\LARGE}{\@setfontsize\LARGE\@xxpt{25}}
\newcommand{\huge}{\@setfontsize\huge\@xxvpt{30}}
\let\Huge=\huge
\let\HUGE=\huge
%</12pt>
%<*14pt>
\newcommand{\miniscule}{\@setfontsize\miniscule\@viiipt{9.5}}
\newcommand{\tiny}{\@setfontsize\tiny\@ixpt{10}}
\newcommand{\scriptsize}{\@setfontsize\scriptsize\@xpt{12}}
\newcommand{\large}{\@setfontsize\large\@xviipt{22}}
\newcommand{\Large}{\@setfontsize\Large\@xxpt{25}}
\newcommand{\LARGE}{\@setfontsize\LARGE\@xxvpt{30}}
\let\huge=\LARGE
\let\Huge=\LARGE
\let\HUGE=\LARGE
%</14pt>
%<*17pt>
\newcommand{\miniscule}{\@setfontsize\miniscule\@ixpt{10}}
\newcommand{\tiny}{\@setfontsize\tiny\@xpt{12}}
\newcommand{\scriptsize}{\@setfontsize\scriptsize\@xipt{13.6}}
\newcommand{\large}{\@setfontsize\large\@xxpt{25}}
\newcommand{\Large}{\@setfontsize\Large\@xxvpt{30}}
\newcommand{\LARGE}{\@setfontsize\LARGE\@xxvpt{30}}
\let\LARGE=\Large
\let\huge=\Large
\let\Huge=\Large
\let\HUGE=\Large
%</17pt>
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
%    \begin{macrocode}
%</9pt|10pt|11pt|12pt|14pt|17pt>
%<*class>
%    \end{macrocode}
%
% \begin{macro}{\captionsize}
%  This internal command holds the font size for captions. 
%    \begin{macrocode}
\newcommand{\captionsize}{\normalsize}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Paragraphing}
%
% \begin{macro}{\lineskip}
% \begin{macro}{\normallineskip}
%    These parameters control \TeX's behaviour when two lines tend to
%    come too close together.
%    \begin{macrocode}
\setlength\lineskip{1\p@}
\setlength\normallineskip{1\p@}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\baselinestretch}
%    This is used as a multiplier for |\baselineskip|. The default is
%    to \emph{not} stretch the baselines.
%    \begin{macrocode}
\renewcommand{\baselinestretch}{}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\parskip}
% \begin{macro}{\onelineskip}
%
% |\parskip| is additional vertical space between paragraphs;  default
% is zero.
%
%
%    |\onelineskip| is the default space between baselines.
% \changes{v0.11 (mem14.clo)}{2001/10/30}{Changed \cs{onelineskip} from 17.28pt to 17.5pt}
%    \begin{macrocode}
\setlength\parskip{0\p@ \@plus \p@}
%</class>
%<*9pt|10pt|11pt|12pt|14pt|17pt>
%<9pt>\setlength{\onelineskip}{\@xpt\p@}
%<10pt>\setlength{\onelineskip}{\@xiipt\p@}
%<11pt>\setlength{\onelineskip}{13.6\p@}
%<12pt>\setlength{\onelineskip}{14.5\p@}
%<14pt>\setlength{\onelineskip}{17.5\p@}
%<17pt>\setlength{\onelineskip}{22\p@}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\parindent}
%    |\parskip| gives extra vertical space between paragraphs and
%    |\parindent| is the width of the paragraph indentation. 
%    \begin{macrocode}
\if@twocolumn
  \setlength\parindent{1em}
\else
%<9pt>  \setlength\parindent{12\p@}
%<10pt>  \setlength\parindent{15\p@}
%<11pt>  \setlength\parindent{17\p@}
%<12pt>  \setlength\parindent{1.5em}
%<14pt>  \setlength\parindent{1.5em}
%<17pt>  \setlength\parindent{1.5em}
\fi
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\smallskipamount}
% \begin{macro}{\medskipamount}
% \begin{macro}{\bigskipamount}
% The values for these three parameters are set in the LaTeX kernel.
% Historically they have been size invariant.
%    \begin{macrocode}
\setlength\smallskipamount{3\p@ \@plus 1\p@ \@minus 1\p@}
\setlength\medskipamount{6\p@ \@plus 2\p@ \@minus 2\p@}
\setlength\bigskipamount{12\p@ \@plus 4\p@ \@minus 4\p@}
%</9pt|10pt|11pt|12pt|14pt|17pt>
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\@lowpenalty}
% \begin{macro}{\@medpenalty}
% \begin{macro}{\@highpenalty}%
%    The commands |\nopagebreak| and |\nolinebreak| put in penalties
%    to discourage these breaks at the point they are put in.
%    They use |\@lowpenalty|, |\@medpenalty| or |\@highpenalty|,
%    dependent on their argument.
%    \begin{macrocode}
%<*class>
\@lowpenalty   51
\@medpenalty  151
\@highpenalty 301
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\clubpenalty}
% \begin{macro}{\widowpenalty}
%    These penalties are used to discourage club and widow lines.
%    The default values are 150 each, but we want stronger discouragement.
%    \begin{macrocode}
\clubpenalty  1000
\widowpenalty 1000
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\displaywidowpenalty}
% \begin{macro}{\predisplaypenalty}
% \begin{macro}{\postdisplaypenalty}
%    Discourage, but do not prevent, widows in front of a math display
%    and forbid breaking directly in front of a display. Allow break
%    after a display without a penalty. The default values are
%    used, therefore we only show them here.
%    \begin{macrocode}
% \displaywidowpenalty 50
% \predisplaypenalty   10000
% \postdisplaypenalty  0
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\interlinepenalty}
%    Allow the breaking of a page in the middle of a paragraph.
%    \begin{macrocode}
% \interlinepenalty 0
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\brokenpenalty}
%    We allow the breaking of a page after a hyphenated line.
%    \begin{macrocode}
% \brokenpenalty 100
%    \end{macrocode}
% \end{macro}
%
% 
%
%
% \subsection{Vertical spacing}
%
% \begin{macro}{\headheight}
% \begin{macro}{\headsep}
% \begin{macro}{\topskip}
% \begin{macro}{\footskip}
%    The |\headheight| is the height of the box that will contain the
%    running head. In this class it is point size dependent (normally it
% is a constant 12pt). 
%
% The |\headsep| is the distance between the bottom
%    of the running head and the top of the text. 
%
% The |\topskip| is the
%    |\baselineskip| for the first line on a page; 
%    \LaTeX's output routine will not work properly if it has the
%    value 0pt, so do not do that!
%
%    The distance from the baseline of the box which contains the
%    running footer to the baseline of last line of text is controlled
%    by the |\footskip|.
% \changes{v0.11 (mem14.clo)}{2001/10/30}{Changed \cs{headheight} from 16pt to 17.5pt}
% \changes{v0.11 (mem11.clo)}{2002/03/10}{Changed \cs{headheight} from 13pt to 13.6pt}
%    \begin{macrocode}
%</class>
%<*9pt|10pt|11pt|12pt|14pt|17pt>
%<*9pt>
\setlength\headheight{11\p@}
\setlength\headsep{.225in}
\setlength\topskip{9\p@}
\setlength\footskip{.33in}
%</9pt>
%<*10pt>
\setlength\headheight{12\p@}
\setlength\headsep{.25in}
\setlength\topskip{10\p@}
\setlength\footskip{.35in}
%</10pt>
%<*11pt>
\setlength\headheight{13.6\p@}
\setlength\headsep{.275in}
\setlength\topskip{11\p@}
\setlength\footskip{.38in}
%</11pt>
%<*12pt>
\setlength\headheight{14\p@}
\setlength\headsep{.275in}
\setlength\topskip{12\p@}
\setlength\footskip{30\p@}
%</12pt>
%<*14pt>
\setlength\headheight{17.5\p@}
\setlength\headsep{.30in}
\setlength\topskip{14.4\p@}
\setlength\footskip{.4in}
%</14pt>
%<*17pt>
\setlength\headheight{22\p@}
\setlength\headsep{.30in}
\setlength\topskip{14.4\p@}
\setlength\footskip{.4in}
%</17pt>
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\maxdepth}
% \begin{macro}{\@maxdepth}
%    The \TeX\ primitive register |\maxdepth| has a function that is
%    similar to that of |\topskip|. The register |\@maxdepth| should
%    always contain a copy of |\maxdepth|. In both plain \TeX\ and
%    \LaTeX~2.09 |\maxdepth| had a fixed value of \texttt{4pt}; in
%    native \LaTeX 2e mode we let the value depend on the typesize. We
%    set it so that |\maxdepth| $+$ |\topskip| $=$ typesize $\times
%    1.5$. As it happens, in these classes |\topskip| is equal to the
%    typesize, therefor we set |\maxdepth| to half the value of
%    |\topskip|.
%    \begin{macrocode}
\setlength\maxdepth{.5\topskip}
\setlength\@maxdepth\maxdepth
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
%
% \subsection{Footnotes}
%
% \begin{macro}{\footnotesep}
%    |\footnotesep| is the height of the strut placed at the beginning
%    of every footnote. It equals the height of a normal |\footnotesize|
% strut, so no extra space appears between footnotes
%    \begin{macrocode}
%<9pt>\setlength\footnotesep{6.0\p@}  %% ????????
%<10pt>\setlength\footnotesep{6.65\p@}
%<11pt>\setlength\footnotesep{7.7\p@}
%<12pt>\setlength\footnotesep{8.4\p@}
%<14pt>\setlength\footnotesep{10.0\p@} %% ????????
%<17pt>\setlength\footnotesep{12\p@} %% ????????
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\footins}
%    |\skip\footins| is the space between the last line of the main
%    text and the top of the first footnote.
%    \begin{macrocode}
%<9pt>\setlength{\skip\footins}{8\p@ \@plus 2\p@ \@minus 2\p@}
%<10pt>\setlength{\skip\footins}{9\p@ \@plus 4\p@ \@minus 2\p@}
%<11pt>\setlength{\skip\footins}{10\p@ \@plus 4\p@ \@minus 2\p@}
%<12pt>\setlength{\skip\footins}{10.8\p@ \@plus 4\p@ \@minus 2\p@}
%<14pt>\setlength{\skip\footins}{13\p@ \@plus 4\p@ \@minus 2\p@}
%<17pt>\setlength{\skip\footins}{14\p@ \@plus 5\p@ \@minus 3\p@}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Floats}
%
% \paragraph{Floats on a text page}
%
% \begin{macro}{\floatsep}
% \begin{macro}{\textfloatsep}
% \begin{macro}{\intextsep}
%    When a floating object is placed on a page with text, these
%    parameters control the separation between the float and the other
%    objects on the page. These parameters are used for both
%    one-column mode and single-column floats in two-column mode.
%
%    |\floatsep| is the space between adjacent floats that are moved
%    to the top or bottom of the text page.
%
%    |\textfloatsep| is the space between the main text and floats
%    at the top or bottom of the page.
%
%    |\intextsep| is the space between in-text floats and the text.
%    \begin{macrocode}
%<*9pt>
\setlength\floatsep    {12\p@ \@plus 2\p@ \@minus 2\p@}
\setlength\textfloatsep{20\p@ \@plus 2\p@ \@minus 4\p@}
\setlength\intextsep   {12\p@ \@plus 2\p@ \@minus 2\p@}
%</9pt>
%<*10pt>
\setlength\floatsep    {12\p@ \@plus 2\p@ \@minus 2\p@}
\setlength\textfloatsep{20\p@ \@plus 2\p@ \@minus 4\p@}
\setlength\intextsep   {12\p@ \@plus 2\p@ \@minus 2\p@}
%</10pt>
%<*11pt>
\setlength\floatsep    {12\p@ \@plus 2\p@ \@minus 2\p@}
\setlength\textfloatsep{20\p@ \@plus 2\p@ \@minus 4\p@}
\setlength\intextsep   {12\p@ \@plus 2\p@ \@minus 2\p@}
%</11pt>
%<*12pt>
\setlength\floatsep    {12\p@ \@plus 2\p@ \@minus 2\p@}
\setlength\textfloatsep{20\p@ \@plus 2\p@ \@minus 4\p@}
\setlength\intextsep   {14\p@ \@plus 4\p@ \@minus 4\p@}
%</12pt>
%<*14pt>
\setlength\floatsep    {14\p@ \@plus 4\p@ \@minus 4\p@}
\setlength\textfloatsep{20\p@ \@plus 4\p@ \@minus 4\p@}
\setlength\intextsep   {14\p@ \@plus 4\p@ \@minus 4\p@}
%</14pt>
%<*17pt>
\setlength\floatsep    {15\p@ \@plus 4\p@ \@minus 4\p@}
\setlength\textfloatsep{25\p@ \@plus 5\p@ \@minus 5\p@}
\setlength\intextsep   {16\p@ \@plus 5\p@ \@minus 5\p@}
%</17pt>
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\dblfloatsep}
% \begin{macro}{\dbltextfloatsep}
%    When floating objects that span the whole |\textwidth| are placed
%    on a text page and \LaTeX{} is in twocolumn mode the separation
%    between the float and the text is controlled by |\dblfloatsep|
%    and |\dbltextfloatsep|.
%
%    |\dblfloatsep| is the space between adjacent floats that are moved
%    to the top or bottom of the text page.
%
%    |\dbltextfloatsep| is the space between the main text and floats
%    at the top or bottom of the page.
%
%    \begin{macrocode}
%<*9pt>
\setlength\dblfloatsep    {12\p@ \@plus 2\p@ \@minus 2\p@}
\setlength\dbltextfloatsep{20\p@ \@plus 2\p@ \@minus 4\p@}
%</9pt>
%<*10pt>
\setlength\dblfloatsep    {12\p@ \@plus 2\p@ \@minus 2\p@}
\setlength\dbltextfloatsep{20\p@ \@plus 2\p@ \@minus 4\p@}
%</10pt>
%<*11pt>
\setlength\dblfloatsep    {12\p@ \@plus 2\p@ \@minus 2\p@}
\setlength\dbltextfloatsep{20\p@ \@plus 2\p@ \@minus 4\p@}
%</11pt>
%<*12pt>
\setlength\dblfloatsep    {14\p@ \@plus 2\p@ \@minus 4\p@}
\setlength\dbltextfloatsep{20\p@ \@plus 2\p@ \@minus 4\p@}
%</12pt>
%<*14pt>
\setlength\dblfloatsep    {14\p@ \@plus 2\p@ \@minus 4\p@}
\setlength\dbltextfloatsep{20\p@ \@plus 2\p@ \@minus 4\p@}
%</14pt>
%<*17pt>
\setlength\dblfloatsep    {15\p@ \@plus 4\p@ \@minus 4\p@}
\setlength\dbltextfloatsep{25\p@ \@plus 5\p@ \@minus 5\p@}
%</17pt>
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \paragraph{Floats on their own page or column}
%
% \begin{macro}{\@fptop}
% \begin{macro}{\@fpsep}
% \begin{macro}{\@fpbot}
%    When floating objects are placed on separate pages the layout of
%    such pages is controlled by these parameters. At the top of the
%    page |\@fptop| amount of stretchable whitespace is inserted, at
%    the bottom of the page we get an |\@fpbot| amount of stretchable
%    whitespace. Between adjacent floats the |\@fpsep| is inserted.
%
%    These parameters are used for the placement of floating objects
%    in one column mode, or in single column floats in two column
%    mode.
%
%    Note that at least one of the two parameters |\@fptop| and
%    |\@fpbot| should contain a |plus ...fil| to allow filling the
%    remaining empty space.
%    \begin{macrocode}
%<*9pt>
\setlength\@fptop{0\p@ \@plus 1fil}
\setlength\@fpsep{9\p@ \@plus 2fil}
\setlength\@fpbot{0\p@ \@plus 1fil}
%</9pt>
%<*10pt>
\setlength\@fptop{0\p@ \@plus 1fil}
\setlength\@fpsep{8\p@ \@plus 2fil}
\setlength\@fpbot{0\p@ \@plus 1fil}
%</10pt>
%<*11pt>
\setlength\@fptop{0\p@ \@plus 1fil}
\setlength\@fpsep{8\p@ \@plus 2fil}
\setlength\@fpbot{0\p@ \@plus 1fil}
%</11pt>
%<*12pt>
\setlength\@fptop{0\p@ \@plus 1fil}
\setlength\@fpsep{10\p@ \@plus 2fil}
\setlength\@fpbot{0\p@ \@plus 1fil}
%</12pt>
%<*14pt>
\setlength\@fptop{0\p@ \@plus 1fil}
\setlength\@fpsep{10\p@ \@plus 2fil}
\setlength\@fpbot{0\p@ \@plus 1fil}
%</14pt>
%<*17pt>
\setlength\@fptop{0\p@ \@plus 1fil}
\setlength\@fpsep{12\p@ \@plus 2fil}
\setlength\@fpbot{0\p@ \@plus 1fil}
%</17pt>
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@dblfptop}
% \begin{macro}{\@dblfpsep}
% \begin{macro}{\@dblfpbot}
%    Double column floats in two column mode are handled with similar
%    parameters.
%    \begin{macrocode}
%<*9pt>
\setlength\@dblfptop{0\p@ \@plus 1fil}
\setlength\@dblfpsep{7\p@ \@plus 2fil}
\setlength\@dblfpbot{0\p@ \@plus 1fil}
%</9pt>
%<*10pt>
\setlength\@dblfptop{0\p@ \@plus 1fil}
\setlength\@dblfpsep{8\p@ \@plus 2fil}
\setlength\@dblfpbot{0\p@ \@plus 1fil}
%</10pt>
%<*11pt>
\setlength\@dblfptop{0\p@ \@plus 1fil}
\setlength\@dblfpsep{8\p@ \@plus 2fil}
\setlength\@dblfpbot{0\p@ \@plus 1fil}
%</11pt>
%<*12pt>
\setlength\@dblfptop{0\p@ \@plus 1fil}
\setlength\@dblfpsep{10\p@ \@plus 2fil}
\setlength\@dblfpbot{0\p@ \@plus 1fil}
%</12pt>
%<*14pt>
\setlength\@dblfptop{0\p@ \@plus 1fil}
\setlength\@dblfpsep{12\p@ \@plus 2fil}
\setlength\@dblfpbot{0\p@ \@plus 1fil}
%</14pt>
%<*17pt>
\setlength\@dblfptop{0\p@ \@plus 1fil}
\setlength\@dblfpsep{12\p@ \@plus 2fil}
\setlength\@dblfpbot{0\p@ \@plus 1fil}
%</17pt>
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsection{The measure}
%
% The width of a line of text (and therefore the text block) is termed
% the \emph{measure}.
%
% \begin{macro}{\lxvchars}
% The length |\lxvchars| is the approximate length of a normal
% text line containing 65 characters (a typesetters rule of thumb is
% that there should be about 60--70 characters per line).
%    \begin{macrocode}
%<9pt>\setlength\lxvchars{276\p@}  %
%<10pt>\setlength\lxvchars{300\p@} % standard 345pt
%<11pt>\setlength\lxvchars{324\p@} % standard 360pt
%<12pt>\setlength\lxvchars{336\p@} % standard 390pt
%<14pt>\setlength\lxvchars{408\p@} %
%<17pt>\setlength\lxvchars{444\p@} %
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\xlvchars}
% The length |\xlvchars| is the approximate length of a normal
% double column text line containing 45 characters 
% (a typesetters rule of thumb is
% that there should be about 40--50 characters per column line).
%    \begin{macrocode}
%<9pt>\setlength\xlvchars{192\p@}  %
%<10pt>\setlength\xlvchars{204\p@} % 
%<11pt>\setlength\xlvchars{216\p@} % 
%<12pt>\setlength\xlvchars{240\p@} % 
%<14pt>\setlength\xlvchars{288\p@} %
%<17pt>\setlength\xlvchars{312\p@} %
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\marginparsep}
% \begin{macro}{\marginparpush}
% |\marginparsep| is the horizontal space between the text block and 
% marginal notes, while |\marginparpush| is the minimum vertical
% separation between the notes.
%    \begin{macrocode}
\if@twocolumn
  \setlength\marginparsep{10\p@}
\else
  \setlength\marginparsep{7\p@}
\fi
%<9pt|10pt|11pt>\setlength{\marginparpush}{5\p@}
%<12pt|14pt>\setlength{\marginparpush}{7\p@}
%<17pt>\setlength{\marginparpush}{10\p@}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%    \begin{macrocode}
%</9pt|10pt|11pt|12pt|14pt|17pt>
%<*class>
%    \end{macrocode}
%
% \section{Page Layout}
%
%
% \subsection{The typeblock and margins}
%
%    All margin dimensions are measured from a point one inch from the
%    top and lefthand side of the page.
%
% \begin{macro}{\setlxvchars}
% \begin{macro}{\setxlvchars}
% These were suggested and supplied by Morten H{\o}gholm (18 May 2003).
%
% |\setlxvchars|\oarg{fontspec} sets |\lxvchars| to the length of a line
% containing 65 characters in the \meta{fontspec}. 
%
% Similarly
% |\setxlvchars|\oarg{fontspec} sets |\xlvchars| for 45 characters.
% \changes{v1.4}{2003/11/22}{Added \cs{setlxvchars} and \cs{setxlvchars}
%                              (from patch v1.6)}
%    \begin{macrocode}
\newcommand{\setlxvchars}[1][\normalfont]{\begingroup
  #1
  \settowidth{\lxvchars}{abcdefghijklmnopqrstuvwxyz}%
  \setlength{\lxvchars}{2.042\lxvchars}%
  \addtolength{\lxvchars}{33.41pt}%
  \global\lxvchars=\lxvchars
  \endgroup}
\newcommand{\setxlvchars}[1][\normalfont]{\begingroup
  #1
  \settowidth{\xlvchars}{abcdefghijklmnopqrstuvwxyz}%
  \setlength{\xlvchars}{1.415\xlvchars}%
  \addtolength{\xlvchars}{23.03pt}%
  \global\xlvchars=\xlvchars
  \endgroup}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\setrectanglesize}
%  The macro |\setrectanglesize{|\meta{H}|}{|\meta{W}|}{|\meta{r}|}| 
% calculates the height and width of a rectangle given any two out of
% the three arguments. An unvalued argument is denoted by |*|.
%
% Table~\ref{tab:setrect} shows the argument combinations and the result
% for each combination.
% \begin{table}
% \DeleteShortVerb{\|}
% \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}
% \MakeShortVerb{\|}
% \end{table}
%
% The
% calculated height and width are stored in |\@tempdima| and |\@tempdimb|
% respectively. Both lengths are set to zero if there is an error.
%    \begin{macrocode}
\newcommand{\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}
      \@tempdima = \z@
      \@tempdimb = \z@
    \else                               % W
      \nametest{#3}{*}%
      \ifsamename                       % r = *
        \@tempdimb = #2\relax
        \@tempdima = \@tempdimb
      \else                             % r
        \@tempdimb = #2\relax
        \@tempdima = #3\@tempdimb
      \fi
    \fi
  \else                                 % H
    \nametest{#2}{*}%
    \ifsamename                         % W = *
      \nametest{#3}{*}%
      \ifsamename                       % r = *
        \@tempdima = #1\relax
        \@tempdimb = \@tempdima
      \else                             % r
        \@tempdima = #1\relax
        \@tempdimb = #3\@tempdima
      \fi
    \else                               % W
      \@tempdima = #1\relax
      \@tempdimb = #2\relax
    \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 
% |\setfillsize{|\meta{T}|}{|\meta{C}|}{|\meta{L}|}{|\meta{R}|}{|\meta{r}|}|
% maintains these constraints among the variables, where an unvalued 
% argument is denoted by |*|. 
%
% Table~\ref{tab:setfill} shows the argument combinations and the result
% for each combination.
% \begin{table}
% \DeleteShortVerb{\|}
% \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}
% \MakeShortVerb{\|}
% \end{table}
%
%
% The
% calculated values of |C|, |L| and |R| are stored |\@tempdimc|, 
% |\@tempdima| and |\@tempdimb| respectively. If there is an error
% the lengths are set to zero.
%    \begin{macrocode}
\newcommand{\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}
        \@tempdima = \z@
        \@tempdimb = \z@
        \@tempdimc = \z@
      \else                                  % R 
        \nametest{#5}{*}%
        \ifsamename                          % r = *
          \@tempdimb = #4\relax
          \@tempdima = \@tempdimb
          \@tempdimc = #1\relax
          \advance\@tempdimc -\@tempdima
          \advance\@tempdimc -\@tempdimb
        \else                                % r 
          \@tempdimb = #4\relax
          \@tempdima = #5\@tempdimb
          \@tempdimc = #1\relax
          \advance\@tempdimc -\@tempdima
          \advance\@tempdimc -\@tempdimb
        \fi
      \fi
    \else                                    % L 
      \nametest{#4}{*}%
      \ifsamename                            % R = *
        \nametest{#5}{*}%
        \ifsamename                          % r = *
          \@tempdima = #3\relax
          \@tempdimb = \@tempdima
          \@tempdimc = #1\relax
          \advance\@tempdimc -\@tempdima
          \advance\@tempdimc -\@tempdimb
        \else                                % r 
          \@tempdima = #3\relax
          \@tempdimb = #5\@tempdima
          \@tempdimc = #1\relax
          \advance\@tempdimc -\@tempdima
          \advance\@tempdimc -\@tempdimb
        \fi
      \else                                  % R
        \@tempdima = #3\relax
        \@tempdimb = #4\relax
        \@tempdimc = #1\relax
        \advance\@tempdimc -\@tempdima
        \advance\@tempdimc -\@tempdimb
      \fi
    \fi
  \else                                      % C is valued
    \nametest{#3}{*}%
    \ifsamename                              % L = *
      \nametest{#4}{*}%
      \ifsamename                            % R = *
        \nametest{#5}{*}%
        \ifsamename                          % r = *
          \@tempdimc = #2\relax
          \@tempdima = #1\relax
          \advance\@tempdima -\@tempdimc
          \@tempdima = 0.5\@tempdima
          \@tempdimb = \@tempdima
        \else                                % r (CODE PERHAPS FIXED)
          \@tempdimc = #2\relax         % C
          \@tempdimb = #1\relax         % 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 
        \@tempdimc = #2\relax
        \@tempdimb = #4\relax
        \@tempdima = #1\relax
        \advance\@tempdima -\@tempdimc
        \advance\@tempdima -\@tempdimb
      \fi
    \else                                    % L 
      \nametest{#4}{*}%
      \ifsamename                            % R = *
        \@tempdimc = #2\relax
        \@tempdima = #3\relax
        \@tempdimb = #1\relax
        \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}%
        \@tempdima = \z@
        \@tempdimb = \z@
        \@tempdimc = #2\relax
      \fi
    \fi
  \fi
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\setstocksize}
% \begin{macro}{\settrims}
% \begin{macro}{\settrimmedsize}
% |\setstocksize{|\meta{height}|}{|\meta{width}|}| sets the height
% and width of the stock material and 
% |\settrims{|\meta{top}|}{|\meta{edge}|}| sets the trim lengths
% for the top and side (fore edge) of the stock.
% The macro |\settrimmedsize{|\meta{height}|}{|\meta{width}|}{|\meta{ratio}|}|
% sets the size for the trimmed page, based on |\setrectanglesize|.
%    \begin{macrocode}
\newcommand{\setstocksize}[2]{%
  \setlength{\stockheight}{#1}%
  \setlength{\stockwidth}{#2}}
\newcommand{\settrims}[2]{%
  \setlength{\trimtop}{#1}%
  \setlength{\trimedge}{#2}}
\newcommand{\settrimmedsize}[3]{%
  \setrectanglesize{#1}{#2}{#3}%
  \setlength{\paperheight}{\@tempdima}%
  \setlength{\paperwidth}{\@tempdimb}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\settypeblocksize}
% |\settypeblocksize{|\meta{height}|}{|\meta{width}|}{|\meta{ratio}|}|
% calulates the |\textheight| and |\textwidth| from two out of the
% three arguments. 
%    \begin{macrocode}
\newcommand{\settypeblocksize}[3]{%
  \setrectanglesize{#1}{#2}{#3}%
  \setlength{\textheight}{\@tempdima}%
  \setlength{\textwidth}{\@tempdimb}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\spinemargin}
% \begin{macro}{\foremargin}
% \begin{macro}{\setlrmargins}
% |\setlrmargins{|\meta{L}|}{|\meta{R}|}{|\meta{r}|}| sets the Left (spine)
% and Right (fore edge) margins with constant typeblock.
%    \begin{macrocode}
\newlength{\spinemargin}
\newlength{\foremargin}
\newcommand{\setlrmargins}[3]{%
  \setfillsize{\paperwidth}{\textwidth}{#1}{#2}{#3}%
  \setlength{\textwidth}{\@tempdimc}%
  \setlength{\spinemargin}{\@tempdima}%
  \setlength{\foremargin}{\@tempdimb}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\setlrmarginsandblock}
% |\setlrmarginsandblock{|\meta{L}|}{|\meta{R}|}{|\meta{r}|}| sets the Left (spine)
% and Right (fore edge) margins with variable typeblock.
%    \begin{macrocode}
\newcommand{\setlrmarginsandblock}[3]{%
  \setfillsize{\paperwidth}{*}{#1}{#2}{#3}%
  \setlength{\textwidth}{\@tempdimc}%
  \setlength{\spinemargin}{\@tempdima}%
  \setlength{\foremargin}{\@tempdimb}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\uppermargin}
% \begin{macro}{\lowermargin}
% \begin{macro}{\setulmargins}
% |\setulmargins{|\meta{L}|}{|\meta{R}|}{|\meta{r}|}| sets the Left (upper)
% and Right (lower) margins with constant typeblock.
%    \begin{macrocode}
\newlength{\uppermargin}
\newlength{\lowermargin}
\newcommand{\setulmargins}[3]{%
  \setfillsize{\paperheight}{\textheight}{#1}{#2}{#3}%
  \setlength{\textheight}{\@tempdimc}%
  \setlength{\uppermargin}{\@tempdima}%
  \setlength{\lowermargin}{\@tempdimb}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\setulmarginsandblock}
% |\setulmarginsandblock{|\meta{L}|}{|\meta{R}|}{|\meta{r}|}| sets the Left (upper)
% and Right (lower) margins with variable typeblock.
%    \begin{macrocode}
\newcommand{\setulmarginsandblock}[3]{%
  \setfillsize{\paperheight}{*}{#1}{#2}{#3}%
  \setlength{\textheight}{\@tempdimc}%
  \setlength{\uppermargin}{\@tempdima}%
  \setlength{\lowermargin}{\@tempdimb}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\headdrop}
% \begin{macro}{\setheaderspaces}
% |\setheaderspaces{|\meta{L}|}{|\meta{R}|}{|\meta{r}|}| sets the Left (head margin)
% and Right (headsep) spacing with constant headheight.
%    \begin{macrocode}
\newlength{\headdrop}
\newcommand{\setheaderspaces}[3]{%
  \setfillsize{\uppermargin}{\headheight}{#1}{#2}{#3}%
  \setlength{\headheight}{\@tempdimc}%
  \setlength{\headdrop}{\@tempdima}%
  \setlength{\headsep}{\@tempdimb}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\setheadfoot}
% |\setheadfoot{|\meta{headheight}|}{|\meta{footskip}|}|
% sets the headheight and the footskip.
%    \begin{macrocode}
\newcommand{\setheadfoot}[2]{%
  \setlength{\headheight}{#1}%
  \setlength{\footskip}{#2}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\setcolsepandrule}
% |\setcolsepandrule{|\meta{colsep}|}{|\meta{thickness}|}|
% sets the column separation and the rule thickness.
% \changes{v1.0}{2001/09/20}{Changed \cs{setcolseprule} to 
% \cs{setcolsepandrule} to match the documentation.}
%    \begin{macrocode}
\newcommand{\setcolsepandrule}[2]{%
  \setlength{\columnsep}{#1}%
  \setlength{\columnseprule}{#2}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\setmarginnotes}
% |\setmarginnotes{|\meta{sep}|}{|\meta{width}|}{|\meta{push}|}|
% sets the marginpar parameters.
%    \begin{macrocode}
\newcommand{\setmarginnotes}[3]{%
  \setlength{\marginparsep}{#1}%
  \setlength{\marginparwidth}{#2}%
  \setlength{\marginparpush}{#3}}

%    \end{macrocode}
% \end{macro}
%
%
%
%    Initialise the paper size and trimming to their default values.
%    \begin{macrocode}
\settrimmedsize{\stockheight}{\stockwidth}{*}
\settrims{\z@}{\z@}

%    \end{macrocode}
%
%
%
%    What now follows is the standard class's method for setting up
% the dimensions.
%
%    Set |\@tempdimb| to size-dependent initial line length and
% set |\@tempdima| to the maximum textwidth for the paper width, 
% an inch margin on either side. In the standard classes the initial
% line length is about 14\% greater than |\lxvchars|.
%    \begin{macrocode}
\setlength{\@tempdimb}{1.14\lxvchars}
\setlength\@tempdima{\paperwidth}
  \addtolength\@tempdima{-2in}
%    \end{macrocode}
%
% \begin{macro}{\textwidth}
% Now set the |\textwidth| depending on the number of columns. In twocolumn
% mode each column should be no wider than |\@tempdimb|.
%    \begin{macrocode}
\if@twocolumn
  \ifdim\@tempdima>2\@tempdimb\relax
    \setlength\textwidth{2\@tempdimb}
  \else
    \setlength\textwidth{\@tempdima}
  \fi
%    \end{macrocode}
% In onecolumn
% the text should not be wider than the minumum of the paperwidth (less
% 2in for the margins) and the maximum length of the character line.
%    \begin{macrocode}
\else
  \ifdim\@tempdima>\@tempdimb\relax
    \setlength\textwidth{\@tempdimb}
  \else
    \setlength\textwidth{\@tempdima}
  \fi
\fi
%    \end{macrocode}
% Adjust the width to be a whole number of points.
%    \begin{macrocode}
\@settopoint\textwidth

%    \end{macrocode}
% \end{macro}
%  
% \begin{macro}{\textheight}
%    The |\textheight| is the height of the text block, excluding
% headers and footers. This is set according to the |\paperheight|,
% to an integral number of lines, and allowing a 1in margin at the
% top and bottom and a further 1.5in for headers and footers.
%    \begin{macrocode}
\setlength\@tempdima{\paperheight}
  \addtolength\@tempdima{-3.5in}
%    \end{macrocode}
% Divide this height by the |\baselineskip| to get the number of lines.
% Then (re)calculate the |\textheight| and finally add the |\topskip|.
%    \begin{macrocode}
  \divide\@tempdima\baselineskip
\@tempcnta=\@tempdima
\setlength\textheight{\@tempcnta\baselineskip}
  \addtolength\textheight{\topskip}

%    \end{macrocode}
% \end{macro}
%
%    The margins are calculated. 
%
% \begin{macro}{\oddsidemargin}
% \begin{macro}{\marginparwidth}
% \begin{macro}{\evensidemargin}
%    The margins depend on the paper size, also for two sided
% printing the inner margin is made smaller than the outer.
%    \begin{macrocode}
\if@twoside
  \setlength\@tempdima       {\paperwidth}
  \addtolength\@tempdima     {-\textwidth}
  \setlength\oddsidemargin   {.4\@tempdima}
  \addtolength\oddsidemargin {-1in}
  \setlength\marginparwidth  {.6\@tempdima}
  \addtolength\marginparwidth{-\marginparsep}
  \addtolength\marginparwidth{-0.4in}
\else
  \setlength\@tempdima       {\paperwidth}
  \addtolength\@tempdima     {-\textwidth}
  \setlength\oddsidemargin   {.5\@tempdima}
  \addtolength\oddsidemargin {-1in}
  \setlength\marginparwidth  {.5\@tempdima}
  \addtolength\marginparwidth{-\marginparsep}
  \addtolength\marginparwidth{-0.8in} % don't know why this isn't .4
\fi
\ifdim\marginparwidth>2in
  \setlength\marginparwidth{2in}
\fi
%    \end{macrocode}
% Set these values to integer numbers of points, and calculate the
% |\evensidemargin|.
%    \begin{macrocode}
\@settopoint\oddsidemargin
\@settopoint\marginparwidth

\setlength\evensidemargin  {\paperwidth}
\addtolength\evensidemargin{-2in}
\addtolength\evensidemargin{-\textwidth}
\addtolength\evensidemargin{-\oddsidemargin}
\@settopoint\evensidemargin
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\topmargin}
%    The |\topmargin| is the distance below the top of the printable
% area (1in below the top of the paper) and the top of the box
% containing the running head.
%    \begin{macrocode}
\setlength\topmargin  {\paperheight}
\addtolength\topmargin{-2in}
\addtolength\topmargin{-\headheight}
\addtolength\topmargin{-\headsep}
\addtolength\topmargin{-\textheight}
\addtolength\topmargin{-\footskip}
\addtolength\topmargin{-.5\topmargin}
\@settopoint\topmargin

%    \end{macrocode}
% \end{macro}
%
% That is the end of the classical algorithm. Now calculate the 
% user-friendly dimensions. The calculations are simpler than in
% the general case as the |\paperwidth| and |\paperheight| is the 
% same as the |\stockwidth| and |\stockheight|. 
%
%    We can get the spine 
% and edge margins from the |\oddsidemargin|.
%    \begin{macrocode}
\setlength{\spinemargin}{\oddsidemargin}
\addtolength{\spinemargin}{1in}
\setlrmargins{\spinemargin}{*}{*}

%    \end{macrocode}
%
%    Similarly we can get the upper and lower margins from the
% |\topmargin|, |\headheight| and |\headskip|.
%    \begin{macrocode}
\setlength{\uppermargin}{\topmargin}
\addtolength{\uppermargin}{1in}
\addtolength{\uppermargin}{\headheight}
\addtolength{\uppermargin}{\headsep}
\setulmargins{\uppermargin}{*}{*}

%    \end{macrocode}
%
% \begin{macro}{\@memznegtest}
% DA suggested this in a private email (2003/002/13) to make error
% checking and reporting a bit more (space) efficient. Use like \\
% |\@memznegtest{\marginparsep}| instead of \\
% \begin{verbatim}
% \ifdim\marginparsep>\z@\else
%    \@memerror{\protect\marginparsep\space is zero or negative}{\@ehd}%
% \fi
% \end{verbatim}
% If its length variable argument is zero or less it reports an error.
% 
% \changes{v1.4}{2003/02/27}{Added \cs{@memznegtest}}
%    \begin{macrocode}
\newcommand*{\@memznegtest}[1]{%
  \ifdim#1>\z@\else
    \@memerror{\protect#1\space is zero or negative}{\@ehd}%
  \fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@memnegtest}
% Reports an error if its length variable argument is negative.
% \changes{v1.4}{2003/02/27}{Added \cs{@memnegtest}}
%    \begin{macrocode}
\newcommand*{\@memnegtest}[1]{%
  \ifdim#1<\z@
    \@memerror{\protect#1\space is negative}{\@ehd}%
  \fi}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\checkthelayout}
% This macro checks that the current layout dimensions are sensible and
% checks the arithmetic.
%
% \changes{v1.0a}{2002/01/18}{Added \cs{checkthelayout}}
%    \begin{macrocode}
\newcommand{\checkthelayout}{%
%    \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 need to adjust the |\textheight| and the
% |\lowermargin| to get an integral number of lines.
%    \begin{macrocode}
  \@tempdima = \textheight
    \divide\@tempdima \baselineskip
  \@tempcnta=\@tempdima
  \setlength{\textheight}{\@tempcnta\baselineskip}
    \addtolength{\textheight}{\topskip}
  \setulmargins{\uppermargin}{*}{*}
%    \end{macrocode}
%
% Check that all the sums add up correctly, or
% at least to within a small (|\@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}{\fixthelayout}
%   Calculate the normal
% \LaTeX{} page layout parameter values. We'll do the heights first
% as they are independent of the number of columns and the side printing.
% \changes{v1.0a}{2002/01/18}{Added \cs{fixthelayout}}
%    \begin{macrocode}
\newcommand{\fixthelayout}{%
  \topmargin = \trimtop
    \advance\topmargin \uppermargin
    \advance\topmargin -\headsep
    \advance\topmargin -\headheight
    \advance\topmargin -1in\relax
%    \end{macrocode}
%
%    Now the |\oddsidemargin|.
%    \begin{macrocode}
  \oddsidemargin = \stockwidth
    \advance\oddsidemargin -\trimedge
    \advance\oddsidemargin -\paperwidth
    \advance\oddsidemargin \spinemargin
    \advance\oddsidemargin -1in\relax
%    \end{macrocode}
%
%    And the |\evensidemargin|.
%    \begin{macrocode}
  \evensidemargin = \trimedge
    \advance\evensidemargin \foremargin
    \advance\evensidemargin -1in\relax
%    \end{macrocode}
%
%
%    Set the values to the nearest whole point.
%    \begin{macrocode}
  \@settopoint\textwidth
  \@settopoint\oddsidemargin
  \@settopoint\evensidemargin
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\typeoutlayout}
% Why not type out the calculated versions of the designed values?
% \changes{v1.0a}{2002/01/18}{Added \cs{typeoutlayout}}
% \changes{v1.0a}{2002/01/25}{Added column info to \cs{typeoutlayout}}
%    \begin{macrocode}
\newcommand{\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{******************************************************}
  \typeout{}
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\checkandfixthelayout}
% This macro checks and fixes the layout, and reports the result.
% \changes{v0.2}{2001/06/03}{Replaced \cs{fixpagelayout} by \cs{checkandfixthelayout}}
% \changes{v1.0a}{2002/01/18}{Partitioned \cs{checkandfixthelayout}}
%    \begin{macrocode}
\newcommand{\checkandfixthelayout}{%
  \checkthelayout
  \fixthelayout
  \typeoutlayout}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\fixpdflayout}
% \begin{macro}{\fixdvipslayout}
% Page layout with pdf\LaTeX{} seems a bit iffy.
% At the suggestion of Lars Madsen, help with 
% setting viewer (e.g., ghostview)
% window sizes for dvi/ps. \cs{fixdvipslayout} does for \file{dvi} output
% as \cs{fixpdflayout} does for \file{pdf} output.
% \changes{v1.4}{2003/11/22}{Added \cs{fixpdflayout} (from patch v1.6)}
% \changes{v1.4}{2003/12/01}{Added \cs{fixdvipslayout} }
%    \begin{macrocode}
\newcommand*{\fixpdflayout}{%
  \pdfpageheight=\the\stockheight
  \pdfpagewidth=\the\stockwidth
  \ifdim\pdfvorigin=0pt\pdfvorigin=1in\fi
  \ifdim\pdfhorigin=0pt\pdfhorigin=1in\fi}
\newcommand*{\fixdvipslayout}{%
  \AtBeginDvi{\special{papersize=\the\stockwidth,\the\stockheight}}}

\AtBeginDocument{%
  \ifpdf
    \ifnum\pdfoutput<\@ne
      \fixdvipslayout
    \else
      \fixpdflayout
    \fi
  \else
    \fixdvipslayout
  \fi}

%    \end{macrocode}
%
% With a landscape document when going \texttt{latex -> dvips} the 
% resulting \file{.ps} file
% may apear upside down in \texttt{ghostview}. If this happens, try putting
% the following in the document preamble:
% \begin{verbatim}
% \addtodef{\fixdvipslayout}{}{%
%   \special{!TeXDict begin /landplus90{true}store end }}
% \end{verbatim}
% See \texttt{http://www.radicaleye.com.dvips.html} (DVIPS Home Page) for
% an explanation.
%
%  Some other potential specials for PostScript printing may be 
% (at least for an HP 5SiMx LaserJet duplex printer):
% \begin{verbatim}
% \special{!TeXDict begin <</Duplex true>> 
%                         setpagedevice end} % duplex
% \special{!TeXDict begin <</Tumble true>> 
%                         setpagedevice end} % short side binding
% \end{verbatim}
% \end{macro}
% \end{macro}
%
% 
% \begin{macro}{\typeoutstandardlayout}
% Types out the current values of the standard page layout parameters.
% \changes{v1.0a}{2002/01/25}{Added \cs{typeoutstandardlayout}}
%    \begin{macrocode}
\newcommand{\typeoutstandardlayout}{%
  \typeout{}
  \typeout{******************************************************}
  \typeout{Page height and width: 
                \the\paperheight\space by \the\paperwidth}
  \typeout{Text height and width: 
               \the\textheight\space by \the\textwidth}
  \typeout{Oddside and evenside margins: 
                \the\oddsidemargin\space and \the\evensidemargin}
  \typeout{Topmargin and footskip: 
                \the\topmargin\space and \the\footskip}
  \typeout{Headheight and headsep: 
                \the\headheight\space and \the\headsep}
  \typeout{Columnsep and columnseprule: 
                \the\columnsep\space and \the\columnseprule}
  \typeout{Marginparsep and marginparwidth: 
                \the\marginparsep\space and \the\marginparwidth}
  \typeout{******************************************************}
  \typeout{}
}

%    \end{macrocode}
% \end{macro}
%
%
%
%
% \subsection{Float placement parameters}
%
%
% All float parameters are given default values in the \LaTeX{}
% kernel. For this reason counters only need to be set with
% |\setcounter| and other parameters are set using |\renewcommand|.
%
% \paragraph{Limits for the placement of floating objects}
%
% The settings here make it easier to place floats than with the
% standard classes.
%
% \begin{macro}{\c@topnumber}
%    The \Lcount{topnumber} counter holds the maximum number of
%    floats that can appear on the top of a text page (classically 2)
%    \begin{macrocode}
\setcounter{topnumber}{3}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\topfraction}
%    This indicates the maximum part of a text page that can be
%    occupied by floats at the top (classically 0.7).
%    \begin{macrocode}
\renewcommand{\topfraction}{.85}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\c@bottomnumber}
%    The \Lcount{bottomnumber} counter holds the maximum number of
%    floats that can appear on the bottom of a text page (classically 1).
%    \begin{macrocode}
\setcounter{bottomnumber}{2}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\bottomfraction}
%    This indicates the maximum part of a text page that can be
%    occupied by floats at the bottom (classically 0.3).
%    \begin{macrocode}
\renewcommand{\bottomfraction}{.5}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\c@totalnumber}
%    This indicates the maximum number of floats that can appear on
%    any text page (classically 3).
%    \begin{macrocode}
\setcounter{totalnumber}{4}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\textfraction}
%    This indicates the minimum part of a text page that has to be
%    occupied by text (classically 0.2).
%    \begin{macrocode}
\renewcommand{\textfraction}{.1}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\floatpagefraction}
%    This indicates the minimum part of a page that has to be
%    occupied by floating objects before a `float page' is 
%    produced (classically 0.5).
%    \begin{macrocode}
\renewcommand{\floatpagefraction}{.7}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\c@dbltopnumber}
%    The \Lcount{dbltopnumber} counter holds the maximum number of
%    two column floats that can appear on the top of a two column text
%    page (classically 2).
%    \begin{macrocode}
\setcounter{dbltopnumber}{3}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\dbltopfraction}
%    This indicates the maximum part of a two column text page that
%    can be occupied by two column floats at the top (classically 0.7).
%    \begin{macrocode}
\renewcommand{\dbltopfraction}{.85}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\dblfloatpagefraction}
%    This indicates the minimum part of a page that has to be
%    occupied by two column wide floating objects before a `float
%    page' is produced (classically 0.5).
%    \begin{macrocode}
\renewcommand{\dblfloatpagefraction}{.7}
%    \end{macrocode}
% \end{macro}
%
%
% \section{Page Styles}
%
%    The page style \pstyle{foo} is defined by defining the command
%    |\ps@foo|.   This command should make only local definitions.
%    There should be no stray spaces in the definition, since they
%    could lead to mysterious extra spaces in the output.
%
% \begin{macro}{\@evenhead}
% \begin{macro}{\@oddhead}
% \begin{macro}{\@evenfoot}
% \begin{macro}{\@oddfoot}
%    The |\ps@...| command defines the macros |\@oddhead|,
%    |\@oddfoot|, |\@evenhead|, and |\@evenfoot| to define the running
%    heads and feet---e.g., |\@oddhead| is the macro to produce the
%    contents of the heading box for odd-numbered pages.  It is called
%    inside an |\hbox| of width |\textwidth|.
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsection{Marking conventions}
%
%    To make headings determined by the sectioning commands, the page
%    style defines the commands |\chaptermark|, |\sectionmark|,
%    \ldots,\\
%     where |\chaptermark{|\meta{TEXT}|}| is called by
%    |\chapter| to set a mark, and so on.
%
%    The |\...mark| commands and the |\...head| macros are defined
%    with the help of the following macros.  (All the |\...mark|
%    commands should be initialized to no-ops.)
%
%    \LaTeX{} extends \TeX's |\mark| facility by producing two kinds
%    of marks, a `left' and a `right' mark, using the following
%    commands:
%    \begin{flushleft}
%     |\markboth{|\meta{LEFT}|}{|\meta{RIGHT}|}|: Adds both marks.
%
%     |\markright{|\meta{RIGHT}|}|: Adds a `right' mark.
%
%     |\leftmark|: Used in the |\@oddhead|, |\@oddfoot|, |\@evenhead|
%                  or |\@evenfoot| macros, it gets the current `left'
%                  mark.  |\leftmark| works like \TeX's |\botmark|
%                  command.
%
%     |\rightmark|: Used in the |\@oddhead|, |\@oddfoot|, |\@evenhead|
%                   or  |\@evenfoot| macros, it gets the current
%                   `right' mark. |\rightmark| works like \TeX's
%                   |\firstmark| command.
%    \end{flushleft}
%
%    The marking commands work reasonably well for right marks
%    `numbered within' left marks--e.g., the left mark is changed by a
%    |\chapter| command and the right mark is changed by a |\section|
%    command.  However, it does produce somewhat anomalous results if
%    two |\markboth|'s occur on the same page.
%
%
%    Commands like |\tableofcontents| that should set the marks in some
%    page styles use a |\@mkboth| command, which is |\let| by the
%    pagestyle command (|\ps@...|)  to |\markboth| for setting the
%    heading or to |\@gobbletwo| to do nothing.
%
%
%
% \subsection{Defining the page styles}
%
%    This class provides a set of commands for the user to define new
% pagestyles. Essentially defining a pagestyle consists of defining the
% macros |\@evenhead{}|, |\@oddhead{}|, |\@evenfoot{}|, and |\@oddfoot{}|.
% For this class, each header and footer is treated as three parts: a left,
% center, and right part. In this case, defining a pagestyle consists of
% specifying these 12 portions of the running headers and footers. The
% width of the headers/footers may also be specified, rules may be drawn
% below the headers and/or above the footers, and the complete header and/or
% footer may be offset with respect to the textblock when the width is not
% the same as the textwidth.
%
%    In the following \meta{style} is the name of a pagestyle being defined
% (e.g., |ruled|).
%
% \begin{macro}{\makeevenhead}
% The command 
% |\makeevenhead{|\meta{style}|}{|\meta{left}|}{|\meta{center}|}{|\meta{right}|}|
% specifies that the left, center and right portions of the even header for
% pagestyle \meta{style} are defined as the other thre arguments, respectiveley.
% Internally it defines the commands |\styleeheadl|, |\styleeheadc| and
% |\styleeheadr| to be \meta{left}, \meta{center} and \meta{right} respectively.
%    \begin{macrocode}
\newcommand{\makeevenhead}[4]{%
  \@namedef{#1eheadl}{#2}
  \@namedef{#1eheadc}{#3}
  \@namedef{#1eheadr}{#4}
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\makeoddhead}
% \begin{macro}{\makeevenfoot}
% \begin{macro}{\makeoddfoot}
%  These three macros are similar to |\makeevenhead| except that they are
% for the oddhead, evenfoot and oddfoot.
%    \begin{macrocode}
\newcommand{\makeoddhead}[4]{%
  \@namedef{#1oheadl}{#2}
  \@namedef{#1oheadc}{#3}
  \@namedef{#1oheadr}{#4}
}
\newcommand{\makeevenfoot}[4]{%
  \@namedef{#1efootl}{#2}
  \@namedef{#1efootc}{#3}
  \@namedef{#1efootr}{#4}
}
\newcommand{\makeoddfoot}[4]{%
  \@namedef{#1ofootl}{#2}
  \@namedef{#1ofootc}{#3}
  \@namedef{#1ofootr}{#4}
}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\makerunningwidth}
%  The macro |\makerunningwidth{|\meta{style}|}{|\meta{length}|}| sets the
% width of the headers and footers of pagestyle \meta{style} to be \meta{length}.
%    \begin{macrocode}
\newcommand{\makerunningwidth}[2]{%
  \@namedef{#1runwidth}{#2}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\normalrulethickness}
% \begin{macro}{\footruleheight}
% \begin{macro}{\footruleskip}
% \begin{macro}{\makeheadrule}
% \begin{macro}{\makefootrule}
% |\normalrulethickness| is the thickness of a normal horizontal 
% or vertical rule. 
% |\footruleheight| is the height of a normal rule above a footer (actually zero).
% |\footruleskip| is a distance sufficient to ensure that a foot rule 
% will appear between the bottom of the textblock and above any actual footer.
%
% (There was a \ctt{} thread \textit{ngerman, fancyhdr and \cs{footrulewidth} --- bug?}
%  in December 2002 that bears on the definitions below).
% \changes{v1.4}{2003/02/27}{Make \cs{normalrulethickness} a length, 
%                            not a macro (patch 1.1)}
% \changes{v1.4}{2003/02/27}{Initialise \cs{footruleheight} to 0pt, not
%                            \cs{z@skip} (patch 1.1)}
%    \begin{macrocode}
\newlength{\normalrulethickness}
  \setlength{\normalrulethickness}{0.4pt}
\newcommand{\footruleheight}{0pt}
\newcommand{\footruleskip}{0.3\normalbaselineskip}
%    \end{macrocode}
% The macro |\makeheadrule{|\meta{style}|}{|\meta{width}|}{|\meta{height}|}|
% specifies the width and height of the header rule for \meta{style}. 
%
% Similarly
% |\makefootrule{|\meta{style}|}{|\meta{width}|}{|\meta{height}|}{|\meta{skip}|}|
% specifies the width, height and skip for the footrule.
%    \begin{macrocode}
\newcommand{\makeheadrule}[3]{%
  \@namedef{#1headrule}{%
    \hrule\@width #2\@height #3 \vskip-#3}
}
\newcommand{\makefootrule}[4]{%
  \@namedef{#1footrule}{%
    \vskip-#4\vskip-#3
    \hrule\@width #2\@height #3 \vskip #4}
}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%  
% \begin{macro}{\makeheadposition}
% |\makeheadposition{|\meta{style}|}{|\meta{eheadpos}|}{|\meta{oheadpos}|}{|\meta{efootpos}|}{|\meta{ofootpos}|}|
% specifies the horizontal positioning of the even and odd headers and footers,
% respectively, 
% for the pagestyle \meta{style}. Each of the \meta{...pos} arguments may be
% either |flushleft|, |center| or |flushright|, with the obvious meanings.
% An empty argument (or an unrecognised one) is equivalent to |center|.
%    \begin{macrocode}
\newcommand{\makeheadposition}[5]{%
%    \end{macrocode}
% Do the even head position first.
%    \begin{macrocode}
  \nametest{flushleft}{#2}
  \ifsamename
    \@namedef{#1evenhpl}{\relax} \@namedef{#1evenhpr}{\hss}
  \else
    \nametest{flushright}{#2}
    \ifsamename
      \@namedef{#1evenhpl}{\hss} \@namedef{#1evenhpr}{\relax}
    \else
      \@namedef{#1evenhpl}{\hss} \@namedef{#1evenhpr}{\hss}
    \fi
  \fi
%    \end{macrocode}
% And similarly for the odd head and even \& odd footers.
%    \begin{macrocode}   
  \nametest{flushleft}{#3}
  \ifsamename
    \@namedef{#1oddhpl}{\relax} \@namedef{#1oddhpr}{\hss}
  \else
    \nametest{flushright}{#3}
    \ifsamename
      \@namedef{#1oddhpl}{\hss} \@namedef{#1oddhpr}{\relax}
    \else
      \@namedef{#1oddhpl}{\hss} \@namedef{#1oddhpr}{\hss}
    \fi
  \fi
  \nametest{flushleft}{#4}
  \ifsamename
    \@namedef{#1evenfpl}{\relax} \@namedef{#1evenfpr}{\hss}
  \else
    \nametest{flushright}{#4}
    \ifsamename
      \@namedef{#1evenfpl}{\hss} \@namedef{#1evenfpr}{\relax}
    \else
      \@namedef{#1evenfpl}{\hss} \@namedef{#1evenfpr}{\hss}
    \fi
  \fi
  \nametest{flushleft}{#5}
  \ifsamename
    \@namedef{#1oddfpl}{\relax} \@namedef{#1oddfpr}{\hss}
  \else
    \nametest{flushright}{#5}
    \ifsamename
      \@namedef{#1oddfpl}{\hss} \@namedef{#1oddfpr}{\relax}
    \else
      \@namedef{#1oddfpl}{\hss} \@namedef{#1oddfpr}{\hss}
    \fi
  \fi
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\makepsmarks}
% \begin{macro}{\makepshook}
% The macro |\makepsmarks{|\meta{style}|}{|\meta{code}|}| specifies that
% \meta{code} is the definition of the hook for pagestyle \meta{style}.
% |\makepshook| is a deprecated version of |\makepsmarks|.
% \changes{v1.0}{2001/11/15}{Added \cs{makepsmarks} and deprecated 
%                                  \cs{makepshook}}
% \changes{v1.4}{2003/12/14}{Deleted \cs{makepshook}}
%    \begin{macrocode}
\newcommand{\makepsmarks}[2]{\@namedef{#1pshook}{#2}}
%%%% \newcommand{\makepshook}[2]{%
%%%%   \@memwarn{\protect\makepshook\space is deprecated.\MessageBreak
%%%%                         Use \protect\makepsmarks\space instead}
%%%%   \@namedef{#1pshook}{#2}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\m@mhe@dreset}
% To cater for potential baselineskip changes, such as \verb?\linespread{2}?,
% we have to ensure they don't percolate into the header/footer.
% (See CTT thread \textit{memoir + linespread}, 2004/02/11)
% \changes{v1.61}{2004/03/14}{Added \cs{m@mhe@dreset}}
%    \begin{macrocode}
\newcommand*{\m@mhe@dreset}{\def\baselinestretch{1}\normalsize}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\makepagestyle}
%  At last we can put everything together for defining a new pagestyle,
% via the macro |\makepagestyle{|\meta{style}|}| which initially sets up 
% a new pagestyle \meta{style} corresponding the the \LaTeX{} |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}
\newcommand{\makepagestyle}[1]{%
%    \end{macrocode}
% First define the macro |\ps@style|, which in turn defines the macros
% |\@evenhead|, |\@oddhead|, |\@evenfoot| and |\@oddfoot|.
%    \begin{macrocode}
  \@namedef{ps@#1}{%
    \@namedef{#1@evenhead}{%
%    \end{macrocode}
% The code for the definition of |\@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 |\relax| or |\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 |\relax| or |\hss|.
%    \begin{macrocode}
        \@nameuse{#1headrule}}}\@nameuse{#1evenhpr}}
%    \end{macrocode}
% The code for the |\@oddhead|, |\@evenfoot| and |\@oddfoot| follows
% a similar pattern. Here is |\@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 |\@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 |\@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 |\@evenhead| etc., in terms of |#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 |\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 |\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 |\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 |\aliaspagestyle{|\meta{aliasl}|}{|\meta{original}|}|
% defines the \meta{alias} pagestyle to be an alias for the
% \meta{original} pagestyle.
%    \begin{macrocode}
\newcommand{\aliaspagestyle}[2]{%
  \@namedef{ps@#1}{\@nameuse{ps@#2}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\copypagestyle}
% The command |\copypagestyle{|\meta{new}|}{|\meta{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}
\newcommand{\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 |\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}
% |\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. |\if@fcolmade| is specified in \file{ltoutput.dtx}.
% \changes{v1.0}{2001/11/14}{Added \cs{ifonlyfloats}}
%    \begin{macrocode}
\newcommand{\ifonlyfloats}[2]{\if@fcolmade #1\else #2\fi}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\mergepagefloatstyle}
% |\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}
\newcommand{\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 |\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 |\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 |\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 |\markboth| and |\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 |\let\tocmark\@gobble| in the myheadings
% pagestyle did nasty things
% to the ToC, etc. Now using |\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, |\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 |\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 |\textwidth|.
%    \begin{macrocode}
\makeheadrule{ruled}{\textwidth}{\normalrulethickness}
%    \end{macrocode}
%
% \begin{macro}{\@ruledmarks}
% We have to make sure that the |\chapter| and |\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}
\newcommand{\@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 |\headwidth| length for this.
% \changes{v1.1}{2002/03/10}{Added \cs{tocmark} etc., to \cs{ps@companion}}
%    \begin{macrocode}
\newlength{\headwidth}

\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{Page numbering}
%
%    The kernel includes the |\pagenumbering| command for setting the
% style (arabic, roman, etc.) of the page numbers, and at the same
% time it resets the page counter. I want a version that resets the style
% but not the number\footnote{Added to meet a request by Daniel Richard G.
% (\texttt{skunk@mit.edu}) September 2001.}.
%
% \begin{macro}{\pagenumbering}
% \begin{macro}{\pagenumbering*}
% |\pagenumbering{|\meta{style}|}| is the normal version whereas
% the starred version does not reset the counter.
% \changes{v1.0}{2001/09/21}{Added \cs{pagenumbering} and \cs{pagenumbering*} }
%    \begin{macrocode}
\renewcommand{\pagenumbering}{%
  \@ifstar{\@smempnum}{\@mempnum}}
%    \end{macrocode}
% \begin{macro}{\@smempnum}
%    \begin{macrocode}
\newcommand{\@smempnum}[1]{%
  \gdef\thepage{\csname @#1\endcsname \c@page}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\@mempnum}
%    \begin{macrocode}
\newcommand{\@mempnum}[1]{%
  \@smempnum{#1}\global\c@page \@ne}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\c@storedpagenumber}
% A counter to store the page number.
%    \begin{macrocode}
\newcounter{storedpagenumber}
  \setcounter{storedpagenumber}{1}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\savepagenumber}
% \begin{macro}{\restoregenumber}
% |\savepagenumber| saves the current page number and |\restorepagenumber|
% sets the page number to the stored value.
% \changes{v1.0}{2001/09/24}{Added \cs{savepagenumber} and \cs{restorepagenumber}}
%    \begin{macrocode}
\newcommand{\savepagenumber}{\global\c@storedpagenumber \c@page}
\newcommand{\restorepagenumber}{\global\c@page \c@storedpagenumber}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \section{Titles and abstracts}
%
% \subsection{Titles}
%
%    For books the title is usually designed specifically for the particular
% work, so originally the class did not provide a |\maketitle| command or 
% any of the |\title| and other commands that normally go along with this. 
%    After some thought I decided to add in the code from the 
% \Lpack{titling} package~\cite{TITLING}. However, in this case life
% is a bit simpler as there is no pre-existing class code.
% \changes{v0.3}{2001/07/09}{Added titling code}
%
% \begin{macro}{\pretitle}
% \begin{macro}{\@bspretitle}
% \begin{macro}{\posttitle}
% \begin{macro}{\@bsposttitle}
% \begin{macro}{\preauthor}
% \begin{macro}{\@bspreauthor}
% \begin{macro}{\postauthor}
% \begin{macro}{\@bspostauthor}
% \begin{macro}{\predate}
% \begin{macro}{\@bspredate}
% \begin{macro}{\postdate}
% \begin{macro}{\@bspostdate}
%    To provide some flexibilty in the titling style of the document,
% user level commands are provided that can be changed to reconfigure
% the appearance resulting from |\maketitle|. 
%    \begin{macrocode}
\newcommand{\pretitle}[1]{\def\@bspretitle{#1}}
\newcommand{\posttitle}[1]{\def\@bsposttitle{#1}}
\newcommand{\preauthor}[1]{\def\@bspreauthor{#1}}
\newcommand{\postauthor}[1]{\def\@bspostauthor{#1}}
\newcommand{\predate}[1]{\def\@bspredate{#1}}
\newcommand{\postdate}[1]{\def\@bspostdate{#1}}

%    \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}
%
% These are defined initially
% to mimic the normal \LaTeX{} style.
%    \begin{macrocode}
  \pretitle{\begin{center}\LARGE}
  \posttitle{\par\end{center}\vskip 0.5em}
  \preauthor{\begin{center}
    \large \lineskip .5em%
    \begin{tabular}[t]{c}}
  \postauthor{\end{tabular}\par\end{center}}
  \predate{\begin{center}\large}
  \postdate{\par\end{center}}

%    \end{macrocode}
%
% \begin{macro}{\maketitlehooka}
% \begin{macro}{\maketitlehookb}
% \begin{macro}{\maketitlehookc}
% \begin{macro}{\maketitlehookd}
% The four hooks which will be called by |\maketitle|. These are initially
% vacuous.
%    \begin{macrocode}
\newcommand{\maketitlehooka}{}
\newcommand{\maketitlehookb}{}
\newcommand{\maketitlehookc}{}
\newcommand{\maketitlehookd}{}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\thanksmarkseries}
% \begin{macro}{\@bsmarkseries}
% \begin{macro}{\symbolthanksmark}
%  These are for specifying the kind of series for thanks markers.
%    \begin{macrocode}
\newcommand{\thanksmarkseries}[1]{%
  \def\@bsmarkseries{\renewcommand{\thefootnote}%
                     {\@nameuse{#1}{footnote}}}}
\newcommand{\symbolthanksmark}{\thanksmarkseries{\fnsymbol}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\continuousmarks}
% \begin{macro}{\@bscontmark}
% These are for (non) zeroing of the footnote counter.
%    \begin{macrocode}
\newcommand{\@bscontmark}{\setcounter{footnote}{0}}
\newcommand{\continuousmarks}{\def\@bscontmark{}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\thanksheadextra}
% \begin{macro}{\@bsthanksheadpre}
% \begin{macro}{\@bsthanksheadpost}
% These are for inserting stuff before and after a mark in the titling.
%    \begin{macrocode}
\newcommand{\thanksheadextra}[2]{%
  \def\@bsthanksheadpre{#1}%
  \def\@bsthanksheadpost{#2}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\thanksfootextra}
% These are for inserting stuff before and after a thanks mark in the 
% footnoting.
% \changes{v1.2}{2002/08/10}{Deprecated \cs{thanksfootextra}}
% \changes{v1.2}{2002/08/10}{Removed \cs{thanksfootpre} and \cs{thanksfootpost}}
% \changes{v1.4}{2003/12/14}{Removed \cs{thanksfootextra}}
%    \begin{macrocode}
%%%% \newcommand{\thanksfootextra}[2]{%
%%%%  \@memwarn{Use \protect\thanksmarkstyle\space instead of
%%%%                        \protect\thanksfootextra}%
%%%%  \def\thanksfootpre{#1}%
%%%%  \def\thanksfootpost{#2}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\thanksmark}
% This adds a thanks mark. The |\footnotemark| could have been used directly
% but it is fragile in a moving argument.
%    \begin{macrocode}
\DeclareRobustCommand{\thanksmark}[1]{\footnotemark[#1]}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\thanksgap}
% This specifies some horizontal space.
%    \begin{macrocode}
\newcommand{\thanksgap}[1]{\hspace{#1}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\tamark}
% This stores the current definition of |\@thefnmark|. For some reason
% using |\@thefnmark| directly only gave the last value.
%    \begin{macrocode}
\newcommand{\tamark}{\@thefnmark}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\thanksmarkwidth}
% A length determining the size of the box for typesetting
% a thanks marker.
%    \begin{macrocode}
\newlength{\thanksmarkwidth}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\thanksmarksep}
% A length determining the inset of thanks footnotes.
% \changes{v1.2}{2002/08/10}{Replaced \cs{thanksmargin} by \cs{thanksmarksep}}
%    \begin{macrocode}
\newlength{\thanksmarksep}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\thanksmarkstyle}
% \begin{macro}{\thanksscript}
% |\thanksscript| is a wrapper round the actual mark stuff to be typeset. 
% The user can define this via |\thanksmarkstyle|\marg{code}.
% The default is a superscript mark.
% \changes{v1.2}{2002/08/10}{Added \cs{thanksmarkstyle}}
%    \begin{macrocode}
\newcommand{\thanksmarkstyle}[1]{\def\thanksscript##1{#1}}
\thanksmarkstyle{\textsuperscript{#1}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\makethanksmarkhook}
% A vacuous macro used as a hook into |\makethanksmark|.
%    \begin{macrocode}
\newcommand{\makethanksmarkhook}{}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\thanksfootmark}
% This typesets the thanks footnote mark.
% \changes{v1.2}{2002/08/10}{Complete rewrite of \cs{thanksfootmark}}
%    \begin{macrocode}
\newcommand{\thanksfootmark}{%
  \ifdim\thanksmarkwidth < \z@
%    \end{macrocode}
% Negative width, mark is in the margin.
%    \begin{macrocode}
    \llap{\hb@xt@ -\thanksmarkwidth{%
          \hss\normalfont\thanksscript{\tamark}}%
          \hspace*{-\thanksmarkwidth}}%
  \else
    \ifdim\thanksmarkwidth = \z@
%    \end{macrocode}
% Zero width, mark is at (inside) the margin.
%    \begin{macrocode}
      {\normalfont\thanksscript{\tamark}}%
    \else
%    \end{macrocode}
% Positive width.
%    \begin{macrocode}
      \hb@xt@\thanksmarkwidth{\hss\normalfont\thanksscript{\tamark}}%
    \fi
  \fi}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\makethanksmark}
% This sets the general indentations for the thanks footnote, 
% and typesets the mark. The code is a simplified version of that
% for typesetting ToC entries.
% \changes{v1.2}{2002/08/10}{Minor changes to \cs{makethanksmark}}
%    \begin{macrocode}
\newcommand{\makethanksmark}{%
  \leavevmode%
  \parindent 1em\noindent
  \leftskip\thanksmarksep\relax
  \advance\leftskip\thanksmarkwidth \null\nobreak\hskip-\leftskip\relax
  \makethanksmarkhook\relax
  \thanksfootmark}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\usethanksrule}
% \begin{macro}{\cancelhanksrule}
%  Simple macros that let |\footnoterule| to another rule definition.
%    \begin{macrocode}
\newcommand{\usethanksrule}{\let\footnoterule\thanksrule}
\newcommand{\cancelthanksrule}{\let\footnoterule\@bsfootnoterule}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
%    Now set up the rest of the thanks defaults, styling having been
% done earlier.
%    \begin{macrocode}
\thanksmarkseries{fnsymbol}  % symbols
\thanksheadextra{}{}
\setlength{\thanksmarkwidth}{1.8em}
\setlength{\thanksmarksep}{-\thanksmarkwidth}

%    \end{macrocode}
% \begin{macro}{\thanksrule}
% \begin{macro}{\@bsfootnoterule}
% These are saved versions of the |\footnoterule| definition as it
% is at the end of the preamble.
%    \begin{macrocode}
\AtBeginDocument{%
  \let\thanksrule\footnoterule
  \let\@bsfootnoterule\footnoterule
}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\droptitle}
% A titling block has |\droptitle| amount of additional vertical space 
% above it (normally zero).
%    \begin{macrocode}
\newlength{\droptitle}
\setlength{\droptitle}{0pt}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\maketitle}
% The following is a
% modification of |\maketitle| as in the \Lpack{article}, \Lpack{report},
% and \Lpack{book} classes. It sets the pagestyle to \pstyle{title}.
% \changes{v1.4}{2004/01/21}{Removed the \cs{rlap} from \cs{@makefnmark}
%                            in \cs{maketitle} so multiple \cs{thanks}
%                            work per \cs{footnote}}
% \changes{v1.618}{2005/09/03}{Fix to remove extra blank page when
%   \cs{maketitle} (and \cs{@maketitle}) is used within adjustwidth
%    inside a titlingpage (mempatch v3.2)}
%    \begin{macrocode}
\newcommand{\maketitle}{\par
  \begingroup
    \@bsmarkseries
    \def\@makefnmark{\@textsuperscript{%
       \normalfont\@bsthanksheadpre \tamark \@bsthanksheadpost}}%
    \long\def\@makefntext##1{\makethanksmark ##1}
    \if@twocolumn
      \ifnum \col@number=\@ne
        \@maketitle
      \else
        \twocolumn[\@maketitle]%
      \fi
    \else
      \ifdim\pagetotal>\z@
        \newpage
      \fi
      \global\@topnum\z@
      \@maketitle
    \fi
    \thispagestyle{title}\@thanks
  \endgroup
  \@bscontmark  %  \setcounter{footnote}{0}%
  }
\aliaspagestyle{title}{plain}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@mem@titlefootkill}
% |\@mem@titlefootkill|\marg{note} Warn about footnotes in titles.
%    \begin{macrocode}
\newcommand*{\@mem@titlefootkill}[1]{%
  \@memwarn{Do not use \string\footnote\space in 
            \string\maketitle.\MessageBreak
            Use \protect\thanks\space instead}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@maketitle}
% Our version of |\@maketitle|.
% Footnotes are killed in the title; see the thread `|\title|, |\author|
% and |\footnote| feature in memoir class', February 2003.
% \changes{v1.4}{2003/11/16}{Killed footnote in titles (from patch v1.4)}
%    \begin{macrocode}
\newcommand{\@maketitle}{%               
  \let\footnote\@mem@titlefootkill
  \ifdim\pagetotal>\z@
    \newpage
  \fi
  \null
  \vskip 2em%
        \vspace*{\droptitle}
  \maketitlehooka
  {\@bspretitle \@title \@bsposttitle}
  \maketitlehookb
  {\@bspreauthor \@author \@bspostauthor}
  \maketitlehookc
  {\@bspredate \@date \@bspostdate}
  \maketitlehookd
  \par
  \vskip 1.5em}

%    \end{macrocode}
% \end{macro}
%
% \begin{environment}{titlingpage}
%  The |titlingpage| environment sets the pagestyle to be \pstyle{titlingpage},
% disables the footnote rule and ensures that the page is single
% column. At the end it switches back to twocolumn if neccessary,
% and then starts a new page as number 1.
%    \begin{macrocode}
\newenvironment{titlingpage}%
  {\let\footnoterule\relax
   \let\footnotesize\small
   \if@twocolumn
     \@restonecoltrue\onecolumn
   \else
     \@restonecolfalse
   \fi
   \thispagestyle{titlingpage}%
   \setcounter{page}{\@ne}%
  }{%
   \thispagestyle{titlingpage}%
   \if@restonecol \twocolumn \fi
   \if@twoside \cleardoublepage \else \clearpage \fi
   \setcounter{page}{\@ne}}
\aliaspagestyle{titlingpage}{empty}

%    \end{macrocode}
% \end{environment}
%
% \begin{macro}{\emptythanks}
% This macro discards all prior |\thanks| texts.
%    \begin{macrocode}
\newcommand{\emptythanks}{\global\let\@thanks\@empty}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\andnext}
%  The kernel |\and| macro puts space between author's names. The
% |\andnext| macro puts a newline between the names.
% \changes{v1.6}{2004/01/28}{Added \cs{andnext}}
%    \begin{macrocode}
\newcommand*{\andnext}{%
  \end{tabular}\\ \begin{tabular}[t]{c}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@bsmtitlempty}
%    |\@bsmtitlempty| is a helper macro to save some macro space. 
% It empties some elements of |\maketitle|.
%    \begin{macrocode}
\newcommand{\@bsmtitlempty}{%
  \global\let\maketitle\relax
  \global\let\@maketitle\relax
  \global\let\title\relax
  \global\let\author\relax
  \global\let\date\relax
  \global\let\thanksmarkseries\relax
  \global\let\thanksheadextra\relax
  \global\let\thanksfootextra\relax
  \global\let\thanksmark\relax
  \global\let\thanksgap\relax
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\keepthetitle}
% This macro undefines all the titling commands except for
% |\thetitle|, |\theauthor| and |\thedate|.
% \changes{v1.6}{2004/01/28}{Xhanged \cs{keepthetitle} to cater for \cs{andnext}}
%    \begin{macrocode}
\newcommand{\keepthetitle}{%
  \@bsmtitlempty
  \global\let\thanks\relax
  \global\let\and\relax
  \global\let\andnext\relax
  \global\let\@thanks\@empty
  \global\let\@title\@empty
  \global\let\@author\@empty
  \global\let\@date\@empty
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\killtitle}
% |\killtitle| undefines the remaining macros of |\maketitle|.
%    \begin{macrocode}
\newcommand{\killtitle}{%
  \keepthetitle
  \global\let\thetitle\relax
  \global\let\theauthor\relax
  \global\let\thedate\relax
}

%    \end{macrocode}
% \end{macro}
%
% 
% \begin{macro}{\thetitle}
% \begin{macro}{\theauthor}
% \begin{macro}{\thedate}
%    In order to make the |\title|, etc., values available for printing
% their definitions (in \file{ltsect.dtx}) need extending to save their 
% arguments. We have to make sure that extraneous material,
% like |\thanks|, is excluded from the saved texts.
% \changes{v1.4}{2003/11/16}{Gobble footnotes in \cs{title}, \cs{author}
%               and \cs{date} (from patch v1.4)}
% \changes{v1.6}{2004/01/28}{Added \cs{andnext} handling to \cs{author}}
%    \begin{macrocode}
\addtoiargdef{\title}{%
  \begingroup\let\footnote\@gobble}{%
  \begingroup
    \renewcommand{\thanks}[1]{}
    \renewcommand{\thanksmark}[1]{}
    \renewcommand{\thanksgap}[1]{}
    \protected@xdef\thetitle{#1}
  \endgroup\endgroup}
\addtoiargdef{\author}{%
  \begingroup\let\footnote\@gobble}{%
  \begingroup
    \renewcommand{\thanks}[1]{}
    \renewcommand{\and}{\unskip, }
    \renewcommand{\andnext}{\unskip, }
    \renewcommand{\thanksmark}[1]{}
    \renewcommand{\thanksgap}[1]{}
    \protected@xdef\theauthor{#1}
  \endgroup\endgroup}
\addtoiargdef{\date}{%
  \begingroup\let\footnote\@gobble}{%
  \begingroup
    \renewcommand{\thanks}[1]{}
    \renewcommand{\thanksmark}[1]{}
    \renewcommand{\thanksgap}[1]{}
    \protected@xdef\thedate{#1}
  \endgroup\endgroup}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsection{Abstracts}
%
% Books usually do not have abstracts, but I decided to add in the code 
% from the \Lpack{abstract} 
% package~\cite{ABSTRACT}.
% \changes{v0.3}{2001/07/09}{Added code for abstracts}
%
%
% We just have the \Lpack{report} or \Lpack{article} style for the
% abstract with no \Lopt{titlepage} option. The original code,
% from \file{classes.dtx} for this is:
% \begin{verbatim}
% \newenvironment{abstract}{%
%   \if@twocolumn
%     \section*{\abstractname}
%   \else
%     \small
%     \begin{center}%
%       {\bfseries \abstractname\vspace{-.5em}\vspace{\z@}}%%
%     \end{center}%
%     \quotation
%   \fi}%
%   {\if@twocolumn\else\endquotation\fi}
% \end{verbatim}
%
%
%    The following |\if...| commands are for implementing various options.
% \begin{macro}{\if@bsonecol}
% \begin{macro}{\ifadd@bstotoc}
% \begin{macro}{\ifnumber@bs}
% \begin{macro}{\if@bsrunin}
%    \begin{macrocode}
\newif\if@bsonecol
  \@bsonecoltrue
\newif\ifadd@bstotoc
  \add@bstotocfalse
\newif\ifnumber@bs
  \number@bsfalse
\newif\if@bsrunin
  \@bsruninfalse

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\abstractcol}
% \begin{macro}{\abstractintoc}
% \begin{macro}{\abstractnum}
% \begin{macro}{\abstractrunin}
% These are the use-level commands for setting the options. If
% the abstract is runin, then it must not be numbered otherwise
% adding it to the ToC might result in something peculiar.
%    \begin{macrocode}
\newcommand{\abstractcol}{\@bsonecolfalse}
\newcommand{\abstractintoc}{\add@bstotoctrue}
\newcommand{\abstractnum}{\number@bstrue\@bsruninfalse}
\newcommand{\abstractrunin}{\@bsrunintrue\number@bsfalse}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% 
%    The next set of macros comprise the 
% implementation of the |abstract| environment.
%
% \begin{macro}{\abstractnamefont}
% \begin{macro}{\abstracttextfont}
%    These two macros are for specifying the fonts for typesetting the
% abstract's title and text. They are initialised for the default case
% (i.e., no class options).
%    \begin{macrocode}
\newcommand{\abstractnamefont}{\normalfont\small\bfseries}
\newcommand{\abstracttextfont}{\normalfont\small}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\absnamepos}
% \begin{macro}{\abstitleskip}
%    |\absnamepos| specifies the environment in which the abstract's title
% name will be typeset, and the length |\abstitleskip| is an adjustement
% to the vertical space between the title and the abstract's text. These
% are initialised for the default case.
%    \begin{macrocode}
\newcommand{\absnamepos}{center}
\newlength{\abstitleskip} \setlength{\abstitleskip}{-0.5em}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\absleftindent}
% \begin{macro}{\abs@leftindent}
% \begin{macro}{\absrightindent}
% \begin{macro}{\absparindent}
% \begin{macro}{\absparsep}
%    The abstract's text is typeset as a single item list, called 
% |@bstr@ctlist|. These lengths set the left and right margin indents, the
% paragraph indentation, and the inter-paragraph vertical space. Their
% initial values are all class option-dependent.
% \changes{v1.618}{2005/09/03}{Added \cs{abs@leftindent} per Lars Madsen (mempatch v3.0)}
%    \begin{macrocode}
\newlength{\absleftindent} 
\newdimen\abs@leftindent
  \abs@leftindent=\leftmargin
\newlength{\absrightindent}
\newlength{\absparindent}
\newlength{\absparsep}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\abslabeldelim}
% \begin{macro}{\@bslabeldelim}
% The contents of |\abslabeldelim| are typeset after a run-in heading.
%    \begin{macrocode}
\newcommand{\abslabeldelim}[1]{\def\@bslabeldelim{#1}}
\abslabeldelim{}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@bsrunintitle}
% This macro typeset the run-in heading.
%    \begin{macrocode}
\newcommand{\@bsrunintitle}{%
  \hspace*{\abstitleskip}{\abstractnamefont\abstractname\@bslabeldelim}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\setup@bstract}
%    Now arrange to set all the class option-dependent values.
% \changes{v1.618}{2005/09/03}{Used \cs{abs@leftindent} in \cs{setup@bstract} (mempatch v3.0)}
%    \begin{macrocode}
\newcommand{\setup@bstract}{%
  \if@twocolumn
%    \end{macrocode}
% Values for the \Lopt{twocolumn} class option.
%    \begin{macrocode}
    \if@bsonecol 
      \abs@leftindent=\absleftindent
    \else
      \abs@leftindent=\z@
      \renewcommand{\abstractnamefont}{\normalfont\Large\bfseries}
      \renewcommand{\abstracttextfont}{\normalfont}
      \renewcommand{\absnamepos}{flushleft}
      \setlength{\abstitleskip}{-2ex}
    \fi
  \else
%    \end{macrocode}
% Values for the default, if not already initialised.
%    \begin{macrocode}
    \abs@leftindent=\absleftindent
  \fi
%    \end{macrocode}
% Finally, values that apply for all cases.
%    \begin{macrocode}
  \setlength{\absrightindent}{\absleftindent}
}

%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
\AtBeginDocument{\setlength{\absparindent}{\parindent}
                 \setlength{\absparsep}{\parskip}}

%    \end{macrocode}
%
% \begin{environment}{@bstr@ctlist}
%    The abstract's text is typeset within the |@bstr@ctlist| list environment.
% \changes{v1.618}{2005/09/03}{Used \cs{abs@leftindent} in \cs{@bstr@ctlist} (mempatch v3.0)}
%    \begin{macrocode}
\newenvironment{@bstr@ctlist}{%
  \list{}{%
          %%\topsep        \z@
          \partopsep     \z@
          \listparindent \absparindent
          \itemindent    \listparindent
          \leftmargin    \abs@leftindent
          \rightmargin   \absrightindent
          \parsep        \absparsep}%
  \item\relax}
  {\endlist}

%    \end{macrocode}
% \end{environment}
%
% \begin{macro}{\put@bsintoc}
%    This macro adds the abstract's title to the ToC. It does nothing if
% the abstract is being numbered.
%    \begin{macrocode}
\newcommand{\put@bsintoc}{%
  \ifadd@bstotoc
    \ifnumber@bs\else
      \phantomsection
      \addcontentsline{toc}{chapter}{\abstractname}
    \fi
  \fi}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\num@bs}
%    This macro generates a numbered abstract heading.
%    \begin{macrocode}
\newcommand{\num@bs}{\chapter{\abstractname}}

%    \end{macrocode}
% \end{macro}
%
% \begin{environment}{abstract}
%  At last we are in position to define the |abstract| environment. This
% follows very much along the lines of the standard class definitions, but with
% macros inserted at strategic points.
%
% The \Lopt{twocolumn} option and the default case. These use the same code
% as any style differences are embedded in the new macros.
%    \begin{macrocode}
\newenvironment{abstract}{%
  \setup@bstract
  \if@bsrunin\else
    \ifnumber@bs \num@bs \else
      \begin{\absnamepos}\abstractnamefont\abstractname\end\absnamepos%
      \vspace{\abstitleskip}%
    \fi
  \fi
  \put@bsintoc%
  \begin{@bstr@ctlist}\if@bsrunin\@bsrunintitle\fi\abstracttextfont}%
  {\par\end{@bstr@ctlist}}

%    \end{macrocode}
% \end{environment}
%
% \begin{environment}{onecolabstract}
%  An environment for typesetting a single column abstract, 
% particularly as the optional argument to the |\twocolumn| command.
%    \begin{macrocode}
\newenvironment{onecolabstract}{%
  \begin{@twocolumnfalse}\begin{abstract}}{%
  \end{abstract}\end{@twocolumnfalse}}

%    \end{macrocode}
% \end{environment}
%
% \begin{macro}{\thanks}
% \begin{macro}{\@bs@thanks}
%    We have to keep the contents of the |\thanks| commands as normally these
% are emptied by the |\maketitle| command. I do this by extending the definition
% of the |\thanks| (from \file{ltsect.dtx}) command, 
% so that |\@bs@thanks| has a copy of the contents
% of |\@thanks|.
%    \begin{macrocode}
\addtoiargdef{\thanks}{}{%
  \protected@xdef\@bs@thanks{\@bs@thanks
    \protect\footnotetext[\the\c@footnote]{#1}}%
}
\let\@bs@thanks\@empty

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\saythanks}
% This macro typesets any |\thanks| commands \emph{after} using 
% |onecolabstract|. 
%    \begin{macrocode}
\newcommand{\saythanks}{\begingroup
  \renewcommand{\thefootnote}{\fnsymbol{footnote}}\@bs@thanks
  \endgroup\global\let\@bs@thanks\@empty}
  
%    \end{macrocode}
% \end{macro}
%
%
% 
% \section{Parts, chapters and other divisions}
%
% \subsection{Building blocks} The definitions in this part of a
%    class file usually make use of two internal macros, |\@startsection| and
%    |\secdef|. To understand
%    what is going on here, we describe their syntax.
%
% \DescribeMacro{\@startsection}
%    The macro |\@startsection| has 6 required arguments, optionally
%    followed by  a $*$, an optional argument and a required argument:
%
%    |\@startsection|\meta{name}\meta{level}\meta{indent}^^A
%                    \meta{beforeskip}\meta{afterskip}\meta{style}
%            optional *\\
%    \null\hphantom{\bslash @startsection}^^A
%            |[|\meta{altheading}|]|\meta{heading}
%
%    It is a generic command to start a section, the arguments have
%    the following meaning:
%
%    \begin{description}
%    \item[\meta{name}] The name of the user level command, e.g.,
%          `section'.
%    \item[\meta{level}] A number, denoting the depth of the section
%          --- e.g., chapter = 0, section = 1, etc.  A section number
%          will be printed if and only if \meta{level} $<=$  the value
%          of the \Lcount{secnumdepth} counter.
%    \item[\meta{indent}] The indentation of the heading from the left
%          margin
%    \item[\meta{beforeskip}] The absolute value of this argument
%          gives the skip to leave above the heading. If it is
%          negative, then the paragraph indent of the text following
%          the heading is suppressed.
%    \item[\meta{afterskip}] If positive, this gives the skip to leave
%          below the heading, else it gives the skip to leave to the
%          right of a run-in heading.
%    \item[\meta{style}] Commands to set the style of the heading.
%    \item[$*$] When this is missing the heading is numbered and the
%          corresponding counter is incremented.
%    \item[\meta{altheading}] Gives an alternative heading to use in
%          the table of contents and in the running heads. This should
%          be present when the $*$ form is used.
%    \item[\meta{heading}] The heading of the new section.
%    \end{description}
%  A sectioning command is normally defined to |\@startsection| and
%  its first six arguments.
%
% \DescribeMacro{\secdef}
%    The macro |\secdef| can be used when a sectioning command is
%    defined without using |\@startsection|. It has two arguments:
%
%    |\secdef|\meta{unstarcmds}\meta{starcmds}
%
%    \begin{description}
%    \item[\meta{unstarcmds}] Used for the normal form of a
%          sectioning command.
%    \item[\meta{starcmds}] Used for the $*$-form of a
%          sectioning command.
%    \end{description}
%
%    You can use |\secdef| as follows:
% \begin{verbatim}
%       \def\chapter { ... \secdef \CMDA \CMDB }
%       \def\CMDA    [#1]#2{ ... }  % Command to define
%                                   % \chapter[...]{...}
%       \def\CMDB    #1{ ... }      % Command to define
%                                   % \chapter*{...}
% \end{verbatim}
%
% \DescribeMacro{\@hangfrom}
% Internally the |\@startsection| macro uses |\@hangfrom{NUM}|, where
% |NUM| is the sectional number, to produce a hanging paragraph. That is,
% the second and later lines of a multiline title are indented from the
% left margin by the width of the number.
% The definition of |\@hangfrom|, from \file{ltsect.dtx}, is:
% \begin{verbatim}
% \def\@hangfrom#1{\setbox\@tempboxa\hbox{{#1}}%
%   \hangindent \wd\@tempboxa\noindent\box\@tempboxa}
% \end{verbatim}
%
% To get a normal paragraphed title you can do: \\
% |\renewcommand{\@hangfrom}[1]{#1}| \\
% or as a block paragraph: \\
% |\renewcommand{\@hangfrom}[1]{\noindent #1}| \\
%
% \DescribeMacro{\@seccntformat}
% The |\@startsection| macro also uses |\@seccntformat{NUM}| to format
% the section number, including the space after it. Its definition,
% from \file{ltsect.dtx}, is
% \begin{verbatim}
% \def\@seccntformat#1{\csname the#1\endcsname\quad}
% \end{verbatim}
% This is the command to change if you need different number formatting. For
% example the combination
% \begin{verbatim}
% \renewcommand{\@seccntformat}[1]{\llap{\csname the#1\endcsname\quad}}
% \renewcommand{\@hangfrom}[1]{\noindent #1}
% \end{verbatim}
% will hang the section numbers in the margin.
%
% \begin{table}
% \centering
% \caption{Document division levels}\label{tab:levels}
% \begin{tabular}{lr} \hline
% Division & Level \\ \hline
% part          & -1 \\
% chapter       &  0 \\
% section       &  1 \\
% subsection    &  2 \\
% subsubsection &  3 \\
% paragraph     &  4 \\
% subparagraph  &  5 \\ 
% \hline
% \end{tabular}
% \end{table}
%
%     The values used for the document division levels are the same
% as defined by the standard \LaTeX{} classes and are given in 
% Table~\ref{tab:levels}.
%
% \subsection{Mark commands}
%
% \begin{macro}{\partmark}
% \begin{macro}{\chaptermark}
% \begin{macro}{\sectionmark}
% \begin{macro}{\subsectionmark}
% \begin{macro}{\subsubsectionmark}
% \begin{macro}{\paragraphmark}
% \begin{macro}{\subparagraphmark}
%  The default initialisations of the |\...mark| commands for use in
% the pagestyles. Most are already defined in the kernel but they are
% all noted here.
%    \begin{macrocode}
\newcommand*{\partmark}[1]{}
\newcommand*{\chaptermark}[1]{}
% \newcommand*{\sectionmark}[1]{}
% \newcommand*{\subsectionmark}[1]{}
% \newcommand*{\subsubsectionmark}[1]{}
% \newcommand*{\paragraphmark}[1]{}
% \newcommand*{\subparagraphmark}[1]{}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\bibmark}
%  Marks for the bibliography, which may be filled with |\bibname|.
% \changes{v1.1}{2002/03/10}{Added \cs{bibmark}}
%    \begin{macrocode}
\newcommand*{\bibmark}{}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\indexmark}
% Marks for the index, which may be filled with |\indexname|.
% \changes{v1.1}{2002/03/10}{Added \cs{indexmark}}
%    \begin{macrocode}
\newcommand*{\indexmark}{}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\glossarymark}
% Marks for the glossary, which may be filled with \cs{glossaryname}.
% \changes{v1.618}{2005/09/25}{Added \cs{glossarymark}}
%    \begin{macrocode}
\newcommand*{\glossarymark}{}

%    \end{macrocode}
% \end{macro}
%
%
% \subsection{Define Counters}
%
% \begin{macro}{\c@secnumdepth}
%    The value of the counter \Lcount{secnumdepth} gives the depth of
%    the highest-level sectioning command that is to produce section
%    numbers.
%    \begin{macrocode}
\setcounter{secnumdepth}{2}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\c@part}
% \begin{macro}{\c@chapter}
% \begin{macro}{\c@section}
% \begin{macro}{\c@subsection}
% \begin{macro}{\c@subsubsection}
% \begin{macro}{\c@paragraph}
% \begin{macro}{\c@subparagraph}
%    These counters are used for the sectioning numbers. The macro\\
%    |\newcounter{|\meta{newctr}|}[|\meta{oldctr}|]|\\
%     defines\meta{newctr} to be a counter, which is reset to zero when
%    counter \meta{oldctr} is stepped. Counter \meta{oldctr} must
%    already be defined.
%
%    Part and chapter are the top level document divisions.
%    \begin{macrocode}
\newcounter{part}
\newcounter{chapter}
%    \end{macrocode}
%    The lower level divisions get reset by higher level divisions.
%    \begin{macrocode}
\newcounter{section}[chapter]
\newcounter{subsection}[section]
\newcounter{subsubsection}[subsection]
\newcounter{paragraph}[subsubsection]
\newcounter{subparagraph}[paragraph]
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\thepart}
% \begin{macro}{\thechapter}
% \begin{macro}{\thesection}
% \begin{macro}{\thesubsection}
% \begin{macro}{\thesubsubsection}
% \begin{macro}{\theparagraph}
% \begin{macro}{\thesubparagraph}
%    For any counter \Lcount{CTR}, |\theCTR| is a macro that defines
%    the printed version of counter \Lcount{CTR}.  It is defined in
%    terms of the following macros:
%
%    |\arabic{|\Lcount{COUNTER}|}| prints the value of
%    \Lcount{COUNTER} as an arabic numeral.
%
%    |\roman{|\Lcount{COUNTER}|}| prints the value of
%    \Lcount{COUNTER} as a lowercase roman numeral.
%
%    |\Roman{|\Lcount{COUNTER}|}| prints the value of
%    \Lcount{COUNTER} as an uppercase roman numeral.
%
%    |\alph{|\Lcount{COUNTER}|}| prints the value of \Lcount{COUNTER}
%    as a lowercase letter: $1 =$~a, $2 =$~ b, etc.
%
%    |\Alph{|\Lcount{COUNTER}|}| prints the value of \Lcount{COUNTER}
%    as an uppercase letter: $1 =$~A, $2 =$~B, etc.
%
%    \begin{macrocode}
\renewcommand{\thepart}{\@Roman\c@part}
\renewcommand{\thechapter}{\@arabic\c@chapter}
\renewcommand{\thesection}{\thechapter.\@arabic\c@section}
\renewcommand{\thesubsection}{%
              \thesection.\@arabic\c@subsection}
\renewcommand{\thesubsubsection}{%
              \thesubsection.\@arabic\c@subsubsection}
\renewcommand{\theparagraph}{%
              \thesubsubsection.\@arabic\c@paragraph}
\renewcommand{\thesubparagraph}{%
              \theparagraph.\@arabic\c@subparagraph}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@chapapp}
% |\@chapapp| is initially defined as |\chaptername|. The |\appendix|
% command redfines it as |\appendixname|.
%    \begin{macrocode}
\newcommand{\@chapapp}{\chaptername}

%    \end{macrocode}
% \end{macro}
%
% \subsection{Front, main and back matter}
%
% These are the three main logical divisions in a book. As noted earlier,
% the boolean |\if@mainmatter| is TRUE iff the main matter is being
% processed. Chapters will be unnumbered when |\if@mainmatter| is FALSE.
%
% \begin{macro}{\frontmatter}
% \begin{macro}{\frontmatter*}
% The |\frontmatter| command starts roman numbering and turns off 
% chapter numbering.
% It ensures that lower level divisions will not have chapter numbers,
% nor will figures or tables. It
% also ensures that the next page will be recto.
% The starred version makes no changes to the page numbering\footnote{The
% starred versions were added to meet a request by Daniel Richard G.
% (\texttt{skunk@mit.edu}) in September 2001.}.
% \changes{v0.3}{2001/07/09}{\cs{frontmatter} switches off numbering via
%                the secnumdepth counter}
% \changes{v1.0}{2001/09/21}{Added \cs{frontmatter*} command}
%    \begin{macrocode}
\newcommand{\frontmatter}{%
  \@ifstar{\@smemfront}{\@memfront}}
%    \end{macrocode}
% \begin{macro}{\@smemfront}
%    \begin{macrocode}
\newcommand{\@smemfront}{%
  \cleardoublepage
  \@mainmatterfalse
  \setcounter{secnumdepth}{-10}
  \counterwithout{figure}{chapter}
  \counterwithout{table}{chapter}
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\@memfront}
%    \begin{macrocode}
\newcommand{\@memfront}{%
  \@smemfront\pagenumbering{roman}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\mainmatter}
% \begin{macro}{\mainmatter*}
% The |\mainmatter| command starts arabic numbering and turns on chapter numbering.
% It sets numbering to the normal state. It
% also ensures that the next page will be recto. The starred version does 
% not change the page numbering.
%
% Romano Giannetti suggested that |\mainmatter| (and |\backmatter|) should
% be almost no-ops with the article option.
% \changes{v0.3}{2001/07/09}{\cs{mainmatter} switches on section numbering
%                via the secnumdepth counter}
% \changes{v1.0}{2001/09/21}{Added \cs{mainmatter*} command}
%    \begin{macrocode}
\newcommand{\mainmatter}{%
  \@ifstar{\@smemmain}{\@memmain}}
%    \end{macrocode}
% \begin{macro}{\@smemmain}
% \changes{v1.1}{2002/03/10}{Modified \cs{@smemmain} to reset the secnumdepth
%                counter to maxsecnumdepth}
% \changes{v1.4}{2003/11/22}{Included cognizance of article option in \cs{@smemmain}
%                            (from patch v1.7)}
%    \begin{macrocode}
\newcommand{\@smemmain}{
  \@mainmattertrue
  \setcounter{secnumdepth}{\value{maxsecnumdepth}}
  \ifartopt
    \clearpage
  \else
    \cleardoublepage
    \counterwithin{figure}{chapter}
    \counterwithin{table}{chapter}
  \fi}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\@memmain}
%    \begin{macrocode}
\newcommand{\@memmain}{%
  \@smemmain\pagenumbering{arabic}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\backmatter}
% This command turns off chapter numbering but leaves the page numbering alone. 
% It twiddles the numbering caption numbering.
% The back matter may start on any new page.
% \changes{v0.3}{2001/07/09}{\cs{backmatter} uses the secnumdepth counter}
% \changes{v1.4}{2003/11/22}{Included cognizance of article option in \cs{backmatter}
%                            (from patch v1.7)}
%    \begin{macrocode}
\newcommand{\backmatter}{%
  \ifartopt
    \clearpage
  \else
    \if@openright
      \cleardoublepage
    \else
      \clearpage
    \fi
  \fi
  \@mainmatterfalse
  \setcounter{secnumdepth}{-10}
  \ifartopt\else
    \counterwithout{figure}{chapter}
    \counterwithout{table}{chapter}
    \setcounter{figure}{0}
    \setcounter{table}{0}
  \fi}

%    \end{macrocode}
% \end{macro}
%
%
%  
%
% \subsection{Part}
%
%
%
% \begin{macro}{\part}
% |\part{|\meta{title}|}| starts a new Part called \meta{title}.
% The actual typesetting of the title is done by |\@part| or |\@spart|.
%    \begin{macrocode}
\newcommand{\part}{%
  \@setuppart
  \secdef\@part\@spart}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\beforepartskip}
% \begin{macro}{\midpartskip}
% \begin{macro}{\afterpartskip}
% These three macros are the skips before, in the middle, and after the
% Part heading.
%    \begin{macrocode}
\newcommand{\beforepartskip}{\null\vfil}
\newcommand{\midpartskip}{\par\vskip 20pt}
\newcommand{\afterpartskip}{\vfil\newpage}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@setuppart}
% This macro does the work of setting up for the |\part| command.
% A single column page, normally recto, with a \pstyle{part} pagestyle is started.
%    \begin{macrocode}
\newcommand{\@setuppart}{%
  \if@openright
    \cleardoublepage
  \else
    \clearpage
  \fi
  \thispagestyle{part}%
  \if@twocolumn
    \onecolumn
    \@tempswatrue
  \else
    \@tempswafalse
  \fi
  \beforepartskip}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\partnamefont}
% \begin{macro}{\partnumfont}
% \begin{macro}{\parttitlefont}
% These three macros specify the fonts for the part name and number, and
% for the part title, respectively.
% \changes{v0.2}{2001/06/03}{Rewrote the typesetting for Part titles}
%    \begin{macrocode}
\newcommand{\partnamefont}{\normalfont\huge\bfseries}
\newcommand{\partnumfont}{\normalfont\huge\bfseries}
\newcommand{\parttitlefont}{\normalfont\Huge\bfseries}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\printpartname}
% \begin{macro}{\partnamenum}
% \begin{macro}{\printpartnum}
% \begin{macro}{\printparttitle}
% Macros to print the various parts of a Part heading.
%    \begin{macrocode}
\newcommand{\printpartname}{\partnamefont \partname}
\newcommand{\partnamenum}{\space}
\newcommand{\printpartnum}{\partnumfont \thepart}
\newcommand{\printparttitle}[1]{\parttitlefont #1}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\mempartinfo}
% \begin{macro}{\mempartstarinfo}
% \verb?\mempartinfo{\thepart}{short}{long}? \\
% \verb?\mempartstarinfo{long}? \\
%    \begin{macrocode}
\newcommand*{\mempartinfo}[3]{}
\newcommand*{\mempartstarinfo}[1]{}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@part}
% |\@part{|\meta{short}|]{|\meta{long}|}| typesets the title of a |\part|
% part. There is a number if |\secnumdepth| is greater than -2.
% \changes{v1.3}{2002/11/14}{Used \cs{partnumberline} in \cs{@part}}
% \changes{v1.4}{2003/11/16}{Added \cs{M@gettitle} to \cs{@part}}
% \changes{v1.618}{2005/09/03}{Replaced \cs{markboth} by \cs{partmark} 
%          in \cs{@part} courtesy of Frederic Connes (mempatch v3.11)}
%    \begin{macrocode}
\long\def\@part[#1]#2{%
  \M@gettitle{#1}%
  \ifnum \c@secnumdepth >-2\relax
    \refstepcounter{part}%
    \addcontentsline{toc}{part}%
      {\protect\partnumberline{\thepart}#1}%
    \mempartinfo{\thepart}{#1}{#2}%
  \else
    \addcontentsline{toc}{part}{#1}%
    \mempartinfo{}{#1}{#2}%
  \fi
%    \end{macrocode}
% Empty the marks, center the title on the page, and set the normal font.
%    \begin{macrocode}
  \partmark{#1}%
  {\centering
   \interlinepenalty \@M
   \normalfont
%    \end{macrocode}
% Print the number, if there is one, then the title below. Macro |\@endpart|
% tidies everything up at the end.
%    \begin{macrocode}
   \ifnum \c@secnumdepth >-2\relax
     \printpartname \partnamenum \printpartnum
     \midpartskip
   \fi
   \printparttitle{#2}\par}%
  \@endpart}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@spart}
% |\@spart{|\meta{long}|}| formats the title of a |\part*| part. It is 
% simpler than |\@part| because there is no  number to print.
% \changes{v1.4}{2003/11/16}{Added \cs{M@gettitle} to \cs{@spart}}
%    \begin{macrocode}
\def\@spart#1{%
  \M@gettitle{#1}%
  \mempartstarinfo{#1}%
  {\centering
   \interlinepenalty \@M
   \normalfont
   \printparttitle{#1}\par}%
  \@endpart}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@endpart}
% This finishes off both |\@part| and |\@spart|. The current page is flushed.
% In the standard \Lpack{book} class if 
% two-sided mode is on a blank page is then produced. I think that this looks
% odd when the \Lopt{openany} option is in force, so here it only produces
% an extra blank page if both \Lopt{twoside} and \Lopt{openright} are
% in effect. If necessary, two
% column mode is switched back on.
%    \begin{macrocode}
\def\@endpart{\afterpartskip
  \if@twoside
    \if@openright
      \null
      \thispagestyle{empty}%
      \newpage
    \fi
  \fi
  \if@tempswa
    \twocolumn
  \fi}

%    \end{macrocode}
% \end{macro}
%
% \subsection{Chapter}
%
% \begin{macro}{\chapter}
%    The command to start a new chapter. Chapters always start on a new page
% with a \pstyle{chapter} pagestyle. Floats are not allowed at the top of the page.
% The typesetting is done by either \cs{@chapter} or \cs{@schapter}.
% \changes{v0.3}{2001/07/09}{Changed \cs{chapter} for article option}
% \changes{v1.0}{2001/10/24}{Changed chapter page clearing to \cs{clearforchapter}}
% \changes{v1.2}{2002/07/27}{New optarg in \cs{chapter} for short heading}
% \changes{v1.618}{2005/09/03}{Made article chapter start on a newline (mempatch v3.5)}
%    \begin{macrocode}
\newcommand\chapter{%
  \ifartopt\par\else
    \clearforchapter
    \thispagestyle{chapter}
    \global\@topnum\z@
  \fi
  \@afterindentfalse
  \@ifstar{\@m@mschapter}{\@m@mchapter}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@m@mchapter}
% \begin{macro}{\ch@pt@c}
% \begin{macro}{\m@m@empty}
% Intermediate and support macros for the extra optional argument
% to \cs{chapter}. Have to do this long windedly otherwise dear old
% \Lpack{hyperref} barfs.
%
% The code for two optional arguments is based on a posting
% to \ctt{} by Robin Fairbairns (1997/04/12 \textit{Re: Several optional 
% arguments for macro?}). 
% \changes{v1.618}{2005/09/04}{Major changes to \cs{chapter} macros (from Morten Hogholm 2005/08/30)}
%    \begin{macrocode}
\newcommand{\@m@mchapter}[1][]{%
  \def\ch@pt@c{#1}% capture first optional arg
  \@ifnextchar[{\@chapter}{\@chapter[]}%
}
\def\m@m@empty{\@empty}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\memchapinfo}
% \begin{macro}{\memchapstarinfo}
% \begin{macro}{\memappchapinfo}
% \begin{macro}{\memappchapstarinfo}
% \verb?\memchapinfo{num}{toc}{head}{full}? \\
% \verb?\memchapstarinfo{short}{full}?
% \changes{v1.618}{2005/09/06}{Added \cs{memchapterinfo}}
%    \begin{macrocode}
\newcommand*{\memchapinfo}[4]{}
\newcommand*{\memchapstarinfo}[2]{}
\newcommand*{\memappchapinfo}[4]{}
\newcommand*{\memappchapstarinfo}[2]{}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@chapter}
% \begin{macro}{\f@rtoc}
% \begin{macro}{\f@rhdr}
% \cs{@chapter}\oarg{tocmark}\marg{title}
% typesets the title of a \cs{chapter}. 
% There is a number if \cs{secnumdepth} is greater than -1 and \cs{@mainmatter}
% is TRUE. 
%
% \changes{v1.2}{2002/07/27}{Complete rewrite of \cs{@chapter}}
% \changes{v1.3}{2002/11/14}{Changed \cs{numberline} to \cs{chapternumberline}
%                            in \cs{@chapter}}
%    \begin{macrocode}
\def\@chapter[#1]#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
%    \end{macrocode}
% Store the (short) title via |\chaptermark|, and add some whitespace to
% the LoF and LoT. Then fiddle when we are using two columns, calling
% |\@makechapterhead| to do the typesetting.
% \changes{v0.3}{2001/07/09}{Changed \cs{@chapter} for article option}
%    \begin{macrocode}
  \chaptermark{\f@rhdr}
  \ifartopt
    \@makechapterhead{#2}%
    \@afterheading
  \else
    \insertchapterspace
    \if@twocolumn
      \@topnewpage[\@makechapterhead{#2}]%
    \else
      \@makechapterhead{#2}%
    \fi
    \@afterheading
  \fi
%    \end{macrocode}
% Vittorio De Martino (\texttt{demartino.vittoria@grtn.it}) on
% 31 March 2003 reported that for articled chapters, which do not do
% any |\clearpage|, |\addcontents| had to come after the title.
% \changes{v1.4}{2003/11/16}{Changed \cs{@chapter} to match patches v1.n}
%    \begin{macrocode}
  \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
%    \end{macrocode}
% Add hook for title referencing.
% \changes{v1.4}{2003/11/16}{Added \cs{M@gettitle} to \cs{@chapter}}
%    \begin{macrocode}
  \ifheadnameref\M@gettitle{\f@rhdr}\else\M@gettitle{\f@rtoc}\fi
}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\@makechapterhead}
% This \emph{really} typesets a \cs{chapter}. Leave some whitespace and prepare to
% set |\raggedright|.
% \changes{v1.3}{2002/10/10}{Added \cs{printchapternonum} to 
%                            \cs{@makechapterhead}}
%    \begin{macrocode}
\def\@makechapterhead#1{%
  \chapterheadstart%  \vspace*{50\p@}%
  {\parindent \z@ \raggedright \normalfont
%    \end{macrocode}
% If there is a number, typeset it, otherwise call |\printchapternonum|..
%    \begin{macrocode}
   \ifm@m@And
     \printchaptername \chapternamenum \printchapternum 
     \afterchapternum % \par\nobreak \vskip 20\p@
   \else
     \printchapternonum
   \fi
%    \end{macrocode}
% Typeset the title.
%    \begin{macrocode}
   \interlinepenalty\@M
   \printchaptertitle{#1} % \Huge \bfseries #1
   \afterchaptertitle % \par\nobreak \vskip 40\p@
  }}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\insertchapterspace}
% By default, a \cs{chapter} inserts some vertical space into the LoF and LoT.
% The macro \cs{insertchapterspace} performs the insertion.
% \changes{v0.33}{2001/08/31}{Added \cs{insertchapterspace}}
%    \begin{macrocode}
\newcommand{\insertchapterspace}{%
  \addtocontents{lof}{\protect\addvspace{10pt}}%
  \addtocontents{lot}{\protect\addvspace{10pt}}%
}

%    \end{macrocode}
% \end{macro}
%
%
%
% \begin{macro}{\@chs@def@ult}
% This sets up all the definitions used in \cs{@makechapterhead}
% and \cs{@makeschapterhead}.
% \changes{v1.3}{2002/10/10}{Added \cs{printchapternonum} to \cs{@chs@def@ult}}
% \begin{macro}{\chapterheadstart}
% \begin{macro}{\printchaptername}
% \begin{macro}{\chapternamenum}
% \begin{macro}{\printchapternum}
% \begin{macro}{\afterchapternum}
% \begin{macro}{\printchapternum}
% \begin{macro}{\printchapternonum}
% \begin{macro}{\printchaptertitle}
% \begin{macro}{\afterchaptertitle}
%    \begin{macrocode}
\newcommand{\@chs@def@ult}{%
  \def\chapterheadstart{\vspace*{\beforechapskip}}
  \def\printchaptername{\chapnamefont \@chapapp}
  \def\chapternamenum{\space}
  \def\printchapternum{\chapnumfont \thechapter}
  \def\afterchapternum{\par\nobreak\vskip \midchapskip}
  \def\printchapternonum{}
  \def\printchaptertitle##1{\chaptitlefont ##1}
  \def\afterchaptertitle{\par\nobreak\vskip \afterchapskip}
}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% 
% \begin{macro}{\chapnamefont}
% \begin{macro}{\chapnumfont}
% \begin{macro}{\chaptitlefont}
% Fonts for setting the chapter number, name (e.g., Chapter) and title.
%    \begin{macrocode}
\newcommand{\chapnamefont}{\normalfont\huge\bfseries}
\newcommand{\chapnumfont}{\normalfont\huge\bfseries}
\newcommand{\chaptitlefont}{\normalfont\Huge\bfseries}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\beforechapskip}
% \begin{macro}{\midchapskip}
% \begin{macro}{\afterchapskip}
% Lengths separating the various parts of a chapter heading.
%    \begin{macrocode}
\newlength{\beforechapskip}\setlength{\beforechapskip}{50pt}
\newlength{\midchapskip}\setlength{\midchapskip}{20pt}
\newlength{\afterchapskip}\setlength{\afterchapskip}{40pt}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\@m@mschapter}
% This deals with the new optional argument for starred chapters.
%    \begin{macrocode}
\newcommand{\@m@mschapter}[2][\@empty]{%
  \@schapter{#2}%
  \ifx \@empty#1
    \def\f@rhdr{#2}%
  \else   % opt arg
    \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}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@schapter}
% \cs{@schapter}\marg{long} typesets the title of a 
% \cs{chapter*}. It is easier than the \cs{@chapter} as there is no number
% to worry about.
% \changes{v0.3}{2001/07/09}{Changed \cs{@schapter} for article option}
% \changes{v1.2}{2002/07/27}{Major rewrite of \cs{@schapter}}
% \changes{v1.4}{2003/11/16}{Added \cs{M@gettitle} to \cs{@schapter}}
%    \begin{macrocode}
\newcommand{\@schapter}[1]{%
  \ifartopt
    \@makeschapterhead{#1}%
    \@afterheading
  \else
    \if@twocolumn
      \@topnewpage[\@makeschapterhead{#1}]%
    \else
      \@makeschapterhead{#1}%
    \fi
    \@afterheading
  \fi
  \M@gettitle{#1}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@makeschapterhead}
% This \emph{really} typesets a \cs{chapter*}, and is similar to \cs{@makechapterhead}.
% \changes{v1.3}{2002/10/10}{Added \cs{printchapternonum} to 
%                            \cs{@makeschapterhead}}
%    \begin{macrocode}
\def\@makeschapterhead#1{%
  \chapterheadstart
  {\parindent \z@ \raggedright \normalfont
   \printchapternonum
   \interlinepenalty\@M
   \printchaptertitle{#1}
   \afterchaptertitle
  }
}

%    \end{macrocode}
% \end{macro}
%
%
% \subsubsection{Chapter styling}
%
%
% \begin{macro}{\makechapterstyle}
% \begin{macro}{\chapterstyle}
% \cs{chapterstyle}\marg{style} is like \cs{pagestyle}, except it's for 
% chapters. 
%
% \cs{makechapterstyle}\marg{style}\marg{text} creates
% or overrides the \meta{style} chapter style defining it as \meta{text}
% So, \verb?\makechapterstyle{fred}{code}? specifies that the chapter style 
% \pstyle{fred}
% is defined as the macro \verb?\chs@fred{code}?, 
% and \verb?\chapterstyle{fred}? calls the macro \cs{chs@fred}.
%    \begin{macrocode}
\newcommand{\makechapterstyle}[2]{\@namedef{chs@#1}{\@chs@def@ult #2}}
\newcommand{\chapterstyle}[1]{\@nameuse{chs@#1}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% Set the \pstyle{default} chapter style.
% \changes{v0.2}{2001/06/03}{Added code when making the default chapterstyle}
%    \begin{macrocode}
\makechapterstyle{default}{%
  \renewcommand{\chapnamefont}{\normalfont\huge\bfseries}
  \renewcommand{\chapnumfont}{\normalfont\huge\bfseries}
  \renewcommand{\chaptitlefont}{\normalfont\Huge\bfseries}
  \setlength{\beforechapskip}{50pt}
  \setlength{\midchapskip}{20pt}
  \setlength{\afterchapskip}{40pt}
}
\chapterstyle{default}

%    \end{macrocode}
%
% \begin{macro}{\chs@section}
% The \pstyle{section} chapter style. It prints the heading as though it
% were a section.
% \changes{v1.0a}{2002/01/18}{Made \cs{chapnumfont} change in section chapterstyle explicit}
%    \begin{macrocode}
\makechapterstyle{section}{%
  \renewcommand{\printchaptername}{}
  \renewcommand{\chapternamenum}{}
  \renewcommand{\chapnumfont}{\normalfont\Huge\bfseries}
  \renewcommand{\printchapternum}{\chapnumfont \thechapter\space}
  \renewcommand{\afterchapternum}{}
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\chs@article}
% The \pstyle{article} chapter style. It prints the heading as though it
% were a section in an \Lpack{article} class document.
% \changes{v0.3}{2001/07/09}{Added article chapterstyle}
% \changes{v1.0a}{2002/01/18}{Made \cs{chapnumfont} change in article chapterstyle explicit}
% \changes{v1.4}{2003/11/22}{Replaced \cs{space} by \cs{quad} in article chapterstyle
%               (from patch v1.7)}
%    \begin{macrocode}
\makechapterstyle{article}{%
  \renewcommand{\chapterheadstart}{\vspace{\beforechapskip}}
  \setlength{\beforechapskip}{3.5ex \@plus 1ex \@minus .2ex}
  \setlength{\afterchapskip}{2.3ex \@plus .2ex}
  \renewcommand{\printchaptername}{}
  \renewcommand{\chapternamenum}{}
  \renewcommand{\chaptitlefont}{\normalfont\Large\bfseries}
  \renewcommand{\chapnumfont}{\normalfont\Large\bfseries}
  \renewcommand{\printchapternum}{\chapnumfont \thechapter\quad}
  \renewcommand{\afterchapternum}{}
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\chs@hangnum}
% The \pstyle{hangnum} style puts the chapter number in the margin.
%    \begin{macrocode}
\makechapterstyle{hangnum}{%
  \renewcommand{\chapnumfont}{\chaptitlefont}
  \settowidth{\chapindent}{\chapnumfont 999}
  \renewcommand{\printchaptername}{}
  \renewcommand{\chapternamenum}{}
  \renewcommand{\printchapternum}{%
    \noindent\llap{\makebox[\chapindent][l]{\chapnumfont \thechapter}}}
  \renewcommand{\afterchapternum}{}
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\chapindent}
% \begin{macro}{\chs@companion}
% The \pstyle{companion} style is like that in the \textit{\LaTeX{} Companion}
% series. It requires the |\chapindent| length. The user needs
% to be careful to have a wide enough spine margin on verso pages
% if the title may appear on a verso page.
% \changes{v0.31}{2001/07/24}{Changed adjustwidth* to adjustwidth in companion chapterstyle}
%    \begin{macrocode}
\newlength{\chapindent}

\makechapterstyle{companion}{%
  \renewcommand{\chapnamefont}{\normalfont\LARGE\scshape}
  \renewcommand{\printchaptername}{\raggedleft\chapnamefont \@chapapp}
  \renewcommand{\chapnumfont}{\normalfont\Huge}
  \setlength{\chapindent}{\marginparsep}
  \addtolength{\chapindent}{\marginparwidth}
  \renewcommand{\printchaptertitle}[1]{%
    \begin{adjustwidth}{}{-\chapindent}
      \raggedleft \chaptitlefont ##1\par\nobreak
    \end{adjustwidth}}
}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\chs@demo}
% An exotic chapter style for demonstration purposes.
%    \begin{macrocode}
\makechapterstyle{demo}{
  \renewcommand{\printchaptername}{\centering}
  \renewcommand{\printchapternum}{\chapnumfont \numtoName{\c@chapter}}
  \renewcommand{\chaptitlefont}{\normalfont\Huge\sffamily}
  \renewcommand{\printchaptertitle}[1]{%
    \hrule\vskip\onelineskip \raggedleft \chaptitlefont ##1}
  \renewcommand{\afterchaptertitle}%
               {\vskip\onelineskip \hrule\vskip \afterchapskip}
}

%    \end{macrocode}
% \end{macro}
%
%
%
% \subsection{Lower level headings}
%
%    These commands all make use of \cs{@startsection}. However,
% for the purposes of the class the kernel version needs modification
% to support:
% \begin{itemize}
% \item make short pages (where a section heading is moved to the top
%       of the next page) ragged bottom;
% \item provide headings with two optional arguments.
% \end{itemize}
%
%
%    We will tweak \cs{@startsection} so that a short page (where a section
% heading is moved from the bottom of the page to the top of the next)
% can be set \cs{raggedbottom}. 
%
% \begin{macro}{\ifraggedbottomsection}
% |\raggedbottomsectiontrue| for ragged short pages.
% \changes{v1.3}{2002/11/14}{Added \cs{ifraggedbottomsection}}
%    \begin{macrocode}
\newif\ifraggedbottomsection
  \raggedbottomsectionfalse
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\bottomsectionskip}
% Decreasing this length increases short page bottom flushness.
% \changes{v1.3}{2002/11/14}{Added \cs{bottomsectionskip}}
%    \begin{macrocode}
\newlength{\bottomsectionskip}
  \setlength{\bottomsectionskip}{10mm}

%    \end{macrocode}
% \end{macro}
%
% We will add a second optional argument for \cs{section}, etc.
% See the \ctt{} thread \textit{`Long headers'} 15 Jan 2003.
%
% The kernel \cs{@dblarg} is used in situations where the default
% value for an optional argument is the same as the required argument.
% Schematically, \\
% |\@dblarg -> Main, Main| \\
% The macros \cs{@trplargomm} and \cs{@trplargoom} are extensions to this
% idea for two optional arguments.
%
% \begin{macro}{\@trplargomm}
% \begin{macro}{\@xtrplargomm}
% \begin{macro}{\@xxtrplarg}
% |\@trplargomm -> Opt, Main, Main|
%    \begin{macrocode}
\newcommand{\@trplargomm}[1]{%
  \@ifnextchar[{\@xtrplargomm{#1}}%
               {\@xxtrplarg{#1}}}
\long\def\@xtrplargomm#1[#2]{\@dblarg{#1[#2]}}
\newcommand{\@xxtrplarg}[2]{#1[{#2}][{#2}]{#2}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@trplargoom}
% \begin{macro}{\@xtrplargoom}
% |\@trplargoom -> Opt, Opt, Main|
%    \begin{macrocode}
\newcommand{\@trplargoom}[1]{%
  \@ifnextchar[{\@xtrplargoom{#1}}%
               {\@xxtrplarg{#1}}}
\long\def\@xtrplargoom#1[#2]{%
  \@ifnextchar[{#1[{#2}]}%
               {#1[{#2}][{#2}]}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\memsecinfo}
% \begin{macro}{\memsecstarinfo}
% \verb?\memsecinfo{secname}{\thenum}{toc}{head}{title}? \\
% \verb?\memsecstarinfo{secname}{title}? \\
%    \begin{macrocode}
\newcommand*{\memsecinfo}[5]{}
\newcommand*{\memsecstarinfo}[2]{}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@startsection}
% \begin{macro}{\m@msecn@me}
% Change kernel \cs{@startsection} to: \\
% \begin{itemize}
% \item Make short pages raggedbottom. This is based on the thread 
% \textit{Can \cs{flushbottom} and \cs{section} be made to live together?} 
% on \ctt{} in September 2002.
% \changes{v1.3}{2002/11/14}{Modified \cs{@startsection} for raggedbottoming}
% \item add an extra optional argument.
% \changes{v1.4}{2003/11/22}{Added extra optional arg to \cs{@startsection}
%                            (from patch v1.4)}
% \end{itemize}
% The original is in \file{ltsect.dtx}. \\
% \cs{@startsection}\marg{name}\marg{level}\marg{indent} \\
% \hspace*{1.5in} \marg{beforeskip}\marg{afterskip}\marg{style}
%    \begin{macrocode}
\renewcommand{\@startsection}[6]{%
%    \end{macrocode}
% Do raggedbottom stuff.
%    \begin{macrocode}
  \ifraggedbottomsection\if@nobreak\else
    \vskip\z@\@plus\bottomsectionskip
    \penalty\z@
    \vskip\z@\@plus -\bottomsectionskip
  \fi\fi
%    \end{macrocode}
% Save the section name.
%    \begin{macrocode}
  \def\m@msecn@me{#1}%
%    \end{macrocode}
% The original code.
%    \begin{macrocode}
  \if@noskipsec \leavevmode \fi
  \par
  \@tempskipa #4\relax
  \@afterindenttrue
  \ifdim \@tempskipa <\z@
    \@tempskipa -\@tempskipa \@afterindentfalse
  \fi
  \if@nobreak
    \everypar{}%
  \else
    \addpenalty\@secpenalty\addvspace\@tempskipa
  \fi
%    \end{macrocode}
% For the extra optional argument, change the original \verb?\@dblarg{\@sect{...?
% below to \verb?\@trplargoom{\M@sect{...?.
%    \begin{macrocode}
  \@ifstar
    {\@ssect{#3}{#4}{#5}{#6}}%
    {\@trplargoom{\M@sect{#1}{#2}{#3}{#4}{#5}{#6}}}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\M@sect}
% At least the \Lpack{hyperref} and \Lpack{nameref} packages, and
% possibly other packages,  modify the kernel \cs{@sect} assuming 8 args.
% That's why I've called my version, which has 9 args, something 
% else (i.e., \cs{M@sect}). This version also has the support
% for title referencing. \\
% \cs{M@sect}\marg{name}\marg{level}\marg{indent}\marg{beforeskip}\marg{afterskip}\marg{style}\\ \hspace*{0.5in} \oarg{toc}\oarg{head}\marg{title}
% \changes{v1.4}{2003/11/22}{Added \cs{M@sect}}
%    \begin{macrocode}
\def\M@sect#1#2#3#4#5#6[#7][#8]#9{%
  \ifheadnameref\M@gettitle{#8}\else\M@gettitle{#7}\fi
  \ifnum #2>\c@secnumdepth
    \let\@svsec\@empty
    \memsecinfo{#1}{}{#7}{#8}{#9}%
  \else
    \refstepcounter{#1}%
    \protected@edef\@svsec{\@seccntformat{#1}\relax}%
    \memsecinfo{#1}{\@nameuse{the#1}}{#7}{#8}{#9}%
  \fi
  \@tempskipa #5\relax
  \ifdim \@tempskipa>\z@
    \begingroup
      #6{%
      \@hangfrom{\hskip #3\relax\@svsec}%
        \interlinepenalty \@M #9\@@par}%
    \endgroup
    \csname #1mark\endcsname{#8}%
    \addcontentsline{toc}{#1}{%
      \ifnum #2>\c@secnumdepth \else
        \protect\numberline{\csname the#1\endcsname}%
      \fi
      #7}%
  \else
    \def\@svsechd{%
      #6{\hskip #3\relax
     \@svsec #9}%
     \csname #1mark\endcsname{#8}%
     \addcontentsline{toc}{#1}{%
       \ifnum #2>\c@secnumdepth \else
        \protect\numberline{\csname the#1\endcsname}%
       \fi
       #7}}%
  \fi
  \@xsect{#5}}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@ssect}
% Add hook for name reference to \cs{section*} etc. \\
% \cs{@ssect}\marg{indent}\marg{beforeskip}\marg{afterskip}\marg{style}\marg{title}
%    \begin{macrocode}
\let\@mem@old@ssect\@ssect
\def\@ssect#1#2#3#4#5{%
  \M@gettitle{#5}%
  \memsecstarinfo{#1}{\m@msecn@me}{#5}%
  \@mem@old@ssect{#1}{#2}{#3}{#4}{#5}}

%    \end{macrocode}
% \end{macro}
%
%
% 
% \begin{macro}{\section}
% A normal heading with white space above and below and no indentation
% of the first paragraph. 
% By default the heading is set in a \verb?\Large\bfseries? font.
%    \begin{macrocode}
\newcommand{\section}{%
  \sechook%
  \@startsection{section}{1}%  level 1
      {\secindent}%            heading indent
      {\beforesecskip}%        skip before the heading
      {\aftersecskip}%         skip after the heading
      {\normalfont\secheadstyle}} % font
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sechook}
% \begin{macro}{\setsechook}
% |\sechook| is called at the start of a |\section| and 
% |\setsechook{|\meta{code}|}| redefines it.
%    \begin{macrocode}
\newcommand{\sechook}{}
\newcommand{\setsechook}[1]{\renewcommand{\sechook}{#1}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\secindent}
% \begin{macro}{\beforesecskip}
% \begin{macro}{\aftersecskip}
% \begin{macro}{\secheadstyle}
% \begin{macro}{\setsecindent}
% \begin{macro}{\setbeforesecskip}
% \begin{macro}{\setaftersecskip}
% \begin{macro}{\setsecheadstyle}
% |\secindent| is the indentation of the section heading from the left margin,
% |\beforesecskip| and |\aftersecskip| specify the white space before
% and after the heading, and |\secheadstyle| specifies the heading style.
% These are set to the default values for the \Lpack{book} class,
% except that the heading will be |\raggedright|, thus preventing
% hyphenation. The
% |\set...| commands are for the user to change the values.
%    \begin{macrocode}
\newlength{\secindent}
\newcommand{\setsecindent}[1]{\setlength{\secindent}{#1}}
  \setsecindent{\z@}
\newskip\beforesecskip
\newcommand{\setbeforesecskip}[1]{\setlength{\beforesecskip}{#1}}
  \setbeforesecskip{-3.5ex \@plus -1ex \@minus -.2ex}
\newskip\aftersecskip
\newcommand{\setaftersecskip}[1]{\setlength{\aftersecskip}{#1}}
  \setaftersecskip{2.3ex \@plus .2ex}
\newcommand{\secheadstyle}{}
\newcommand{\setsecheadstyle}[1]{\renewcommand{\secheadstyle}{#1}}
  \setsecheadstyle{\Large\bfseries\raggedright}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\subsection}
% A normal heading with white space above and below and no indentation
% of the first paragraph.
% By default the heading is set in a |\large\bfseries| font.
%    \begin{macrocode}
\newcommand{\subsection}{%
  \subsechook%
  \@startsection{subsection}{2}%  level 2
      {\subsecindent}%            heading indent
      {\beforesubsecskip}%        skip before the heading
      {\aftersubsecskip}%         skip after the heading
      {\normalfont\subsecheadstyle}} % font
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\subsechook}
% \begin{macro}{\setsubsechook}
% |\subsechook| is called at the start of a |\subsection| and 
% |\setsubsechook{|\meta{code}|}| redefines it.
%    \begin{macrocode}
\newcommand{\subsechook}{}
\newcommand{\setsubsechook}[1]{\renewcommand{\subsechook}{#1}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\subsecindent}
% \begin{macro}{\beforesubsecskip}
% \begin{macro}{\aftersubsecskip}
% \begin{macro}{\subsecheadstyle}
% \begin{macro}{\setsubsecindent}
% \begin{macro}{\setbeforesubsecskip}
% \begin{macro}{\setaftersubsecskip}
% \begin{macro}{\setsubsecheadstyle}
% The macros for controlling |\subsection| headings.
%    \begin{macrocode}
\newlength{\subsecindent}
\newcommand{\setsubsecindent}[1]{\setlength{\subsecindent}{#1}}
  \setsubsecindent{\z@}
\newskip\beforesubsecskip
\newcommand{\setbeforesubsecskip}[1]{\setlength{\beforesubsecskip}{#1}}
  \setbeforesubsecskip{-3.25ex \@plus -1ex \@minus -.2ex}
\newskip\aftersubsecskip
\newcommand{\setaftersubsecskip}[1]{\setlength{\aftersubsecskip}{#1}}
  \setaftersubsecskip{1.5ex \@plus .2ex}
\newcommand{\subsecheadstyle}{}
\newcommand{\setsubsecheadstyle}[1]{\renewcommand{\subsecheadstyle}{#1}}
  \setsubsecheadstyle{\large\bfseries\raggedright}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\subsubsection}
% A normal heading with white space above and below and no indentation
% of the first paragraph.
% By default the heading is set in a |\normalsize\bfseries| font.
%    \begin{macrocode}
\newcommand{\subsubsection}{%
  \subsubsechook%
  \@startsection{subsubsection}{3}%  level 3
      {\subsubsecindent}%            heading indent
      {\beforesubsubsecskip}%        skip before the heading
      {\aftersubsubsecskip}%         skip after the heading
      {\normalfont\subsubsecheadstyle}} % font
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\subsubsechook}
% \begin{macro}{\setsubsubsechook}
% |\subsubsechook| is called at the start of a |\subsubsection| and 
% |\setsubsubsechook{|\meta{code}|}| redefines it.
%    \begin{macrocode}
\newcommand{\subsubsechook}{}
\newcommand{\setsubsubsechook}[1]{\renewcommand{\subsubsechook}{#1}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\subsubsecindent}
% \begin{macro}{\beforesubsubsecskip}
% \begin{macro}{\aftersubsubsecskip}
% \begin{macro}{\subsubsecheadstyle}
% \begin{macro}{\setsubsubsecindent}
% \begin{macro}{\setbeforesubsubsecskip}
% \begin{macro}{\setaftersubsubsecskip}
% \begin{macro}{\setsubsubsecheadstyle}
% The macros for controlling |\subsubsection| headings.
%    \begin{macrocode}
\newlength{\subsubsecindent}
\newcommand{\setsubsubsecindent}[1]{%
            \setlength{\subsubsecindent}{#1}}
  \setsubsubsecindent{\z@}
\newskip\beforesubsubsecskip
\newcommand{\setbeforesubsubsecskip}[1]{%
            \setlength{\beforesubsubsecskip}{#1}}
  \setbeforesubsubsecskip{-3.25ex \@plus -1ex \@minus -.2ex}
\newskip\aftersubsubsecskip
\newcommand{\setaftersubsubsecskip}[1]{%
            \setlength{\aftersubsubsecskip}{#1}}
  \setaftersubsubsecskip{1.5ex \@plus .2ex}
\newcommand{\subsubsecheadstyle}{}
\newcommand{\setsubsubsecheadstyle}[1]{%
            \renewcommand{\subsubsecheadstyle}{#1}}
  \setsubsubsecheadstyle{\normalsize\bfseries\raggedright}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% 
%
% \begin{macro}{\paragraph}
% A runin heading with white space above and to the right of the heading.
% By default the heading is set in a |\normalsize\bfseries| font.
%    \begin{macrocode}
\newcommand{\paragraph}{%
  \parahook%
  \@startsection{paragraph}{4}%  level 4
      {\paraindent}%            heading indent
      {\beforeparaskip}%        skip before the heading
      {\afterparaskip}%         skip after the heading
      {\normalfont\paraheadstyle}} % font
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\parahook}
% \begin{macro}{\setparahook}
% |\parahook| is called at the start of a |\paragraph| and 
% |\setparahook{|\meta{code}|}| redefines it.
%    \begin{macrocode}
\newcommand{\parahook}{}
\newcommand{\setparahook}[1]{\renewcommand{\parahook}{#1}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\paraindent}
% \begin{macro}{\beforeparaskip}
% \begin{macro}{\afterparaskip}
% \begin{macro}{\paraheadstyle}
% \begin{macro}{\setparaindent}
% \begin{macro}{\setbeforeparaskip}
% \begin{macro}{\setafterparaskip}
% \begin{macro}{\setparaheadstyle}
% The macros for controlling |\paragraph| headings.
%    \begin{macrocode}
\newlength{\paraindent}
\newcommand{\setparaindent}[1]{\setlength{\paraindent}{#1}}
  \setparaindent{\z@}
\newskip\beforeparaskip
\newcommand{\setbeforeparaskip}[1]{\setlength{\beforeparaskip}{#1}}
  \setbeforeparaskip{3.25ex \@plus 1ex \@minus .2ex}
\newskip\afterparaskip
\newcommand{\setafterparaskip}[1]{\setlength{\afterparaskip}{#1}}
  \setafterparaskip{-1em}
\newcommand{\paraheadstyle}{}
\newcommand{\setparaheadstyle}[1]{\renewcommand{\paraheadstyle}{#1}}
  \setparaheadstyle{\normalsize\bfseries}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% 
%
% \begin{macro}{\subparagraph}
% A runin heading with white space above and to the right of the heading.
% By default the heading is set in a |\normalsize\bfseries| font.
%    \begin{macrocode}
\newcommand{\subparagraph}{%
  \subparahook%
  \@startsection{subparagraph}{5}%  level 5
      {\subparaindent}%            heading indent
      {\beforesubparaskip}%        skip before the heading
      {\aftersubparaskip}%         skip after the heading
      {\normalfont\subparaheadstyle}} % font
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\subparahook}
% \begin{macro}{\setsubparahook}
% |\subparahook| is called at the start of a |\subparagraph| and 
% |\setsubparahook{|\meta{code}|}| redefines it.
%    \begin{macrocode}
\newcommand{\subparahook}{}
\newcommand{\setsubparahook}[1]{\renewcommand{\subparahook}{#1}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\subparaindent}
% \begin{macro}{\beforesubparaskip}
% \begin{macro}{\aftersubparaskip}
% \begin{macro}{\subparaheadstyle}
% \begin{macro}{\setsubparaindent}
% \begin{macro}{\setbeforesubparaskip}
% \begin{macro}{\setaftersubparaskip}
% \begin{macro}{\setsubparaheadstyle}
% The macros for controlling |\subparagraph| headings.
%    \begin{macrocode}
\newlength{\subparaindent}
\newcommand{\setsubparaindent}[1]{%
            \setlength{\subparaindent}{#1}}
  \setsubparaindent{\parindent}
\newskip\beforesubparaskip
\newcommand{\setbeforesubparaskip}[1]{%
            \setlength{\beforesubparaskip}{#1}}
  \setbeforesubparaskip{3.25ex \@plus 1ex \@minus .2ex}
\newskip\aftersubparaskip
\newcommand{\setaftersubparaskip}[1]{%
            \setlength{\aftersubparaskip}{#1}}
  \setaftersubparaskip{-1em}
\newcommand{\subparaheadstyle}{}
\newcommand{\setsubparaheadstyle}[1]{%
            \renewcommand{\subparaheadstyle}{#1}}
  \setsubparaheadstyle{\normalsize\bfseries}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\sethangfrom}
% The macro |\sethangfrom{|\meta{code}|}| is a user-level command
% for changing the definition of |\@hangfrom|. Use |#1| in \meta{code}
% for the argument to |\@hangfrom| (or |##1| if used inside another macro).
% \changes{v0.2}{2001/06/03}{Added \cs{sethangfrom}}
%    \begin{macrocode}
\newcommand{\sethangfrom}[1]{\renewcommand{\@hangfrom}[1]{#1}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\setsecnumformat}
% The macro |\setsecnumformat{|\meta{code}|}| is a user-level command
% for changing the definition of |\@seccntformat|. Use |#1| in \meta{code}
% for the argument to |\@seccntformat| (or |##1| if used inside another macro).
% \changes{v0.2}{2001/06/03}{Added \cs{setsecnumformat}}
%    \begin{macrocode}
\newcommand{\setsecnumformat}[1]{\renewcommand{\@seccntformat}[1]{#1}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\hangsecnum}
% \begin{macro}{\defaultsecnum}
% These are declarations for putting sectional numbers in the margin,
% or the default sectional number formatting.
% \changes{v0.2}{2001/06/03}{Added \cs{hangsecnum} and \cs{defaultsecnum}}
%    \begin{macrocode}
\newcommand{\hangsecnum}{%
  \def\@seccntformat##1{\llap{\csname the##1\endcsname\quad}}}
\newcommand{\defaultsecnum}{%
  \def\@seccntformat##1{\csname the##1\endcsname\quad}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsubsection{Anonymous headings}
%
% \begin{macro}{\plainbreak}
% |\plainbreak{|\meta{num}|}| generates \meta{num} blanks lines and
% suppresses the indentation of a following paragraph. The starred
% version, |\plainbreak*|, does not suppress paragraph indentation.
% \changes{v0.2}{2001/06/03}{Added starred version of \cs{plainbreak}}
%    \begin{macrocode}
\newcommand{\plainbreak}{\@ifstar{\@spbreak}{\@pbreak}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@pbreak}
% \begin{macro}{\@spbreak}
% These are the internal forms for the two versions of |\plainbreak|.
% The code for |\@pbreak| is almost a straight copy of code posted to
% \ctt{} by Donald Arseneau on 2001/03/26.
%    \begin{macrocode}
\newcommand*{\@pbreak}[1]{\par
  \penalty -100
  \vskip #1\onelineskip \@plus 2\onelineskip
  \penalty -20
  \vskip \z@ \@plus -2\onelineskip
  \@afterindentfalse
  \@afterheading}
\newcommand*{\@spbreak}[1]{\par
  \penalty -100
  \vskip #1\onelineskip \@plus 2\onelineskip
  \penalty -20
  \vskip \z@ \@plus -2\onelineskip
  \@afterindenttrue
  \@afterheading}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\fancybreak}
% |\fancybreak{|\meta{text}|}| typesets \meta{text} centered. For example,
% |\fancybreak{{*}\\{* * *}\\{*}}|. It suppresses indentation of the 
% following paragraph. The starred version leaves indentation as is.
% \changes{v0.2}{2001/06/03}{Added starred version of \cs{fancybreak}}
% \changes{v1.3}{2002/11/14}{Tweaked \cs{fancybreak} to remove underfull hbox}
%    \begin{macrocode}
\newcommand{\fancybreak}{\@ifstar{\@sfbreak}{\@fbreak}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@fbreak}
% \begin{macro}{\@sfbreak}
% These are the internal forms for the two versions of |\fancybreak|.
%    \begin{macrocode}
\newcommand{\@fbreak}[1]{\par
  \penalty -100
  \noindent\parbox{\linewidth}{\centering #1}\null
  \penalty -20
%%  \vskip -\onelineskip
  \@afterindentfalse
  \@afterheading}
\newcommand{\@sfbreak}[1]{\par
  \penalty -100
  \noindent\parbox{\linewidth}{\centering #1}\null
  \penalty -20
%%  \vskip -\onelineskip
  \@afterindenttrue
  \@afterheading}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\plainfancybreak}
% The |\plainfancybreak{|\meta{space}|}{|\meta{num}|}{|\meta{text}|}| acts
% like |\plainbreak{|\meta{num}|}| in the middle of a page and like
% |\fancybreak{|\meta{text}|}| at the bottom of a page. The \meta{space}
% argument is the vertical space required for the \meta{num} blank lines
% and some additional lines of text. From experiments, it seems that
% \meta{space} should be at least (3+\meta{num}) lines (|\onelineskip|).
% There is also a starred version, |\plainfancybreak*|, to match the other
% starred break commands.
% \changes{v0.2}{2001/06/03}{Added \cs{plainfancybreak} and supporting macros}
%    \begin{macrocode}
\newcommand{\plainfancybreak}{\@ifstar{\@spfbreak}{\@pfbreak}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@pfbreak}
% \begin{macro}{\@spfbreak}
% These are the internal macros for the unstarred and starred versions
% of |\plainfancybreak|. They essentially do the same thing, except they call, 
% respectively, the unstarred and starred internal versions of |\plainbreak|
% and |\fancybreak|. The code for checking the amount of space left on the
% page is from an early, and abandoned, version of the code for |\needspace|.
%    \begin{macrocode}
\newcommand{\@pfbreak}[3]{\par
  \@tempdimc\pagegoal \advance\@tempdimc-\pagetotal
  \ifdim #1>\@tempdimc \@fbreak{#3}\else \@pbreak{#2}\fi}
\newcommand{\@spfbreak}[3]{\par
  \@tempdimc\pagegoal \advance\@tempdimc-\pagetotal
  \ifdim #1>\@tempdimc \@sfbreak{#3}\else \@spbreak{#2}\fi}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% From the thread \textit{Customizing section hooks in memoir.cls} on \ctt{}
% in October 2002 it appeared that |\plainfancybreak| may be fragile.
% Donald Arseneau said that |\plainfancybreak| guessed at the amount
% of space available and gave code based on a modified output routine
% to make it more robust. The following code is based on that thread.
% \changes{v1.3}{2002/11/14}{Added lots of code for new \cs{fpbreak}}
%
% \begin{macro}{\pen@ltyabovefpbreak}
% \begin{macro}{\pen@ltybelowfpbreak}
% Penalities for communication with the output routine.
%    \begin{macrocode}
\newcommand*{\pen@ltyabovepfbreak}{2}
\newcommand*{\pen@ltybelowpfbreak}{-4}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\pfbreakskip}
% The vertical space taken by the plain and fancy breaks.
%    \begin{macrocode}
\newlength{\pfbreakskip}
  \setlength{\pfbreakskip}{2\baselineskip}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\pfbreakdisplay}
% This is the fancybreak display, which must fit into |\pfbreakskip|
% vertical space.
%    \begin{macrocode}
\newcommand{\pfbreakdisplay}{*\quad*\quad*}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\pfbre@kdispl@y}
% Typesets |\pfbreakdisplay| vertically and horizontally centered.
%    \begin{macrocode}
\def\pfbre@kdispl@y{\vbox to 1\pfbreakskip{\vss
  \hb@xt@ \columnwidth{\hss \pfbreakdisplay \hss}%
  \vss}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\nopfbreakOutput}
% Save the current output routine.
%    \begin{macrocode}
\edef\nopfbreakOutput{\the\output}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\pfbreakOutput}
% Special output to handle the |\pfbreak|.
%    \begin{macrocode}
\def\pfbreakOutput{%
  \ifnum\outputpenalty=\pen@ltyabovepfbreak
    \nopfbreakOutput
    \pfbre@kdispl@y
    \nobreak
    \vskip-\pfbreakskip
  \else\ifnum\outputpenalty=\pen@ltybelowpfbreak
    \unvbox 255\relax
    \nobreak
    \vskip-\pfbreakskip
    \pfbre@kdispl@y
    \break
  \else
    \nopfbreakOutput
  \fi
  \fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\output}
% Use the new |\pfbreak| output routine.
%    \begin{macrocode}
\output={\pfbreakOutput}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\pfbreak}
% Typesets a plain break in the middle of the page, otherwise
% a fancybreak at either the bottom or top of the page.
%    \begin{macrocode}
\newcommand{\pfbreak}{\@ifstar{\@spfbreakgap}{\@pfbreakgap}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@pfbreakgap}
% Unstarred version of |\pfbreak|.
%    \begin{macrocode}
\newcommand{\@pfbreakgap}{%
  \par {%
  \skip@\lastskip
  \nobreak
  \vskip -\ifdim\prevdepth>\maxdepth \maxdepth
          \else\ifdim\prevdepth>-1000pt\prevdepth
            \else\ifinner 0pt
              \else \pagedepth
          \fi \fi \fi
  \vskip -\skip@
  \ifdim\skip@<\pfbreakskip
    \advance\skip@ -1\skip@ \advance\skip@ 1\pfbreakskip
  \fi
  \penalty\pen@ltyabovepfbreak
  \vskip\skip@
  \penalty\pen@ltybelowpfbreak
  }
  \@afterindentfalse
  \@afterheading
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@spfbreakgap}
% Starred version of |\pfbreak|.
%    \begin{macrocode}
\newcommand{\@spfbreakgap}{%
  \par {%
  \skip@\lastskip
  \nobreak
  \vskip -\ifdim\prevdepth>\maxdepth \maxdepth
          \else\ifdim\prevdepth>-1000pt\prevdepth
            \else\ifinner 0pt
              \else \pagedepth
          \fi \fi \fi
  \vskip -\skip@
  \ifdim\skip@<\pfbreakskip
    \advance\skip@ -1\skip@ \advance\skip@ 1\pfbreakskip
  \fi
  \penalty\pen@ltyabovepfbreak
  \vskip\skip@
  \penalty\pen@ltybelowpfbreak
  }
  \@afterindenttrue
  \@afterheading
}

%    \end{macrocode}
% \end{macro}
%
%
%    While on the subject of breaks\ldots
%
% \begin{macro}{\noprelistbreak}
% Putting this immediately before a list (e.g., |itemize|) should
% prevent a page break at that point.
% \changes{v1.2}{2002/07/27}{Added \cs{noprelistbreak}}
%    \begin{macrocode}
\newcommand*{\noprelistbreak}{\@nobreaktrue\nopagebreak}

%    \end{macrocode}
% \end{macro}
%
%
% \subsection{Appendices}
%
% \begin{macro}{\ifanappendix}
% \begin{macro}{\anappendixtrue}
% \begin{macro}{\anappendixfalse}
% \begin{macro}{\appendix}
% In the standard \Lpack{book} class the |\appendix| command does the 
% following:
%    \begin{itemize}
%    \item Resets the chapter and section counters to zero
%    \item Sets |\@chapapp| to |\appendixname|.
%    \item Redefines |\thechapter| to produce alphabetic appendix numbers.
%    \end{itemize}
% \changes{v1.618}{2005/09/03}{Added \cs{ifanappendix} (mempatch v3.12)}
%    \begin{macrocode}
\newif\ifanappendix
  \anappendixfalse
\newcommand{\appendix}{\par
  \setcounter{chapter}{0}%
  \setcounter{section}{0}%
  \gdef\@chapapp{\appendixname}%
  \gdef\thechapter{\@Alph\c@chapter}%
  \anappendixtrue}

%    \end{macrocode}
% \end{macro}  
% \end{macro}  
% \end{macro}  
% \end{macro}  
%
%
% This class provides additional appendixing capabilities.
%
%
% \begin{macro}{\appendixpage}
% \begin{macro}{\appendixpage*}
% The command to typeset a page announcing the start of the appendices.
% It is based on the |\part| definition. The normal version
% makes an entry in the ToC but the starred version does not.
% \changes{v0.33}{2001/08/31}{Changed implementation of \cs{appendixpage}}
%    \begin{macrocode}
\newcommand{\appendixpage}{%
  \@ifstar{\@sapppage}{\@apppage}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\memapppageinfo}
% \begin{macro}{\memapppagestarinfo}
% \verb?\memapppageinfo{\appendixpagename}? \\
% \verb?\memapppagestarinfo{\appendixpagename}? \\
%    \begin{macrocode}
\newcommand*{\memapppageinfo}[1]{}
\newcommand*{\memapppagestarinfo}[1]{}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@apppage}
% |\@apppage| typesets an appendix page and makes an entry in the ToC.
%    \begin{macrocode}
\def\@apppage{%
  \@setuppart
  \addappheadtotoc
  \partmark{\appendixpagename}%
  \memapppageinfo{\appendixpagename}%
  {\centering
   \interlinepenalty \@M
   \normalfont
   \printparttitle{\appendixpagename}\par}%
  \@endpart}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@sapppage}
% |\@sapppage| typesets an appendix page and does not make an entry in the ToC.
%    \begin{macrocode}
\def\@sapppage{%
  \@setuppart
  \partmark{\appendixpagename}%
  \memapppagestarinfo{\appendixpagename}%
  {\centering
   \interlinepenalty \@M
   \normalfont
   \printparttitle{\appendixpagename}\par}%
  \@endpart}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\addappheadtotoc}
% This command adds an `appendices' line to the ToC. The style is the same
% as used in \Lpack{tocbibind} for the `List of figures' line. That is,
% as a Chapter heading.
%    \begin{macrocode}
\def\addappheadtotoc{\addcontentsline{toc}{chapter}{\appendixtocname}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@ppsavesec}
% \begin{macro}{\@pprestoresec}
% \begin{macro}{\@ppsaveapp}
% \begin{macro}{\restoreapp}
%  For the |appendices| environment we need to save and restore the
% main document division number and the appendix number. The |\restoreapp|
% command is the one for the user.
%    \begin{macrocode}
\newcounter{@ppsavesec}
\newcounter{@ppsaveapp}
\setcounter{@ppsaveapp}{0}
\newcommand{\@ppsavesec}{%
  \setcounter{@ppsavesec}{\value{chapter}}}
\newcommand{\@pprestoresec}{%
  \setcounter{chapter}{\value{@ppsavesec}}}
\newcommand{\@ppsaveapp}{%
  \setcounter{@ppsaveapp}{\value{chapter}}}
\newcommand{\restoreapp}{%
  \setcounter{chapter}{\value{@ppsaveapp}}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\@resets@pp}
% Resets the appropriate sectioning counters and names. This does almost
% exactly
% what the default |\appendix| command does, except that it saves and 
% restores sectional numbering. It saves the sectional number at the start
% and restores the appendix number at the end.
%    \begin{macrocode}
\newcommand{\@resets@pp}{%
  \par
  \@ppsavesec
  \setcounter{section}{0}%
  \setcounter{chapter}{0}%
  \renewcommand\@chapapp{\appendixname}%
  \renewcommand\thechapter{\@Alph\c@chapter}%
  \restoreapp
}

%    \end{macrocode}
% \end{macro}
%
% \begin{environment}{appendices}
%  This is the heart of the package. Start it off by doing the resetting
% done by the |\appendix| command but also save the main counters. At the
% end of the environment save the appendix number and restore the main counters.
%    \begin{macrocode}
\newenvironment{appendices}%
  {\@resets@pp\anappendixtrue}%
  {\@ppsaveapp\@pprestoresec\anappendixfalse}

%    \end{macrocode}
% \end{environment}
%
% \begin{macro}{\setthesection}
% The user commands for specifying the numbering style for subappendices.
%    \begin{macrocode}
\newcommand{\setthesection}{\thechapter.\Alph{section}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@resets@ppsub}
% Similar to |\@resets@pp| except that it is for use within the 
% |subappendices| envirionment; as such, it is a bit simpler.
%    \begin{macrocode}
\newcommand{\@resets@ppsub}{
  \par
  \setcounter{section}{0}
  \renewcommand{\thesection}{\setthesection}
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ifnamesubappendix}
% Switch for adding an `appendix' name before subappendix numbers.
% \changes{v1.0}{2001/11/13}{Added \cs{ifnamesubappendix}}
%    \begin{macrocode}
\newif\ifnamesubappendix
  \namesubappendixfalse

%    \end{macrocode}
% \end{macro}
%
% \begin{environment}{subappendices}
%  The environment for subappendices. Start it off by doing the resetting
% of the |\section| command. 
% \changes{v1.0}{2001/11/13}{Modified subappendices environment}
%    \begin{macrocode}
\newenvironment{subappendices}{%
  \@resets@ppsub
%    \end{macrocode}
% Change the definition of |\addappheadtotoc| to give a |section| entry.
% \changes{v1.618}{2005/09/02}{Fixed bookmark for \cs{appendixpage} (mempatch v2.3)}
%    \begin{macrocode}
  \def\addappheadtotoc{\phantomsection
    \addcontentsline{toc}{section}{\appendixtocname}}
%    \end{macrocode}
% To implement the naming we do cunning things with the
% |\@seccntformat| command.
%    \begin{macrocode}
  \ifnamesubappendix
    \def\sectionname{\appendixname}
    \def\@seccntformat##1{\@ifundefined{##1name}%
                         {}{\csname ##1name\endcsname\ }%
        \csname the##1\endcsname\quad}
  \fi
%    \end{macrocode}
% That's it.
%    \begin{macrocode}
  }{}

%    \end{macrocode}
% \end{environment}
%
% \begin{macro}{\@formatsecmark@pp}
% Formats the page header for a redefined |\sectionmark|.
%    \begin{macrocode}
\newcommand{\@formatsecmark@pp}[1]{%
  \MakeUppercase{\appendixname\space
    \ifnum \c@secnumdepth >\z@
      \thesection\quad
    \fi
    #1}}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Appendixpage-like pages}
% \changes{v1.618}{2005/09/03}{Added \cs{newleadpage} and friends (mempatch v3.0)}
%
%
% This capability was suggested to me by Lars Madsen on 2004/11/28.
%
% \begin{macro}{\leadpagetoclevel}
% \begin{macro}{\newleadpage}
% \begin{macro}{\renewleadpage}
% \verb?\newleadpage[<page-style>]{<cmdname>}{<title>}? creates new macros 
% called \verb?\cmdname? and \verb?\cmdname*? that when called typeset a 
% page like an  Appendixpage, with a title \verb?<title>? using
% page style \verb?<page-style>? (default empty). The
% plain version adds an entry to the ToC but the starred \verb?\cmdname*? 
% does not. \cs{renewleadpage} changes the definitions.
%
%    The ToC entry style is set by \cs{leadpagetoclevel} (default chapter). To have a part-type entry: \\
% \verb?\renewcommand*{\leadpagetoclevel}{part}?. \\
% The \cs{partmark} command is used if you need to mark the title.
%    \begin{macrocode}
\newcommand*{\leadpagetoclevel}{chapter}
\newcommand*{\newleadpage}[3][empty]{%
  \@namedef{#2}{\@ifstar{\dlfm@msapppage{#1}{#2}{#3}}%
                        {\dlfm@mapppage{#1}{#2}{#3}}}}
\newcommand*{\renewleadpage}[3][empty]{%
  \@namedef{#2}{\@ifstar{\dlfm@msapppage{#1}{#2}{#3}}%
                        {\dlfm@mapppage{#1}{#2}{#3}}}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\memleadpageinfo}
% \begin{macro}{\memleadpagestarinfo}
% \verb?\memleadpageinfo{pstyle}{name}{title}? \\
% \verb?\memleadpagestarinfo{pstyle}{name}{title}? \\
%    \begin{macrocode}
\newcommand{\memleadpageinfo}[3]{}
\newcommand{\memleadpagestarinfo}[3]{}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\dlfm@msapppage}
% \begin{macro}{\dlfm@mapppage}
% Implement the starred and regular versions of \verb?\(re)newleadpage?
%    \begin{macrocode}
\newcommand*{\dlfm@msapppage}[3]{%
  \@setuppart
  \partmark{#3}%
  \memleadpagestarinfo{#1}{#2}{#3}%
  {\centering
  \interlinepenalty \@M
  \normalfont
  \printparttitle{#3}\par
  \thispagestyle{#1}}%
  \dlfm@m@endpart{#1}}
\newcommand*{\dlfm@mapppage}[3]{%
  \@setuppart
  \phantomsection
  \addcontentsline{toc}{\leadpagetoclevel}{#3}%
  \partmark{#3}%
  \memleadpageinfo{#1}{#2}{#3}%
  {\centering
  \interlinepenalty \@M
  \normalfont
  \printparttitle{#3}\par
  \thispagestyle{#1}}%
  \dlfm@m@endpart{#1}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\dlfm@m@endpart}
% Finishes off a part-like page.
%    \begin{macrocode}
\newcommand*{\dlfm@m@endpart}[1]{%
  \if@twoside
    \if@openright
      \null
      \thispagestyle{#1}%
      \newpage
    \fi
  \fi
  \if@tempswa
    \twocolumn
  \fi}

%    \end{macrocode}
% \end{macro}
%
%
%
%
% \subsection{Paragraphs}
%
% \subsubsection{Ragged}
%
% The kernel code for raggedright (in \file{ltmiscen.dtx}):
% \begin{verbatim}
% \def\raggedright{%
%   \let\\\@centercr\@rightskip\@flushglue \rightskip\@rightskip
%   \leftskip\z@skip
%   \parindent\z@}
% %% \@flushglue = 0pt plus 1fil  %% from ltalloc.dtx
% \end{verbatim}
% produces very ragged text with no paragraph indent. 
%
% \begin{macro}{\ragrparindent}
% \begin{macro}{\raggedyright}
% |\raggedyright|\oarg{plus} provides controllable ragged right paragraphs.
% \changes{v1.4}{2003/12/10}{Added \cs{raggedyright}}
%    \begin{macrocode}
\newdimen\ragrparindent
  \setlength{\ragrparindent}{\parindent}
\newcommand{\raggedyright}[1][2em]{%
  \let\\\@centercr\@rightskip \z@ \@plus #1\relax \rightskip\@rightskip
  \leftskip\z@skip
  \parindent\ragrparindent}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsubsection{Hanging}
%
% \begin{macro}{\hangfrom}
% This is a user-level
% version of the kernel \cs{@hangfrom} macro (only the name is
% changed) as defined in \file{ltsec.dtx}.
%
% \cs{hangfrom}\marg{text} puts \meta{text} in a box and makes a hanging
% paragraph of the following material (a bit like a description item).
% \changes{v1.4}{2003/11/22}{Added \cs{hangfrom} (from patch v1.9)}
%    \begin{macrocode}
\newcommand{\hangfrom}[1]{%
  \setbox\@tempboxa\hbox{{#1}}%
  \hangindent \wd\@tempboxa\noindent\box\@tempboxa}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\hangpara}
% \cs{hangpara}\marg{indent}\marg{afternum} at the start of a 
% paragraph will make it hung. If \meta{indent} is positive the left
% will be indented, otherwise the right. If \meta{afternum}, say N,
% is positive the N+1 th lines onwards will be indented. For N negative,
% the first N lines will be indented.
%    \begin{macrocode}
\newcommand{\hangpara}[2]{\hangindent#1\hangafter#2\noindent}
%    \end{macrocode}
% \end{macro}
%
% \begin{environment}{hangparas}
% \verb?\begin{hangparas}?\marg{indent}\marg{afternum} hangs 
% a series of paragraphs.
%    \begin{macrocode}
\newenvironment{hangparas}[2]{\setlength{\parindent}{\z@}
  \everypar={\hangpara{#1}{#2}}}{\par}

%    \end{macrocode}
% \end{environment}
%
%
%
%
% \section{Lists}
%
% \subsection{General List Parameters}
%
% The following commands are used to set the default values for the list
% environment's parameters. See the \LaTeX{} manual for an explanation
% of the meanings of the parameters.  Defaults for the list
% environment are set as follows.  First, |\rightmargin|,
% |\listparindent| and |\itemindent| are set to 0pt.  Then, for a Kth
% level list, the command |\@listK| is called, where `K' denotes `i',
% '`i', ... , `vi'.  (I.e., |\@listiii| is called for a third-level
% list.)  By convention, |\@listK| should set |\leftmargin| to
% |\leftmarginK|.
%
%
% \begin{macro}{\leftmargin}
% \begin{macro}{\leftmargini}
% \begin{macro}{\leftmarginii}
% \begin{macro}{\leftmarginiii}
% \begin{macro}{\leftmarginiv}
% \begin{macro}{\leftmarginv}
% \begin{macro}{\leftmarginvi}
% For efficiency, level-one list's values are defined at top level, and
% |\@listi| is defined to set only |\leftmargin|.
%
% In two column mode the margins should be smaller than in one column typesetting.
%    \begin{macrocode}
\if@twocolumn
  \setlength{\leftmargini}{2em}
\else
  \setlength{\leftmargini}{2.5em}
\fi
%    \end{macrocode}
%    The value of |\leftmargin| has to be set at this outer level.
%    \begin{macrocode}
\leftmargin \leftmargini
%    \end{macrocode}
%    Lower level list margins are calculated so that they are indented
% more than the label in an outer list.
%    \begin{macrocode}
\setlength{\leftmarginii}{2.2em}
\setlength{\leftmarginiii}{1.87em}
\setlength{\leftmarginiv}{1.7em}
\if@twocolumn
  \setlength{\leftmarginv}{.5em}
  \setlength{\leftmarginvi}{.5em}
\else
  \setlength{\leftmarginv}{1em}
  \setlength{\leftmarginvi}{1em}
\fi
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\itemindent}
%    Here we set the |\itemindent| which is the extra indentation before
%    a label.
%    \begin{macrocode}
\setlength{\itemindent}{\z@}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\labelsep}
% \begin{macro}{\labelwidth}
%    |\labelsep| is the distance between the label and the text of an
%    item; |\labelwidth| is the width of the label.
%    \begin{macrocode}
\setlength{\labelsep}{0.5em}
\setlength{\labelwidth}{\leftmargini}
  \addtolength{\labelwidth}{-\labelsep}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@beginparpenalty}
% \begin{macro}{\@endparpenalty}
%    These penalties are inserted before and after a list or paragraph
%    environment. They are set to a bonus value to encourage page
%    breaking at these points.
% \begin{macro}{\@itempenalty}
%    This penalty is inserted between list items.
%    \begin{macrocode}
\@beginparpenalty -\@lowpenalty
\@endparpenalty   -\@lowpenalty
\@itempenalty     -\@lowpenalty

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\parsepi}
% \begin{macro}{\topsepi}
% \begin{macro}{\itemsepi}
% \begin{macro}{\parsepii}
% \begin{macro}{\topsepii}
% \begin{macro}{\topsepiii}
% Lists may be nested and the exact layout depends on the level
% of nesting. These lengths are used to control the nesting-level
% aspects.
%    \begin{macrocode}
\newlength{\parsepi}
\newlength{\topsepi}
\newlength{\itemsepi}
\newlength{\parsepii}
\newlength{\topsepii}
\newlength{\topsepiii}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\defaultlists}
% The standard \LaTeX{} classes have list parameters that give some separation
% between lists and |\item|s in a list. This macro sets these values,
% which also depend on the font size.
% \changes{v0.2}{2001/06/03}{Introduced \cs{defaultlist} and \cs{tightlist}}
% \changes{v0.3}{2001/07/09}{Changed \cs{defaultlist} and \cs{tightlist} to
%                \cs{defaultlists} and \cs{tightlists} respectively,
%  and their supports}
%    \begin{macrocode} 
\newcommand{\defaultlists}{%   %% initialise for 10pt
  \setlength\partopsep{2\p@ \@plus 1\p@ \@minus 1\p@}
  \parsepi=   4\p@   \@plus2\p@   \@minus\p@
  \topsepi=   8\p@   \@plus2\p@   \@minus4\p@
  \itemsepi=  4\p@   \@plus2\p@   \@minus\p@
  \parsepii=  2\p@   \@plus\p@    \@minus\p@
  \topsepii=  4\p@   \@plus2\p@   \@minus\p@
  \topsepiii= 2\p@   \@plus\p@    \@minus\p@
\ifcase\@ptsize  % 0 = 10pt
\or              % 1 = 11pt
  \setlength\partopsep{3\p@ \@plus 1\p@ \@minus 1\p@}
  \parsepi=   4.5\p@ \@plus2\p@   \@minus\p@
  \topsepi=   9\p@   \@plus3\p@   \@minus5\p@
  \itemsepi=  4.5\p@ \@plus2\p@   \@minus\p@
  \parsepii=  2\p@   \@plus\p@    \@minus\p@
  \topsepii=  4.5\p@ \@plus2\p@   \@minus\p@
  \topsepiii= 2\p@   \@plus\p@    \@minus\p@
\or              % 2 = 12pt
  \setlength\partopsep{3\p@ \@plus 2\p@ \@minus 2\p@}
  \parsepi=   5\p@   \@plus2.5\p@ \@minus\p@
  \topsepi=  10\p@   \@plus4\p@   \@minus6\p@
  \itemsepi=  5\p@   \@plus2.5\p@ \@minus\p@
  \parsepii=  2.5\p@ \@plus\p@    \@minus\p@
  \topsepii=  5\p@   \@plus2.5\p@ \@minus\p@
  \topsepiii= 2.5\p@ \@plus\p@    \@minus\p@
\or              % 3 = 13pt  %% not available
\or              % 4 = 14pt
  \setlength\partopsep{4\p@ \@plus 2\p@ \@minus 2\p@}
  \parsepi=   6\p@   \@plus2.5\p@ \@minus\p@
  \topsepi=  12\p@   \@plus4\p@   \@minus6\p@
  \itemsepi=  6\p@   \@plus2.5\p@ \@minus\p@
  \parsepii=  3\p@   \@plus\p@    \@minus\p@
  \topsepii=  6\p@   \@plus2.5\p@ \@minus\p@
  \topsepiii= 3\p@   \@plus\p@    \@minus\p@
\or              % 5 = 15pt  %% not available
\or              % 6 = 16pt  %% not available
\or              % 7 = 17pt
  \setlength\partopsep{5\p@ \@plus 2\p@ \@minus 2\p@}
  \parsepi=   7\p@   \@plus3\p@   \@minus\p@
  \topsepi=  14\p@   \@plus6\p@   \@minus8\p@
  \itemsepi=  7\p@   \@plus3\p@   \@minus\p@
  \parsepii=  3\p@   \@plus\p@    \@minus\p@
  \topsepii=  7\p@   \@plus3\p@   \@minus\p@
  \topsepiii= 3.5\p@ \@plus\p@    \@minus\p@
\or              % 8 = 18pt  %% not available
\or              % 9 = 9pt
  \setlength\partopsep{2\p@ \@plus 1\p@ \@minus 1\p@}
  \parsepi=   3.5\p@ \@plus2\p@   \@minus\p@
  \topsepi=   7\p@   \@plus3\p@   \@minus5\p@
  \itemsepi=  3.5\p@ \@plus2\p@   \@minus\p@
  \parsepii=  2\p@   \@plus\p@    \@minus\p@
  \topsepii=  4\p@   \@plus2\p@   \@minus\p@
  \topsepiii= 2\p@   \@plus\p@    \@minus\p@
\fi
}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\tightlists}
% This macro sets the parameters for lists that have less open vertical
% space in them. I think that these look neater than the defaults.
% Effectively, no additional vertical space is added. If there is a
% blank line before a list, then an extra 1/2 blank line is added before
% \emph{and} after the list (making one blank line in total).
%    \begin{macrocode}
\newcommand{\tightlists}{%
  \setlength\partopsep{0.5\onelineskip \@plus 0\p@ \@minus 0\p@}
  \parsepi=       \parskip
  \itemsepi =     \p@ \@plus\p@ \@minus\p@
  \topsepi =   0.5\parskip
  \parsepii =     \parskip
  \topsepii =  0.5\parskip
  \topsepiii = 0.5\parskip
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@listI}
% \begin{macro}{\@listi}
% |\@listI| defines top level and |\@listi| values of
% |\leftmargin|, |\parsep|, |\topsep|, and |\itemsep|
%
%    \begin{macrocode}
\def\@listi{\leftmargin\leftmargini
  \parsep\parsepi
  \topsep\topsepi
  \itemsep\itemsepi}
\let\@listI\@listi
%    \end{macrocode}
%    We should initialise these parameters to the standard defaults
%    \begin{macrocode}
\defaultlists
\@listi

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\@listii}
% \begin{macro}{\@listiii}
% \begin{macro}{\@listiv}
% \begin{macro}{\@listv}
% \begin{macro}{\@listvi}
%    Here are the same macros for the lower level lists.
%    \begin{macrocode}
\def\@listii{\leftmargin\leftmarginii
             \labelwidth\leftmarginii
             \advance\labelwidth-\labelsep
             \topsep\topsepii
             \parsep\parsepii
             \itemsep\parsep}

\def\@listiii{\leftmargin\leftmarginiii
              \labelwidth\leftmarginiii
              \advance\labelwidth-\labelsep
              \topsep\topsepiii
              \parsep\z@
              \partopsep \p@ \@plus\z@ \@minus\p@
              \itemsep\topsep}

\def\@listiv{\leftmargin\leftmarginiv
             \labelwidth\leftmarginiv
             \advance\labelwidth-\labelsep}

\def\@listv{\leftmargin\leftmarginv
            \labelwidth\leftmarginv
            \advance\labelwidth-\labelsep}

\def\@listvi{\leftmargin\leftmarginvi
             \labelwidth\leftmarginvi
             \advance\labelwidth-\labelsep}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\firmlist}
% \begin{macro}{\tightlist}
% These two macros can be used at the start of a list environment to
% reduce the vertical gaps. |\tightlist| removes all interior spaces
% while |\firmlist| only removes some.
% \changes{v0.3}{2001/07/09}{Added \cs{firmlist} and reimplemented the
% original \cs{tightlist}}
%    \begin{macrocode}
\newcommand{\firmlist}{%
  \setlength{\itemsep}{0.5\itemsep}\setlength{\parskip}{0.5\parskip}}
\newcommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%    The space before and after a |trivlist| environment is controlled
% by the |\topsep| and |\partopsep| skips. There are several environments,
% such as |center|, that are defined as a |trivlist|.
% 
% \begin{macro}{\m@msavetopsep}
% \begin{macro}{\m@msavepartopsep}
% \begin{macro}{\savetrivseps}
% \begin{macro}{\restoretrivseps}
% Two skips to store the |\topsep| and |\partopsep| values and a means
% of setting them and restoring them.
% \changes{v1.6}{2004/01/28}{Added methods for adjusting the space around
%                            a trivlist}
%    \begin{macrocode}
\newskip\m@msavetopsep
\newskip\m@msavepartopsep
\newcommand*{\savetrivseps}{%
  \m@msavetopsep\topsep
  \m@msavepartopsep\partopsep}
\newcommand*{\restoretrivseps}{%
  \topsep\m@msavetopsep
  \partopsep\m@msavepartopsep}
%    \end{macrocode}
% Save the initial |\topsep| and |\partopsep| values.
%    \begin{macrocode}
\savetrivseps

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\zerotrivseps}
%  A macro to zero |\topsep| and |\partopsep|.
%    \begin{macrocode}
\newcommand*{\zerotrivseps}{%
  \topsep\z@
  \partopsep\z@}

%    \end{macrocode}
% \end{macro}
%
%
% \subsection{Enumerate}
%
%
%    The enumerate environment uses four counters: \Lcount{enumi},
%    \Lcount{enumii}, \Lcount{enumiii} and \Lcount{enumiv}, where
%    \Lcount{enumN} controls the numbering of the Nth level
%    enumeration.
%
% \begin{macro}{\theenumi}
% \begin{macro}{\theenumii}
% \begin{macro}{\theenumiii}
% \begin{macro}{\theenumiv}
%    The counters are already defined in \file{latex.dtx}, but their
%    representation is changed here.
%
%    \begin{macrocode}
\renewcommand{\theenumi}{\@arabic\c@enumi}
\renewcommand{\theenumii}{\@alph\c@enumii}
\renewcommand{\theenumiii}{\@roman\c@enumiii}
\renewcommand{\theenumiv}{\@Alph\c@enumiv}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\labelenumi}
% \begin{macro}{\labelenumii}
% \begin{macro}{\labelenumiii}
% \begin{macro}{\labelenumiv}
%    The label for each item is generated by the commands\\
%    |\labelenumi| \ldots\ |\labelenumiv|.
%    \begin{macrocode}
\newcommand{\labelenumi}{\theenumi.}
\newcommand{\labelenumii}{\theenumii)}
\newcommand{\labelenumiii}{\theenumiii.}
\newcommand{\labelenumiv}{\theenumiv.}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\p@enumii}
% \begin{macro}{\p@enumiii}
% \begin{macro}{\p@enumiv}
%    The expansion of |\p@enumN||\theenumN| defines the output of a
%    |\ref| command when referencing an item of the Nth level of an
%    enumerated list.
%    \begin{macrocode}
\renewcommand{\p@enumii}{\theenumi}
\renewcommand{\p@enumiii}{\theenumi(\theenumii)}
\renewcommand{\p@enumiv}{\p@enumiii\theenumiii}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
%    The following is taken directly from David Carlisle's 
% \Lpack{enumerate} package.
%
% START OF DAVID CARLISLE'S CODE AND COMMENTARY
%
% This package gives the enumerate environment an optional argument
% which determines the style in which the counter is printed.
%
% An occurence of one of the tokens |A a I i| or |1| produces the value
% of the counter printed with (respectively) |\Alph \alph \Roman \roman|
% or |\arabic|.
%
% These letters may be surrounded by any strings involving any other
% \TeX\ expressions, however the tokens |A a I i 1| must be inside a
% |{ }| group if they are not to be taken as special.
%
%
% \begin{macro}{\@enlab}
% Internal token register used to build up the label command from the
% optional argument.
%    \begin{macrocode}
\newtoks\@enLab
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@enQmark}
% This just expands to a `?'. |\ref| will produce this, if no counter
% is printed.
%    \begin{macrocode}
\def\@enQmark{?}
%    \end{macrocode}
% \end{macro}
%
% The next four macros build up the command that will print the item
% label. They each gobble one token or group from the optional argument,
% and add corresponding tokens to the register |\@enLab|. They each end
% with a call to |\@enloop|, which starts the processing of the next
% token.
% \begin{macro}{\@enLabel}
% Add the counter to the label. |#2| will be one of the `special'
% tokens |A a I i 1|, and is thrown away. |#1| will be a command
% like |\Roman|.
%    \begin{macrocode}
\def\@enLabel#1#2{%
  \edef\@enThe{\noexpand#1{\@enumctr}}%
  \@enLab\expandafter{\the\@enLab\csname the\@enumctr\endcsname}%
  \@enloop}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@enSpace}
% \begin{macro}{\@enSp@ce}
% Add a space to the label. The tricky bit is to gobble the space token,
% as you can not do this with a macro argument.
%    \begin{macrocode}
\def\@enSpace{\afterassignment\@enSp@ce\let\@memtempa= }
\def\@enSp@ce{\@enLab\expandafter{\the\@enLab\space}\@enloop}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@enGroup}
% Add a |{ }| group to the label.
%    \begin{macrocode}
\def\@enGroup#1{\@enLab\expandafter{\the\@enLab{#1}}\@enloop}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@enOther}
% Add anything else to the label
%    \begin{macrocode}
\def\@enOther#1{\@enLab\expandafter{\the\@enLab#1}\@enloop}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@enloop}
% \begin{macro}{\@enloop@}
% The body of the main loop.
% Eating tokens this way instead of using |\@tfor| lets you see
% spaces and {\bf all} braces. |\@tfor| would treat {\tt a} and
% |{a}| as  special, but not |{{a}}|.
%    \begin{macrocode}
\def\@enloop{\futurelet\@entemp\@enloop@}
%    \end{macrocode}
%    \begin{macrocode}
\def\@enloop@{%
  \ifx A\@entemp         \def\@memtempa{\@enLabel\Alph  }\else
  \ifx a\@entemp         \def\@memtempa{\@enLabel\alph  }\else
  \ifx i\@entemp         \def\@memtempa{\@enLabel\roman }\else
  \ifx I\@entemp         \def\@memtempa{\@enLabel\Roman }\else
  \ifx 1\@entemp         \def\@memtempa{\@enLabel\arabic}\else
  \ifx \@sptoken\@entemp \let\@memtempa\@enSpace         \else
  \ifx \bgroup\@entemp   \let\@memtempa\@enGroup         \else
  \ifx \@enum@\@entemp   \let\@memtempa\@gobble          \else
                         \let\@memtempa\@enOther
%    \end{macrocode}
%    Hook for possible extensions
%    \begin{macrocode}
                         \@enhook
%    \end{macrocode}
%
%    \begin{macrocode}
             \fi\fi\fi\fi\fi\fi\fi\fi
%    \end{macrocode}
% \end{macro}
% Process the current token, then look at the next.
%    \begin{macrocode}
  \@memtempa}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@enhook}
% Hook for possible extensions.
% Some packages may want to extend the number of special characters
% that are associated with counter representations. This feature
% was requested to enable Russian alphabetic counting, but here
% I give an example of a footnote symbol counter, triggered by |*|.
%
% To enable a new counter type based on a letter, you just need
% to add a new |\ifx| clause by analogy with the code above.
% So for example to make |*| trigger footnote symbol counting.
% a package should do the following.
%
% Initialise the hook, in case the package is loaded before
% \textsf{enumerate}.
%\begin{verbatim}
% \providecommand\@enhook{}
%\end{verbatim}
%
% Add to the hook a new |\ifx| clause that associates |*| with the
% |\fnsymbol| counter command.
%\begin{verbatim}
% \g@addto@macro\@enhook{%
%   \ifx *\@entemp
%     \def\@memtempa{\@enLabel\fnsymbol}%
%   \fi}
%\end{verbatim}
% This code sequence should work whether it is loaded before or after
% this enumerate package. Any number of new counter types may be added
% in this way.
%
% At this point we just need initialise the hook, taking care not
% to over write any definitions another package may already have added.
% (PRW: as this is now in a class, it can be defined instead of
% |\provide|d).
%    \begin{macrocode}
%% \providecommand\@enhook{}
  \newcommand\@enhook{}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\enumerate}
% The new {\tt enumerate} environment. This is the first half of the
% original enumerate environment. If there is an optional argument, call
% |\@@enum@| to define the label commands, otherwise call
% |\@enum@| which is the second half of the original definition.
%    \begin{macrocode}
\def\enumerate{%
  \ifnum \@enumdepth >3 \@toodeep\else
      \advance\@enumdepth \@ne
      \edef\@enumctr{enum\romannumeral\the\@enumdepth}\fi
  \@ifnextchar[{\@@enum@}{\@enum@}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@enum@}
% Handle the optional argument..
%    \begin{macrocode}
\def\@@enum@[#1]{%
%    \end{macrocode}
% Initialise the loop which will break apart the optional argument.
% The command to print the label is built up in |\@enlab|.
% |\@enThe| will be used to define |\theenum|\,$n$.
%    \begin{macrocode}
  \@enLab{}\let\@enThe\@enQmark
%    \end{macrocode}
% The |\@enum@| below is never expanded, it is used to detect the end
% of the token list.
%    \begin{macrocode}
  \@enloop#1\@enum@
%    \end{macrocode}
% Issue a warning if we did not find one of the `special' tokens.
%    \begin{macrocode}
  \ifx\@enThe\@enQmark\@warning{The counter will not be printed.%
   ^^J\space\@spaces\@spaces\@spaces The label is: \the\@enLab}\fi
%    \end{macrocode}
% Define |\labelenum|$\,n$ and |\theenum|$\,n$.
%    \begin{macrocode}
  \expandafter\edef\csname label\@enumctr\endcsname{\the\@enLab}%
  \expandafter\let\csname the\@enumctr\endcsname\@enThe
%    \end{macrocode}
% Set the counter to 7 so that we get the width of `vii' if roman
% numbering is in force then set |\leftmargin|$\,n$. to the width of the
% label plus |\labelsep|.
%    \begin{macrocode}
  \csname c@\@enumctr\endcsname7
  \expandafter\settowidth
            \csname leftmargin\romannumeral\@enumdepth\endcsname
            {\the\@enLab\hspace{\labelsep}}%
%    \end{macrocode}
% Finally call |\@enum@| which is the second half of the original
% definition.
%    \begin{macrocode}
  \@enum@}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@enum@}
% All the list parameters have now been defined, so call |\list|. This
% is taken straight from the original definition of |\enumerate|.
%    \begin{macrocode}
\def\@enum@{\list{\csname label\@enumctr\endcsname}%
           {\usecounter{\@enumctr}\def\makelabel##1{\hss\llap{##1}}}}

%    \end{macrocode}
% \end{macro}
%
% END OF DAVID CARLISLE'S CODE AND COMMENTARY
%
%
%
% \subsection{Itemize}
%
%
% \begin{macro}{\labelitemi}
% \begin{macro}{\labelitemii}
% \begin{macro}{\labelitemiii}
% \begin{macro}{\labelitemiii}
%    Itemization is controlled by the commands: |\labelitemi|,
%    |\labelitemii|, etc., which define
%    the labels of the various itemization levels: the symbols used are:
%    bullet (\textbullet), bold en-dash (\textbf{\textendash}),
% centered asterisk (\textasteriskcentered), and centered dot
% (\textperiodcentered).
%
%    \begin{macrocode}
\newcommand{\labelitemi}{\textbullet}
\newcommand{\labelitemii}{\normalfont\bfseries \textendash}
\newcommand{\labelitemiii}{\textasteriskcentered}
\newcommand{\labelitemiv}{\textperiodcentered}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% It seems like a reasonable idea to give the |itemize| environment
% an optional argument to match |enumerate|. Fortunately this seems
% to be much simpler and I might even be able to work it out for myself.
%
% \begin{environment}{itemize}
% This is a hack at the kernel code for |itemize|.
%    \begin{macrocode}
\renewcommand{\itemize}[1][\@empty]{%
  \ifnum \@itemdepth >\thr@@\@toodeep\else
    \advance\@itemdepth\@ne
    \ifx \@empty #1\else % optional argument
      \@namedef{labelitem\romannumeral\the\@itemdepth}{#1}%
    \fi
    \edef\@itemitem{labelitem\romannumeral\the\@itemdepth}%
    \expandafter
    \list
      \csname\@itemitem\endcsname
       {\def\makelabel##1{\hss\llap{##1}}}%
  \fi}
\let\enditemize =\endlist

%    \end{macrocode}
% \end{environment}
%
% \subsection{Description}
%
% The class defines two description environment, the standard one and a `block'
% one.
%
% \changes{v1.2}{2002/07/27}{Now two `description' environments.}
%
% \begin{environment}{description}
%    The description environment is defined here -- while the default itemize
%    and enumerate environments are defined in \file{latex.dtx}.
%
% \changes{v1.2}{2002/07/27}{The \texttt{description} environment now matches
%                            the standard layout.}
%    \begin{macrocode}
\newenvironment{description}%
               {\list{}{\labelwidth\z@ \itemindent-\leftmargin
                        \let\makelabel\descriptionlabel}}%
               {\endlist}
%    \end{macrocode}
% \end{environment}
%
% \begin{macro}{\descriptionlabel}
%    To change the formatting of the label, you must redefine
%    |\descriptionlabel|. 
%
%    \begin{macrocode}
\newcommand*{\descriptionlabel}[1]{\hspace\labelsep
                                   \normalfont\bfseries #1}
%    \end{macrocode}
% \end{macro}
%
% \begin{environment}{blockdescription}
%    The `block' description environment.
%
% \changes{v1.2}{2002/07/27}{Renamed original \texttt{description} environment
%                            to \texttt{blockdescription}.}
%    \begin{macrocode}
\newenvironment{blockdescription}%
               {\list{}{\labelwidth\z@ \itemindent 0.5em \labelsep 0.5em
                        \let\makelabel\blockdescriptionlabel}}%
               {\endlist}
%    \end{macrocode}
% \end{environment}
%
% \begin{macro}{\blockdescriptionlabel}
%    In order to change the formatting of the label, you must redefine 
% the macro \cs{blockdescriptionlabel}. 
%
%    \begin{macrocode}
\newcommand*{\blockdescriptionlabel}[1]{%%% \hspace\labelsep
                                   \normalfont\bfseries #1}
%    \end{macrocode}
% \end{macro}
%
%
% \subsection{Quotation}
%
% \begin{environment}{quotation}
%   The quotation environment is defined by making clever use of
%   the list environment's parameters. The lines in the environment
%   are set smaller than |\textwidth|. The first line of a paragraph
%   inside this environment is indented.
%
%    \begin{macrocode}
\newenvironment{quotation}%
               {\list{}{\listparindent 1.5em%
                        \itemindent    \listparindent
                        \rightmargin   \leftmargin
                        \parsep        \z@ \@plus\p@}%
                \item[]}%
               {\endlist}
%    \end{macrocode}
% \end{environment}
%
% \subsection{Quote}
%
%
% \begin{environment}{quote}
%   The quote environment is like the quotation environment except
%   that paragraphs are not indented.
%
%    \begin{macrocode}
\newenvironment{quote}%
               {\list{}{\rightmargin\leftmargin}%
                \item[]}%
               {\endlist}
%    \end{macrocode}
% \end{environment}
%
% \subsection{Theorem}
%
%    This document class does not define it's own theorem environments,
%    the defaults, supplied by \file{latex.dtx} are available.
%
% \subsection{Listing of symbols and abbreviations}
%
%    Another element is the listing of symbols
%    and abbreviations. 
%
% \begin{environment}{symbols}
% \begin{macro}{\symbollabel}
%    \begin{macrocode}
\newcommand{\symbollabel}[1]{{#1 \hfill}}
%    \end{macrocode}
% \end{macro}
%    \begin{macrocode}
\newenvironment{symbols}{\list{}%
    {\itemindent 0em \leftmargin 8em
     \labelsep 1em \labelwidth 5em
     \let\makelabel\symbollabel}}%
    {\endlist}
%    \end{macrocode}
% \end{environment}
%
% \begin{macro}{\symboldef}
%    Within a |symbols| environment the command 
%    |\symboldef{|\meta{symbol}|}{|\meta{meaning}|}|
%    is used to specify and explain each symbol or abbreviation.
%    \begin{macrocode}
\newcommand{\symboldef}[2]{\item[#1] #2}
%    \end{macrocode}
% \end{macro}
%
%
% \section{Verse}
%
% The class provides a more flexible |verse| environment than the 
% standard classes. This is based on the \Lpack{verse} 
% package~\cite{VERSE}.
% \changes{v1.0}{2001/10/30}{Complete rewrite of verse typesetting}
%
% \subsection{Environments}
%
%
% \begin{macro}{vslineno}
% We need counters for stanza and poem lines.
% \changes{v1.1}{2002/03/10}{Added poemline and modulo@vs counters}
%    \begin{macrocode}
\newcounter{vslineno}
\newcounter{poemline}
\newcounter{modulo@vs}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\poemlines}
% |\poemlines{|\meta{nth}|}| specifies that every \meta{nth} line of
% a poem is to be numbered. 
% \changes{v1.1}{2002/03/10}{Added \cs{poemlines}}
% \changes{v1.2}{2002/07/27}{\cs{poemlines} now just calls \cs{linenumberfrequency}}
%    \begin{macrocode}
\newcommand{\poemlines}[1]{\linenumberfrequency{#1}%
  \@memwarn{Use \string\linenumberfrequency\space
                        instead of \string\poemlines}%
}

%    \end{macrocode}
% \end{macro}
%
% \changes{v1.1}{2002/03/24}{Added \cs{getmodulo@vs}}
% \changes{v1.2}{2002/07/27}{Using \cs{getthelinenumber} instead of \cs{getmodulo@vs}}
%
% \begin{macro}{\ifaltindent}
% This should be set TRUE for indenting alternate lines.
%    \begin{macrocode}
\newif\ifaltindent
  \altindentfalse
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ifpattern}
% This should be set TRUE for indenting lines according to a pattern.
%    \begin{macrocode}
\newif\ifpattern
  \patternfalse
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ifstarpattern}
% This should be set TRUE for indenting lines according in a |patverse*|
% environment.
% \changes{v1.1}{2002/03/8}{Added \cs{ifstarpattern}}
%    \begin{macrocode}
\newif\ifstarpattern
  \starpatternfalse

%    \end{macrocode}
% \end{macro}
%
%
%
%
% \begin{macro}{\vleftskip}
% \begin{macro}{\vrightskip}
% Skips to the left and right of a line of verse.
% \changes{v1.1}{2002/03/24}{Added \cs{vleftskip} and \cs{vrightskip}}
% \changes{v1.2}{2002/07/27}{Changed defaults for \cs{vleftskip} and \cs{vrightskip}}
%    \begin{macrocode}
\newlength{\vleftskip}
  \setlength{\vleftskip}{3em}
\newlength{\vrightskip}
  \setlength{\vrightskip}{1em}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\stanzaskip}
% Skip between stanzas.
% \changes{v1.2}{2002/07/07}{Added \cs{stanzaskip}}
%    \begin{macrocode}
\newlength{\stanzaskip}
  \setlength{\stanzaskip}{\onelineskip}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\flagverse}
% |\flagverse{|\meta{flag}|}| inserts \meta{flag} at the left (of a line).
% \changes{v1.1}{2002/03/24}{Added \cs{flagverse}}
%    \begin{macrocode}
\newcommand{\flagverse}[1]{%
  \hskip-\vleftskip\llap{#1}\hskip\vleftskip\ignorespaces}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\versewidth}
% The length |\versewidth| is a convenience length for the user.
%    \begin{macrocode}
\newlength{\versewidth}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\vgap}
% \begin{macro}{\vin}
% \begin{macro}{\vindent}
% The length |\vgap| is used as the basis for spacing. |\vin| makes
% a horizontal space of |\vgap| and |\vindent| is the indentation
% of wrapped lines.
%    \begin{macrocode}
\newlength{\vgap} \setlength{\vgap}{1.5em}
\newcommand{\vin}{\hspace*{\vgap}}
\newlength{\vindent} \setlength{\vindent}{2\vgap}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\vinphantom}
% Macro to leave blank space coresponding to a string.
% \changes{v1.4}{2003/02/27}{Added \cs{vinphantom} (patch 1.1)}
%    \begin{macrocode}
\newcommand{\vinphantom}[1]{\leavevmode\phantom{#1}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\vleftofline}
% Macro to insert something immediately to the left of the start 
% of the line.
% \changes{v1.618}{2005/09/14}{Added \cs{vleftofline}}
%    \begin{macrocode}
\newcommand*{\vleftofline}[1]{\leavevmode\llap{#1}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\vleftmargin}
% Length to adjust the default left margin within a verse environment.
% \changes{v1.618}{2005/09/18}{Added \cs{vleftmargin}}
%    \begin{macrocode}
\newdimen\vleftmargin
  \vleftmargin=\leftmargini

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\verselinebreak}
% Break a verse line by inserting |\newline|. 
% \changes{v1.1}{2002/03/28}{Modified \cs{verselinebreak}}
%    \begin{macrocode}
\newcommand{\verselinebreak}[1][\z@]{\newline\hspace*{#1}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\incr@vsline}
% Increment the line counters.
% \changes{v1.1}{2002/03/24}{Added poemline counter to \cs{incr@vsline}}
%    \begin{macrocode}
\newcommand{\incr@vsline}{%
  \refstepcounter{poemline}%
  \stepcounter{vslineno}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@vsifbang}
% Like the kernel |\@ifstar| except it looks for an exclamation mark!
% \changes{v1.1}{2002/03/24}{Added \cs{@vsifbang}}
%    \begin{macrocode}
\newcommand{\@vsifbang}[1]{\@ifnextchar !{\@firstoftwo{#1}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@vsifgt}
% Like the kernel |\@ifstar| except it looks for a |>| character.
% \changes{v1.1}{2002/03/28}{Added \cs{@vsifgt}}
%    \begin{macrocode}
\newcommand{\@vsifgt}[1]{\@ifnextchar >{\@firstoftwo{#1}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@vscentercr}
% This puts the poem line number in the margin, increments
% the line numbers, and then deals with the options. It is based
% on the kernel |\@centercr|.
% This has to handle various forms of the |\\| command: |\\|,
% |\\*|, |\\!|, and |\\>|, together with an optional length argument.
% \changes{v1.1}{2002/03/28}{Major rewrite of \cs{@vscentercr} and friends}
% \changes{v1.2}{2002/07/27}{Changed \cs{@vscentercr} to use general
%                linenumbering}
%    \begin{macrocode}
\newcommand{\@vscentercr}{%
  \ifhmode \unskip\else \@nolnerr\fi
  \hfill\rlap{\kern\vrightskip\kern\rightmargin%
              \vlvnumfont\getthelinenumber{poemline}}%
%    \end{macrocode}
% For |>| call |\verselinebreak| to process it.
%    \begin{macrocode}
  \@vsifgt{\verselinebreak}{%
  \incr@vsline
%    \end{macrocode}
% If the call is |\\*...| call |\@vsxcentercr| to handle the |*...|.
% If the call is |\\!|, do nothing.
% If the call is |\\![...]|, call |\@vsicentercr| to handle the |[...]|.
% Otherwise, call |\@vsxcentercr|.
%    \begin{macrocode}
  \par\@ifstar{\nobreak\@vsxcentercr}{%
%    \end{macrocode}
%    \begin{macrocode}
    \@vsifbang{\@ifnextchar[ {\@vsicentercr}{}}{\@vsxcentercr}}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@vsxcentercr}
% Processes |\\*|, and either calls |\@vsicentercr| to handle a |[length]|,
% or |\start@vsline|.
% \changes{v1.1}{2002/03/24}{Added \cs{@vsxcentercr}}
%    \begin{macrocode}
\newcommand{\@vsxcentercr}{\addvspace{-\parskip}%
  \@ifnextchar[ {\@vsicentercr}{\start@vsline}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@vsicentercr}
% Processes |(\\...)[length]| and then calls |\start@vsline|.
% \changes{v1.1}{2002/03/24}{Added \cs{@vsicentercr}}
%    \begin{macrocode}
\def\@vsicentercr[#1]{\vskip #1\ignorespaces \start@vsline}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\start@vsline}
% This is called at the start of every verse line except the first.
% \changes{v1.1}{2002/03/28}{Added \cs{ifstarpattern} to \cs{start@vsline}}
% \changes{v1.61}{2004/03/21}{Used counter for vslineno, not \cs{thevslineno}
%                in \cs{start@vsline}}
%    \begin{macrocode}
\newcommand{\start@vsline}{%
  \ifaltindent\ifodd\c@vslineno\else\vin\fi\fi%
  \ifpattern\get@vsindent\fi%
  \ifstarpattern\getstar@vsindent\fi}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\theHpoemline}
% For the \Lpack{hyperref} package need a way of distinguishing
% lines of a poem. See the thread \textit{PDFTEX/Hyperef hates memoir verse
% environment?} on \ctt{} October 2002.
% \changes{v1.3}{2002/11/14}{Added verse counter and \cs{theHpoemline}}
%    \begin{macrocode}
\newcounter{verse}
\setcounter{verse}{0}
\newcommand{\theHpoemline}{\theverse.\thepoemline}

%    \end{macrocode}
% \end{macro}
% 
% \begin{environment}{verse}
% The extended |verse| environment. It sets the verse line counter,
% then defines the particular list environment adjusting the margins
% to center according to the length parameter. If the length parameter
% is at least the |\linewidth| then the `centering' defaults to
% the original |verse| layout.
% \changes{v1.1}{2001/08/03}{Initialise the line counter to 1, not 0, in
%                all cases}
% \changes{v1.1}{2002/03/28}{Changes to verse environment for line numbering}
% \changes{v1.3}{2002/11/14}{Changes to verse environment for hyperref}
% \changes{v1.4}{2002/02/27}{Let verses wider than the \cs{linewidth}
%                            take the full width (patch(1.1)}
% \changes{v1.618}{2005/09/03}{Fix vertical spacing in the verse environment (mempatch v3.3)}
%    \begin{macrocode}
\newenvironment{verse}[1][\linewidth]{%
  \refstepcounter{verse}%
  \setcounter{poemline}{0}\refstepcounter{poemline}%
  \setcounter{vslineno}{1}%
  \let\\=\@vscentercr
  \list{}{\itemsep     \z@
          \itemindent  -\vindent
          \listparindent\itemindent
          \leftmargin   \vleftmargin
          \parsep       \stanzaskip
          \ifdim #1<\linewidth%   %% short line
            \rightmargin        \z@
            \leftmargin         \linewidth
            \advance\leftmargin -#1\relax
            \advance\leftmargin -0.5\leftmargin
            \advance\leftmargin \vindent
          \else
            \ifdim #1>\linewidth%  %% long line
              \rightmargin \z@
              \leftmargin  \vindent
            \else%                   %% default
              \rightmargin \leftmargin
              \advance\leftmargin \vindent
            \fi
          \fi}
  \item[]}{\endlist}

%    \end{macrocode}
% \end{environment}
%
% \begin{environment}{altverse}
% This sets |\altindenttrue| (afterwards false) and initialises the line counter.
% \changes{v1.1}{2002/03/28}{Added \cs{starpatternfalse} to altverse}
%    \begin{macrocode}
\newenvironment{altverse}%
  {\starpatternfalse\patternfalse\altindenttrue
   \setcounter{vslineno}{1}}%
  {\altindentfalse}

%    \end{macrocode}
% \end{environment}
%
%
% \subsection{Patterns}
%
%    The pattern code is based on the idea of converting a string of
% digits to an array of digits, and then being able to access the
% digit at a particular position in the array.
%
% \begin{macro}{\@nameedef}
% A shorthand for using |\protected@edef|.
%    \begin{macrocode}
\newcommand{\@nameedef}[1]{
  \expandafter\protected@edef\csname #1\endcsname}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\ifbounderror}
% A flag set TRUE if an attempt is made to access an array element outside
% the array limits.
%    \begin{macrocode}
\newif\ifbounderror
  \bounderrorfalse
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ifinteger}
%  A flag to indicate if a `number' is an integer (TRUE) or not (FALSE).
%    \begin{macrocode}
\newif\ifinteger

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\c@chrsinstr}
% A counter for the number of characters.
%    \begin{macrocode}
\newcounter{chrsinstr}  % CHARactersINSTRing

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\newarray}
% |\newarray{|\meta{arrayname}|}{|\meta{low}|}{|\meta{high}|}| defines
% an array called \meta{arrayname} (no backslash e.g. |MyArray|), 
% with low and high limts \meta{low} and \meta{high}.
%    \begin{macrocode}
\newcommand{\newarray}[3]{%
  \@nameedef{#1-low}{#2}%
  \@nameedef{#1-high}{#3}%
  \ifnum #3<#2
    \@memerror{Limits for array #1 are in reverse order}{\@ehc}%
  \fi
}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\stringtoarray}
% |\stringtoarray{|\meta{arrayname}|}{|\meta{string}|}| puts each character
% from \meta{string} sequentially into the \meta{arrayname} array, starting
% with \meta{low} = 1. It checks for an empty \meta{string} and handles that
% specially.
%    \begin{macrocode}
\newcommand{\stringtoarray}[2]{%
  \def\@vsarrayname{#1}%
  \protected@edef\the@vsstring{#2}%
  \newarray{\@vsarrayname}{1}{1}%
  \@ifmtarg{#2}{%
    \c@chrsinstr \z@
    \@namedef{\@vsarrayname-1}{}
  }{%
    \c@chrsinstr \@ne
    \expandafter\@vsstringtoarray \the@vsstring\@vsend
  }
}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@vsstringtoarray}
% Recursively adds characters to the array |\@vsarrayname|, incrementing
% the array's high limit.
%    \begin{macrocode}
\def\@vsstringtoarray #1#2\@vsend{%
  \@namedef{\@vsarrayname-\the\c@chrsinstr}{#1}
  \@nameedef{\@vsarrayname-high}{\the\c@chrsinstr}
  \@ifmtarg{#2}{%
    \def\@vsinext{}%
  }{%
    \advance\c@chrsinstr \@ne
    \def\@vsinext{%
      \@vsstringtoarray #2\@vsend%
    }%
  }
  \@vsinext
}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\setarrayelement}
% |\setarrayelement{|\meta{arrayname}|}{|\meta{index}|}{|\meta{value}|}| sets
% the \meta{arrayname} array's element at \meta{index} to \meta{value}.
%    \begin{macrocode}
\newcommand{\setarrayelement}[3]{%
  \checkarrayindex{#1}{#2}%
  \@nameedef{#1-#2}{#3}%
}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\getarrayelement}
% |\getarrayelement{|\meta{arrayname}|}{|\meta{index}|}{|\meta{value}|}|
% defines the parameterless macro \meta{value} (e.g., |\result|) to be
% the value at \meta{index} in the \meta{arrayname} array.
%    \begin{macrocode}
\newcommand{\getarrayelement}[3]{%
  \checkarrayindex{#1}{#2}%
  \protected@edef#3{\@nameuse{#1-#2}}%
}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\checkarrayindex}
% |\checkarrayindex{|\meta{arrayname}|}{|\meta{index}|}| checks that
% the \meta{index} of the \meta{arrayname} array is valid. |\ifbounderror|
% is set FALSE if everything is OK, otherwise it is set TRUE.
%    \begin{macrocode}
\newcommand{\checkarrayindex}[2]{%
  \bounderrorfalse
  \expandafter\ifx\csname #1-low\endcsname\relax%
    \ifpattern\else
      \@memerror{No array called #1}{\@ehc}%
    \fi
    \bounderrortrue
  \fi
  \ifnum #2<\@nameuse{#1-low}\relax%
    \ifpattern\else
      \@memerror{Index #2 outside limits for array #1}{\@ehc}%
    \fi
    \bounderrortrue
  \fi
  \ifnum #2>\@nameuse{#1-high}\relax%
    \ifpattern\else
      \@memerror{Index #2 outside limits for array #1}{\@ehc}%
    \fi
    \bounderrortrue
  \fi
}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\arraytostring}
% |\arraytostring{|\meta{arrayname}|}{|\meta{string}|}| converts the
% characters in the \meta{arrayname} array into the parameterless macro
% \meta{string} (e.g., |\MyString|).
%    \begin{macrocode}
\newcommand{\arraytostring}[2]{%
  \def#2{}%
  \c@chrsinstr = \@nameuse{#1-low}%
  \@vsarraytostring{#1}{#2}%
}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@vsarraytostring}
% |\@vsarraytostring{|\meta{arrayname}|}{|\meta{string}|}| recursively
% adds the (character) elements from \meta{arrayname} to \meta{string}.
%    \begin{macrocode}
\newcommand{\@vsarraytostring}[2]{%
  \ifnum\c@chrsinstr>\@nameuse{#1-high}\else
    \protected@edef#2{#2\@nameuse{#1-\thechrsinstr}}%
    \advance\c@chrsinstr\@ne%
    \@vsarraytostring{#1}{#2}%
  \fi%
}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\checkifinteger}
% |\checkifinteger{|\meta{num}|}| checks if \meta{num} is an integer.
% If it is, then |\ifinteger| is set TRUE, otherwise it is set FALSE.
% (Code based on Donald Arseneau's \Lpack{cite} package).
%    \begin{macrocode}
\newcommand{\checkifinteger}[1]{%
  \protected@edef\@vsa{#1}%
  \ifcat _\ifnum9<1\gobm{#1} _\else A\fi
    \integertrue%
  \else
    \integerfalse%
  \fi%
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\gobm}
% |\gobm{|\meta{num}|}| is defined as \meta{num}. It could be defined as: \\
% |\newcommand{\gobm}[1]{\ifx-#1\expandafter\gobm\else#1\fi}| \\
% which would remove a leading minus sign (hyphen) from its argument
% (|gobm| = gobble minus sign).
% (Code from a posting to \ctt{} by Donald Arseneau on 1997/07/21).
%    \begin{macrocode}
\newcommand{\gobm}[1]{#1}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\indentpattern}
% |\indentpattern{|\meta{digits}|}| stores \meta{digits} for use as
% a verse indentation pattern.
%    \begin{macrocode}
\newcommand{\indentpattern}[1]{%
  \stringtoarray{Array@vs}{#1}}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\get@vsindent}
% |\get@vsindent| gets the indent pattern digit for the |\thevslineno|,
% then uses this to specify the line indentation as |digit*\vgap|.
% \changes{v1.61}{2004/03/21}{Changed \cs{get@vsindent} to use counter
% value number instead of \cs{thevslineno}}
%    \begin{macrocode}
\newcommand{\get@vsindent}{%
  \getarrayelement{Array@vs}{\number\value{vslineno}}{\@vspat}%
  \ifbounderror
    \arraytostring{Array@vs}{\@vsp@t}%
    \@memwarn{%
      Index `\thevslineno' for pattern `\@vsp@t' is out of bounds}%
    \def\@vspat{0}%
  \else
    \checkifinteger{\@vspat}%
    \ifinteger\else
      \arraytostring{Array@vs}{\@vsp@t}%
      \@memwarn{%
       `\@vspat' at index `\thevslineno' in pattern `\@vsp@t' 
       is not a digit}%
      \def\@vspat{0}%
    \fi
  \fi
  \ifcase\@vspat\else\hspace*{\@vspat\vgap}\fi}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\getstar@vsindent}
% |\getstar@vsindent| gets the indent pattern digit for the |patverse*|
% environment,
% then uses this to specify the line indentation as |digit*\vgap|.
% It lets the pattern repeat by resetting the |vslineno| counter.
% \changes{v1.1}{2002/03/28}{Added \cs{getstar@vsindent}}
%    \begin{macrocode}
\newcommand{\getstar@vsindent}{%
  \expandafter\ifx\csname Array@vs-high\endcsname\relax
    \@memerror{A pattern has not been specified}{\@ehc}
  \else
    \ifnum\c@vslineno>\@nameuse{Array@vs-high}%
      \setcounter{vslineno}{1}%
     \fi
     \get@vsindent
  \fi}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{environment}{patverse}
% The environment for setting verse line indents according to a pattern.
% It starts by setting |\ifpattern| TRUE, any other flags to FALSE,
% and initialises the line number. It ends by setting |\ifpattern| FALSE.
% \changes{v1.1}{2002/03/28}{Added \cs{starpatternfalse} to patverse}
%    \begin{macrocode}
\newenvironment{patverse}%
  {\starpatternfalse\patterntrue\altindentfalse
   \setcounter{vslineno}{1}}%
  {\patternfalse}

%    \end{macrocode}
% \end{environment}
%
% \begin{environment}{patverse*}
% The environment for setting verse line indents according to a repeating
% pattern.
% It starts by setting |\ifstarpattern| TRUE, any other flags to FALSE,
% and initialises the line number. It ends by setting |\ifstarpattern| FALSE.
% \changes{v1.1}{2002/03/28}{Added patverse* environment}
%    \begin{macrocode}
\newenvironment{patverse*}%
  {\starpatterntrue\patternfalse\altindentfalse
   \setcounter{vslineno}{1}}%
  {\starpatternfalse}

%    \end{macrocode}
% \end{environment}
%
%
% \subsection{Titles}
%
% \begin{macro}{\poemtitle}
% Typeset a poem title (like |\section| or other). The actual work
% is done by |\@vsptitle| (plain) or |\@vssptitle| (starred).
%    \begin{macrocode}
\newcommand{\poemtitle}{\par%
  \secdef\@vsptitle\@vssptitle}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\poemtoc}
% The kind of entry |\poemtitle| is to make in the ToC.
%    \begin{macrocode}
\newcommand{\poemtoc}{section}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\mempoeminfo}
% \begin{macro}{\mempoemstarinfo}
% \verb?\mempoeminfo{title}? \\
% \verb?\mempoemstarinfo{title}? \\
%    \begin{macrocode}
\newcommand*{\mempoeminfo}[1]{}
\newcommand*{\mempoemstarinfo}[1]{}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@vsptitle}
% Typeset a \cs{poemtitle}.
% \changes{v1.3}{2002/11/14}{Added \cs{phantomsection} to \cs{@vsptitle}}
% \changes{v1.4}{2003/11/16}{Added \cs{M@gettitle} to \cs{@vsptitle}}
%    \begin{macrocode}
\long\def\@vsptitle[#1]#2{%
  \phantomsection
  \addcontentsline{toc}{\poemtoc}{#1}%
  \M@gettitle{#1}%
  \mempoeminfo{#1}%
  \poemtitlemark{#1}%
  \@vstypeptitle{#2}%
  \@afterheading}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@vssptitle}
% Typeset a \cs{poemtitle*}.
% \changes{v1.4}{2003/11/16}{Added \cs{M@gettitle} to \cs{@vssptitle}}
%    \begin{macrocode}
\long\def\@vssptitle#1{%
  \M@gettitle{#1}%
  \mempoemstarinfo{#1}%
  \@vstypeptitle{#1}%
  \@afterheading}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@vstypeptitle}
% This \emph{really} typesets the title.
%    \begin{macrocode}
\newcommand{\@vstypeptitle}[1]{%
  \vspace{\beforepoemtitleskip}%
  {\poemtitlefont #1\par}%
  \vspace{\afterpoemtitleskip}%
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\poemtitlefont}
% \begin{macro}{\poemtitlemark}
% Sets the appearance to the title of a poem, and something for a header.
%    \begin{macrocode}
\newcommand{\poemtitlefont}{\normalfont\large\bfseries\centering}
\newcommand{\poemtitlemark}[1]{}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\beforepoemtitleskip}
% \begin{macro}{\afterpoemtitleskip}
% Lengths before and after a poem title, using approcimately \cs{section} 
% values.
%    \begin{macrocode}
\newlength{\beforepoemtitleskip}
  \setlength{\beforepoemtitleskip}{3.5ex \@plus 1ex \@minus .2ex}
\newlength{\afterpoemtitleskip}
  \setlength{\afterpoemtitleskip}{2.3ex \@plus.2ex}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\if@numptitle}
% \begin{macro}{\@numptitletrue}
% \begin{macro}{\@numptitlefalse}
% \begin{macro}{\NumberPoemTitle}
% \begin{macro}{\PlainPoemTitle}
% Following the \cs{NumberPoemTitle} declaration \cs{PoemTitle}s are numbered.
% The \cs{PlainPoemTitle} declaration produces unnumbered titles.
%    \begin{macrocode}
\newif\if@numptitle
\newcommand*{\NumberPoemTitle}{\@numptitletrue}
\newcommand*{\PlainPoemTitle}{\@numptitlefalse}
\NumberPoemTitle

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\c@poem}
% \begin{macro}{\thepoem}
% \begin{macro}{\theHpoem}
% Counter for poem titles.
%    \begin{macrocode}
\newcounter{poem}\setcounter{poem}{0}
  \renewcommand*{\thepoem}{\@arabic\c@poem}
\newcommand*{\theHpoem}{\arabic{poem}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\poemtitlestarmark}
% \begin{macro}{\poemtitlepstyle}
% \begin{macro}{\poemtitlestarpstyle}
% \cs{poemtitlemark}\marg{poemf@rhdr} is used for marks for a PoemTitle and 
% \cs{poemtitlestarmark}\marg{poemf@hdr} is for marks for \cs{PoemTitle*}. 
% The \cs{poemtitlepstyle} and \cs{poemtitlestarpstyle} macros are provided
% as hooks so that, for example, pagestyles can be set for the regular
% and starred versions of \cs{PoemTitle}. By default these macros do nothing.
%    \begin{macrocode}
\newcommand*{\poemtitlestarmark}[1]{}
\newcommand*{\poemtitlepstyle}{}
\newcommand*{\poemtitlestarpstyle}{}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\PoemTitle}
%    The command to produce a article style chapter-like (numbered) poem title.
% \changes{v1.618}{2005/09/09}{Added \cs{PoemTitle}}
%    \begin{macrocode}
\newcommand\PoemTitle{%
  \par
  \@afterindentfalse
  \@ifstar{\@m@msPoemTitle}{\@m@mPoemTitle}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@m@mPoemTitle}
% \begin{macro}{\poemt@c}
% Intermediate and support macros for the extra optional argument
% to \cs{PoemTitle}. Have to do this long windedly otherwise dear old
% \Lpack{hyperref} barfs.
%    \begin{macrocode}
\newcommand{\@m@mPoemTitle}[1][]{%
  \def\poemt@c{#1}% capture first optional arg
  \@ifnextchar[{\@PoemTitle}{\@PoemTitle[]}%
}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\memPoemTitleinfo}
% \begin{macro}{\memPoemTitlestarinfo}
% \verb?\memPoemTitleinfo{num}{toc}{head}{full}? \\
% \verb?\memPoemTitlestarinfo{short}{full}?
%    \begin{macrocode}
\newcommand*{\memPoemTitleinfo}[4]{}
\newcommand*{\memPoemTitlestarinfo}[2]{}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@PoemTitle}
% \begin{macro}{\poemf@rtoc}
% \begin{macro}{\poemf@rhdr}
% \cs{@PoemTitle}\oarg{tocmark}\marg{title}
% typesets the title of a \cs{PoemTitle}. 
% There is a number for \cs{@numptitletrue} and \cs{@mainmattertrue}.
%    \begin{macrocode}
\def\@PoemTitle[#1]#2{%
  \phantomsection
  \ifx\poemt@c\@empty % no optional args
    \def\poemf@rtoc{#2}%
    \def\poemf@rhdr{#2}%
  \else                  % at least one opt arg
    \let\poemf@rtoc\poemt@c
    \ifx\@empty#1\@empty
      \let\poemf@rhdr\poemt@c
    \else
      \def\poemf@rhdr{#1}%
    \fi
  \fi
  \m@m@Andfalse
  \if@numptitle
    \if@mainmatter
      \m@m@Andtrue
    \fi
  \fi
  \ifm@m@And
    \refstepcounter{poem}%
  \fi
%    \end{macrocode}
% Store the (short) title via \cs{poemtitlemark} and call
% \cs{\@makePoemTitlehead} to do the typesetting.
%    \begin{macrocode}
  \@makePoemTitlehead{#2}%
  \@afterheading
  \poemtitlemark{\poemf@rhdr}%
  \poemtitlepstyle
%    \end{macrocode}
% Add the title to the ToC.
%    \begin{macrocode}
  \ifm@m@And
    \addcontentsline{toc}{\poemtoc}{%
      \protect\numberline{\thepoem}\poemf@rtoc}%
    \memPoemTitleinfo{\thepoem}{\poemf@rtoc}{\poemf@rhdr}{#2}%
  \else
    \addcontentsline{toc}{\poemtoc}{\poemf@rtoc}%
    \memPoemTitleinfo{}{\poemf@rtoc}{\poemf@rhdr}{#2}%
  \fi
%    \end{macrocode}
% Add hook for title referencing.
%    \begin{macrocode}
  \ifheadnameref\M@gettitle{\poemf@rhdr}\else\M@gettitle{\poemf@rtoc}\fi
}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\@makePoemTitlehead}
% This \emph{really} typesets a \cs{PoemTitle}. Leave some whitespace.
%    \begin{macrocode}
\def\@makePoemTitlehead#1{{%
  \PoemTitleheadstart
  \parindent \z@ \normalfont
%    \end{macrocode}
% If there is a number, typeset it, otherwise call \cs{printPoemTitlenonum}.
%    \begin{macrocode}
   \ifm@m@And
     \printPoemTitlenum 
     \afterPoemTitlenum
   \else
     \printPoemTitlenonum
   \fi
%    \end{macrocode}
% Typeset the title.
%    \begin{macrocode}
   \interlinepenalty\@M
   \printPoemTitletitle{#1}% 
   \afterPoemTitle 
  }}

%    \end{macrocode}
% \end{macro}
%
%
%
% \begin{macro}{\@PTchs@def@ult}
% This sets up all the definitions used in \cs{@makePoemTitlehead}
% and \cs{@makesPoemTitlehead}.
% \begin{macro}{\PoemTitleheadstart}
% \begin{macro}{\printPoemTitlenum}
% \begin{macro}{\afterPoemTitlenum}
% \begin{macro}{\printPoemTitlenonum}
% \begin{macro}{\printPoemTitletitle}
% \begin{macro}{\afterPoemTitle}
%    \begin{macrocode}
\newcommand{\@PTchs@def@ult}{%
  \def\PoemTitleheadstart{\vspace{\beforePoemTitleskip}}
  \def\printPoemTitlenum{\PoemTitlenumfont \thepoem}
  \def\afterPoemTitlenum{\par\nobreak\vskip \midPoemTitleskip}
  \def\printPoemTitlenonum{}
  \def\printPoemTitletitle##1{\PoemTitlefont ##1}
  \def\afterPoemTitle{\par\nobreak\vskip \afterPoemTitleskip}
}
\@PTchs@def@ult

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% 
% \begin{macro}{\PoemTitlenumfont}
% \begin{macro}{\PoemTitlefont}
% Fonts for setting the Poem Title number and title.
%    \begin{macrocode}
\newcommand*{\PoemTitlenumfont}{\normalfont\large\centering}
\newcommand*{\PoemTitlefont}{\normalfont\large\centering}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\beforePoemTitleskip}
% \begin{macro}{\midPoemTitleskip}
% \begin{macro}{\afterPoemTitleskip}
% Lengths separating the various parts of a Poem Title heading.
%    \begin{macrocode}
\newlength{\beforePoemTitleskip}
  \setlength{\beforePoemTitleskip}{1\onelineskip}
\newlength{\midPoemTitleskip}
  \setlength{\midPoemTitleskip}{0pt}
\newlength{\afterPoemTitleskip}
  \setlength{\afterPoemTitleskip}{1\onelineskip}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\@m@msPoemTitle}
% This deals with the optional argument for starred PoemTitles.
%    \begin{macrocode}
\newcommand{\@m@msPoemTitle}[2][\@empty]{%
  \@sPoemTitle{#2}%
  \ifx \@empty#1
    \def\poemf@rhdr{#2}%
  \else   % opt arg
    \def\poemf@rhdr{#1}%
  \fi
  \poemtitlestarmark{\poemf@rhdr}%
  \poemtitlestarpstyle
  \memPoemTitlestarinfo{\poemf@rhdr}{#2}%
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@sPoemTitle}
% \cs{@sPoemTitle}\marg{long} typesets the title of a 
% \cs{PoemTitle*}. It is easier than the \cs{@PoemTitle} as there is no number
% or ToC entry to worry about.
%    \begin{macrocode}
\newcommand{\@sPoemTitle}[1]{%
  \@makesPoemTitlehead{#1}%
  \@afterheading
  \M@gettitle{#1}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@makesPoemTitlehead}
% This \emph{really} typesets a \cs{PoemTitle*}, and is similar to 
% \cs{@makePoemTitlehead}.
%    \begin{macrocode}
\def\@makesPoemTitlehead#1{{%
  \PoemTitleheadstart
  \parindent \z@ \normalfont
  \printPoemTitlenonum
  \interlinepenalty\@M
  \printPoemTitletitle{#1}
  \afterPoemTitle}}

%    \end{macrocode}
% \end{macro}
%
%
%
%
% \section{Setting parameters for existing environments}
%
% \subsection{Array and tabular}
%
% \begin{macro}{\arraycolsep}
%    The columns in an array environment are separated by
%    2|\arraycolsep|.
%    \begin{macrocode}
\setlength\arraycolsep{5\p@}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\tabcolsep}
%    The columns in an tabular environment are separated by
%    2|\tabcolsep|.
%    \begin{macrocode}
\setlength\tabcolsep{6\p@}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\arrayrulewidth}
%    The width of rules in the array and tabular environments is given
%    by\\ |\arrayrulewidth|.
%    \begin{macrocode}
\setlength\arrayrulewidth{.4\p@}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\doublerulesep}
%    The space between adjacent rules in the array and tabular
%    environments is given by |\doublerulesep|.
%    \begin{macrocode}
\setlength\doublerulesep{2\p@}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Tabbing}
%
% \begin{macro}{\tabbingsep}
%    This controls the space that the |\'| command puts in. (See
%    \LaTeX{} manual for an explanation.)
%    \begin{macrocode}
\setlength\tabbingsep{\labelsep}

%    \end{macrocode}
% \end{macro}
%
% \subsection{Minipage}
%
% \begin{macro}{\@minipagerestore}
%    The macro |\@minipagerestore| is called upon entry to a minipage
%    environment to set up things that are to be handled differently
%    inside a minipage environment. In the current styles, it does
%    nothing.
% \end{macro}
%
% \begin{macro}{\@mpfootins}
%    Minipages have their own footnotes; |\skip||\@mpfootins| plays
%    same r\^ole for footnotes in a minipage as |\skip||\footins| does
%    for ordinary footnotes.
%
%    \begin{macrocode}
\skip\@mpfootins = \skip\footins

%    \end{macrocode}
% \end{macro}
%
% \subsection{Framed boxes}
%
% \begin{macro}{\fboxsep}
%    The space left by |\fbox| and |\framebox| between the box and the
%    text in it.
% \begin{macro}{\fboxrule}
%    The width of the rules in the box made by |\fbox| and |\framebox|.
%    \begin{macrocode}
\setlength\fboxsep{3\p@}
\setlength\fboxrule{.4\p@}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Equation and eqnarray}
%
% \begin{macro}{\theequation}
%    The equation counter will be reset at beginning of a new chapter
%    and the equation number will be prefixed by the chapter number.
%
%    This code  must follow the |\chapter| definition, or more exactly
%    the definition of the chapter counter.
%    \begin{macrocode}
\@addtoreset{equation}{chapter}
\renewcommand{\theequation}{%
  \ifnum \c@chapter>\z@ \thechapter.\fi \@arabic\c@equation}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\jot}
%    |\jot| is the extra space added between lines of an eqnarray
%    environment. The default value is used.
%    \begin{macrocode}
% \setlength\jot{3pt}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@eqnnum}
%    The macro |\@eqnnum| defines how equation numbers are to appear in
%    equations. Again the default is used.
%
%    \begin{macrocode}
% \def\@eqnnum{(\theequation)}

%    \end{macrocode}
% \end{macro}
%
%
% \section{Array and tabular}
%
% \subsection{Array}%
%
% The description and code are essentially copied from the \Lpack{array}
% package~\cite{ARRAY}.
%
%    \begin{macrocode}

%%%%%%%%%% Array package code %%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% With acknowledgements to %%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% Frank Mittelbach & David Carlisle %%%%%%%%%%%%%%%%%%%%%

%    \end{macrocode}
%
%
% \subsubsection{The construction of the preamble}
%
% \begin{PW}
% PW: The original \Lpack{array} package has redundant code (caused by a quick 
% mashing of an early \Lpack{array} and the \Lpack{newarray} packages). I
% have tried to remove the redundancies and overlaps.
% \end{PW}
%
% It is obvious that those environments will consist mainly of an
% \cs{halign}, because \TeX\ typesets tables using this primitive.
% That is why we will now take a look at the algorithm which determines
% a preamble for a \cs{halign} starting with a given user preamble
% using the options mentioned above.
%
% The most interesting macros of this implementation are without doubt
% those which are responsible for the construction of the preamble for
% the \cs{halign}. The underlying algorithm was developed by
% \textsc{Lamport} (resp.\ \textsc{Knuth}, see texhax V87\#??), and it
% has been extended and improved.
%
% The user preamble will be read \textsf{token} by \textsf{token}.  A
% \textsf{token} is a single character like \texttt{c} or a block
% enclosed in \verb?{...}?. For example the preamble of
% \verb?\begin{tabular}?\linebreak[0]\verb?{lc||c@{\hspace{1cm}}}? consists of
% the \textsf{token} \texttt{l}, \texttt{c}, \verb?|?, \verb?|?,
% \texttt{@} and \verb?\hspace{1cm}?.
%
% The currently used \textsf{token} and the one, used before, are needed
% to decide on how the construction of the preamble has to be
% continued.
% In the example mentioned above the \texttt{l} causes the preamble
% to begin with \verb?\hskip\tabcolsep?. Furthermore
% \verb?# \hfil? would be appended to define a flush left column.
% The next \textsf{token} is a \texttt{c}. Because it was preceded by an
% \texttt{l} it generates a new column. This is done with
% \verb?\hskip \tabcolsep & \hskip \tabcolsep?. The column which is to
% be centered will be appended with \verb?\hfil # \hfil?.
% The \textsf{token} \verb?|? would then add a space of
% \verb?\hskip \tabcolsep?
% and a vertical line because the last
% \textsf{tokens} was a \texttt{c}.
% The following \textsf{token} \verb?|? would only add a space
% \verb?\hskip \doublerulesep? because it was preceded by the
% \textsf{token} \verb?|?. We will not discuss our example further but
%  rather take a look at the general case of constructing preambles.
%
% The example shows that the desired preamble for the
% \cs{halign} can be constructed as soon as the action of all
% combinations
% of the preamble \textsf{tokens} are specified. There are 18 such
% \textsf{tokens}
% so we have $19 \cdot 18 = 342$ combinations if we count the
% beginning of
% the preamble as a special \textsf{token}. Fortunately, there are many
% combinations which generate the same spaces, so we can define
% \textsf{token} classes. We will identify a
% \textsf{token} within a class with a number, so we can insert the
% formatting (for example of a column).
% Table~\ref{tab:Klassen} lists all \textsf{token} classes and
% their corresponding numbers.
% \begin{table}[ht]
% \begin{center} \ttfamily
% ^^A   \begin{tabular}[t]{>{\ttfamily}ccc}
%    \begin{tabular}[t]{ccc}
%       \textsf{token} & \cs{@chclass} & \cs{@chnum} \\[2mm]
%       c   & 0  & 0 \\
%       l   & 0  & 1 \\
%       r   & 0  & 2 \\
%       p-arg    & 0  & 3 \\
%       t-arg    & 0  & 4 \\
%       b-arg    & 0  & 5 \\
%       \verb?|?   & 1  & 0 \\
%       !-arg    & 1  & 1 \\
%       <-arg    & 2  & --- \\
%       >-arg    & 3  & ---
%    \end{tabular}
%    \kern3mm \vrule \kern3mm%
% ^^A   \begin{tabular}[t]{>{\ttfamily}ccc}
%    \begin{tabular}[t]{ccc}
%       \textsf{token} & \cs{@chclass} & \cs{@chnum} \\[2mm]
%       Start    & 4  & --- \\
%       @-arg    & 5  & --- \\
%       !   & 6  & --- \\
%       @   & 7  & --- \\
%       <   & 8  & --- \\
%       >   & 9  & --- \\
%       p   & 10 & 3 \\
%       t   & 10 & 4 \\
%       b   & 10 & 5
%    \end{tabular}
% \end{center}
% \caption{Classes of preamble \textsf{tokens}}
% \label{tab:Klassen}
% \end{table}
%
%
% \begin{macro}{\@chclass}
% \begin{macro}{\@chnum}
% \begin{macro}{\@lastchclass}
%    The class and the number of the current \textsf{token} are saved in
%    the
%    \textsf{count} registers \cs{@chclass}
%    and \cs{@chnum}, while the class of the previous
%    \textsf{token} is stored in the
%    \textsf{count} register \cs{lastchclass}.
%    All of the mentioned registers are already allocated in
%    \texttt{latex.tex},
%    which is the reason why the following three lines of code are
%    commented out.
%    Later throughout the text I will not mention it again explicitely
%    whenever I use a \verb?%? sign. These parts are already defined in
%    \texttt{latex.tex}.
%    \begin{macrocode}
% \newcount \@chclass
% \newcount \@chnum
% \newcount \@lastchclass
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
%
% \begin{macro}{\@addtopreamble}
%    We will save the already constructed preamble for
%    the \cs{halign}
%    in the global macro \cs{@preamble}. This will then be
%     enlarged with
%    the command \cs{@addtopreamble}.
%    \begin{macrocode}
\def\@addtopreamble#1{\xdef\@preamble{\@preamble #1}}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@testpach}
%    With the help of \cs{@lastchclass} we can now define a macro
%    which determines the class and the number of a given preamble
%    \textsf{token}
%    and assigns them to the registers
%    \cs{@chclass} and \cs{@chnum}.
%    \begin{macrocode}
\def\@testpach{\@chclass
%    \end{macrocode}
%    First we deal with the cases in which the \textsf{token}
%    (\verb?#1?) is the argument of \texttt{!}, \texttt{@}, \texttt{<} or
%    \texttt{>}. We can see this from the value of \cs{@lastchclass}:
%    \begin{macrocode}
 \ifnum \@lastchclass=6 \@ne \@chnum \@ne \else
  \ifnum \@lastchclass=7 5 \else
   \ifnum \@lastchclass=8 \tw@ \else
    \ifnum \@lastchclass=9 \thr@@
%    \end{macrocode}
%    Otherwise we will assume that the \textsf{token} belongs to the
%    class $0$
%    and assign the corresponding number to \cs{@chnum} if our
%    assumption is correct.
%    \begin{macrocode}
   \else \z@
%    \end{macrocode}
%    If the last \textsf{token} was a \texttt{p}, \texttt{m} or a
%    \texttt{b}, \cs{@chnum} already has the right value. This is the
%    reason for the somewhat curious choice of the \textsf{token}
%    numbers in class $10$.
%    \begin{macrocode}
   \ifnum \@lastchclass = 10 \else
%    \end{macrocode}
%    Otherwise we will check if \cs{@nextchar} is either a \texttt{c},
%    \texttt{l} or an \texttt{r}.  Some applications change the
%    catcodes of certain characters like ``\texttt{@}'' in
%    \texttt{amstex.sty}. As a result the tests below would fail since
%    they assume non-active character tokens. Therefore we evaluate
%    \cs{@nextchar} once thereby turning the first token of its
%    replacement text into a char. At this point here this should have
%    been the only char present in \cs{@nextchar} which put into via a
%    \cs{def}.
%    \begin{macrocode}
   \edef\@nextchar{\expandafter\string\@nextchar}%
   \@chnum
   \if \@nextchar c\z@ \else
    \if \@nextchar l\@ne \else
     \if \@nextchar r\tw@ \else
%    \end{macrocode}
%    If it is a different \textsf{token}, we know that the class was
%    not $0$. We assign the value $0$ to \cs{@chnum} because this value
%    is needed for the \verb?|?--\textsf{token}. Now we must check
%    the remaining classes.  Note that the value of \cs{@chnum} is
%    insignificant here for most classes.
%    \begin{macrocode}
   \z@ \@chclass
   \if\@nextchar |\@ne \else
    \if \@nextchar !6 \else
     \if \@nextchar @7 \else
      \if \@nextchar <8 \else
       \if \@nextchar >9 \else
%    \end{macrocode}
%    The remaining permitted \textsf{tokens} are \texttt{p},
%    \texttt{m} and \texttt{b} (class $10$).
%    \begin{macrocode}
  10
  \@chnum
  \if \@nextchar m\thr@@\else
   \if \@nextchar p4 \else
    \if \@nextchar b5 \else
%    \end{macrocode}
%    Now the only remaining possibility is a forbidden \textsf{token},
%    so we choose class $0$ and number $0$ and give an error message.
%    Then we finish the macro by closing all \cs{if}'s.
%    \begin{macrocode}
   \z@ \@chclass \z@ \@preamerr \z@ \fi \fi \fi \fi
   \fi \fi  \fi  \fi  \fi  \fi  \fi \fi \fi \fi \fi \fi}
%    \end{macrocode}
% \end{macro}
%
% The preamble will be enlarged with the help of \cs{xdef}, but the
% arguments of \texttt{>}, \texttt{<},~\texttt{!}\ and \texttt{@} are
% not supposed to be expanded during the construction (we want an
% implementation that doesn't need a \cs{protect}). So we have to find a
% way to inhibit the expansion of those arguments.
%
% We will solve this problem with \textsf{token} registers. We need
% one register for every \texttt{!}\ and \texttt{@}, while we need two
% for every \texttt{c}, \texttt{l}, \texttt{r}, \texttt{m}, \texttt{p}
% or \texttt{b}. This limits the number of columns of a table because
% there are only 256 \textsf{token} registers. But then, who needs
% tables with more than 100 columns?
%
% So how do we proceed? Let us assume that we had \verb?!{foo}? in the
% user preamble and say we saved \texttt{foo} in
% \textsf{token} register $5$. Then we call
% \verb?\@addtopreamble{\the@toks5}? where
% \cs{the@toks} is defined in a way that it does not expand
% (for example it could be equivalent to \cs{relax}). Every
% following call
% of \cs{@addtopreamble} leaves \cs{the@toks5} unchanged in
% \cs{@preamble}. If the construction of the preamble is completed
% we change the definition of \cs{the@toks} to
% \verb?\the\toks? and expand \cs{@preamble} for the last time.
% During this process all parts of the form
%    \cs{the@toks}\meta{Number}
% will be substituted by the contents of the respective \textsf{token}
% registers.
%
% As we can see from this informal discussion the construction of the
% preamble has to take place within a group, so that the
% \textsf{token} registers we use will be freed later on. For that
% reason we keep all assignments to \cs{@preamble} global; therefore the
% replacement text of this macro will remain the same after we leave
% the group.
%
% \begin{macro}{\count@}
%    We further need a \textsf{count} register to remember which
%    \textsf{token} register is to be used next. This will be
%    initialized with $-1$ if we want to begin with the \textsf{token}
%    register $0$. We use the \PlainTeX\ scratch register \cs{count@}
%    because everything takes place locally. All we have to do is
%    insert \cs{the@toks} \cs{the} \cs{count@} into the preamble.
%    \cs{the@toks} will remain unchanged and \verb?\the\count@? expands into
%    the saved number.
% \end{macro}
%
% \begin{macro}{\prepnext@tok}
%    The macro \cs{prepnext@tok} is in charge of preparing the next
%    \textsf{token} register. For that purpose we increase
%    \cs{count@} by $1$:
%    \begin{macrocode}
\def\prepnext@tok{\advance \count@ \@ne
%    \end{macrocode}
%    Then we locally delete any contents the
%    \textsf{token} register might have.
%    \begin{macrocode}
   \toks\count@{}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\save@decl}
%    During the construction of the preamble the current
%    \textsf{token} is always saved in the macro \cs{@nextchar} (see the
%    definition of \cs{@mkpream}). The macro
%    \cs{save@decl} saves it into the next free \textsf{token} register,
%    i.e.\ in \verb?\toks\count@?.
% We do not assume that the token register is free, we add the new
% declarations to the front of the register. This is to allow user
% preambles of the form, \verb?>{foo}>{bar}..?. Users are not encouraged to
% enter such expressions directly, but they may result from the
% rewriting of \cs{newcolumntype}'s.
%    \begin{macrocode}
\def\save@decl{\toks \count@ = \expandafter\expandafter\expandafter
                  {\expandafter\@nextchar\the\toks\count@}}

%    \end{macrocode}
% \end{macro}
%
% How does the situation look like, if we want to add another column
% to the preamble, i.e.\ if we have found a \texttt{c}, \texttt{l},
% \texttt{r}, \texttt{p}, \texttt{m} or \texttt{b} in the user
% preamble?  In this case we have the problem of the \textsf{token}
% register from \verb?>{..}? and \verb?<{..}? having to be inserted at this
% moment because formating instructions like \cs{hfil} have to be set
% around them. On the other hand it is not known yet, if any \verb?<{..}?
% instruction will appear in the user preamble at all.
%
% We solve this problem by adding two \textsf{token} registers at a
% time.  This explains, why we have freed the \textsf{token} registers
% in \cs{prepnext@tok}.
%
% \begin{macro}{\insert@column}
% \begin{macro}{\@sharp}
%    We now define the macro \cs{insert@column} which will do
%    this work for us.
%    \begin{macrocode}
\def\insert@column{%
%    \end{macrocode}
%    Here, we assume that the \textsf{count} register
%    \cs{@tempcnta} has saved the value $\verb?\count@? - 1$.
%    \begin{macrocode}
   \the@toks \the \@tempcnta
%    \end{macrocode}
%    Next follows the \verb?#? sign which specifies the place
%    where the text of the column shall be inserted. To avoid
%    errors during the expansions in
%    \cs{@addtopreamble} we hide this sign in the command
%    \cs{@sharp} which is temporarily occupied with
%    \cs{relax} during the build-up of the preamble.
%    To remove unwanted spaces before and after the column text, we set
%    an \cs{ignorespaces}  in front and a \cs{unskip} afterwards.
%    \begin{macrocode}
   \ignorespaces \@sharp \unskip
%    \end{macrocode}
%    Then the second \textsf{token} register follows whose number should
%    be saved in \cs{count@}.
%    We make sure that there will be no further expansion after reading
%    the number, by finishing with \cs{relax}. The case above is not
%    critical since it is ended by \cs{ignorespaces}.
%    \begin{macrocode}
   \the@toks \the \count@ \relax}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\m@mold@addamp}
% \begin{macro}{\m@m@addamp}
% \begin{macro}{\@addamp}
%
%    In the preamble a \verb?&? has to be inserted between any two columns;
%    before the first column there should not be a \verb?&?. As the user
%    preamble may start with a \verb?|? we have to remember somehow
%    if we have already inserted a \verb?#? (i.e.\ a column). This is done
%    with the boolean variable \cs{if@firstamp} that we test in
%    \cs{@addamp}, the macro that inserts the \verb?&?.
%
% \begin{PW}
%    PW: Later on, for horizontal lines in `continuous' tabulars,
% I need to know how many columns there are in a tabular. I need a modified
% kernel's \cs{@addamp} to do this and use the kernel's \cs{@curtab}
% (a counter used in tabbing) to store the number of columns.
%
% \cs{m@mold@addamp} stores the kernel's definition of \cs{@addamp}, and
% \cs{m@m@addamp} is the revised definition.
% \end{PW}
%
%    \begin{macrocode}
%    \newif \@iffirstamp
\let\m@mold@addamp\@addamp
\newcommand*{\m@m@addamp}{%
  \if@firstamp 
    \@firstampfalse
    \global\@curtab\@ne
  \else
    \@addtopreamble{&}
    \global\advance\@curtab\@ne  
  \fi}
\let\@addamp\m@m@addamp

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@acol}
% \begin{macro}{\@acolampacol}
% \begin{macro}{\col@sep}
%    We will now define some abbreviations for the extensions,
%    appearing most often in the preamble build-up.
%    Here \cs{col@sep} is a \textsf{dimen} register which is set
%    equivalent to \cs{arraycolsep} in an \textsf{array}--environment,
%    otherwise it is set equivalent to \cs{tabcolsep}.
%    \begin{macrocode}
\newdimen\col@sep
\def\@acol{\@addtopreamble{\hskip\col@sep}}
%    \def\@acolampacol{\@acol\@addamp\@acol}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@mkpream}
% \begin{macro}{\the@toks}
%    Now we can define the macro which builds up the preamble for the
%    \cs{halign}.
%    First we initialize \cs{@preamble}, \cs{@lastchclass}
%    and the boolean variable \cs{if@firstamp}.
%    \begin{macrocode}
\def\@mkpream#1{\gdef\@preamble{}\@lastchclass 4 \@firstamptrue
%    \end{macrocode}
%    During the build-up of the preamble we cannot directly use the
%    \verb?#? sign; this would lead to an error message in the next
%    \cs{@addtopreamble} call.
%    Instead, we use the command \cs{@sharp} at places where later
%    a \verb?#? will be.
%    This command is at first given the meaning \cs{relax};
%    therefore it will not be expanded when the preamble
%    is extended.
%    In the macro \cs{@array}, shortly before the \cs{halign}
%    is carried out, \cs{@sharp} is given its final meaning.
%
%    In a similar way,
%    we deal with the commands \cs{@startpbox} and
%    \cs{@endpbox}, although the reason is different here: these
%    macros expand in many \textsf{tokens} which would delay the
%    build-up of the preamble.
%    \begin{macrocode}
   \let\@sharp\relax \let\@startpbox\relax \let\@endpbox\relax
%    \end{macrocode}
%    Now we remove possible  $*$-forms and user-defined column
%    specifiers in the user preamble by repeatedly executing the list
%    \cs{NC@list} until the re-writes have no more effect. The
%    expanded preamble will then be in the token register
%    \cs{@temptokena}. Actually we need to know at this point that
%    this is not \cs{toks0}.
%    \begin{macrocode}
   \@temptokena{#1}\@tempswatrue
   \@whilesw\if@tempswa\fi{\@tempswafalse\the\NC@list}%
%    \end{macrocode}
%    Afterwards we initialize all registers and macros, that we need
%    for the build-up of the preamble.
%    Since we want to start with the \textsf{token} register $0$,
%    \cs{count@} has to contain the value $-1$.
%    \begin{macrocode}
   \count@\m@ne
   \let\the@toks\relax
%    \end{macrocode}
%    Then we call up \cs{prepnext@tok} in order to prepare the
%    \textsf{token} register $0$ for use.
%    \begin{macrocode}
   \prepnext@tok
%    \end{macrocode}
% Having expanded all tokens defined using \cs{newcolumntype} (including
% \verb?*?), we evaluate the remaining tokens, which are saved in
% \cs{@temptokena}.  We use the \LaTeX--macro \cs{@tfor} to inspect each
% token in turn.
%    The strange appearing construction with \cs{expandafter} is
%    based on the fact that we have to put the replacement text of
%    \cs{@tempa} and not the macro \cs{@tempa} to this
%    \LaTeX--macro.
%    \begin{macrocode}
   \expandafter \@tfor \expandafter \@nextchar
    \expandafter :\expandafter =\the\@temptokena \do
%    \end{macrocode}
%    The body of this loop (the group after the \cs{do})
%    is executed for one \textsf{token} at a time, whereas
%    the current \textsf{token} is saved in \cs{@nextchar}.
%    At first we evaluate the current \textsf{token} with the already
%    defined macro \cs{@testpach}, i.e.\ we assign to
%    \cs{@chclass} the character class and to \cs{@chnum}
%    the character number of this \textsf{token}.
%    \begin{macrocode}
   {\@testpach
%    \end{macrocode}
%    Then we branch out depending on the value of \cs{@chclass} into
%    different macros that extend the preamble respectively.
%    \begin{macrocode}
   \ifcase \@chclass \@classz \or \@classi \or \@classii
     \or \save@decl \or \or \@classv \or \@classvi
     \or \@classvii \or \@classviii
%    \end{macrocode}
%    Class 9 is equivalent to class 10.
%    \begin{macrocode}
     \or \@classx
     \or \@classx \fi
%    \end{macrocode}
%    Two cases deserve our special attention: Since the current
%    \textsf{token} cannot have the character class $4$ (start) we
%    have skipped this possibility. If the character class is $3$,
%    only the content of \cs{@nextchar} has to be saved into the current
%    \textsf{token} register; therefore we call up \cs{save@decl}
%    directly and save a macro name.  After the preamble has been
%    extended we assign the value of \cs{@chclass} to the counter
%    \cs{@lastchclass} to assure that this information will be available
%    during the next run of the loop.
%    \begin{macrocode}
   \@lastchclass\@chclass}%
%    \end{macrocode}
%    After the loop has been finished space must still be added to
%    the created preamble, depending on the last \textsf{token}.
%    Depending on the value of \cs{@lastchclass} we perform
%    the necessary operations.
%    \begin{macrocode}
   \ifcase\@lastchclass
%    \end{macrocode}
%    If the last class equals $0$ we add a
%    \verb?\hskip \col@sep?.
%    \begin{macrocode}
   \@acol \or
%    \end{macrocode}
%    If it equals $1$ we do not add any additional space so that the
%    horizontal lines do not exceed the vertical ones.
%    \begin{macrocode}
   \or
%    \end{macrocode}
%    Class $2$ is treated like class $0$ because a \verb?<{...}?  can
%    only directly follow after class $0$.
%    \begin{macrocode}
   \@acol \or
%    \end{macrocode}
%    Most of the other possibilities can only appear if the user
%    preamble was defective. Class $3$ is not allowed since after a
%    \verb?>{..}? there must always follow a \texttt{c}, \texttt{l},
%    \texttt{r}, \texttt{p},\texttt{m} or \texttt{b}. We report an
%    error and ignore the declaration given by \verb?{..}?.
%    \begin{macrocode}
   \@preamerr \thr@@ \or
%    \end{macrocode}
%    If \cs{@lastchclass} is $4$ the user preamble has been empty.
%    To continue, we insert a \verb?#? in the preamble.
%    \begin{macrocode}
   \@preamerr \tw@ \@addtopreamble\@sharp \or
%    \end{macrocode}
%    Class $5$ is allowed again. In this case
%    (the user preamble ends with \verb?@{..}?) we need not
%    do anything.
%    \begin{macrocode}
   \or
%    \end{macrocode}
%    Any other case means that the arguments to \verb?@?, \texttt{!},
%    \texttt{<}, \texttt{>}, \texttt{p}, \texttt{m} or \texttt{b} have
%    been forgotten. So we report an error and ignore the last
%    \textsf{token}.
%    \begin{macrocode}
   \else  \@preamerr \@ne \fi
%    \end{macrocode}
%    Now that the build-up of the preamble is almost finished we can
%    insert the \textsf{token} registers and therefore redefine
%    \cs{the@toks}. The actual insertion, though, is performed
%    later.
%    \begin{macrocode}
   \def\the@toks{\the\toks}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% The preamble is extended by the macros \cs{@classz} to
% \cs{@classx} which are called by \cs{@mkpream}
%  depending on \cs{@lastchclass}
% (i.e. the character class of the last \textsf{token}).
% \begin{macro}{\@classx}
%    First we define \cs{@classx} because of its important r\^ole.
%    When it is called we find that the current
%    \textsf{token} is \texttt{p}, \texttt{m} or \texttt{b}.
%    That means that a new column has to start.
%    \begin{macrocode}
\def\@classx{%
%    \end{macrocode}
%    Depending on the value of \cs{@lastchclass} different actions
%    must take place:
%    \begin{macrocode}
  \ifcase \@lastchclass
%    \end{macrocode}
%    If the last character class was $0$ we separate the columns by
%    \verb?\hskip\col@sep? followed by \verb?&? and another
%    \verb?\hskip\col@sep?.
%    \begin{macrocode}
  \@acolampacol \or
%    \end{macrocode}
%    If the last class was class $1$ --- that means that a vertical
%    line was
%    drawn, --- before this line a \verb?\hskip\col@sep? was inserted.
%    Therefore there has to be only a \verb?&? followed by
%    \verb?\hskip\col@sep?. But this \verb?&? may be inserted only
%    if this is not the first column. This process is controlled
%    by \cs{if@firstamp} in the macro \cs{addamp}.
%    \begin{macrocode}
  \@addamp \@acol \or
%    \end{macrocode}
%    Class $2$ is treated like class $0$ because \verb?<{...}? can only
%    follow after class $0$.
%    \begin{macrocode}
  \@acolampacol \or
%    \end{macrocode}
%    Class $3$ requires no actions because all things necessary have
%    been done by the preamble \textsf{token} \texttt{>}.
%    \begin{macrocode}
  \or
%    \end{macrocode}
%    Class $4$ means that we are at the beginning of the preamble.
%    Therefore we start the preamble with \verb?\hskip\col@sep? and
%    then call \cs{@firstampfalse}. This makes sure that a later
%    \cs{@addamp} inserts the character
%    \verb?&? into the preamble.
%    \begin{macrocode}
  \@acol \@firstampfalse \or
%    \end{macrocode}
%    For class $5$ \textsf{tokens} only the character \verb?&? is inserted
%    as a column separator. Therefore we call \cs{@addamp}.
%    \begin{macrocode}
  \@addamp
%    \end{macrocode}
%    Other cases are impossible. For an example
%    \verb?\@lastchclass = 6? ---a s it might appear in a
%    preamble of the form \verb?...!p...? --- \texttt{p} would have
%    been taken as an  argument of \texttt{!} by \cs{@testpach}.
%    \begin{macrocode}
  \fi}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@classz}
%    If the character class of the last \textsf{token} is $0$ we have
%    \texttt{c}, \texttt{l}, \texttt{r} or an argument of \texttt{m},
%    \texttt{b} or\ \texttt{p}. In the first three cases the preamble
%    must be extended the same way as if we had class $10$. The
%    remaining two cases do not require any action because the space
%    needed was generated by the last \textsf{token} (i.e.\
%    \texttt{m}, \texttt{b} or \texttt{p}). Since \cs{@lastchclass} has
%    the value $10$ at this point nothing happens when \cs{@classx} is
%    called. So the macro \cs{@chlassz} may start like this:
%    \begin{macrocode}
\def\@classz{\@classx
%    \end{macrocode}
%    According to the definition of \cs{insert@column} we must store
%    the number of the \textsf{token} register in which a preceding
%    \verb?>{..}? might have stored its argument into
%    \cs{@tempcnta}.
%    \begin{macrocode}
   \@tempcnta \count@
%    \end{macrocode}
%    To have \verb?\count@ = \@tmpcnta + 1? we prepare
%    the next \textsf{token} register.
%    \begin{macrocode}
   \prepnext@tok
%    \end{macrocode}
%    Now the preamble must be extended with the column whose format
%    can be determinated by \cs{@chnum}.
%    \begin{macrocode}
   \@addtopreamble{\ifcase \@chnum
%    \end{macrocode}
%    If \cs{@chnum} has the value $0$ a centered column has to be
%    generated.
%    So we begin with stretchable space.
%    \begin{macrocode}
      \hfil
%    \end{macrocode}
%    The command \cs{d@llarbegin} follows expanding into \cs{begingroup}
%    (in the \textsf{tabular}--environment) or into \verb?$?.  Doing this
%    (provided an appropriate setting of \cs{d@llarbegin}) we achieve
%    that the contents of the columns of an \textsf{array}--environment
%    are set in math mode while those of a \textsf{tabular}--environment
%    are set in LR mode.
%    \begin{macrocode}
      \d@llarbegin
%    \end{macrocode}
%    Now we insert the contents of the two \textsf{token} registers
%    and the symbol
%    for the column entry (i.e.\ \verb?#? or
%    more precise \cs{@sharp}) using \cs{insert@column}.
%    \begin{macrocode}
      \insert@column
%    \end{macrocode}
%    We end this case with \cs{d@llarend} and \cs{hfil} where \cs{d@llarend}
%    again is either \verb?$? or \cs{endgroup}.
%    \begin{macrocode}
      \d@llarend \hfil \or
%    \end{macrocode}
%    The templates for \texttt{l} and \texttt{r} (i.e.\ \cs{@chnum} $1$
%    or $2$) are generated the same way. Since one  \cs{hfil} is
%    missing the text is moved to the relevant side.
%    The \verb?\kern\z@? is needed in case of an empty column
%    entry. Otherwise
%    the \cs{unskip} in \cs{insert@column} removes the
%    \cs{hfil}. Changed to \verb?\hskip1sp? so that it interacts better with
%    \cs{@bsphack}.
%    \begin{macrocode}
      \hskip1sp\d@llarbegin \insert@column \d@llarend \hfil \or
      \hfil\hskip1sp\d@llarbegin \insert@column \d@llarend \or
%    \end{macrocode}
%    The templates for \texttt{p}, \texttt{m} and \texttt{b} mainly
%    consist of a \textsf{box}. In case of \texttt{m} it is generated
%    by \cs{vcenter}.  This command is allowed only in math
%    mode. Therefore we start with a~\verb?$?.
%    \begin{macrocode}
   $\vcenter
%    \end{macrocode}
%    The part of the templates which is the same in all three cases
%    (\texttt{p}, \texttt{m} and \texttt{b})
%    is built by the macros \cs{@startpbox} and
%    \cs{@endpbox}. \cs{@startpbox} has an argument:
%    the width of the column which is stored in the current
%    \textsf{token} (i.e.\ \cs{@nextchar}).
%    Between these two macros we find the well known
%    \cs{insert@column}.
%    \begin{macrocode}
   \@startpbox{\@nextchar}\insert@column \@endpbox $\or
%    \end{macrocode}
%    The templates for \texttt{p} and \texttt{b} are generated in the
%    same way though we do not need the \verb?$? characters because we use
%    \cs{vtop} or \cs{vbox}.
%    \begin{macrocode}
   \vtop \@startpbox{\@nextchar}\insert@column \@endpbox \or
   \vbox \@startpbox{\@nextchar}\insert@column \@endpbox
%    \end{macrocode}
%    Other values for \cs{@chnum} are impossible. Therefore we
%    end the arguments to \cs{@addtopreamble} and \cs{ifcase}.
%    Before we come to the end of \cs{@classz} we have to
%    prepare the next \textsf{token} register.
%    \begin{macrocode}
  \fi}\prepnext@tok}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@classix}
%    Class $9$ (\texttt{>}--\textsf{token}) prevented repeated
% \verb?>? declarations for the same column. This restriction has been eased,
% making class $9$ equivalent to class $10$. 
% \changes{v1.618}{2005/09/03}{Let \cs{@classix} to \cs{relax} (mempatch v3.10)}
%    \begin{macrocode}
\let\@classix\relax

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@classviii}
%    If the current \textsf{token} is a \texttt{<} the last character
%    class must be $0$, or $2$ (as repeated \verb?<? expressions are allowed). 
%    In this case it is not necessary to extend the
%    preamble. Otherwise we output an error message, set \cs{@chclass}
%    to $6$ and call \cs{@classvi}.  By doing this we achieve that
%    \texttt{<} is treated like \texttt{!}.
%    \begin{macrocode}
\def\@classviii{\ifnum \@lastchclass >\z@\ifnum\@lastchclass=\tw@\else
      \@preamerr 4\@chclass 6 \@classvi \fi\fi}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@arrayrule}
%    There is only one incompatibility with the original definition:
%    the definition of \cs{@arrayrule}. In the original a line without
%    width (e.g., the space between \verb?cc? and \verb?c|c?
%    is equal)  is created by multiple insertions of 
% \verb?\hskip .5\arrayrulewidth?.  
%    We only insert a vertical line into the
%    preamble.  This is done to prevent problems with \TeX's main
%    memory when generating tables with many vertical lines in them
%    (especially in the case of \textsf{floats}).
%    \begin{macrocode}
\def\@arrayrule{\@addtopreamble \vline}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@classvii}
%    As a consequence it follows that in case of class $7$
%    (\verb?@? \textsf{token})  the preamble need not to be extended.
%    In the original definition \verb?\@lastchclass = 1?
%    is treated by inserting \verb?\hskip .5\arrayrulewidth?.
%    We only check if the last \textsf{token} was of class $3$ which is
%    forbidden.
%    \begin{macrocode}
\def\@classvii{\ifnum \@lastchclass = \thr@@
%    \end{macrocode}
%    If this is true we output an error message and
%    ignore the declarations stored
%    by the last  \verb?>{...}?, because these are overwritten
%    by the argument of \texttt{@}.
%    \begin{macrocode}
   \@preamerr \thr@@ \fi}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@classvi}
%    If the current \textsf{token} is a regular \texttt{!}\ and the
%    last class was $0$ or $2$ we extend the preamble with
%    \verb?\hskip\col@sep?.  If the last \textsf{token} was of class $1$
%    (for instance \verb?|?) we extend with \verb?\hskip \doublerulesep?
%    because the construction \verb?!{...}? has to be treated like
%    \verb?|?.
%    \begin{macrocode}
\def\@classvi{\ifcase \@lastchclass
      \@acol \or
      \@addtopreamble{\hskip \doublerulesep}\or
      \@acol \or
%    \end{macrocode}
%    Now \verb?\@preamerr...? should follow because a
%    user preamble of the form \verb?..>{..}!.? is not allowed.
%    To save memory we call \cs{@classvii} instead which also
%    does what we want.
%    \begin{macrocode}
      \@classvii
%    \end{macrocode}
%    If \cs{@lastchclass} is $4$ or $5$ nothing has to be done.
%    Class $6$ to $10$ are not possible.
%    So we finish the macro.
%    \begin{macrocode}
      \fi}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@classii}
% \begin{macro}{\@classiii}
%    In the case of character classes $2$ and $3$ (i.e.\ the argument
%    of \texttt{<} or \texttt{>}) we only have to store the current
%    \textsf{token} (\cs{@nextchar}) into the corresponding
%    \textsf{token} register since the preparation and
%    insertion of these registers
%    are done by the macro \cs{@classz}.
%    This is equivalent to calling \cs{save@decl} in the case of
%    class $3$. To save command identifiers we do this call up
%     in the macro \cs{@mkpream}.
%
%    Class $2$ exhibits a more complicated situation: the
%    \textsf{token} registers have already been inserted by
%    \cs{@classz}. So the value of \cs{count@} is too high
%    by one. Therefore we decrease \cs{count@} by $1$.
%    \begin{macrocode}
\def\@classii{\advance \count@ \m@ne
%    \end{macrocode}
%    Next we store the current \textsf{token} into the correct 
%    \textsf{token} register by calling \cs{save@decl} and then
%    increase the value of \cs{count@} again. At this point we
%    can save memory once more (at the cost of time) if we use the
%     macro \cs{prepnext@tok}.
%    \begin{macrocode}
   \save@decl\prepnext@tok}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@classv}
% Class $5$ is \texttt{@}-expressions (and is also called by class 1)
% We do not expand the
% \verb?@?-expression, but instead explicitly replace an
% \cs{extracolsep} command by an assignment to \cs{tabskip} by a
% method similar to the \cs{newcolumntype} system described later.
%    \begin{macrocode}
\def\@classv{\save@decl
   \expandafter\NC@ecs\@nextchar\extracolsep{}\extracolsep\@@@
   \@addtopreamble{\d@llarbegin\the@toks\the\count@\relax\d@llarend}%
   \prepnext@tok}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\NC@ecs}
% Rewrite the first occurrence of \verb?\extracolsep{1in}? to
% \verb?\tabskip1in\relax?. As a side effect discard any tokens after a
% second \cs{extracolsep}, there is no point in the user entering two of
% these commands anyway, so this is not really a restriction.
%    \begin{macrocode}
\def\NC@ecs#1\extracolsep#2#3\extracolsep#4\@@@{\def\@tempa{#2}%
  \ifx\@tempa\@empty\else\toks\count@={#1\tabskip#2\relax#3}\fi}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@classi}
%    In the case of class $0$ we were able to generate the necessary
%    space between columns by using the macro \cs{@classx}.
%    Analogously the macro \cs{@classvi} can be used for class $1$.
%    \begin{macrocode}
\def\@classi{\@classvi
%    \end{macrocode}
%    Depending on \cs{@chnum} a vertical line
%    \begin{macrocode}
   \ifcase \@chnum \@arrayrule \or
%    \end{macrocode}
%    or (in case of \verb?!{...}?) the current \textsf{token} --- stored
%    in \cs{@nextchar} --- has to be inserted into the preamble.
%    This corresponds to calling \cs{@classv}.
%    \begin{macrocode}
      \@classv \fi}

%    \end{macrocode}
% \end{macro}
%
%
%
% \begin{macro}{\@startpbox}
%    In \cs{@classz}  the macro \cs{@startpbox} is used.
%    The width of the \textsf{parbox} is passed as an argument.
%    \cs{vcenter}, \cs{vtop} or \cs{vbox} are already in the
%    preamble. So we start with the braces for the wanted box.
%    \begin{macrocode}
\def\@startpbox#1{\bgroup
%    \end{macrocode}
%    The argument is the width of the box. This information has to be
%    assigned to \cs{hsize}.
%    Then we assain default values to several parameters used in a
%    \textsf{parbox}.
%    \begin{macrocode}
  \setlength\hsize{#1}\@arrayparboxrestore
%    \end{macrocode}
%    Our main problem is to obtain the same distance between succeeding
%    lines of the \textsf{parbox}.
%    We have to remember that the distance between two \textsf{parboxes}
%    should be defined by \cs{@arstrut}. That means that it can be
%    greater than the distance in a \textsf{parbox}.
%    Therefore it is not enough to set a \cs{@arstrut} at the
%    beginning and at the end of the \textsf{parbox}. This would
%    dimension the distance
%    between first and second line and the distance between the two
%    last lines of the \textsf{parbox} wrongly.
%    To prevent this we set an invisible rule of height
%    \cs{@arstrutbox}
%    at the beginning of the \textsf{parbox}. This has no effect on the
%    depth of the first line. At the end of the \textsf{parbox} we set
%    analogously another invisible rule which only affects the depth
%    of the last line. It is necessary to wait inserting this strut
%    until the paragraph actually starts to allow for things like
%    \cs{parindent} changes via \verb?>{...}?.
%    \begin{macrocode}
   \everypar{%
      \vrule \@height \ht\@arstrutbox \@width \z@
      \everypar{}}%
   }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@endpbox}
%    If there are any declarations defined by \verb?>{...}?
%    and \verb?<{...}?
%    they now follow in the macro \cs{@classz} --- the contents
%    of the column in between.
%    So the macro \cs{@endpbox} must insert the \textsf{specialstrut}
%    mentioned earlier and then close the group opened by
%    \cs{@startpbox}.
%    \begin{macrocode}
\def\@endpbox{\@finalstrut\@arstrutbox \egroup\hfil}
%    \end{macrocode}
% \end{macro}
%
%
% \subsubsection{Building and calling \texttt{\textbackslash halign}}
%
% \begin{macro}{\@array}
%    After we have discussed the macros needed for the evaluation
%    of the user preamble we can define the macro \cs{@array}
%    which uses these macros to create a \cs{halign}.
%    It has two arguments. The first one is a position argument
%    which can be \texttt{t}, \texttt{b} or \texttt{c}; the
%    second one describes the wanted preamble,
%    e.g.\ it has the form \verb?|c|c|c|?.
%    \begin{macrocode}
\def\@array[#1]#2{%
%    \end{macrocode}
%    First we define a \textsf{strut} whose size basically corresponds
%     to a normal \textsf{strut} multiplied by the factor
%    \cs{arraystretch}.
%    This \textsf{strut} is then  inserted into every row and enforces
%     a minimal distance between two rows.
%    Nevertheless, when using horizontal lines, large letters
%    (like accented capital letters) still collide with such lines.
%    Therefore at first we add to the height of a normal \textsf{strut}
%    the value of the parameter \cs{extrarowheight}.
%    \begin{macrocode}
  \@tempdima \ht \strutbox
  \advance \@tempdima by\extrarowheight
  \setbox \@arstrutbox \hbox{\vrule
             \@height \arraystretch \@tempdima
             \@depth \arraystretch \dp \strutbox
             \@width \z@}%
%    \end{macrocode}
%    Then we open a group, in which the user preamble is evaluated by
%    the macro \cs{@mkpream}. As we know this must happen locally.
%    This macro creates a preamble for a \cs{halign} and saves
%    its result globally in the control sequence \cs{@preamble}.
%    \begin{macrocode}
  \begingroup
  \@mkpream{#2}%
%    \end{macrocode}
%    We again redefine \cs{@preamble} so that a call up of \cs{@preamble}
%    now starts the \cs{halign}. Thus also the arguments of \texttt{>},
%    \texttt{<}, \texttt{@} and \texttt{!}, saved in the
%    \textsf{token} registers are inserted into the preamble.  The
%    \cs{tabskip} at the beginning and end of the preamble is set to
%    \textsf{0pt} (in the beginning by the use of \cs{ialign}). Also the
%    command \cs{@arstrut} is build in, which inserts the
%    \cs{@arstrutbox}, defined above. Of course, the opening brace after
%    \cs{ialign} has to be implicit as it will be closed in \cs{endarray}
%    or another macro.
%
%    The \cs{noexpand} in front of \cs{ialign} does no harm in standard \LaTeX{}
%    and was added since some experimental support for using text glyphs in math
%    redefines \cs{halign} with the result that is becomes expandable with
%    disastrous results in cases like this. 
%    In the kernel definition for this macro the problem does
%    not surface because there \cs{protect} is set (which is not necessary in this 
%    implementation as there is no arbitrary user input that can get expanded) and
%    the experimental code made the redefinition robust. Whether this is the right
%    approach is open to question; consider the \cs{noexpand} a curtesy to allow an
%    unsupported redefinition of a \TeX{} primitive for the moment (as people rely
%    on that experimental code).
%    \begin{macrocode}
  \xdef\@preamble{\noexpand \ialign \@halignto
                  \bgroup \@arstrut \@preamble
                          \tabskip \z@ \cr}%
%    \end{macrocode}
%    What we have not explained yet is the macro \cs{@halignto}
%    that was just used. Depending on its replacement text the
%    \cs{halign} becomes a \cs{halign} \texttt{to} \meta{dimen}.
%    Now we close the group again. Thus
%    \cs{@startpbox} and \cs{@endpbox} as well as all
%    \textsf{token} registers get their former meaning back.
%    \begin{macrocode}
  \endgroup
%    \end{macrocode}
%     To support other packages  we include a hook
%     into this part of the code which is a no-op in the main package.
%    \begin{macrocode}
  \@arrayleft
%    \end{macrocode}
%    Now we decide depending on the position argument in which
%    \textsf{box} the \cs{halign} is to be put. (\cs{vcenter} may be used
%    because we are in math mode.)
%    \begin{macrocode}
  \if #1t\vtop \else \if#1b\vbox \else \vcenter \fi \fi
%    \end{macrocode}
%    Now another implicit opening brace appears; then definitions
%    which shall stay local follow. While constructing the
%    \cs{@preamble} in \cs{@mkpream} the \verb?#? sign must be
%    hidden in the macro \cs{@sharp} which is \cs{let} to
%    \cs{relax} at that moment (see definition of \cs{@mkpream}).
%    All these now get their actual meaning.
%    \begin{macrocode}
  \bgroup
  \let \@sharp ##\let \protect \relax
%    \end{macrocode}
%    With the above defined \textsf{struts} we fix down the distance
%    between rows by setting \cs{lineskip} and \cs{baselineskip}
%    to \textsf{0pt}. Since there have to be set \verb?$?'s
%    around every column in the \textsf{array}--environment
%     the parameter \cs{mathsurround} should
%    also be set to \textsf{0pt}. This prevents additional space between
%    the rows. The
%    \PlainTeX--macro \cs{m@th} does this.
%    \begin{macrocode}
  \lineskip \z@
  \baselineskip \z@
  \m@th
%    \end{macrocode}
%    Beside, we have to assign a special meaning (which we still have
%    to specify) to the line separator \verb?\\?. We also have to
%    redefine the command \cs{par} in such a way that empty lines in
%    \cs{halign} cannot do any damage. We succeed in doing  so
%    by choosing something that will disappear when expanding.
%    After that we only have to call up \cs{@preamble} to
%    start the wanted \cs{halign}.
%    \begin{macrocode}
  \let\\\@arraycr \let\tabularnewline\\\let\par\@empty \@preamble}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\extrarowheight}
%    The \textsf{dimen} parameter used above also needs to be
%    allocated.  As a default value we use \textsf{0pt}, to ensure
%    compatibility with standard \LaTeX.
%    \begin{macrocode}
\newdimen \extrarowheight
\extrarowheight=0pt
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@arstrut}
%    Now the insertion of \cs{@arstrutbox} through \cs{@arstut}
%    is easy since we know exactly in which mode \TeX\ is while working
%    on the \cs{halign} preamble.
%    \begin{macrocode}
\def\@arstrut{\unhcopy\@arstrutbox}
%    \end{macrocode}
% \end{macro}
%
%
% \subsubsection{The line separator \texttt{\textbackslash\textbackslash}}
%
% \begin{macro}{\@arraycr}
%    In the macro \cs{@array} the line separator \verb?\\? is
%    \cs{let} to the command \cs{@arraycr}.
%    Its definition starts with a special brace which I have directly
%    copied from the original definition. It is
%    necessary, because the \cs{futurlet} in \cs{@ifnextchar}
%    might
%    expand a  following \verb?&? \textsf{token} in a construction like
%    \verb?\\ &?. This would otherwise end the alignment template at a
%    wrong time. On the other hand we have to be careful to avoid
%    producing a real group, i.e., \verb?{}?, because the command will also
%    be used for the array environment, i.e., in math mode. In that
%    case an extra \verb?{}? would produce an ord atom which could mess up
%    the spacing. For this reason we use a combination that does not
%    really produce a group at all but modifies the master counter so
%    that a \verb?&? will not be considered belonging to the current
%    \cs{halign} while we are looking for a \verb?*? or \verb?[?.
%    For further information see
%    \cite[Appendix D]{bk:knuth}.
%    \begin{macrocode}
\def\@arraycr{\relax\iffalse{\fi\ifnum 0=`}\fi
%    \end{macrocode}
%    Then we test whether the user is using the star form and ignore
%    a possible star (I also disagree with this procedure, because a
%    star does not make any sense here).
%    \begin{macrocode}
  \@ifstar \@xarraycr \@xarraycr}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@xarraycr}
%    In the command \cs{@xarraycr} we test if an optional argument
%    exists.
%    \begin{macrocode}
\def\@xarraycr{\@ifnextchar [%
%    \end{macrocode}
%    If it does, we branch out into the macro \cs{@argarraycr} if
%    not we close the special brace (mentioned above) and end the row
%    of the \cs{halign} with a \cs{cr}.
%    \begin{macrocode}
  \@argarraycr {\ifnum 0=`{}\fi\cr}}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@argarraycr}
%    If additional space is requested by the user this case is treated
%    in the macro \cs{@argarraycr}. First we close the special brace
%    and then we test if the additional space is positive.
%    \begin{macrocode}
\def\@argarraycr[#1]{\ifnum0=`{}\fi\ifdim #1>\z@
%    \end{macrocode}
%    If this is the case we create an invisible vertical rule with
%    depth \verb?\dp\@arstutbox?${}+{}$\meta{wanted\ space}.
%    Thus we achieve that all vertical lines specified
%    in the user preamble by a \verb?|? are now
%    generally drawn.
%    Then the row ends with a \cs{cr}.
%
%    If the space is negative we end the row at once with a \cs{cr}
%    and move back up with a \cs{vskip}.
%
%    While testing these macros I found out that the
%    \cs{endtemplate}
%    created by \cs{cr} and \verb?&? is something like an
%    \cs{outer} primitive and therefore it should not appear in
%    incomplete \cs{if} statements. Thus the following solution was
%    chosen which hides the \cs{cr} in other macros when \TeX\
%    is skipping conditional text.
%    \begin{macrocode}
  \expandafter\@xargarraycr\else 
  \expandafter\@yargarraycr\fi{#1}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@xargarraycr}
% \begin{macro}{\@yargarraycr}
%    The following macros were already explained above.
%    \begin{macrocode}
\def\@xargarraycr#1{\unskip
  \@tempdima #1\advance\@tempdima \dp\@arstrutbox
  \vrule \@depth\@tempdima \@width\z@ \cr}
\def\@yargarraycr#1{\cr\noalign{\vskip #1}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
%
%
% \subsubsection{Spanning several columns}
%
% \begin{macro}{\multicolumn}
%    If several columns should be held together with a special format
%    the command \cs{multicolumn} must be used. It has three
%    arguments: the number of columns to be covered; the format for
%    the result column and the actual column entry.
%    \begin{macrocode}
\long\def\multicolumn#1#2#3{%
%    \end{macrocode}
%    First we combine the given number of columns into a single one;
%    then we start a new block so that the following definition is kept
%    local.
%    \begin{macrocode}
   \multispan{#1}\begingroup
%    \end{macrocode}
%    Since a \cs{multicolumn} should only describe the format of a
%    result column, we redefine \cs{@addamp} in such a way that one gets
%    an error message if one uses more than one \texttt{c},
%    \texttt{l}, \texttt{r}, \texttt{p}, \texttt{m} or \texttt{b} in
%    the second argument. One should consider that this definition is
%    local to the build-up of the preamble; an \textsf{array}-- or
%    \textsf{tabular}--environment in the third argument of the
%    \cs{multicolumn} is therefore worked through correctly as well.
%    \begin{macrocode}
   \def\@addamp{\if@firstamp \@firstampfalse \else
                \@preamerr 5\fi}%
%    \end{macrocode}
%    Then we evaluate the second argument with the help of
%    \cs{@mkpream}.
%    Now we still have to insert the contents of the \textsf{token}
%    register into the \cs{@preamble}, i.e.\ we have to say
%    \verb?\xdef\@preamble{\@preamble}?. This is achieved shorter by
%    writing:
%    \begin{macrocode}
   \@mkpream{#2}\@addtopreamble\@empty
%    \end{macrocode}
%    After the \cs{@preamble} is created we forget all local
%    definitions and occupations of the \textsf{token} registers.
%    \begin{macrocode}
   \endgroup
%    \end{macrocode}
%    In the special situation of  \cs{multicolumn} \cs{@preamble}
%    is not needed as preamble for a \cs{halign} but it is directly
%    inserted into our table. Thus instead of \cs{sharp}
%    there has to be the column entry (\verb?#3?) wanted by the user.
%    \begin{macrocode}
   \def\@sharp{#3}%
%    \end{macrocode}
%    Now we can pass the \cs{@preamble} to \TeX\ . For safety
%    we start with an \cs{@arstrut}. This should usually be in the
%    template for the first column however we do not know if this
%    template was overwritten by our \cs{multicolumn}.
%    We also add a \cs{null} at the right end to prevent any following
%    \cs{unskip} (for example from \verb?\\[..]?) to remove the \cs{tabcolsep}.
%    \begin{macrocode}
   \@arstrut \@preamble
   \null
   \ignorespaces}
%    \end{macrocode}
% \end{macro}
%
%
%
%   \subsubsection{The environment definitions}
%
% After these preparations we are able to define the environments. They
% only differ in the initialisations of \cs{d@llar...}, \cs{col@sep}
%  and \cs{@halignto}.
%
% \begin{macro}{\@halignto}
% \begin{macro}{\d@llarbegin}
% \begin{macro}{\d@llarend}
%    In order to relieve the \textsf{save stack} we assign the
%    replacement texts for \cs{@halignto} globally. \cs{d@llar} has to be
%    local since otherwise nested \textsf{tabular} and \textsf{array}
%    environments (via \cs{multicolumn}) are impossible.
%    When the new font selection scheme is in force we have to 
%    we surround all \cs{halign} entries 
%    with braces. See remarks in TUGboat 10\#2. Actually we are going
%    to use \cs{begingroup} and \cs{endgroup}. However, this is only
%    necessary when we are in text mode. In math the surrounding
%    dollar signs will already serve as the necessary extra grouping
%    level. Therefore we switch the settings of \cs{d@llarbegin} and
%    \cs{d@llarend} between groups and dollar signs.
%    \begin{macrocode}
\let\d@llarbegin\begingroup
\let\d@llarend\endgroup
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\array}
%    Our new definition of \cs{array} then reads:
%    \begin{macrocode}
\def\array{\col@sep\arraycolsep
  \def\d@llarbegin{$}\let\d@llarend\d@llarbegin\gdef\@halignto{}%
%    \end{macrocode}
%    Since there might be an optional argument we call another
%    macro which is also used by the other environments.
%    \begin{macrocode}
  \@tabarray}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@tabarray}
%    This macro tests for an optional bracket and then calls up
%    \cs{@@array} or \verb?\@@array[c]? (as default).
%    \begin{macrocode}
\def\@tabarray{\@ifnextchar[{\@@array}{\@@array[c]}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\@@array}
%    This macro could then test an optional delimiter before the
%    left brace of the main preamble argument. At this point 
%    it simply is let to be \cs{@array}.
%    \begin{macrocode}
\let\@@array\@array
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\tabular}
% \begin{macro}{\tabular*}
%    The environments \textsf{tabular} and \textsf{tabular$*$} differ
%    only in the initialisation of the command \cs{@halignto}. Therefore
%    we define
%    \begin{macrocode}
\def\tabular{\gdef\@halignto{}\@tabular}
%    \end{macrocode}
%     and analoguesly for the star form. We evalute the argument first 
%     using \cs{setlength} so that users of the \texttt{calc} package can
%     write code like\\ \verb?\begin{tabular*}{(\columnwidth-1cm)/2}...?
%     \begin{macrocode}
\expandafter\def\csname tabular*\endcsname#1{%
       \setlength\dimen@{#1}%
       \xdef\@halignto{to\the\dimen@}\@tabular}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@tabular}
%    The rest of the job is carried out by the \cs{@tabular} macro:
%    \begin{macrocode}
\def\@tabular{%
%    \end{macrocode}
%    First of all we have to make sure that we start out in
%    \textsf{hmode}.  Otherwise we might find our table dangling by
%    itself on a line.
%    \begin{macrocode}
  \leavevmode
%    \end{macrocode}
%    It should be taken into consideration that the macro \cs{@array}
%    must be called in math mode. Therefore we open a \textsf{box},
%    insert a \verb?$? and then assign the correct values to \cs{col@sep} and
%    \cs{d@llar...}.
%    \begin{macrocode}
  \hbox \bgroup $\col@sep\tabcolsep \let\d@llarbegin\begingroup
                                    \let\d@llarend\endgroup
%    \end{macrocode}
%    Now everything \textsf{tabular} specific is done and we are able to
%    call the \cs{@tabarray} macro.
%    \begin{macrocode}
  \@tabarray}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\endarray}
% \begin{macro}{\@arrayleft}
% \begin{macro}{\@arrayright}
%    When the processing of \textsf{array} is finished we have to
%    close the \cs{halign}
%    and afterwards the surrounding \textsf{box} selected by
%    \cs{@array}. To save \textsf{token} space we then redefine
%    \cs{@preamble}
%    because its replacement text isn't longer needed.
%    We have to declare the hook, \cs{@arrayleft}, we put into \cs{@array} above.
%    A similar hook `\cs{@arrayright}' is inserted into the
%    \cs{endarray} to gain control. Both defaults to empty.
%    \begin{macrocode}
\def\endarray{\crcr \egroup \egroup \@arrayright \gdef\@preamble{}}
\let\@arrayleft\@empty
\let\@arrayright\@empty
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\endtabular}
% \begin{macro}{\endtabular*}
%    To end a \textsf{tabular} or \textsf{tabular$*$} environment we
%    call up \cs{endarray}, close the math mode and then the surrounding
%    \cs{hbox}.
%    \begin{macrocode}
\def\endtabular{\endarray $\egroup}
\expandafter\let\csname endtabular*\endcsname=\endtabular
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% We should \cs{let} all macros
% to \cs{relax} that were used in the kernel but are no longer
%  necessary.
%    \begin{macrocode}
\let\@ampacol=\relax        \let\@expast=\relax
\let\@arrayclassiv=\relax   \let\@arrayclassz=\relax
\let\@tabclassiv=\relax     \let\@tabclassz=\relax
\let\@arrayacol=\relax      \let\@tabacol=\relax
\let\@tabularcr=\relax      \let\@@endpbox=\relax
\let\@argtabularcr=\relax   \let\@xtabularcr=\relax
%    \end{macrocode}
%
% \begin{macro}{\@preamerr}
%    We also have to redefine the error routine \cs{@preamerr} since
%    new kind of errors are possible.
%    The code for this macro is not perfect yet;
%    it still needs too much memory.
%    \begin{macrocode}
\def\@preamerr#1{\def\@tempd{{..} at wrong position: }%
   \ClassError{memoir}{%
   \ifcase #1 Illegal pream-token (\@nextchar): `c' used\or %0
    Missing arg: token ignored\or                           %1
    Empty preamble: `l' used\or                             %2
    >\@tempd token ignored\or                               %3
    <\@tempd changed to !{..}\or                            %4
    Only one column-spec. allowed.\fi}\@ehc}                %5

%    \end{macrocode}
% \end{macro}
%
%
%
% \subsubsection{Defining your own column specifiers}
%
%
% \begin{macro}{\newcolumntype}
%    In \texttt{newarray.sty} the macro for specifying new columns was
%    named \cs{newcolumn}. When the functionality was added to
%    \texttt{array.sty} the command was renamed \cs{newcolumntype}.
% The \cs{newcolumntype} macro gives users the chance
% to define letters, to be used in the same way as the primitive
% column specifiers, `c' `p' etc.
%    \begin{macrocode}
\def\newcolumntype#1{%
%    \end{macrocode}
% \cs{NC@char} is so that active characters, like \verb?@? in
% AMS\LaTeX\ may be used. Note that we need to use the possibly active token,
% \verb?#1?, in several places, as that is the token that actually
% appears in the preamble argument.
%    \begin{macrocode}
  \edef\NC@char{\string#1}%
%    \end{macrocode}
% First we check whether there is already a definition for this column.
% Unlike \cs{newcommand} we give a warning rather than an error if it is
% defined. If it is a new column, add \cs{NC@do} \meta{column} to
% the list \cs{NC@list}.
%    \begin{macrocode}
  \@ifundefined{NC@find@\NC@char}%
    {\@tfor\next:=<>clrmbp@!|\do{\if\noexpand\next\NC@char
        \@memwarn{Redefining primitive column \NC@char}\fi}%
     \NC@list\expandafter{\the\NC@list\NC@do#1}}%
    {\@memwarn{Column \NC@char\space is already defined}}%
%    \end{macrocode}
% Now we define a macro with an argument delimited by the new column
% specifier, this is used to find occurences of this specifier in the
% user preamble.
%    \begin{macrocode}
  \@namedef{NC@find@\NC@char}##1#1{\NC@{##1}}%
%    \end{macrocode}
% If an optional argument was not given, give a default argument of 0.
%    \begin{macrocode}
  \@ifnextchar[{\newcol@{\NC@char}}{\newcol@{\NC@char}[0]}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\newcol@}
% We can now define the macro which does the rewriting,
% \cs{@reargdef} takes the same arguments as \cs{newcommand}, but
% does not check that the command is new. For a column, say `D' with
% one argument, define a command \cs{NC@rewrite@D} with one
% argument, which recursively calls \cs{NC@find} on the user preamble
% after replacing the first token or group with the replacement text
% specified in the \cs{newcolumntype} command. \cs{NC@find} will find the
% next occurrence of `D' as it will be \cs{let} equal to
% \cs{NC@find@D} by \cs{NC@do}.
%    \begin{macrocode}
\def\newcol@#1[#2]#3{\expandafter\@reargdef
     \csname NC@rewrite@#1\endcsname[#2]{\NC@find#3}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\NC@}
% Having found an occurence of the new column, save the preamble
% before the column in \cs{@temptokena}, then check to see if we
% are at the end of the preamble. (A dummy occurrence of the column
% specifier will be placed at the end of the preamble by \cs{NC@do}).
%    \begin{macrocode}
\def\NC@#1{%
  \@temptokena\expandafter{\the\@temptokena#1}\futurelet\next\NC@ifend}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\NC@ifend}
% We can tell that we are at the end as \cs{NC@do} will place a \cs{relax}
% after the dummy column.
%    \begin{macrocode}
\def\NC@ifend{%
%    \end{macrocode}
% If we are at the end, do nothing. (The whole preamble will now be in
% \cs{@temptokena}.)
%    \begin{macrocode}
  \ifx\next\relax
%    \end{macrocode}
% Otherwise set the flag \cs{if@tempswa}, and rewrite the column.
%    \begin{macrocode}
    \else\@tempswatrue\expandafter\NC@rewrite\fi}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\NC@do}
% If the user has specified `C' and `L' as new columns, the list of
% rewrites (in the token register \cs{NC@list}) will look like
% \verb?\NC@do *? \verb?\NC@do C? \verb?\NC@do L?.
% So we need to define \cs{NC@do} as a one argument macro which
% initialises the rewriting of the specified column. Let us assume that
% `C' is the argument.
%    \begin{macrocode}
\def\NC@do#1{%
%    \end{macrocode}
% First we let \cs{NC@rewrite} and \cs{NC@find} be
% \cs{NC@rewrite@C} and \cs{NC@find@C} respectively.
%    \begin{macrocode}
  \expandafter\let\expandafter\NC@rewrite
    \csname NC@rewrite@\string#1\endcsname
  \expandafter\let\expandafter\NC@find
    \csname NC@find@\string#1\endcsname
%    \end{macrocode}
% Clear the token register \cs{@temptokena} after putting the present
% contents of the register in front of the token \cs{NC@find}. At the
% end we place the tokens `\verb?C\relax?' which \cs{NC@ifend} will use
% to detect the end of the user preamble.
%    \begin{macrocode}
  \expandafter\@temptokena\expandafter{\expandafter}%
        \expandafter\NC@find\the\@temptokena#1\relax}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\showcols}
% This macro is useful for debugging \cs{newcolumntype} specifications,
% it is the equivalent of the primitive \cs{show} command for macro
% definitions.  All we need to do is locally redefine \cs{NC@do} to take
% its argument (say `C') and then \cs{show} the (slightly modified)
% definition of \cs{NC@rewrite@C}. Actually as the the list always
% starts off with \verb?\NC@do *? and we do not want to print the
% definition of the $*$-form, define \cs{NC@do} to throw away the first
% item in the list, and then redefine itsef to print the rest of the
% definitions.
%    \begin{macrocode}
\def\showcols{{\def\NC@do##1{\let\NC@do\NC@show}\the\NC@list}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\NC@show}
% If the column `C' is defined as above, then
% \verb?\show\NC@rewrite@C? would output\\
% \verb?\long macro: ->\NC@find >{$}c<{$}?.
% We want to strip the \verb?\long macro: ->? and the \cs{NC@find}. So first we
% use \cs{meaning} and then apply the macro \cs{NC@strip} to the tokens so
% produced and then \cs{typeout} the required string.
%    \begin{macrocode}
\def\NC@show#1{%
  \typeout{Column #1\expandafter\expandafter\expandafter\NC@strip
  \expandafter\meaning\csname NC@rewrite@#1\endcsname\@@}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\NC@strip}
% Delimit the arguments to \cs{NC@strip} with `\texttt{:}', `\texttt{->}',
% a space, and \cs{@@} to pull out the required parts of the output from
% \cs{meaning}.
%    \begin{macrocode}
\def\NC@strip#1:#2->#3 #4\@@{#2 -> #4}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\NC@list}
% Allocate the token register used for the rewrite list.
%    \begin{macrocode}
\newtoks\NC@list
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{The $*$--form}
% We view the $*$-form as a slight generalisation of the system
% described in the previous subsection. The idea is to define a $*$
% column by a command of the form:
% \begin{verbatim}
% \newcolumntype{*}[2]{%
%    \count@=#1\ifnum\count@>0
%       \advance\count@ by -1 #2*{\count@}{#2}\fi}
% \end{verbatim}
% \begin{macro}{\NC@rewrite@*}\label{NC@rewrite@*}
% This does not work however as \cs{newcolumntype} takes great care not
% to expand anything in the preamble, and so the \cs{if} is never
% expanded. \cs{newcolumntype} sets up various other parts of the
% rewrite correctly though so we can define:
%    \begin{macrocode}
\newcolumntype{*}[2]{}
%    \end{macrocode}
% Now we must correct the definition of \cs{NC@rewrite@*}. The
% following is probably more efficient than a direct translation of
% the idea sketched above, we do not need to put a $*$ in the preamble
% and call the rewrite recursively, we can just put \verb?#1? copies of
% \verb?#2? into \cs{@temptokena}. (Nested $*$ forms will be expanded
% when the whole rewrite list is expanded again, see \cs{@mkpream})
% \changes{v1.618}{2005/09/03}{Fixed original bug in \cs{NC@rewrite@*} (mempatch v3.3)}
%    \begin{macrocode}
\long\@namedef{NC@rewrite@*}#1#2{%
%    \end{macrocode}
% Store the number.
%    \begin{macrocode}
  \count@#1\relax
%    \end{macrocode}
% Put \verb?#1? copies of \verb?#2? in the token register.
%    \begin{macrocode}
  \loop
  \ifnum\count@>\z@
    \advance\count@\m@ne
    \@temptokena\expandafter{\the\@temptokena#2}%
  \repeat
%    \end{macrocode}
% \cs{NC@do} will ensure that \cs{NC@find} is \cs{let} equal
% to \cs{NC@find@*}.
%    \begin{macrocode}
  \NC@find}

%    \end{macrocode}
% \end{macro}
%
%
% \subsection{Getting the spacing around rules right}
%
% The Companion~\cite[p.137]{GOOSSENS94} suggests two additional
% commands to control the allignments in case of tabulars with
% horizontal lines. They are now added to this package.
%
%  \begin{macro}{\extratabsurround}
%    The extra space around a table when \cs{firsthline} or \cs{lasthline}
%    are used.
%    \begin{macrocode}
\newlength{\extratabsurround}
\setlength{\extratabsurround}{2pt}
%    \end{macrocode}
%  \end{macro}
%
% \begin{macro}{\backup@length}
%    This register will be used internally by \cs{firsthline} and
%    \cs{lasthline}.
%    \begin{macrocode}
\newlength{\backup@length}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\firsthline}
%    This code can probably be improved but for the moment it should
%    serve.
%
%    We start by producing a single tabular row without any visible
%    content that will produce the external reference point in case
%    \verb?[t]? is used.
%    \begin{macrocode}
\newcommand{\firsthline}{%
  \multicolumn1c{%
%    \end{macrocode}
%    Within this row we calculate \cs{backup@length} to be the height
%    plus depth of a standard line. In addition we have to add the
%    width of the \cs{hline}, something that was forgotten in the
%    original definition.
%    \begin{macrocode}
    \global\backup@length\ht\@arstrutbox
    \global\advance\backup@length\dp\@arstrutbox
    \global\advance\backup@length\arrayrulewidth
%    \end{macrocode}
%    Finally we do want to make the height of this first line be a bit
%    larger than usual, for this we place the standard array strut
%    into it but raised by \cs{extratabsurround}
%    \begin{macrocode}
     \raise\extratabsurround\copy\@arstrutbox
%    \end{macrocode}
%    Having done all this we end the line and back up by the value of
%    \cs{backup@length} and then finally place our \cs{hline}. This should
%    place the line exactly at the right place but keep the reference
%    point of the whole tabular at the baseline of the first row.
%    \begin{macrocode}
    }\\[-\backup@length]\hline
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\lasthline}
%    For \cs{lasthline} the situation is even worse and I got it
%    completely wrong initially.
%
%    The problem in this case is that if the optional argument \verb?[b]?
%    is used we do want the reference point of the tabular be at the
%    baseline of the last row but at the same time do want the the
%    depth of this last line increased by \cs{extratabsurround} without
%    changing the placement \cs{hline}.
%
%    We start by placing the rule followed by an invisible row.
%    \begin{macrocode}
\newcommand{\lasthline}{\hline\multicolumn1c{%
%    \end{macrocode}
%    We now calculate \cs{backup@length} to be the height and depth of
%    two lines plus the width of the rule.
%    \begin{macrocode}
    \global\backup@length2\ht\@arstrutbox
    \global\advance\backup@length2\dp\@arstrutbox
    \global\advance\backup@length\arrayrulewidth
%    \end{macrocode}
%    This will bring us back to the baseline of the second last row:
%    \begin{macrocode}
    }\\[-\backup@length]%
%    \end{macrocode}
%    Thus if we now add another invisible row the reference point of
%    that row will be at the baseline of the last row (and will be the
%    reference for the whole tabular). Since this row is invisible we
%    can enlarge its depth by the desired amount.
%    \begin{macrocode}
    \multicolumn1c{%
       \lower\extratabsurround\copy\@arstrutbox
       }%
}
%    \end{macrocode}
% \end{macro}
%
%
%    Beside a larger functionality \file{array.sty} has one
%    important difference to the standard \texttt{tabular} and
%    \texttt{array} environments: horizontal and vertical rules make a
%    table larger or wider, e.g., \cs{doublerulesep} really denotes
%    the space between two rules and isn't measured from the middle of
%    the rules.
%
%  \begin{macro}{\@xhline}
%    For vertical rules this is implemented by the definitions above,
%    for horizontal rules we have to take out the backspace.
%    \begin{macrocode}
\CheckCommand*\@xhline{\ifx\reserved@a\hline
               \vskip\doublerulesep
               \vskip-\arrayrulewidth
             \fi
      \ifnum0=`{\fi}}
\renewcommand*\@xhline{\ifx\reserved@a\hline
               \vskip\doublerulesep
             \fi
      \ifnum0=`{\fi}}

%    \end{macrocode}
% \end{macro}
%
%
%
%    \begin{macrocode}
%%%%%%%%%%% end Array package code %%%%%%%%%%%%%%%%%%%%%

%    \end{macrocode}
%
% \subsection{D column specifiers} \label{sec:dcolumns}
%
% The description and code are essentially copied from the 
% \Lpack{dcolumn} package~\cite{DCOLUMN}.
%
%    \begin{macrocode}
%%%%%%%%%% Dcolumn package code %%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% With acknowledgements to David Carlisle %%%%%%%%%%%%%%%%%%%%%

%    \end{macrocode}
%
%
% The \Lpack{dcolumn} package~\cite{DCOLUMN} defines \texttt{D} 
% to be a column specifier
% with three arguments.\\
% \verb?D?\marg{sep.tex}\marg{sep.dvi}\marg{decimal places}
% for columns which are to be aligned on a `decimal point'.
% 
%
% The basic ideas behind these macros have been explained earlier.
% However they use three
% tricks which may be useful in other contexts.
% \begin{itemize}
% \item The separator is surrounded in extra \verb?{ }?, so that it is set
% with \cs{mathord} spacing, otherwise, for instance a `,' would have
% extra space after it.
% \item The separator is not given its special definition by making it
% active, as this would not work for an entry such as \verb?& .5 &?, as the
% first token of an alignment entry is read \emph{before} the preamble
% part, incase it is an \cs{omit}, in which case the preamble is to be
% omitted. Instead we switch the mathcode to (hex) 8000, which makes the
% token act as if it were active.
% \item Although \verb?\mathcode`.="8000?  makes \texttt{.} act as if it
% were active, it is still not allowed in constructions such as
% \verb?\def.{}?, even in math-mode, so we have to construct an active
% version of the separator, this is done by making it the uppercase of
% \verb?~?, and then using the construct\\
% \verb?\uppercase{\def~}?\marg{definition}.\\
% Note that the \meta{definition} is not uppercased, so the definition
% can refer to the standard, non-active use of the separator.
% \end{itemize}
%
% \begin{macro}{\DC@}
% Set up uppercase tables as required, and then grab the first part of
% the numerical argument into \cs{count@}.
%    \begin{macrocode}
\def\DC@#1#2#3{%
  \uccode`\~=`#1\relax
  \m@th
  \afterassignment\DC@x\count@#3\relax{#1}{#2}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\DC@x}
% If \cs{count@} is negative, centre on the decimal point. If it is
% positive either \verb?#1? will be empty in which case bad out decimal
% part to the number of digits specified by \cs{count@} or 
% it is none empty in which case \cs{count@} contains the
% number of digits to the left of the point, and \verb?#1? contains a junk
% token (probably \verb?.?) followed by the number of digits to the right
% of the point. In either of these latter cases, \cs{DC@right} is used.
%    \begin{macrocode}
\def\DC@x#1\relax#2#3{%
  \ifnum\z@>\count@
    \expandafter\DC@centre
  \else
    \expandafter\DC@right
  \fi
  {#2}{#3}{#1}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\DC@centre}
% If centering on the decimal point, just need to box up the two halves.
%    \begin{macrocode}
\def\DC@centre#1#2#3{%
  \let\DC@end\DC@endcentre
  \uppercase{\def~}{$\egroup\setbox\tw@=\hbox\bgroup${#2}}%
  \setbox\tw@=\hbox{${\phantom{{#2}}}$}%
  \setbox\z@=\hbox\bgroup$\mathcode`#1="8000 }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\DC@endcentre}
% and then pad out the smaller of the two boxes so there is the same
% amount of stuff either side of the point.
%    \begin{macrocode}
\def\DC@endcentre{$\egroup
    \ifdim \wd\z@>\wd\tw@
      \setbox\tw@=\hbox to\wd\z@{\unhbox\tw@\hfill}%
    \else
      \setbox\z@=\hbox to\wd\tw@{\hfill\unhbox\z@}\fi
    \box\z@\box\tw@}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\DC@right}
% This deals with both the cases where a specified number of decimal
% places is given.
%    \begin{macrocode}
\def\DC@right#1#2#3{%
  \ifx\relax#3\relax
%    \end{macrocode}
% If \verb?#3? is empty, add \cs{hfill} to right align the column, and 
% Just set \cs{DC@rl} to begin a group, so nothing fancy is done with
% the whole number part.
%    \begin{macrocode}
    \hfill
    \let\DC@rl\bgroup
  \else
%    \end{macrocode}
% Otherwise  set \cs{DC@rl} so that the whole number part is put in a
% box \cs{count@} times as wide as a digit.
% In order to share code with the other branch, then move \verb?#3? (the
% number of decimal places) into \cs{count@} throwing away the `.' from
% the user syntax.
%    \begin{macrocode}
    \edef\DC@rl{to\the\count@\dimen@ii\bgroup\hss\hfill}%
    \count@\@gobble#3\relax
  \fi
%    \end{macrocode}
%
%    \begin{macrocode}
  \let\DC@end\DC@endright
%    \end{macrocode}
% Box 2 contains the decimal part, set to \cs{dimen@} which is
% calculated below to be \cs{count@} times the width of a digit, plus
% the width of the `decimal point'.
%    \begin{macrocode}
  \uppercase{\def~}{$\egroup\setbox\tw@\hbox to\dimen@\bgroup${#2}}%
   \setbox\z@\hbox{$1$}\dimen@ii\wd\z@
   \dimen@\count@\dimen@ii
   \setbox\z@\hbox{${#2}$}\advance\dimen@\wd\z@
   \setbox\tw@\hbox to\dimen@{}%
%    \end{macrocode}
% Box 0 contains the whole number part, either just at its natural
% size for right aligned columns, or set to (the old value of)
% \cs{count@} times the width of a digit. \cs{DC@rl} defined above
% determines the two cases.
%    \begin{macrocode}
   \setbox\z@\hbox\DC@rl$\mathcode`#1="8000 }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\DC@endright}
% Just finish off the second box, and then put out both boxes.
%    \begin{macrocode}
\def\DC@endright{$\hfil\egroup\box\z@\box\tw@}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{D}
% The user interface, define the {\tt D} column to take three arguments.
% For special purposes, you may need to directly access \cs{DC@} rather
% than the \verb?D? column, eg to get a bold version you could use
%\begin{verbatim}
% \newcolumntype{E}[3]{>{\boldmath\DC@{#1}{#2}{#3}}c<{\DC@end}}
%\end{verbatim}
%    \begin{macrocode}
\newcolumntype{D}[3]{>{\DC@{#1}{#2}{#3}}c<{\DC@end}}

%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
%%%%%%%%%% end Dcolumn package code %%%%%%%%%%%%%%%%%%%%%

%    \end{macrocode}
%
% \subsection{Support for delimeters}
%
% The description and code are essentially from the 
% \Lpack{delarray} package~\cite{DELARRAY}, v1.01 1994/03/14.
%
%    \begin{macrocode}
%%%%%%%%%% Delarray package code %%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% With acknowledgements to David Carlisle %%%%%%%%%%%%%%%%%%%%%

%    \end{macrocode}
%
%
% The array syntax is extended by
% supporting the notation of delimiters. To this end we extend the
% array parsing mechanism to include a hook which can be used by this
% (or another) package to do some additional parsing.
%
%
% \begin{macro}{\@@array}
%    This macro tests for an optional delimiter before the left brace
%    of the main preamble argument. If there is no delimiter,
%    \cs{@arrayleft} and \cs{@arrayright} are made a no-ops, and
%    \cs{@array} is called with the positional argument. Otherwise
%    call \cs{@del@array}.
%    \begin{macrocode}
\def\@@array[#1]{\@ifnextchar\bgroup
  {\let\@arrayleft\relax\let\@arrayright\relax\@array[#1]}%
  {\@del@array[#1]}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\@del@array}
%    We now know that we have an {\tt array} (or {\tt tabular}) with
%    delimiters.
%    \begin{macrocode}
\def\@del@array[#1]#2#3#4{%
%    \end{macrocode}
% The following line is completely redundant but it does catch errors
% involving delimiters  before the processing of the alignment begins.
% A common error is likely to be omiting the `.' in a
% \cs{cases}-type construction. This causes the first token of the
% alignment to be gobbled, possibly causing lots of spurious errors
% before the cause of the error, the missing delimiter, is discovered as
% \cs{@arrayright} puts the alignment and the delimiters together.
%    \begin{macrocode}
  \setbox\z@\hbox{$\left#2\right#4$}%
%    \end{macrocode}
% In the case of a `c' argument we do not need to rebox the alignment,
% so we can define \cs{@arrayleft} and \cs{@arrayright} just to
% insert the delimiters.
%    \begin{macrocode}
  \if#1c\def\@arrayleft{\left#2}\def\@arrayright{\right#4}%
%    \end{macrocode}
% Otherwise we (should) have a {\tt[t]} or {\tt[b]} argument, so first we
% store the alignment, without delimiters in box0.
%    \begin{macrocode}
  \else\def\@arrayleft{\setbox\z@}%
%    \end{macrocode}
% Then after the alignment is finished:
%    \begin{macrocode}
  \def\@arrayright{%
%    \end{macrocode}
% Calculate the amount the box needs to be lowered (this will be
% negative in the case of \verb?[b]?). A little bit of arithmetic cf.\
% \theTeXbook, Appendix G, rule 8. We calculate the amount this
% way, rather than just taking the difference between the depth of box0
% and the depth of the box defined below, as the depth of that box may
% be affected by the delimiters if \cs{delimitershortfall} or
% \cs{delimiterfactor} have non-standard values.
%    \begin{macrocode}
     \dimen@=\dp\z@
     \advance\dimen@-\ht\z@
     \divide \dimen@ by \tw@
     \advance\dimen@ by\fontdimen22 \textfont\tw@
%    \end{macrocode}
% Now lower the alignment and the delimiters into place.
%    \begin{macrocode}
     \lower\dimen@\hbox{$\left#2\vcenter{\unvbox\z@}\right#4$}}%
%    \end{macrocode}
% End the \verb?\if#1c?
%    \begin{macrocode}
  \fi
%    \end{macrocode}
% Now that we have defined \cs{@arrayleft} and \cs{@arrayright}, call
% \cs{@array}.
%    \begin{macrocode}
  \@array[#1]{#3}}
%    \end{macrocode}
% \end{macro}
%
%
%    \begin{macrocode}
%%%%%%%%%% end Delarray package code %%%%%%%%%%%%%%%%%%%%%

%    \end{macrocode}
%
% \subsection{The tabularx environment}
%
% The code and description are essentially from David Carlisle's  
% \Lpack{tabularx} package~\cite{TABULARX}, v2.07 1999/01/07.
%
%    \begin{macrocode}
%%%%%%%%%% Tabularx package code %%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% With acknowledgements to David Carlisle %%%%%%%%%%%%%%%%%%%%%

%    \end{macrocode}
%
%
% The \Lpack{tabularx} package~\cite{TABULARX} implements a version of the \texttt{tabular}
% environment in which the widths of certain columns are calculated so
% that the table is is a specified width. The columns that may stretch are
% marked by an \texttt{X} in the preamble argument.
%
%
% First some registers etc.\ that we need.
%    \begin{macrocode}
\newdimen\TX@col@width
\newdimen\TX@old@table
\newdimen\TX@old@col
\newdimen\TX@target
\newdimen\TX@delta
\newcount\TX@cols
\newif\ifTX@
%    \end{macrocode}
%
% Now a trick to get the body of an environment into a token register,
% without doing any expansion. This does not do any real checking of
% nested environments, so if you should need to nest one {\ttfamily
% tabularx} inside another, the inner one must be surrounded by \verb?{ }?.
%
% \begin{macro}{\tabularx}
%  Just save
% the width specification separately, then clear the token register
% \cs{toks@}.
% Finally call \cs{TX@get@body} to begin saving the body of the table.
% The \verb?{\ifnum0=`}\fi? allows \verb?tabularx? to appear
% inside a \cs{halign}.^^A
% \setbox0=\hbox{\footnotesize\verb?\iffalse{\fi\ifnum0=`}\fi?}^^A
% \setbox2=\hbox{\footnotesize\verb?\ifnum0=`{}\fi?}^^A
% \footnote{This adds an extra level of grouping,
% which is not really needed. Instead, I could use \box0\ here, and
% \box2\ below, however the code here would then have to be moved after
% the first line, because of the footnote to page 386 of \theTeXbook,
% and I do not think I should be writing code that is so obscure as to
% be documented in a footnote in an appendix called ``Dirty Tricks''!}
%
% This mechanism of grabbing an environment body does have the
% disadvantage (shared with the AMS alignment environments) that you
% can not make extension environments by code such as
%\begin{verbatim}
%\newenvironment{foo}{\begin{tabularx}{XX}}{\end{tabularx}}
%\end{verbatim}
% as the code is looking for a literal string \verb?\end{tabularx}? to stop
% scanning. One may avoid this problem by using
% \cs{tabularx} and \cs{endtabularx} directly in the definition:
%\begin{verbatim}
%\newenvironment{foo}{\tabularx{XX}}{\endtabularx}
%\end{verbatim}
% The scanner now looks for the end of the current environment (\verb?foo? in
% this example.) There are some restrictions on this usage, the
% principal one being that \cs{endtabularx} is the \emph{first} token of
% the `end code' of the environment.
%    \begin{macrocode}
\def\tabularx#1{%
%    \end{macrocode}
% Allow \cs{tabularx} \cs{endtabularx} (but not \verb?\begin{tabularx}?
% \verb?\end{tabularx}?) to be used in \cs{newenvironment} definitions.
%    \begin{macrocode}
\edef\TX@{\@currenvir}%
  {\ifnum0=`}\fi
%    \end{macrocode}
% \cs{setlength} lets you
% use the \Lpack{calc} package so you could use a width of, say \\
%  \verb?(\textwidth-12pt)/2?.
%    \begin{macrocode}
  \setlength\TX@target{#1}%
  \TX@typeout{Target width: #1 = \the\TX@target.}%
  \toks@{}\TX@get@body}
%    \end{macrocode}
% \end{macro}
%

%  \begin{macro}{\endtabularx}
% This does not do very much\ldots
%    \begin{macrocode}
\let\endtabularx\relax
%    \end{macrocode}
%  \end{macro}
%
% \begin{macro}{\TX@get@body}
% Place all tokens as far as the first \cs{end} into a token register.
% Then call \cs{TX@find@end} to see if we are at \verb?\end{tabularx}?.
%    \begin{macrocode}
\long\def\TX@get@body#1\end
  {\toks@\expandafter{\the\toks@#1}\TX@find@end}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TX@find@end}
% If we are at \verb?\end{tabularx}?, call \cs{TX@endtabularx}, otherwise
% add \verb?\end{...}? to the register, and call \cs{TX@get@body} again.
%    \begin{macrocode}
\def\TX@find@end#1{%
  \def\@tempa{#1}%
  \ifx\@tempa\TX@\expandafter\TX@endtabularx
  \else\toks@\expandafter
    {\the\toks@\end{#1}}\expandafter\TX@get@body\fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TX@}
% The string \texttt{tabularx} as a macro for testing with \cs{ifx}.
%    \begin{macrocode}
\def\TX@{tabularx}
%    \end{macrocode}
% \end{macro}
%
% Now that all the parts of the table specification are stored in
% registers, we can begin the work of setting the table.
%
% The algorithm for finding the correct column widths is as follows.
% Firstly set the table with each \texttt{X} column the width of the
% final table. Assuming that there is at least one \texttt{X} column,
% this will produce a table that is too wide. Divide the excess width by
% the number of \texttt{X} columns, and reduce the column width by
% this amount. Reset the table. If the table is not now the correct
% width, a \cs{multicolumn} entry must be `hiding' one of the {\ttfamily
% X} columns, and so there is one less \texttt{X} column affecting
% the width of the table. So we reduce by 1 the number of X columns and
% repeat the process.
%
% \begin{macro}{\TX@endtabularx}
% Although I have tried to make \texttt{tabularx} look like an
% environment, it is in fact a command, all the work is done by this
% macro.
%    \begin{macrocode}
\def\TX@endtabularx{%
%    \end{macrocode}
% Define the \texttt{X} column, with an internal version of the
% \cs{newcolumntype} command. The \cs{expandafter} commands enable
% \cs{NC@newcol}  to get the \emph{expansion} of\\
% \verb?\tabularxcolumn{\TX@col@width}? as its
% argument. This will be the definition of an \texttt{X} column.
%    \begin{macrocode}
  \expandafter\TX@newcol\expandafter{\tabularxcolumn{\TX@col@width}}%
%    \end{macrocode}
% Initialise the column width, and the number of \texttt{X} columns.
% The number of \texttt{X} columns is set to one, which means that
% the initial count will be one too high, but this value is decremented
% before it is used in the main loop.
%
%    \begin{macrocode}
  \let\verb\TX@verb
%    \end{macrocode}
% Save the values of all \LaTeX\ counters; the list
% \cs{cl@@ckpt} contains the names of all the \LaTeX\ counters that have
% been defined so far. We expand \cs{setcounter} at this point, as it
% results in fewer tokens being stored in \cs{TX@ckpt}, but the actual
% resetting of the counters occurs when \cs{TX@ckpt} is expanded after
% each trial run.
% Actually, use something equivalent to the expansion of the
% original definition of \cs{setcounter}, so that \verb?tabularx? works in
% conjunction with \file{calc.sty}.
%    \begin{macrocode}
  \def\@elt##1{\global\value{##1}\the\value{##1}\relax}%
  \edef\TX@ckpt{\cl@@ckpt}%
  \let\@elt\relax
  \TX@old@table\maxdimen
  \TX@col@width\TX@target
  \global\TX@cols\@ne
%    \end{macrocode}
% Typeout some headings (unless this is disabled).
%    \begin{macrocode}
  \TX@typeout@
    {\@spaces Table Width\@spaces Column Width\@spaces X Columns}%
%    \end{macrocode}%
% First attempt. Modify the \texttt{X} definition to count {\ttfamily
% X} columns.
%    \begin{macrocode}
  \TX@trial{\def\NC@rewrite@X{%
          \global\advance\TX@cols\@ne\NC@find p{\TX@col@width}}}%
%    \end{macrocode}
% Repeatedly decrease column width until table is the correct width,
% or stops shrinking, or the columns become two narrow.
% If there are no multicolumn entries, this will only take one attempt.
%    \begin{macrocode}
  \loop
    \TX@arith
    \ifTX@
    \TX@trial{}%
  \repeat
%    \end{macrocode}
% One last time, with warnings back on (see appendix D)
% use \texttt{tabular*} to put it in a box of the right size, in case
% the algorithm failed to find the correct size.
%
% Locally make \cs{footnotetext} save its argument in a token
% register.
% \cs{toks@} contains the preamble specification,
% and possible optional argument, as well as the table body.
%    \begin{macrocode}
  {\let\@footnotetext\TX@ftntext\let\@xfootnotenext\TX@xftntext
    \csname tabular*\expandafter\endcsname\expandafter\TX@target
      \the\toks@
    \csname endtabular*\endcsname}%
%    \end{macrocode}
% Now the alignment is finished, and the \verb?}? has restored the original
% meaning of \cs{@footnotetext} expand the register \cs{TX@ftn} which will
% execute a series of\\
% \verb?\footnotetext[<num>]{<note>}? \\
% commands. We need to be careful about clearing the register as we may
% be inside a nested \texttt{tabularx}.
%    \begin{macrocode}
  \global\TX@ftn\expandafter{\expandafter}\the\TX@ftn
%    \end{macrocode}
% Now finish off the \texttt{tabularx} environment. Note that we need
% \verb?\end{tabularx}? here as the \verb?\end{tabularx}? in the user's
% file is never expanded. Now use \cs{TX@} rather than \verb?tabularx?.
%
% We also need to finish off the group started by \verb?{\ifnum0=`}\fi? in
% the macro \cs{tabularx}.
%    \begin{macrocode}
  \ifnum0=`{\fi}%
  \expandafter\end\expandafter{\TX@}}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\TX@arith}
% Calculate the column width for the next try, setting the flag
% \cs{ifTX@} to false if the loop should be aborted.
%    \begin{macrocode}
\def\TX@arith{%
  \TX@false
  \ifdim\TX@old@table=\wd\@tempboxa
%    \end{macrocode}
% If we have reduced the column width, but the table width has not
% changed, we stop the loop, and output the table (which will cause an
% over-full alignment) with the previous value of \cs{TX@col@width}.
%    \begin{macrocode}
    \TX@col@width\TX@old@col
    \TX@typeout@{Reached minimum width, backing up.}%
  \else
%    \end{macrocode}
% Otherwise calculate the amount by which the current table is too wide.
%    \begin{macrocode}
    \dimen@\wd\@tempboxa
    \advance\dimen@ -\TX@target
    \ifdim\dimen@<\TX@delta
%    \end{macrocode}
% If this amount is less than \cs{TX@delta}, stop. (\cs{TX@delta}
% should be non-zero otherwise we may miss the target due to rounding
% error.)
%    \begin{macrocode}
      \TX@typeout@{Reached target.}%
    \else
%    \end{macrocode}
% Reduce the number of effective \texttt{X} columns by one. (Checking
% that we do not get 0, as this would produce an error later.) Then
% divide excess width by the number of effective columns, and calculate
% the new column width. Temporarily store this value (times $-1$) in
% \cs{dimen@}.
%    \begin{macrocode}
      \ifnum\TX@cols>\@ne
        \advance\TX@cols\m@ne
      \fi
      \divide\dimen@\TX@cols
      \advance\dimen@ -\TX@col@width
      \ifdim \dimen@ >\z@
%    \end{macrocode}
% If the new width would be too narrow, abort the loop. At the moment
% too narrow, means less than 0\,pt!
%
% If the loop was aborted here and the X columns were left
% with the width of the previous run it may make the table far
% too wide as initial guesses are always too big. Force to 
% \cs{TX@error@width} which defaults to be 1em. 
%    \begin{macrocode}
        \@memwarn{X Columns too narrow (table too wide)\MessageBreak}%
        \TX@col@width\TX@error@width\relax
      \else
%    \end{macrocode}
% Otherwise save the old settings, and set the new column width. Set the
% flag to true so that the table will be set, and the loop will be
% executed again.
%    \begin{macrocode}
        \TX@old@col\TX@col@width
        \TX@old@table\wd\@tempboxa
        \TX@col@width-\dimen@
        \TX@true
      \fi
    \fi
  \fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TX@error@width}
% If the calculated width is negative, use this instead.
%    \begin{macrocode}
\def\TX@error@width{1em}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TX@delta}
% Accept a table that is within \cs{hfuzz} of the correct width.
%    \begin{macrocode}
\TX@delta\hfuzz
%    \end{macrocode}
% \end{macro}
%
% Initialise the \texttt{X} column. The definition can be empty here,
% as it is set for each \texttt{tabularx} environment.
%    \begin{macrocode}
\newcolumntype{X}{}
%    \end{macrocode}
%
% \begin{macro}{\tabularxcolumn}
% The default definition of \texttt{X} is \verb?p{#1}?.
%    \begin{macrocode}
\def\tabularxcolumn#1{p{#1}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TX@newcol}
% A little macro just used to cut down the number of \cs{expandafter}
% commands needed.
%    \begin{macrocode}
\def\TX@newcol{\newcol@{X}[0]}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TX@trial}
% Make a test run.
%    \begin{macrocode}
\def\TX@trial#1{%
  \setbox\@tempboxa\hbox{%
%    \end{macrocode}
% Any extra commands. This is used on the first run to count the number
% of \texttt{X} columns.
%    \begin{macrocode}
    #1\relax
%    \end{macrocode}
% Make \cs{footnotetext} gobble its arguments. Also locally
% clear \cs{TX@vwarn} so that the warning is generated by the {\ttfamily
% final} run, and does not appear in the middle of the table if
% \cs{tracingtabularx}.
%    \begin{macrocode}
  \let\@footnotetext\TX@trial@ftn
  \let\TX@vwarn\@empty
%    \end{macrocode}
% Do not nest \texttt{tabularx} environments during trial runs. This
% would waste time, and the global setting of \cs{TX@cols} would break the
% algorithm.
%    \begin{macrocode}
   \expandafter\let\expandafter\tabularx\csname tabular*\endcsname
   \expandafter\let\expandafter\endtabularx\csname endtabular*\endcsname
%    \end{macrocode}
% Dissable \cs{write}s during a trial run. This trick is
% from \theTeXbook.\footnote{Actually the trick does
% not work correctly, so it has been changed.}
%    \begin{macrocode}
   \def\write{\begingroup
     \def\let{\afterassignment\endgroup\toks@}%
        \afterassignment\let\count@}%
%    \end{macrocode}
% Turn off warnings (see appendix D). Also prevent them being turned
% back on by setting the parameter names to be registers.
%    \begin{macrocode}
    \hbadness\@M
    \hfuzz\maxdimen
    \let\hbadness\@tempcnta
    \let\hfuzz\@tempdima
%    \end{macrocode}
% Make the table, and finish the hbox.
% \cs{toks@} contains the preamble specification,
% and possible optional argument, as well as the table body.
%    \begin{macrocode}
    \expandafter\tabular\the\toks@
    \endtabular}%
%    \end{macrocode}
% Reset all \LaTeX\ counters, by executing \cs{TX@ckpt}.
%    \begin{macrocode}
  \TX@ckpt
%    \end{macrocode}
% Print some statistics.
%    \begin{macrocode}
  \TX@typeout@{\@spaces
     \expandafter\TX@align
        \the\wd\@tempboxa\space\space\space\space\space\@@
     \expandafter\TX@align
        \the\TX@col@width\space\space\space\space\space\@@
     \@spaces\the\TX@cols}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TX@align}
% Macro to improve the printing of the tracing info.
%    \begin{macrocode}
\def\TX@align#1.#2#3#4#5#6#7#8#9\@@{%
  \ifnum#1<10 \space\fi
  \ifnum#1<100 \space\fi
  \ifnum#1<\@m\space\fi
  \ifnum#1<\@M\space\fi
  #1.#2#3#4#5#6#7#8\space\space}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\arraybackslash}
% \verb?\\? hack.
%    \begin{macrocode}
\def\arraybackslash{\let\\\@arraycr}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\tracingtabularx}
% Print statistics on column and table widths.
%    \begin{macrocode}
\def\tracingtabularx{%
  \def\TX@typeout{\ClassWarningNoLine{memoir}}%
  \def\TX@typeout@##1{\typeout{(tabularx) ##1}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TX@typeout}
% The default is to be to be quiet
%    \begin{macrocode}
\let\TX@typeout\@gobble
\let\TX@typeout@\@gobble
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TX@ftn}
% A token register for saving footnote texts.
%    \begin{macrocode}
\newtoks\TX@ftn
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TX@ftntext}
% \begin{macro}{\TX@xftntext}
% Inside the alignment just save up the footnote text in a token
% register.
%    \begin{macrocode}
\long\def\TX@ftntext#1{%
  \edef\@tempa{\the\TX@ftn\noexpand\footnotetext
                    [\the\csname c@\@mpfn\endcsname]}%
  \global\TX@ftn\expandafter{\@tempa{#1}}}%
\long\def\TX@xftntext[#1]#2{%
  \global\TX@ftn\expandafter{\the\TX@ftn\footnotetext[#1]{#2}}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\TX@trial@ftn}
% On trial runs, gobble footnote texts.
%    \begin{macrocode}
\long\def\TX@trial@ftn#1{}
%    \end{macrocode}
% \end{macro}
%
% This last section was added at Version 1.02. Previous versions
% documentented the fact that \cs{verb} did not work inside {\ttfamily
% tabularx}, but that did not stop people using it! This usually put
% \LaTeX\ into an irrecoverable error position, with error messages that
% did not mention the cause of the error. The `poor man's \cs{verb}' (and
% \cs{verb*}) defined here is based on page 382 of \theTeXbook. As
% explained there, doing verbatim this way means that spaces are not
% treated correctly, and so \cs{verb*} may well be useless, however I
% consider this section of code to be error-recovery, rather than a real
% implementation of verbatim.
%
% The mechanism is quite general, and any macro which wants to allow a
% form of \cs{verb} to be used within its argument may
% \verb?\let\verb=\TX@verb?. (Making sure to restore the real definition
% later!)
%
% \cs{verb} and \cs{verb*} are subject to the following restictions:
% \begin{enumerate}
% \item Spaces in the argument are not read verbatim, but may be skipped
%       according to \TeX's usual rules.
% \item Spaces will be added to the output after control words, even if
%       they were not present in the input.
% \item Unless the argument is a single space, any trailing space,
%       whether in the original argument, or added as in (2),
%       will be omitted.
% \item The argument must not end with \verb?\?, so \verb?\verb|\|? is not
%      allowed, however, because of (3), \verb?\verb|\ |? produces
%      \verb?\?.
% \item The argument must be balanced with respect to \verb?{? and \verb?}?. So
%      \verb?\verb|{|? is not allowed.
% \item A comment character like \verb?%? will not appear verbatim. It will
%       act as usual, commenting out the rest of the input line!
% \item The combinations \verb=?`= and \verb?!`? will appear as
%       {\ttfamily?`} and {\ttfamily!`} if the \texttt{cmtt} font is
%       being used.
% \end{enumerate}
%
% \begin{macro}{\TX@verb}
% The internal definition of \cs{verb}. Spaces will be replaced by \verb?~?, so
% for the star-form, \cs{let} \verb?~? be \verb?\verb*| |?, which we obtain as
% \verb?\uppercase{*}?. Use \verb?{\ifnum0=`}\fi? rather than \cs{bgroup} to allow
% \verb?&? to appear in the argument.
%    \begin{macrocode}
{\uccode`\*=`\ %
\uppercase{\gdef\TX@verb{%
  \leavevmode\null\TX@vwarn
  {\ifnum0=`}\fi\ttfamily\let\\\ignorespaces
  \@ifstar{\let~*\TX@vb}{\TX@vb}}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TX@vb}
% Get the `almost verbatim' text using \cs{meaning}. The `\verb?!?' is added to
% the front of the user supplied text, to ensure that the whole argument
% does not consist of a single \verb?{ }? group. \TeX\ would strip the outer
% braces from such a group. The `\verb?!?' will be removed later.
%
% Originally I followed Knuth, and had \verb?\def\@tempa{##1}?, however this
% did not allow \verb?#? to appear in the argument. So I changed
% this to to use a token register, and \cs{edef}. This allows \verb?#? appear,
% but makes each one appear twice!, so later we loop through, replacing
% \verb?##? by \verb?#?.
%    \begin{macrocode}
\def\TX@vb#1{\def\@tempa##1#1{\toks@{##1}\edef\@tempa{\the\toks@}%
    \expandafter\TX@v\meaning\@tempa\\ \\\ifnum0=`{\fi}}\@tempa!}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TX@v}
% Strip the initial segment of the \cs{meaning}, including the `\verb?!?'
% added earlier.
%    \begin{macrocode}
\def\TX@v#1!{\afterassignment\TX@vfirst\let\@tempa= }
%    \end{macrocode}
% \end{macro}
%
% As explained above we are going to replace \verb?##? pairs by \verb?#?. To do
% this we need non-special \verb?#? tokens.  Make \verb?*? into a parameter
% token so that we can define macros with arguments. The normal meanings
% will be restored by the \cs{endgroup} later.
%    \begin{macrocode}
\begingroup
\catcode`\*=\catcode`\#
\catcode`\#=12
%    \end{macrocode}
%
% \begin{macro}{\TX@vfirst}
% As a special case, prevent the first character from being dropped.
% This makes \verb?\verb*| |? produce \verb*? ?. Then call \cs{TX@v@}.
% This is slightly tricky as I have to ensure that an
% actual \verb?#? rather than a command \cs{let} to \verb?#? is passed on if the
% first character is \verb?#?.
%    \begin{macrocode}
\gdef\TX@vfirst{%
  \if\@tempa#%
    \def\@tempb{\TX@v@#}%
  \else
    \let\@tempb\TX@v@
    \if\@tempa\space~\else\@tempa\fi
  \fi
  \@tempb}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TX@v@}
% Loop through the \cs{meaning}, replacing all spaces by \verb?~?. If the last
% character is a space it is dropped, so that \verb?\verb*|\LaTeX|? produces
% \verb?\LaTeX? not \verb*?\LaTeX ?. The rewritten tokens are then further
% processed to replace \verb?##? pairs.
%    \begin{macrocode}
\gdef\TX@v@*1 *2{%
  \TX@v@hash*1##\relax\if*2\\\else~\expandafter\TX@v@\fi*2}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TX@v@hash}
% The inner loop, replacing \verb?##? by \verb?#?.
%    \begin{macrocode}
\gdef\TX@v@hash*1##*2{*1\ifx*2\relax\else#\expandafter\TX@v@hash\fi*2}
%    \end{macrocode}
% \end{macro}
%
% As promised, we now restore the normal meanings of \verb?#? and \verb?*?.
%    \begin{macrocode}
\endgroup
%    \end{macrocode}
%
% \begin{macro}{\TX@vwarn}
% Warn the user the first time this \cs{verb} is used.
%    \begin{macrocode}
\def\TX@vwarn{%
  \@warning{\noexpand\verb may be unreliable inside tabularx}%
  \global\let\TX@vwarn\@empty}

%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
%%%%%%%%%% end Tabularx package code %%%%%%%%%%%%%%%%%%%%%

%    \end{macrocode}
%
%    At this point we had better stop anyone trying to load the several
% tabular-related packages (as effectively they are already loaded).
%    \begin{macrocode}
%%\@memfakeusepackage{array}
%%\@memfakeusepackage{dcolumn}
%%\@memfakeusepackage{delarray}
%%\@memfakeusepackage{tabularx}

%    \end{macrocode}
%
%
%  
% \subsection{Fear's rules}
%
%    Simon Fear disapproves of the default \LaTeX{} table rules and
% wrote the \Lpack{booktabs} package~\cite{BOOKTABS} to provide
% better horizontal rules. Like many typographers, he abhors vertical rules.
%
%
% \begin{macro}{\bktabrule}
% The rules in this section are based on \verb?\hrule \@height#1? but this 
% does not 
% work for continuous tabulars. Provide a version of this that
% can be \cs{let} when necessary.
%    \begin{macrocode}
\newcommand*{\bktabrule}[1]{%
  \hrule \@height#1}

%    \end{macrocode}
% \end{macro}
%
% The following is taken almost verbatim from the \Lpack{booktabs} package,
% version~1.618, March 2003.
%
%    \begin{macrocode}
%%%%%%%%%% Booktabs package code       %%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% slightly modified by PRW    %%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% by permission of Simon Fear %%%%%%%%%%%%%%%%%%%%%

%    \end{macrocode}
% Danie Els is now (2005) the official maintainer of the \Lpack{booktabs} package. He provided me with
% some updates to synchronize \Lpack{memoir}, \Lpack{booktabs} and support for \Lpack{colortbl} with \Lpack{booktabs}. I made the appropriate changes/extensions
% on 2005/09/03 as part of version 1.618 of \Lpack{memoir}.
%
% \begin{macro}{\CT@arc@}
% This supports the \Lpack{bookhands}/\Lpack{colortbl} package combination.
%    \begin{macrocode}
\AtBeginDocument{%
  \providecommand*{\CT@arc@}{}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\heavyrulewidth}
% \begin{macro}{\lightrulewidth}
% \begin{macro}{\cmidrulewidth}
% \begin{macro}{\belowrulesep}
% \begin{macro}{\belowbottomsep}
% \begin{macro}{\aboverulesep}
% \begin{macro}{\abovetopsep}
% \begin{macro}{\cmidrulesep}
% \begin{macro}{\cmidrulekern}
% \begin{macro}{\defaultaddspace}
% First we set up some new dimensions.
%    \begin{macrocode}
\newdimen\heavyrulewidth 
\newdimen\lightrulewidth 
\newdimen\cmidrulewidth  
\newdimen\belowrulesep   
\newdimen\belowbottomsep   
\newdimen\aboverulesep   
\newdimen\abovetopsep   
\newdimen\cmidrulesep    
\newdimen\cmidrulekern   
\newdimen\defaultaddspace
\heavyrulewidth=.08em
\lightrulewidth=.05em
\cmidrulewidth=.03em
\belowrulesep=.65ex
\belowbottomsep=\z@
\aboverulesep=.4ex
\abovetopsep=\z@
\cmidrulesep=\doublerulesep
\cmidrulekern=.5em
\defaultaddspace=.5em

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% 
% \begin{macro}{\@cmidla}
% \begin{macro}{\@cmidlb}
% \begin{macro}{\@aboverulesep}
% \begin{macro}{\@belowrulesep}
% \begin{macro}{\@thisruleclass}
% \begin{macro}{\@lastruleclass}
% \begin{macro}{\@thisrulewidth}
% And some internal counters and other things of no interest to the end user:
%    \begin{macrocode}
\newcount\@cmidla
\newcount\@cmidlb
\newdimen\@aboverulesep
\newdimen\@belowrulesep
\newcount\@thisruleclass
\newcount\@lastruleclass
\@lastruleclass=0
\newdimen\@thisrulewidth

%    \end{macrocode}
% which will be described as needed below.
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\futurenonspacelet}
% \begin{macro}{\@BTfnslone}
% \begin{macro}{\@BTfnsltwo}
% \begin{macro}{\@BTfnslthree}
% Next we define a very useful macro (more-or-less straight from the
% \theTeXbook's Dirty Tricks chapter; documented there). Use
% \cs{futurenonspacelet} instead of \cs{futurelet} when looking for the next
% (non-space) token after a macro that has an argument. (After a macro
% without an argument, space is ignored anyway, so \cs{futurenonspacelet}
% wouldn't be needed.) This hack allows users to type white
% space between successive rule commands.
%    \begin{macrocode}
\def\futurenonspacelet#1{\def\@BTcs{#1}%
 \afterassignment\@BTfnslone\let\nexttoken= }
\def\@BTfnslone{\expandafter\futurelet\@BTcs\@BTfnsltwo}
\def\@BTfnsltwo{\expandafter\ifx\@BTcs\@sptoken\let\next=\@BTfnslthree
 \else\let\next=\nexttoken\fi \next}
\def\@BTfnslthree{\afterassignment\@BTfnslone\let\next= }

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsubsection{Full width  rules}
%
% When we are not in a \texttt{longtable} environment, we can simply
% implement the full width rules as a \cs{hrule} in  a \verb?\noalign{}?
% group. But within a \texttt{longtable}, the rule has to be drawn like a
% \verb?\cmidrule{1-\LT@cols}? (the rationale for this is explained in the
% \texttt{longtable} documentation).

% In order to allow for both, all the rule macros have to
% open a \cs{noalign} group immediately, while they work out whether
% they have been called within a \texttt{longtable}; if you don't do this,
% \TeX's underlying \cs{halign} process gets hiccups.
% I use \LaTeX's
% dirty trick (\verb?\ifnum=0`}?) to fool the parser that the bracket
% count is OK. The bracket really gets closed after all the skipping
% at the end of the \cs{@BTendrule} macro.

% The class 1 rules, and \cs{specialrule}, really only differ in the
% defaults for space above and
% below, and the width, passed to a common routine, \cs{@BTrule},
% described below. The spaces, \cs{@aboverulesep} and \cs{@belowrulesep},
% are set within the \cs{noalign} group, so are inherited by
% \cs{@BTrule}. Similarly, \cs{@BTrule} knows as much as it needs to
% about the routine that called it by examining the inherited
% \cs{@thisruleclass}. The optional width argument is parsed by
% \cs{@BTrule} after being set to default if absent.

%
% \begin{macro}{\toprule}
% \begin{macro}{\midrule}
% \begin{macro}{\bottomrule}
% \begin{macro}{\specialrule}
%    \begin{macrocode}
\def\toprule{\noalign{\ifnum0=`}\fi
  \@aboverulesep=\abovetopsep
  \global\@belowrulesep=\belowrulesep
  \global\@thisruleclass=\@ne
  \@ifnextchar[{\@BTrule}{\@BTrule[\heavyrulewidth]}}

\def\midrule{\noalign{\ifnum0=`}\fi
  \@aboverulesep=\aboverulesep
  \global\@belowrulesep=\belowrulesep
  \global\@thisruleclass=\@ne
  \@ifnextchar[{\@BTrule}{\@BTrule[\lightrulewidth]}}

\def\bottomrule{\noalign{\ifnum0=`}\fi
  \@aboverulesep=\aboverulesep
  \global\@belowrulesep=\belowbottomsep
  \global\@thisruleclass=\@ne
  \@ifnextchar[{\@BTrule}{\@BTrule[\heavyrulewidth]}}

\def\specialrule#1#2#3{\noalign{\ifnum0=`}\fi
  \@aboverulesep=#2\global\@belowrulesep=#3\global\@thisruleclass=\tw@
  \@BTrule[#1]}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \begin{macro}{\addlinespace}
% An \cs{addlinespace} is essentially a zero-width rule with
% zero space above and argument (or default) space below. But because
%  the rule is not actually drawn, but is just a \cs{vskip}, there is no
%  need to check if we're in a longtable, so we don't need to call
%  \cs{@BTrule} as for `real' rules. But we do share the \cs{@BTendrule}
%  lookahead and flagsetting code (described below), and the \cs{vskip} is done there.
%    \begin{macrocode}
\def\addlinespace{\noalign{\ifnum0=`}\fi
  \@ifnextchar[{\@addspace}{\@addspace[\defaultaddspace]}}
\def\@addspace[#1]{\global\@belowrulesep=#1\global\@thisruleclass=\tw@
  \futurelet\@tempa\@BTendrule}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@BTrule}
% All the rules (except \cs{addlinespace}) share this code.
%    \begin{macrocode}
\def\@BTrule[#1]{%
  \global\@thisrulewidth=#1\relax
%    \end{macrocode}
% Save the width argument (if the user didn't give one, then the
% calling routine will have called \cs{@BTrule} with the default) in a global
% variable for later use when drawing the rule.
%    \begin{macrocode}
  \ifnum\@thisruleclass=\tw@\vskip\@aboverulesep\else
%    \end{macrocode}
% Specialrules always insert specified space above. (Note: addlinespaces
% don't come here).
%    \begin{macrocode}
  \ifnum\@lastruleclass=\z@\vskip\@aboverulesep\else
  \ifnum\@lastruleclass=\@ne\vskip\doublerulesep\fi\fi\fi
%    \end{macrocode}
% After text (last rule class 0), precede the rule by \cs{aboverulesep};
% but if immediately after a
% previous rule, insert a \cs{doublerulesep}.
%
% Now we work out, by a very nasty hack, if we're within a
%  \texttt{longtable}. It's easy if \cs{longtable} isn't even defined: then we
%  can't be. But it is not enough just to check if \texttt{longtable} is
%  loaded --- we might be within an ordinary table rather than a
%  longtable. So we look to see if \cs{hline} has been re-defined
%  from its \LaTeX\ definition to be the same as
%  \cs{LT@hline}. (Longtable currently does this redefinition when it opens a
%  \texttt{longtable} environment, but not globally, so it is
%  cleared it when the environment closes.) Another
%  package could potentially do this! And \texttt{longtable} might change the
%  way it implements this! So, it is not entirely safe, but I have
%  found no better way so far.

% We set up \cs{@BTswitch} to call \cs{@BTnormal} or \cs{@BLTrule}, as
%  appropriate, then call it.
%    \begin{macrocode}
  \ifx\longtable\undefined\let\@BTswitch\@BTnormal\else
  \ifx\hline\LT@hline\let\@BTswitch\@BLTrule
  \else\let\@BTswitch\@BTnormal\fi\fi
  \@BTswitch}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@BTnormal}
% This is when we're \emph{not} within a longtable. We are already in a
%  \cs{noalign} group, all we need do is draw an \cs{hrule} and gobble any
%  trailing spaces, then call the closing routine with \cs{@tempa} set
%  equal to the next token in the document.
%    \begin{macrocode}
% \def\@BTnormal{\hrule
%   \@height \@thisrulewidth\futurenonspacelet\@tempa\@BTendrule}
\def\@BTnormal{%
%%  \bktabrule{\@thisrulewidth}
  {\CT@arc@\bktabrule{\@thisrulewidth}}%
  \futurenonspacelet\@tempa\@BTendrule}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@BLTrule}
% This is for full width rule within a \texttt{longtable}. First we check if
%  a kerning argument has been used; if so let \cs{@@BLTrule} read it,
%  else call \cs{@@BLTrule} with an empty string:
%    \begin{macrocode}
\def\@BLTrule{\@ifnextchar({\@@BLTrule}{\@@BLTrule()}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\@@BLTrule}
%    \begin{macrocode}
\def\@@BLTrule(#1){\@setrulekerning{#1}%
\global\@cmidlb\LT@cols
%    \end{macrocode}
% The \cs{@setrulekerning} routine parses the kerning argument tokens
% and sets global kerning widths accordingly (or to defaults, if user
% hasn't set them explicitly). The global assignment to \cs{@cmidlb}
% sets up the column count for the \cs{@cmidruleb} macro, which is
% shared with \verb?cmidrules?.
%    \begin{macrocode}
\ifnum0=`{\fi}%
%    \end{macrocode}
% Close the currently open \cs{noalign} group. Within a \texttt{longtable},
% rules are all to be drawn as leaders within a text box that is
% \cs{LT@cols} columns wide.
%    \begin{macrocode}
\@cmidruleb
%    \end{macrocode}
% Draw the rule. We share the \cs{@cmidruleb} code with ordinary \cs{cmidrule}s.
%    \begin{macrocode}
\noalign{\ifnum0=`}\fi
%    \end{macrocode}
% We have to open a new noalign immediately else \TeX{} will start a new
%    text box where we don't want one. Then, after gobbling any
%    unwanted white space, we call the closing routine.
%    \begin{macrocode}
\futurenonspacelet\@tempa\@BTendrule}

%    \end{macrocode}
% \end{macro}
% \begin{macro}{\@BTendrule}
% We look one step ahead (token is in \cs{@tempa}) to see if another
% rule follows (shame on user!). If so, we set \cs{@lastruleclass} equal
% to \cs{@thisruleclass} (thus setting it up for the following rule). If
% there isn't a following rule, we clear \cs{@lastruleclass} (ie set it
% to zero), which isn't technically true since we have just drawn a
% rule, but sets it up correctly for the next rule encountered, which
% must be following some intervening text.
%    \begin{macrocode}
\def\@BTendrule{%
  \ifx\@tempa\toprule\global\@lastruleclass=\@thisruleclass
  \else\ifx\@tempa\midrule\global\@lastruleclass=\@thisruleclass
  \else\ifx\@tempa\bottomrule\global\@lastruleclass=\@thisruleclass
  \else\ifx\@tempa\cmidrule\global\@lastruleclass=\@thisruleclass
  \else\ifx\@tempa\specialrule\global\@lastruleclass=\@thisruleclass
  \else\ifx\@tempa\addlinespace\global\@lastruleclass=\@thisruleclass
  \else\global\@lastruleclass=\z@\fi\fi\fi\fi\fi\fi
  \ifnum\@lastruleclass=\@ne\relax\else\vskip\@belowrulesep\fi
  \ifnum0=`{\fi}}
%    \end{macrocode}
% \end{macro}
%
%
% \subsubsection{Special subrules}
%
% \begin{macro}{\@setrulekerning}
% The following code parses the trimming arguments (if there are any)
%    for \cs{cmidrule} or a \cs{BLTrule}. The rule
%     will be trimmed left and right by \cs{cmrkern@l} and \cs{cmrkern@l},
%     which are zero by default, set to \cs{cmidrulekern} by the plain
%     \verb?(lr)? arguments, or user set as in \verb?(r{.5em})?. We parse token by
%     token through the arguments. The
%     tokens \verb?r? and \verb?l? cause \cs{cmrkern@r} or \cs{cmrkern@l} to be set to
%     \cs{cmidrulekern}. There is no lookahead to see if a width is the
%     next token; this strategy is efficient for the plain commands,
%     while inefficient for the qualified commands, but more importantly
%     it is much easier to program. Tokens \verb?r? and \verb?l? also set
%     \cs{cmrswitch} so that if the next token turns out to be
%     \marg{wd} then the kerning will be done on the side
%     currently specified. I have been too lazy to program an error
%     message should one encounter tokens other than \verb?r?, \verb?l? or
%     \marg{wd}.
% \changes{v1.618}{2005/09/03}{Fixed bug in booktabs trim command by improving character comparison code in \cs{@setrulekerning} (mempatch v3.8)}
%    \begin{macrocode}
\def\@setrulekerning#1{%
  \global\let\cmrkern@l\z@
  \global\let\cmrkern@r\z@
  \@tfor\@tempa :=#1\do
  {\def\@tempb{r}%
   \ifx\@tempa\@tempb
     \global\let\cmrkern@r\cmidrulekern
      \def\cmrsideswitch{\cmrkern@r}%
   \else
     \def\@tempb{l}%
     \ifx\@tempa\@tempb
       \global\let\cmrkern@l\cmidrulekern
       \def\cmrsideswitch{\cmrkern@l}%
     \else
       \global\expandafter\let\cmrsideswitch\@tempa
     \fi
   \fi}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\cmidrule}
% \begin{macro}{\@cmidrule}
% \begin{macro}{\@@cmidrule}
% The \cs{cmidrule} re-uses \cs{@lastruleclass} in an entirely different
%  way from the full width rules. (Maybe I should have used a
%  different flag; it seemed efficient at the time \ldots). This
% is (left) set to one if you are in the middle of a row of
% \cs{cmidrules}, or starting a new one (with \cs{morecmidrules}).
% Otherwise, when \cs{@lastruleclass} is zero, we precede the
% rule with \cs{aboverulesep}.
%    \begin{macrocode}
\def\cmidrule{\noalign{\ifnum0=`}\fi
    \@ifnextchar[{\@cmidrule}{\@cmidrule[\cmidrulewidth]}}
\def\@cmidrule[#1]{\@ifnextchar({\@@cmidrule[#1]}{\@@cmidrule[#1]()}}
\def\@@cmidrule[#1](#2)#3{\@@@cmidrule[#3]{#1}{#2}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@@cmidrule}
% The above is fiddling around to set defaults for missing optional
% arguments. We also pass to \cs{@@@cmidrule} in a different order,
% namely =[a-b]=\verb?{width required}= \verb?{kerning commands}= (this being
% the order in which the arguments are actually processed):
%    \begin{macrocode}
\def\@@@cmidrule[#1-#2]#3#4{\global\@cmidla#1\relax
    \global\advance\@cmidla\m@ne
    \ifnum\@cmidla>0\global\let\@gtempa\@cmidrulea\else
    \global\let\@gtempa\@cmidruleb\fi
    \global\@cmidlb#2\relax
    \global\advance\@cmidlb-\@cmidla
%    \end{macrocode}
% This has set up a switch (\cs{@gtempa}) to call the relevant routine,
% \cs{@cmidrulea} or \cs{@cmidruleb}, depending on whether we start
% from column one or not.
%    \begin{macrocode}
    \global\@thisrulewidth=#3
%    \end{macrocode}
% That is, set per default or given argument. Then parse any trimming
% arguments to set, globally, \cs{cmrkern@r} and \cs{cmrkern@l} accordingly:
%    \begin{macrocode}
    \@setrulekerning{#4}
%    \end{macrocode}
% Now insert space above if needed, close the \cs{noalign},
% then switch to appropriate rule drawing routine as determined
% above (\cs{let} to \cs{@gtempa}):
%    \begin{macrocode}
    \ifnum\@lastruleclass=\z@\vskip \aboverulesep\fi
    \ifnum0=`{\fi}\@gtempa
%    \end{macrocode}
% Having now drawn the rule,
% open another \cs{noalign}, and call the closing routine:
%    \begin{macrocode}
    \noalign{\ifnum0=`}\fi\futurenonspacelet\@tempa\@xcmidrule}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@xcmidrule}
% In this closing routine, see if another \cs{cmidrule} follows;
% if so, backspace
% vertical so it will line up with the one you just drew,
% and setting \cs{@lastruleclass} to 1 will suppress
% adding space above the next. If a \cs{morecimdrules} follows,
% we add (positive) \cs{cmidrulesep} (and again set \cs{@lastruleclass}
% to one). Otherwise this is the last rule of the current group
% and we can just add \cs{belowrulesep}. Finally, we close the \cs{noalign}.
%    \begin{macrocode}
\def\@xcmidrule{\ifx\@tempa\cmidrule\vskip-\@thisrulewidth
    \global\@lastruleclass=\@ne\else
    \ifx\@tempa\morecmidrules\vskip \cmidrulesep
    \global\@lastruleclass=\@ne\else
    \vskip \belowrulesep\global\@lastruleclass=\z@\fi\fi
    \ifnum0=`{\fi}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@cmidrulea}
% \begin{macro}{\@cmidruleb}
% This code (called below) actually draws the rules. They are drawn
% as boxes in text, rather than in a \cs{noalign} group, which permits
% the left and right kerning.
%    \begin{macrocode}
\def\@cmidrulea{%
  \multispan\@cmidla&\multispan\@cmidlb
%%    \unskip\hskip \cmrkern@l\leaders\bktabrule{\@thisrulewidth}\hfill
    \unskip\hskip \cmrkern@l{%
    \CT@arc@\leaders\bktabrule{\@thisrulewidth}\hfill}%
    \hskip \cmrkern@r\cr}
\def\@cmidruleb{%
  \multispan\@cmidlb
%%    \unskip\hskip \cmrkern@l\leaders\bktabrule{\@thisrulewidth}\hfill
  \unskip\hskip \cmrkern@l{%
    \CT@arc@\leaders\bktabrule{\@thisrulewidth}\hfill}%
    \hskip \cmrkern@r\cr}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\morecmidrules}
% This is really a dummy command; all the work is done above
% within the \cs{cmidrule} routine. We look one step ahead there
% to see if a \cs{morecmidrules} follows the current \cs{cmidrule},
% and if so set the flag. Otherwise,  \cs{morecmidrules} itself
% does nothing.
%    \begin{macrocode}
\def\morecmidrules{\noalign{\relax}}

%%%%%%%%%% end of Booktabs package code %%%%%%%%%%%%%%%%%%%%%

%    \end{macrocode}
% \end{macro}
%
% Ensure that the real \Lpack{booktabs} package can't get called.
% \changes{v1.618}{2005/09/03}{Killed use of real booktabs package (mempatch v3.12)}
%    \begin{macrocode}
%%\@memfakeusepackage{booktabs}

%    \end{macrocode}
%
%
% \subsection{Continuous tabulars}
%
%
%    The standard \texttt{tabular} environment is wrapped inside a box
% (see \verb?\@tabular?),
% which means that it cannot break across pages. Equivalent environments
% are provided which are not boxed and so can continue across page 
% boundaries.
%
% \begin{macro}{\ctableftskip}
% \begin{macro}{\ctabrightskip}
% These skips are inserted at the left and right of continuous tabulars
% so that their horizontal location can be specified.
%    \begin{macrocode}
\newskip\ctableftskip \ctableftskip=\fill
\newskip\ctabrightskip \ctabrightskip=\fill

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\ctabular*}
% \begin{macro}{\@ctabularstar}
% This starts off the \texttt{ctabular*} continuous tabular environment.
% The default location is centered. NOTE: This is not for release.
%    \begin{macrocode}
\expandafter\def\csname ctabular*\endcsname{%
  \@ifnextchar[ {\@ctabularstar}{\@ctabularstar[c]}}
\def\@ctabularstar[#1]#2{\global\@curtab\@ne
  \ctableftskip\fill
  \ctabrightskip\fill
  \if l#1% left
    \ctableftskip\z@ 
  \else
    \if r#1% right
       \ctabrightskip\z@
    \fi
  \fi
  \setlength\dimen@{#2}%
  \xdef\@halignto{to\the\dimen@}\NC@tabular}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\ctabular}
% \verb?\ctabular? is similar to \verb?\ctabular*? except that the
% width is known to be \verb?\hsize?.
%    \begin{macrocode}
\newcommand*{\ctabular}[1][c]{\global\@curtab\@ne
  \ctableftskip\fill
  \ctabrightskip\fill
  \if l#1% left
    \ctableftskip\z@ 
  \else
    \if r#1% right
      \ctabrightskip\z@
    \fi
  \fi
  \gdef\@halignto{to\hsize}\NC@tabular}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\NC@tabular}
% \verb?\NC@tabular?
%    \begin{macrocode}
\newcommand*{\NC@tabular}{%
  \par
  \addvspace{\topsep}
  \col@sep\tabcolsep
  \let\d@llarbegin\begingroup
  \let\d@llarend\endgroup
  \@NCtabarray}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@NCialign}
%    \begin{macrocode}
\newcommand*{\@NCialign}{\everycr{}\tabskip\ctableftskip\halign}

%    \end{macrocode}
% \end{macro}
%  
% \begin{macro}{\@NCtabarray}
% Effectively a modified version of \verb?\@array? 
%    \begin{macrocode}
\newcommand*{\@NCtabarray}[1]{%
  \@tempdima \ht\strutbox
  \advance\@tempdima\extrarowheight
  \setbox \@arstrutbox \hbox{\vrule
    \@height \arraystretch \@tempdima
    \@depth \arraystretch \dp\strutbox
    \@width\z@}%
  \begingroup
%%    \@mkpream{@{\hspace{\@totalleftmargin}}#1@{}}%
    \@mkpream{#1}%
    \xdef\@preamble{\@NCialign \@halignto
                    \bgroup & \tabskip\z@
                      \@arstrut
                      \@preamble
                      \tabskip\ctabrightskip
                      \cr}%
  \endgroup
  \let\@sharp ##\let\protect\relax
  \lineskip\z@
  \baselineskip\z@
  \let\\\@arraycr
  \let\tabularnewline\\%
  \let\par\@empty
  \ctabsetlines
  \@preamble
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\endctabular}
% \begin{macro}{\endctabular*}
% End the environments.
%    \begin{macrocode}
\def\endctabular{%
  \crcr \egroup
  \gdef\@preamble{}%
  \addvspace{\topsep}
  \noindent}
\expandafter\let\csname endctabular*\endcsname=\endctabular

%    \end{macrocode}
% \end{macro}
% \end{macro}
%  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
% \subsubsection{Horizontal lines}
%
%   The standard \verb?\hline? can produce odd results when used with
% continuous tabulars, but \verb?\hhline? or \verb?\cline? are OK. 
% I want a version of \verb?\hline?
% that works with continuous tabulars and has an adjustable width
% (i.e., thickness). This will be based on a modified \verb?\cline?
%
% For reference, here are the essentials of the kernel's definitions.
% \begin{verbatim}
% \def\hline{%
%    \noalign{\ifnum0=`}\fi\hrule \@height \arrayrulewidth \futurelet
%    \reserved@a\@xhline}
% \def\@xhline{%
%    \ifx\reserved@a\hline
%      \vskip\doublerulesep
%      \vskip-\arrayrulewidth
%    \fi
%    \ifnum0=`{\fi}}
% \def\cline#1{\@cline#1\@nil}
% \def\@cline#1-#2\@nil{%
%   \omit
%   \@multicnt#1%
%   \advance\@multispan\m@ne
%   \ifnum\@multicnt=\@ne\@firstofone{&\omit}\fi
%   \@multicnt#2%
%   \advance\@multicnt-#1%
%   \advance\@multispan\@ne
%   \leaders\hrule\@height\arrayrulewidth\hfill
%   \cr
%   \noalign{\vskip-\arrayrulewidth}}
% \end{verbatim}
%
% \begin{macro}{\memcline}
% \begin{macro}{\m@m@cline} 
% This is a version of the kernel \verb?\cline? code that provides
% a variable width.
%    \begin{macrocode}
\newcommand*{\memcline}[2]{\m@m@cline[#1]#2\@nil}
\def\m@m@cline[#1]#2-#3\@nil{%
  \omit
  \@multicnt#2%
  \advance\@multispan\m@ne
  \ifnum\@multicnt=\@ne\@firstofone{&\omit}\fi
  \@multicnt#3%
  \advance\@multicnt-#2%
  \advance\@multispan\@ne
  \leaders\hrule\@height #1\hfill  % <- variable \@height value
  \cr
  \noalign{\vskip- #1}}            % <- variable \@height value

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\memhline}
% \begin{macro}{\m@mhline}
% A version of \verb?\hline?, based on \verb?\memcline?, taking a 
% rule width as argument. It draws a \verb?\memcline? from the
% first to the last (\verb?\@curtab?) column.
%    \begin{macrocode}
\newcommand*{\memhline}[1][\arrayrulewidth]{\memcline{#1}{1-\@curtab}}
\newcommand*{\m@mhline}{\cline{1-\@curtab}}
%    \end{macrocode}
%\end{macro}
%\end{macro}
%
% Fear's rules also do not play well within continuous tabulars.
%
% \begin{macro}{\m@m@BTnormal}
% Special version of \cs{@BTnormal}
%    \begin{macrocode}
\def\m@m@BTnormal{%
  \ifnum0=`{\fi}   % closes the \noalign
  \multispan{\@curtab} \leaders\bktabrule{\@thisrulewidth}\hfill\cr
  \noalign{\ifnum0=`}\fi
  \futurenonspacelet\@tempa\@BTendrule}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\ctabsetlines}
% Continuous tabulars cannot use the standard horizontal lines.
%    \begin{macrocode}
\def\ctabsetlines{%
  \let\hline\m@mhline
  \let\@BTnormal\m@m@BTnormal
}

%    \end{macrocode}
% \end{macro}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
% \subsection{Automated tabulations}
%
%
% It can be convenient, especially while drafting a document, to have
% a list of items put into a tabular without having to mark the ends
% of the rows. 
%
% The following is based on plain \TeX{} code given on pages 307--308 of
% \textit{TeX for the Impatient}~\cite{bk:Impatient}, which provides
% code for typesetting in columns.
%
% 
% \begin{macro}{\abovecolumnspenalty}
% \begin{macro}{\@linestogo}
% \begin{macro}{\@cellstogo}
% \begin{macro}{\@cellsincolumn}
% \begin{macro}{\crtok}
% Counters and such.
%    \begin{macrocode}
\newcount\abovecolumnspenalty
  \abovecolumnspenalty=10000
\newcount\@linestogo           % lines remaining to be procesed
\newcount\@cellstogo           % cells remaining in column or row
\newcount\@cellsincolumn         % number of lines per column
\newtoks\crtok
  \crtok = {\cr}%

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@mincolumnwidth}
% \begin{macro}{\c@lleftskip}
% \begin{macro}{\c@lrightskip}
% More things
%   \begin{macrocode}
\newdimen\@mincolumnwidth
\let\c@lleftskip\hfil   % left skip within a column
\let\c@lrightskip\hfil % right skip within a column

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\preautotab}
% \begin{macro}{\postautotab}
% Hooks into the auto tabulations.
%    \begin{macrocode}
\let\preautotab\relax
\let\postautotab\relax

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\autocols}
% \verb?\autocols?\oarg{width}\marg{pos}\marg{num}\marg{style}\marg{comma separated list}
% arranges the elements in the \meta{comma separated list} into \meta{num}
% columns, the elements filling each column before moving to the next.
% That is, the elements are ordered top to bottom and left to right.
%    \begin{macrocode}
\newcommand{\autocols}[5][0pt]{\par\begingroup
  \ctabsetlines
%    \end{macrocode}
% Set the table position
%    \begin{macrocode}
  \if l#2 
    \raggedright
  \else
    \if r#2
      \raggedleft
    \else
      \centering
    \fi
  \fi
%    \end{macrocode}
% Set the column position style
%    \begin{macrocode}
  \let\c@lleftskip\hfil
  \let\c@lrightskip\hfil
  \if l#4 
    \let\c@lleftskip\relax
  \else
    \if r#4
      \let\c@lrightskip\relax
    \fi
  \fi
%    \end{macrocode}
% Count the number of entries and the minimum width (max entry width)
% for the columns.
%    \begin{macrocode}
  \@mincolumnwidth\z@
  \TX@cols=#3
  \@curtab=#3
  \@linestogo\z@
  \@for\@tempa:=#5\do{
    \advance\@linestogo\@ne
    \settowidth{\@tempdima}{\@tempa}
    \ifdim\@tempdima>\@mincolumnwidth
      \@mincolumnwidth=\@tempdima
    \fi
  }
  \advance\@mincolumnwidth\tabcolsep
  \linespercol
%    \end{macrocode}
% Specify what is to be done after every entry
%    \begin{macrocode}
  \def\@endcolumnactions{%
    \global\advance\@linestogo\m@ne
    \ifnum\@cellstogo<\tw@
      \global\advance\TX@cols\m@ne
      \ifnum\TX@cols>\z@\linespercol\fi
      \the\crtok
    \else 
        &\global\advance\@cellstogo\m@ne
    \fi
  }
%    \end{macrocode}
% Calculate the width of the columns
%    \begin{macrocode}
  \ifdim #1 > \z@
    \TX@col@width=#1
    \divide\TX@col@width \TX@cols
  \else
    \TX@col@width=\@mincolumnwidth
  \fi
  \penalty\abovecolumnspenalty
  \noindent % usually not a paragraph
%    \end{macrocode}
% Create most of the preamble by looping to add \verb?\@cellsincolumn?-1 slots,
% then the last one which is different.
%    \begin{macrocode}
  \def\@preamble{}
  \begingroup
    \let\@sharp\relax
    \ifnum\@cellsincolumn>\@ne
      \loop
        \g@addto@macro{\@preamble}{%
          \hb@xt@ \TX@col@width{%
               \c@lleftskip\strut\@sharp\c@lrightskip} &}%
        \advance\@cellsincolumn\m@ne
      \ifnum\@cellsincolumn>\@ne
      \repeat
    \fi
    \g@addto@macro{\@preamble}{%
      \hb@xt@ \TX@col@width{\c@lleftskip\strut\@sharp\c@lrightskip}}%
  \endgroup
  \let\@sharp ##
%    \end{macrocode}
% Start the \verb?\valign?
%    \begin{macrocode}
  \tabskip\ctableftskip
%%  \tabskip\z@
  \valign \bgroup
    \tabskip\z@
    \@preamble
    \tabskip\ctabrightskip\cr
%    \end{macrocode}
% Add all the entries then finish off.
%    \begin{macrocode}
    \@for\@tempa:=#5\do{
      \@tempa\unskip\space\@endcolumnactions}%
    \the\crtok \egroup \par \endgroup 
}
  
%    \end{macrocode}
% \end{macro}
%  
%
% \begin{macro}{\linespercol}
% \verb?\linespercol? calculates the maximum number of lines that go into
% a column, where there are \verb?\TX@cols? columns and \verb?\@linestogo?
% lines, so that the columns are balanced as well as possible. The result
% is \verb?\@cellstogo?
%    \begin{macrocode}
\newcommand*{\linespercol}{%
  \@cellsincolumn=\@linestogo
  \divide\@cellsincolumn \TX@cols
  \@cellstogo=\@cellsincolumn
  \multiply\@cellstogo \TX@cols
  \@tempcnta=\@linestogo
  \advance\@tempcnta -\@cellstogo
  \ifnum \@tempcnta>\z@
    \advance\@cellsincolumn \@ne
  \fi
  \global\@cellstogo=\@cellsincolumn
}

%    \end{macrocode}
% \end{macro}
%  
%
% \begin{macro}{\autorows}
% \verb?\autorows?\oarg{width}\marg{pos}\marg{num}\marg{style}\marg{comma separated list}
% arranges the elements in the \meta{comma separated list} into \meta{num}
% columns, the elements filling each row before moving to the next.
% That is, the elements are ordered left to right and top to bottom.
% By default, each column is the same width, enough for the widest entry.
% If \meta{width} is a negative length (e.g., -1pt) the columns are set 
% to their natural 
% widths. If \meta{width} is positive (e.g., \cs{textwidth}), column widths 
% are equal widths so that the overal width is \meta{width}.
% The sideways location of thre tabular is \meta{pos} (l, c, or r), and
% the column style id \meta{style} (l, c, or r).
%    \begin{macrocode}
\newcommand{\autorows}[5][0pt]{\par\begingroup
 \ctabsetlines
%    \end{macrocode}
% Set the table position
%    \begin{macrocode}
  \ctableftskip\fill
  \ctabrightskip\fill
  \if l#2 
    \ctableftskip\z@
  \else
    \if r#2
      \ctabrightskip\z@
    \fi
  \fi
%    \end{macrocode}
% Set the column position style
%    \begin{macrocode}
  \let\c@lleftskip\hfil
  \let\c@lrightskip\hfil
  \if l#4 
    \let\c@lleftskip\relax
  \else
    \if r#4
      \let\c@lrightskip\relax
    \fi
  \fi
%    \end{macrocode}
% Count the number of entries and the minimum width (max entry width)
% for the columns.
%    \begin{macrocode}
  \TX@cols=#3\relax
  \@curtab=#3\relax
  \@cellstogo = \TX@cols
  \@mincolumnwidth\z@
  \@linestogo\z@
  \@for\@tempa:=#5\do{%
    \advance\@linestogo\@ne
    \settowidth{\@tempdima}{\@tempa}
    \ifdim\@tempdima>\@mincolumnwidth
      \@mincolumnwidth=\@tempdima
    \fi}%
  \advance\@mincolumnwidth\tabcolsep
%    \end{macrocode}
% Specify what is to be done after every entry
%    \begin{macrocode}
  \def\@endcolumnactions{%
    \global\advance\@linestogo\m@ne
    \global\advance\@cellstogo\m@ne
    \ifnum\@cellstogo<\@ne
      \global\@cellstogo=\TX@cols
      \the\crtok
    \else 
      & 
    \fi}%
%    \end{macrocode}
% Calculate the width of the columns
%    \begin{macrocode}
  \ifdim #1>\z@
    \TX@col@width=#1
  \else
    \TX@col@width=\hsize
  \fi
  \divide\TX@col@width \TX@cols
  \ifdim #1=\z@
    \TX@col@width=\@mincolumnwidth
  \fi
  \penalty\abovecolumnspenalty
  \noindent % usually not a paragraph
  \vskip -\z@ % don't know why we need this, but looks bad without it
%    \end{macrocode}
% Create most of the preamble by looping to add \verb?\@cellsincolumn?-1 slots,
% then the last one which is different.
%    \begin{macrocode}
  \def\@preamble{}
  \begingroup
    \let\@sharp\relax
    \ifnum\TX@cols>\@ne
      \loop
        \ifdim #1<\z@
          \g@addto@macro{\@preamble}{%
            \strut\c@lleftskip\@sharp\c@lrightskip &}%
        \else
          \g@addto@macro{\@preamble}{%
            \hb@xt@ \TX@col@width{%
                    \strut\c@lleftskip\@sharp\c@lrightskip} &}%
        \fi
        \advance\TX@cols\m@ne
      \ifnum\TX@cols>\@ne
      \repeat
    \fi
    \ifdim #1<\z@
      \g@addto@macro{\@preamble}{%
        \strut\c@lleftskip\@sharp\c@lrightskip}%
    \else
      \g@addto@macro{\@preamble}{%
        \hb@xt@ \TX@col@width{\strut\c@lleftskip\@sharp\c@lrightskip}}%
    \fi
  \endgroup
  \let\@sharp ##
%    \end{macrocode}
% Start the \verb?\halign?
%    \begin{macrocode}
  \tabskip\ctableftskip
  \halign to \hsize \bgroup
    \tabskip\z@
    \@preamble
%%    \tabskip\ctabrightskip\cr \preautotab
    \tabskip\ctabrightskip\cr 
%    \end{macrocode}
% Add all the entries then finish off.
%    \begin{macrocode}
    \@for\@tempa:=#5\do{%
      \@tempa\unskip\space\@endcolumnactions}%
%%    \the\crtok \postautotab \the\crtok \egroup \endgroup \par
    \the\crtok \egroup \endgroup \par
}
  
%    \end{macrocode}
% \end{macro}
%  
%
%
%
% \section{Floating objects}
%
%    The file \file{latex.dtx} only defines a number of tools with
%    which floating objects can be defined. This is done in the
%    document class. It needs to define the following macros for each
%    floating object of type \texttt{TYPE} (e.g., \texttt{TYPE} =
%    figure).
%
%    \begin{description}
%    \item[\texttt{\bslash fps@TYPE}]
%        The default placement specifier for floats of type
%        \texttt{TYPE}.
%
%    \item[\texttt{\bslash ftype@TYPE}]
%        The type number for floats of type \texttt{TYPE}.  Each
%        \texttt{TYPE} has associated a unique positive \texttt
%        {TYPE} number, which is a power of two.  E.g., figures might
%        have type number 1, tables type number 2, programs type
%        number 4, etc.
%
%    \item[\texttt{\bslash ext@TYPE}]
%        The file extension indicating the file on which the contents
%        list for float type \texttt{TYPE} is stored.  For example,
%        |\ext@figure| = `lof'.
%
%    \item[\texttt{\bslash fnum@TYPE}]
%        A macro to generate the figure number for a caption. For
%        example, |\fnum@TYPE| == `Figure |\thefigure|'.
%
%    \item[\texttt{\bslash @makecaption}{\meta{num}}{\meta{text}}]
%        A macro to make a caption, with \meta{num} the value produced
%        by |\fnum@...| and \meta{text} the text of the caption. It
%        can assume it's in a |\parbox| of the appropriate width.
%        This will be used for \emph{all} floating objects.
%
%    \end{description}
%
%    The actual environment that implements a floating object such as
%    a figure is defined using the macros |\@float| and |\end@float|,
%    which are defined in \file{latex.dtx}.
%
%    An environment that implements a single column floating object is
%    started with |\@float{|\texttt{TYPE}|}[|\meta{placement}|]| of type
%    \texttt{TYPE} with \meta{placement} as the placement specifier.
%    The default value of \meta{PLACEMENT} is defined by |\fps@TYPE|.
%
%    The environment is ended by |\end@float|.  E.g., |\figure| ==
%    |\@float|{figure}, |\endfigure| == |\end@float|.
%
%
% \subsection{Floats}
%
% To define a float environment, say |fenv|, the following macros must be defined:
% \begin{itemize}
% \item |\fps@fenv| The default placement specifier (normally |tbp|). 
% \item |\ftype@fenv| The type number which is an integer and a power of 2.
% \item |\ext@fenv| The file extension for the contents list.
% \item |\c@fenv| A counter for the environment (for caption numbering).
% \item |\fnum@fenv| A macro to generate the caption `number'.
% \item |\l@fenv| A macro to produce an entry in a list of\ldots.
% \item |\flegtocfenv| A macro to write a |\namedlegend| title to a listof file.
% \item |\flegfenv| A macro to typeset the name of a |\namedlegend|.
% \item |\toclevel@fenv| Holding a bookmark level (required if the
%       \Lpack{hyperref} package will be used).
% \end{itemize}
%
% \begin{macro}{newflo@tctr}
% A counter for the type number of a new float. Normally
% figures are of type 1, tables type 2, and the next float type is then 4, and so
% on. 
%    \begin{macrocode}
\newcounter{newflo@tctr}
  \setcounter{newflo@tctr}{1}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\newfloat}
% |\newfloat[|\meta{within}|]{|\meta{fenv}|}{|\meta{ext}|}{|\meta{capname}|}|
% creates the commands for a new float environment, \meta{fenv} (aka |X|),
% using \meta{ext} (aka |Z|) as the
% file extension and \meta{capname} for the caption name.
%    \begin{macrocode}
\newcommand{\newfloat}[4][\@empty]{%
%    \end{macrocode}
% \begin{macro}{\ftype@X}
%  Define the float type, set it to the float counter, and double
% the counter afterwards.
%    \begin{macrocode}
  \@namedef{ftype@#2}{\value{newflo@tctr}}
  \addtocounter{newflo@tctr}{\value{newflo@tctr}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\c@X}
% Create the counter for the caption, which must not have been previously
% defined.
%    \begin{macrocode}
  \@ifundefined{c@#2}{% counter is not defined
    \ifx \@empty#1\relax
      \newcounter{#2}
    \else
      \newcounter{#2}[#1]
      \expandafter\edef\csname the#2\endcsname{%
  \expandafter\noexpand\csname the#1\endcsname.\noexpand\arabic{#2}}
    \fi}{}
  \setcounter{#2}{0}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ext@X}
% \begin{macro}{Zdepth}
% Define |\ext@X| for the file extension and set the new |Zdepth| 
% depth counter to 1.
%    \begin{macrocode}
  \@namedef{ext@#2}{#3}  % file extension
  \@ifundefined{c@#3depth}{\newcounter{#3depth}}{}
  \setcounter{#3depth}{1}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\fps@X}
% \begin{macro}{\fnum@X}
% \begin{macro}{\flegX}
% \begin{macro}{\flegtocX}
% |\fps@X| is the default float placement specification, |\fnum@X|
% typesets the caption name and number, and |\flegX| and |\flegtocX| are for
% named legends.
%    \begin{macrocode}
  \@namedef{fps@#2}{tbp}                     % position
  \@namedef{fnum@#2}{#4~\@nameuse{the#2}}    % caption naming
  \@namedef{fleg#2}{#4}                      % legend naming
  \@namedef{flegtoc#2}##1{}                  % legend name in ToC

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{environment}{X}
% \begin{environment}{X*}
% Finally define the new float environment, in both normal and starred
% forms.
%    \begin{macrocode}
  \newenvironment{#2}{\@float{#2}}{\end@float}
  \newenvironment{#2*}{\@dblfloat{#2}}{\end@dblfloat}
%    \end{macrocode}
% \end{environment}
% \end{environment}
%
% This ends the definition of |\newfloat|.
%    \begin{macrocode}
} % end \newfloat

%    \end{macrocode}
% \end{macro}
%
%
%
%
%
%
%
% To define subcaptions for use in a new float environment, say |fenv|, the
% following macros must be defined~\cite{SUBFIGURE}:
% \begin{itemize}
% \item A new counter |subfenv| for subcaption numbering.
% \item A new counter |extdepth|, where |ext| is the file extension
%       for the contents list of |fenv|, for setting the contents depth.
% \item |\thesubfenv| for the formatting of the subcaption number.
% \item |\@thesubfenv| for typesetting the number.
% \item |\@@thesubfenv| for alternative label reference.
% \item |\p@subfenv| for prepending to the subcaption number when it is referenced.
% \item |\ext@subfenv| the file extension for the contents list.
% \item |\l@subfenv| for formatting the contents list entry.
% \item |\@makesubfloatcaption| for typesetting the subcaption.
% \item |\toclevel@subfenv| for hyperref bookmarks
% \end{itemize}
%
% \begin{macro}{\newsubfloat}
% |\newsubfloat{|\meta{fenv}|}| creates the commands for a new
% subfloat for \meta{fenv} (aka X).
%    \begin{macrocode}
\newcommand{\newsubfloat}[1]{%
%    \end{macrocode}
% Call |\newlistentry[X]{subX}{extX}{1}| to get most of the work done.
%    \begin{macrocode}
  \newlistentry[#1]{sub#1}{\@nameuse{ext@#1}}{1}
%    \end{macrocode}
%
% \begin{macro}{\ext@subX}
% \begin{macro}{\thesubX}
% \begin{macro}{\@thesubX}
% \begin{macro}{\@@thesubX}
% \begin{macro}{\p@subX}
% \begin{macro}{\toclevel@subX}
% And now for the rest of the commands for subcaptions.
% \changes{v1.1}{2002/03/10}{Added \cs{@namedef} of \cs{@@thesubenv}
%                to \cs{newsubfloat}}
% \changes{v1.1}{2002/03/10}{Added \cs{@namedef} of \cs{toclevel@subenv}
%                to \cs{newsubfloat}}
%    \begin{macrocode}
  \@namedef{ext@sub#1}{\csname ext@#1\endcsname}
  \@namedef{thesub#1}{(\alph{sub#1})}
  \@namedef{@thesub#1}{\@nameuse{thesub#1}%
            \if@tightsubcap\hskip\subfloatlabelskip\else\space\fi}
  \@namedef{@@thesub#1}{\@nameuse{thesub#1}}
  \@namedef{p@sub#1}{\csname the#1\endcsname}
  \@namedef{toclevel@sub#1}{1}
}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% By permission of Steven Douglas Cochran the class provides similar 
% functionality as the \Lpack{subfigure} package~\cite{SUBFIGURE}.
% This requires some changes to be made to |\@float| and |\end@float|.
%
% \begin{macro}{\ifdonemaincaption}
% This is set TRUE after the |\(cont)caption| has been called in a float.
% \changes{v1.1}{2002/03/10}{Changed \cs{if@contmaincaption} to
%          \cs{ifdonemaincaption}}
%    \begin{macrocode}
\newif\ifdonemaincaption
  \donemaincaptionfalse

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@float}
% \begin{macro}{\@dbfloat}
% The kernel |\@float| and |\@dblfloat| macros are redefined to set
% |\ifdonemaincaption| to FALSE, and also to zero the subfloat
% counter, if it is defined.
%  Ignasi Furi\`{o} reported\footnote{Private email from 
% \texttt{ignasi.furio@uib.es}, 2003/10/17} that floats embedded in text 
% created an extra space. This was caused by a missing \verb?%?.
% \changes{v1.4}{2003/11/22}{Deleted spaces from \cs{@float} and
% \cs{@dblfloat} (from patch v1.9)}
%    \begin{macrocode}
\let\@memoldfloat\@float
\renewcommand{\@float}[1]{\donemaincaptionfalse
  \@ifundefined{c@sub#1}{}{\csname c@sub#1\endcsname = 0\relax}%
  \@memoldfloat{#1}}
\let\@memolddblfloat\@dblfloat
\renewcommand{\@dblfloat}[1]{\donemaincaptionfalse
  \@ifundefined{c@sub#1}{}{\csname c@sub#1\endcsname = 0\relax}%
  \@memolddblfloat{#1}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\end@float}
% \begin{macro}{\end@dblfloat}
% The kernel |\end@float| and |\end@dblfloat| macros are redefined to 
% dump out any subcaptions that have not yet been processed.
%    \begin{macrocode}
\let\@memoldefloat\end@float
\def\end@float{%
  \@memlistsubcaptions{\@captype}\@memoldefloat}
\let\@memoldedblfloat\end@dblfloat
\def\end@dblfloat{%
  \@memlistsubcaptions{\@captype}\@memoldedblfloat}

%    \end{macrocode}
% Unfortunately the \Lpack{fixltx2e} package, version 1.1h (current as of 2005/09/03) makes assumptions
% about \cs{end@float} which do not hold for memoir. The code has to be reverted back to that in version 1.0b of \Lpack{fixltx2e}.
% \changes{v1.618}{2005/09/03}{Dealt with incompatibilities between fixltx2e and memoir's \cs{end@dblfloat} (mempatch v3.7)}
%    \begin{macrocode}
\AtBeginDocument{\@ifpackageloaded{fixltx2e}{%
  \def\end@dblfloat{%
    \if@twocolumn
      \@endfloatbox
      \ifnum\@floatpenalty<\z@
        \@largefloatcheck
        \global\dp\@currbox1sp %
        \@cons\@deferlist\@currbox
      \fi
      \ifnum\@floatpenalty=-\@Mii \@Esphack\fi
    \else
      \end@float
    \fi}}{}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Captions}
%
%    The caption styling 
% is accomplished by redefining the |\@makecaption|
% command. First, though, define and initialise the user-level commands.
%
% \begin{macro}{\if@contcw}
% \begin{macro}{\if@conthang}
% \begin{macro}{\if@contindent}
% For use when checking caption width and captioning styles styles.
%    \begin{macrocode}
\newif\if@contcw
\newif\if@conthang
\newif\if@contindent

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\captiondelim}
% \begin{macro}{\@contdelim}
%  For the caption delimeter.
%    \begin{macrocode}
\newcommand{\captiondelim}[1]{\def\@contdelim{#1}}
\captiondelim{: }

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\captionnamefont}
% \begin{macro}{\@contnfont}
% The font for the caption name.
%    \begin{macrocode}
\newcommand{\captionnamefont}[1]{\def\@contnfont{#1}}
\captionnamefont{}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\captiontitlefont}
% \begin{macro}{\@conttfont}
% The font for the caption title.
%    \begin{macrocode}
\newcommand{\captiontitlefont}[1]{\def\@conttfont{#1}}
\captiontitlefont{}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\flushleftright}
% \begin{macro}{\centerlastline}
% These are in addition to the |\centering|, |\raggedleft| and |\raggedright|
% declarations for paragraphing. |\flushleftright| sets the skips to \TeX's
% normal (block) paragraphing values,
% while |\centerlastline| sets the skips to give a centered last line in
% a block paragraph.
%    \begin{macrocode}
\newcommand{\flushleftright}{%
  \leftskip\z@ \rightskip\z@
  \parfillskip=\z@ plus 1fil}
\newcommand{\centerlastline}{%
  \leftskip=\z@ plus 1fil
  \rightskip=\z@ plus -1fil
  \parfillskip=\z@ plus 2fil}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\captionstyle}
% \begin{macro}{\@contcstyle}
% The paragraphing style for the caption.
%    \begin{macrocode}
\newcommand{\captionstyle}[1]{\def\@contcstyle{#1}}
\captionstyle{}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\captionstyle}
% \begin{macro}{\@memcshort}
% \begin{macro}{\@memcnom}
% \begin{macro}{\@contcshortstyle}
% \begin{macro}{\@contcstyle}
% The paragraphing style for the caption.
%
% I had email from J{\o}rgen Larsen (\url{jl@dirac.ruc.dk}), 2003/04/09,
% asking for separate controls for the short and long captions. \\
%  |\captionstyle|\oarg{short}\marg{normal}.
% \changes{v1.4}{2003/11/22}{Extended \cs{captionstyle} (from patch v1.5)}
%    \begin{macrocode}
\renewcommand{\captionstyle}{%
  \@ifnextchar[ {\@memcshort}{\@memcnorm}}
\def\@memcshort[#1]#2{%
  \def\@contcshortstyle{#1}
  \def\@contcstyle{#2}}
\def\@memcnorm#1{%
  \def\@contcshortstyle{#1}
  \def\@contcstyle{#1}}
\captionstyle{}
  
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\@contcwidth}
% \begin{macro}{\captionwidth}
% \begin{macro}{\changecaptionwidth}
% \begin{macro}{\normalcaptionwidth}
% The macros for dealing with the caption width.
%    \begin{macrocode}
\newlength{\@contcwidth}
\newcommand{\captionwidth}[1]{\setlength{\@contcwidth}{#1}}
\captionwidth{\linewidth}
\newcommand{\changecaptionwidth}{\@contcwtrue}
\newcommand{\normalcaptionwidth}{\@contcwfalse}
\normalcaptionwidth

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@contindw}
% \begin{macro}{\hangcaption}
% \begin{macro}{\indentcaption}
% \begin{macro}{\normalcaption}
% The macros for hanging and indented captions.
%    \begin{macrocode}
\newlength{\@contindw}
\newcommand{\hangcaption}{\@conthangtrue\@contindentfalse}
\newcommand{\indentcaption}[1]{\setlength{\@contindw}{#1}%
  \@conthangfalse\@contindenttrue}
\newcommand{\normalcaption}{\@conthangfalse\@contindentfalse}
\normalcaption

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\precaption}
% \begin{macro}{\@contpre}
% \begin{macro}{\postcaption}
% \begin{macro}{\@contpost}
% \begin{macro}{\midbicaption}
% \begin{macro}{\@contmidbi}
% The macros for the pre- and post-caption text/commands, and
% for the mid-caption command for bilingual captions.
%    \begin{macrocode}
\newcommand{\precaption}[1]{\def\@contpre{#1}}
\precaption{}
\newcommand{\postcaption}[1]{\def\@contpost{#1}}
\postcaption{}
\newcommand{\midbicaption}[1]{\def\@contmidbi{#1}}
\midbicaption{}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\abovecaptionskip}
% \begin{macro}{\belowcaptionskip}
% Vertical space above and below a caption. Make them sum to an
% integral number of lines.
%    \begin{macrocode}
\newlength{\abovecaptionskip}
  \setlength{\abovecaptionskip}{0.5\onelineskip}
\newlength{\belowcaptionskip}
  \setlength{\belowcaptionskip}{0.5\onelineskip}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\caption}
% For subfloat support, the (kernel) |\caption| macro needs to note that it
% has been called.
%    \begin{macrocode}
\let\@memoldcaption\caption
\def\caption{\donemaincaptiontrue\@memoldcaption}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\memcaptioninfo}
% \verb?\memcaptioninfo{type}{\thetypenum}{short}{long}?
% \changes{v1.618}{2005/09/10}{Added \cs{memcaptioninfo}}
%    \begin{macrocode}
\newcommand*{\memcaptioninfo}[4]{}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@caption}
% For title referencing support, the (kernel) |\@caption| macro needs 
% to store the title.
% \changes{v1.618}{2005/09/03}{Added braces around second argument of \cs{@memold@caption} (per Dan Luecking, mempatch v3.0)}
%    \begin{macrocode}
\let\@memold@caption\@caption
\long\def\@caption#1[#2]#3{%
  \M@gettitle{#2}%
  \memcaptioninfo{#1}{\csname the#1\endcsname}{#2}{#3}%
  \@memold@caption{#1}[{#2}]{#3}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@makecaption}
%  This is a reimplementation of the kernel |\@makecaption| command.
% As well as including the caption typesetting commands it enables
% captions that include forced newlines (e.g., by |\\|).
%
% The first part is due to 
% Donald Arseneau\footnote{Email: \texttt{asnd@triumf.ca}} from postings
% to the \ctt{} newsgroup and Email discussions. The |\topskip| strut is
% used whenever the caption is the first part of the float. This means,
% among other things, that if a caption comes at the
% top of a page, then the first line of the caption will be aligned with
% the normal first line of a page. The |\abovecaptionskip| is only used
% when there is something above the caption in the current float.
% \changes{v0.31}{2001/07/24}{Changed \cs{@tempa} to \cs{@memtempa} in \cs{@makecaption}}
% \changes{v1.0a}{2002/02/12}{Added extra centering code in \cs{@makecaption}}
% \changes{v1.4}{2003/11/22}{Added separate short/long styles in \cs{@makecaption}
%                             (from patch v1.5)}
%    \begin{macrocode}
\long\def\@makecaption#1#2{\let\@memtempa\relax
  \ifdim\prevdepth>-99\p@ \vskip\abovecaptionskip
  \else \def\@memtempa{\vbox to\topskip{}}\fi
%    \end{macrocode}
% \begin{macro}{\@contfnote}
% \begin{macro}{\@contfmark}
% The caption title will be typeset twice, firstly to measure its width
% and secondly to actually typeset it. To avoid problems caused by
% a footnote in the caption getting processed twice, we temporarily
% disable the expected relevant commands.
%    \begin{macrocode}
  \let\@contfnote\footnote \renewcommand{\footnote}[2][]{}
  \let\@contfmark\footnotemark \renewcommand{\footnotemark}[1][]{}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% Now measure the width of the total caption, not forgetting to take account
% of the font specifications, and then restore the footnoting. 
%    \begin{macrocode}
  \sbox\@tempboxa{\@contnfont #1\@contdelim \@conttfont #2}
  \let\footnote\@contfnote
  \let\footnotemark\@contfmark
%    \end{macrocode}
% If the caption is less than one
% line, then the whole caption needs to be centered on the page (otherwise
% the short caption may be typeset flushleft).
%    \begin{macrocode}
  \ifdim\wd\@tempboxa<\linewidth \centering \fi
  \if@contcw
%    \end{macrocode}
% For typesetting at anything other than the normal width, put the caption
% into a |\parbox| of the specified width. This must be centered.
%    \begin{macrocode}
    \centering
    \parbox{\@contcwidth}{%
%    \end{macrocode}
% Henrik Holm\footnote{Email from \texttt{henrik@tele.ntnu.no} on
% 2002/02/10.} proposed adding the next line to center short, narrow captions.
%    \begin{macrocode}
    \ifdim\wd\@tempboxa<\@contcwidth \centering \fi
  \fi
  \if@conthang
%    \end{macrocode}
% For a hanging caption we have to measure the width of the caption name,
% then typeset the whole caption in a hanging paragraph.
% \changes{v1.1}{2002/03/10}{Changed code for hangcaption in \cs{@makecaption}}
%    \begin{macrocode}
    \sbox\@tempboxa{\@contnfont #1\@contdelim}
    \@contpre%
    {\@contcstyle\hangindent=\wd\@tempboxa
     \noindent\box\@tempboxa\@memtempa \@conttfont #2\par}
  \else
    \if@contindent
%    \end{macrocode}
% An indented caption is similar, except the amount of indentation is
% kept in |\@contindw|.
%    \begin{macrocode}
      \@contpre%
      {\@contnfont #1\@contdelim}\@memtempa 
      {\@contcstyle\hangindent=\@contindw
                   \hangafter=\@ne\@conttfont #2\par}% <- v1.4 
    \else
%    \end{macrocode}
% For the normal style, just typeset the caption.
%    \begin{macrocode}
      \@contpre%
      {\@contnfont #1\@contdelim}\@memtempa 
      {\ifdim\wd\@tempboxa<\linewidth 
         \@contcshortstyle\else \@contcstyle\fi%  <- v1.4 
       \@conttfont #2\par}
    \fi
  \fi
%    \end{macrocode}
% Finish off the typesetting by processing the post-text,  and if not using
% the normal width then close off the |\parbox|, and lastly put in some
% vertical space. 
%    \begin{macrocode}
  \@contpost
  \if@contcw
    \par
    }  % end of the \parbox
  \fi
  \vskip\belowcaptionskip}

%    \end{macrocode}
% \end{macro}
%
%
%
% \subsubsection{Continuation captions and legends}
%
% \begin{macro}{\contcaption}
%    |\contcaption{|\meta{text}|}| is a user-level command. 
%    It is a simplified
%    version of the normal |\caption| command as it doesn't have to deal
%    with numbering or list of \dots entries. 
%
% However,
% Brent Lievers\footnote{ (\texttt{lieversb@post.queensu.ca})
% in \ctt{} thread \textit{Figures}, 2003/11/14.} requested that \cs{label}
% should pick up the correct caption number after a \cs{contcaption}.
% \changes{v1.4}{2003/11/22}{Extended \cs{contcaption} (from patch v1.9)} 
%    \begin{macrocode}
\newcommand{\contcaption}{%
  \addtocounter{\@captype}{\m@ne}\refstepcounter{\@captype}%
  \@contcaption\@captype}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@@contcaption}
% \begin{macro}{\@contcaption}
%    |\@contcaption| is the workhorse for the |\contcaption| command. In turn,
% it uses the |\@makecaption| command (defined in the usual classes)
% to do most of its work. It
% uses the number of the previous |\caption| command in the same
% type of float and its implementation includes much of the code
% used in the \LaTeX{} |\@caption| command.
% 
%    First specify |\@@contcaption{|\meta{type}|}{|\meta{title}|}| 
% to save some repetitive code.
%    \begin{macrocode}
\long\def\@@contcaption#1#2{%
  \par
  \begingroup
     \@parboxrestore
     \if@minipage
       \@setminipage
     \fi
     \normalsize
     \@makecaption{\csname fnum@#1\endcsname}{\ignorespaces #2}\par
  \endgroup}

%    \end{macrocode}
% \end{macro}
%
% Now for |\@contcaption{|\meta{type}|}{|\meta{title}|}|. 
% It has to flush out any subcaptions at the 
% appropriate time, as well as typesetting the caption.
%    \begin{macrocode}
\long\def\@contcaption#1#2{%
  \if@contbotsub
    \@memlistsubcaptions{#1}%
    \@@contcaption{#1}{#2}%
  \else
    \@@contcaption{#1}{#2}%
    \@memlistsubcaptions{#1}%
  \fi}

%    \end{macrocode}
% \end{macro}    
%
%
% \begin{macro}{\memlegendinfo}
% \begin{macro}{\legend}
%    The macro \cs{legend}\marg{text}is intended
% to be used in a float environment for an `anonymous' caption, but can be
% used anywhere.
%
%    The implementation is similar to the \cs{caption} command but we have
% to eliminate printing of a delimeter.
% \changes{v1.4}{2003/11/16}{Added \cs{M@gettitle} to \cs{legend}}
% \changes{v1.618}{2005/09/10}{Added \cs{memlegendinfo}}
%    \begin{macrocode}
\newcommand*{\memlegendinfo}[1]{}
\newcommand{\legend}[1]{%
  \M@gettitle{#1}%
  \memlegendinfo{#1}%
  \par
  \begingroup
     \@parboxrestore
     \if@minipage
       \@setminipage
     \fi
     \normalsize
     \captiondelim{\mbox{}}
     \@makecaption{}{\ignorespaces #1}\par
  \endgroup}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\namedlegend}
% \begin{macro}{\memnamedlegendinfo}
% \cs{namedlegend}\oarg{short-title}\marg{long-title} is like the
% \cs{caption} command except that it does not number the caption.
%
% \cs{memnamedlegendinfo}\marg{type}\marg{short-title}\marg{long-title}
% \changes{v1.618}{2005/09/10}{Added \cs{memnamedlegendinfo}}
%    \begin{macrocode}
\newcommand{\namedlegend}{\@dblarg{\@legend\@captype}}
\newcommand*{\memnamedlegendinfo}[3]{}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@legend}
% \cs{@legend}\marg{type}\oarg{short-title}\marg{long-title}
% is the workhorse for the \cs{namedlegend} command. In turn, it calls
% \cs{@makelegend}. It requires two commands to have been defined, namely
% \cs{flegtoctype} and \cs{flegtype}. The command \cs{flegtoctype}\marg{text}
% is responsible for writing a title text to the appropriate listof file.
% \cs{flegtype} is responsible for typeseting the name of the legend.
% \changes{v1.4}{2003/11/16}{Added \cs{M@gettitle} to \cs{@legend}}
%    \begin{macrocode}
\long\def\@legend#1[#2]#3{%
  \M@gettitle{#2}%
  \memnamedlegendinfo{#1}{#2}{#3}%
  \par
  \csname flegtoc#1\endcsname{#2}%
  \begingroup
    \@parboxrestore
    \if@minipage
      \@setminipage
    \fi
    \normalsize
    \@makecaption{\csname fleg#1\endcsname}{\ignorespaces #3}\par
  \endgroup}

%    \end{macrocode}
% \end{macro}
%
%
% \subsubsection{Non-float captions}
%
% \begin{macro}{\newfixedcaption}
% \begin{macro}{\renewfixedcaption}
% \begin{macro}{\providefixedcaption}
% These commands are defined in terms of their \verb?\...command? counterparts.\\
% Call as \verb?\...fixedcaption?\oarg{capcommand}\marg{command}\marg{env}
%    \begin{macrocode}
\newcommand{\newfixedcaption}[3][\caption]{%
  \newcommand{#2}{\def\@captype{#3}#1}}
\newcommand{\renewfixedcaption}[3][\caption]{%
  \renewcommand{#2}{\def\@captype{#3}#1}}
\newcommand{\providefixedcaption}[3][\caption]{%
  \providecommand{#2}{\def\@captype{#3}#1}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsubsection{Bilingual captions}
%
%    The bilingual caption commands all use internal grouping so
% that any changes are kept local. This has the unfortunate side-effect
% that any |\label| command must be within the grouping otherwise the
% wrong number is picked up. To make the coding, if not necessarily the 
% use, of the commands simpler, I have not used the traditional style
% of square brackets for optional caption text arguments. Instead, empty
% `required' arguments are used as the implementation means. 
%
% \begin{macro}{\membitwonumcaptioninfo}
% \begin{macro}{\membionenumcaptioninfo}
% \begin{macro}{\membicaptioninfo}
% \verb?\membitwonumcaptioninfo{type}{\thetypenum}?\marg{short1}\marg{long1}\\
%  \hspace*{2in} \marg{name2}\marg{short2}\marg{long2} \\
% \verb?\membionenumcaptioninfo{type}{\thetypenum}?\marg{short1}\marg{long1}\\
%  \hspace*{2in} \marg{name2}\marg{short2}\marg{long2} \\
% \verb?\membicaptioninfo{type}{\thetypenum}?\marg{short1}\marg{long1}\\
%  \hspace{2in} \marg{name2}\marg{long2} \\
% \changes{v1.618}{2005/09/10}{Added \cs{membitwonumcaptioninfo},
%   \cs{membitwonumcaptioninfo} and \cs{membicaptioninfo}}
%    \begin{macrocode}
\newcommand*{\membitwonumcaptioninfo}[7]{}
\newcommand*{\membionenumcaptioninfo}[7]{}
\newcommand*{\membicaptioninfo}[6]{}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\bitwonumcaption}
%  The 6 arguments are: optional label, short and long in language 1, 
% name in
% language 2, and short and long in language 2. Both texts are put
% into the List of as numbered entries.
%    \begin{macrocode}
\newcommand{\bitwonumcaption}[6][\@empty]{%
  \begingroup
  \let\memcaptioninfo\@gobblefour
%    \end{macrocode}
% Check if the first language argument is vacuous, then call
% the normal |\caption| for language 1.
%    \begin{macrocode}
  \@ifmtarg{#2}{\def\m@mscapi{#3}\caption{#3}}%
               {\def\m@mscapi{#2}\caption[#2]{#3}}%
%    \end{macrocode}
% Do the optional labeling.
%    \begin{macrocode}
  \ifx \@empty #1\else
    \label{#1}%
  \fi
%    \end{macrocode}
% Remove any extra spacing between the captions, and set the
% NAME for the second caption. Use a command to transfer
% the NAME to the renewal code to avoid circularity if
% for example, we are trying to redefine |\tablename| as
% |\tablename|. Decrement the caption counter.
%    \begin{macrocode}
  \setlength{\abovecaptionskip}{0pt}%
  \setlength{\belowcaptionskip}{0pt}%
  \edef\@memtempc{#4}%
  \expandafter\renewcommand\csname \@captype name\endcsname{\@memtempc}%
  \addtocounter{\@captype}{-1}%
%    \end{macrocode}
% Now repeat for the second language caption.
%    \begin{macrocode}
  \@contmidbi
  \@ifmtarg{#5}{\def\m@mscapii{#6}\caption{#6}}%
               {\def\m@mscapii{#5}\caption[#5]{#6}}%
  \membitwonumcaptioninfo{\@captype}{\@nameuse{the\@captype}}%
                         {\m@mscapi}{#3}{#4}{\m@mscapii}{#6}%
  \endgroup
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\bionenumcaption}
%  The 6 arguments are: optional labelling,
%  short and long in language 1, name in
% language 2, and short and long in language 2. Both texts are put
% into the List of, but only the first is numbered.
%    \begin{macrocode}
\newcommand{\bionenumcaption}[6][\@empty]{%
  \begingroup
  \let\memcaptioninfo\@gobblefour
%    \end{macrocode}
% Check if the first language argument is vacuous, then call
% the normal |\caption| for language 1.
%    \begin{macrocode}
  \@ifmtarg{#2}{\def\m@mscapi{#3}\caption{#3}}%
               {\def\m@mscapi{#2}\caption[#2]{#3}}%
%    \end{macrocode}
% Do the optional labeling.
%    \begin{macrocode}
  \ifx \@empty #1\else
    \label{#1}%
  \fi
%    \end{macrocode}
% Do the between captions code.
%    \begin{macrocode}
  \setlength{\abovecaptionskip}{0pt}%
  \setlength{\belowcaptionskip}{0pt}%
  \edef\@memtempc{#4}%
  \expandafter\renewcommand\csname \@captype name\endcsname{\@memtempc}
%    \end{macrocode}
% Use a continuation caption for the second language, not forgetting
% to add the appropriate unnumbered text to the List.
%    \begin{macrocode}
  \@contmidbi
  \contcaption{#6}%
  \@ifmtarg{#5}{%
    \def\m@mscapii{#6}%
    \addcontentsline{\csname ext@\@captype\endcsname}{\@captype}%
      {\protect\numberline{}{\ignorespaces #6}}}{%
    \def\m@mscapii{#5}%
    \addcontentsline{\csname ext@\@captype\endcsname}{\@captype}%
      {\protect\numberline{}{\ignorespaces #5}}}%
  \membionenumcaptioninfo{\@captype}{\@nameuse{the\@captype}}%
                         {\m@mscapi}{#3}{#4}{\m@mscapii}{#6}%
  \endgroup
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\bicaption}
%  The 5 arguments are: optional labelling, 
% short and long in language 1, name in
% language 2, and long in language 2. 
% Only the first text is put into the List.
%    \begin{macrocode}
\newcommand{\bicaption}[5][\@empty]{%
  \begingroup
  \let\memcaptioninfo\@gobblefour
%    \end{macrocode}
% Check if the first language argument is vacuous, then call
% the normal |\caption| for language 1.
%    \begin{macrocode}
  \@ifmtarg{#2}{\def\m@mscapi{#3}\caption{#3}}%
               {\def\m@mscapi{#2}\caption[#2]{#3}}%
%    \end{macrocode}
% Do the optional labeling.
%    \begin{macrocode}
  \ifx \@empty #1\else
    \label{#1}%
  \fi
%    \end{macrocode}
% Do the between captions code and 
% finally just use |\contcaption| for the
% second language.
%    \begin{macrocode}
  \setlength{\abovecaptionskip}{0pt}%
  \setlength{\belowcaptionskip}{0pt}%
  \edef\@memtempc{#4}
  \expandafter\renewcommand\csname \@captype name\endcsname{\@memtempc}%
  \@contmidbi
  \contcaption{#5}%
  \membicaptioninfo{\@captype}{\@nameuse{the\@captype}}%
                   {\m@mscapi}{#3}{#4}{#5}%
  \endgroup
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\bicontcaption}
%  The 3 arguments are long in language 1, name in
% language 2, and long in language 2.
%    \begin{macrocode}
\newcommand{\bicontcaption}[3]{%
  \begingroup
%    \end{macrocode}
% Call |\contcaption| for language 1.
%    \begin{macrocode}
  \contcaption{#1}%
%    \end{macrocode}
% Do the between captions code and use |\contcaption| for the second
% language.
%    \begin{macrocode}
  \setlength{\abovecaptionskip}{0pt}%
  \setlength{\belowcaptionskip}{0pt}%
  \edef\@memtempc{#2}%
  \expandafter\renewcommand\csname \@captype name\endcsname{\@memtempc}%
  \@contmidbi
  \contcaption{#3}%
  \endgroup
}

%    \end{macrocode}
% \end{macro}
%
%
%
% \subsubsection{Support for the \Lopt{subfigure} package functionality}
%
% Much of the code in this section is based on the \Lpack{subfigure}
% package code, by kind permission of its author, Steven Douglas Cochran.
% To try and avoid clashes with the real \Lpack{subfigure} code I have
% used different macro names, especially when I have copied the code.
%
% \begin{macro}{\subcaptionstyle}
% \begin{macro}{\@contsubcstyle}
% The paragraphing style for subcaptions.
%    \begin{macrocode}
\newcommand{\subcaptionstyle}[1]{\def\@contsubcstyle{#1}}
\subcaptionstyle{}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\if@shortsubcap}
% \begin{macro}{\if@hangsubcap}
% \begin{macro}{\shortsubcaption}
% \begin{macro}{\hangsubcaption}
% \begin{macro}{\normalsubcaption}
%  For dealing with short and hanging subcaptions. 
% Analagous to the \Lpack{subfigure} |nooneline| and |hang| options.
% The default is normal subcaptions.
%    \begin{macrocode}
\newif\if@shortsubcap
\newif\if@hangsubcap
\newcommand*{\shortsubcaption}{\@shortsubcaptrue}
\newcommand*{\hangsubcaption}{\@hangsubcaptrue}
\newcommand*{\normalsubcaption}{\@shortsubcapfalse\@hangsubcapfalse}
\normalsubcaption

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\subfloattopskip}
% \begin{macro}{\subfloatcapskip}
% \begin{macro}{\subfloatcaptopadj}
% \begin{macro}{\subfloatbottomskip}
% \begin{macro}{\subfloatlabelskip}
% \begin{macro}{\subfloatcapmargin}
% These |\subfloat...| lengths are analagous to the \Lpack{subfigure} 
% |\subfig...| lengths.
%    \begin{macrocode}
\newskip\subfloattopskip
\newskip\subfloatcapskip
\newskip\subfloatcaptopadj
\newskip\subfloatbottomskip
\newskip\subfloatlabelskip
\newdimen\subfloatcapmargin
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\if@tightsubcap}
% \begin{macro}{\loosesubcaptions}
% \begin{macro}{\tightsubcaptions}
% Unlike the \Lpack{subfigure} package the class provides no options for
% subcaptions. These macros provide the \Lpack{subfigure} loose/tight
% option functions. Set the default to tight.
%    \begin{macrocode}
\newif\if@tightsubcap
\newcommand{\loosesubcaptions}{%
  \subfloattopskip = 10\p@
  \subfloatcapskip  = 10\p@
  \subfloatcaptopadj = \z@
  \subfloatbottomskip = 10\p@
  \subfloatlabelskip = 0.33em
  \subfloatcapmargin = 10\p@
  \@tightsubcapfalse
}

\newcommand{\tightsubcaptions}{%
  \subfloattopskip = 5\p@
  \subfloatcapskip  = \z@
  \subfloatcaptopadj = 3\p@
  \subfloatbottomskip = 5\p@
  \subfloatlabelskip = 0.33em \@plus 0.07em \@minus 0.03em
  \subfloatcapmargin = \z@
  \@tightsubcaptrue
}
\tightsubcaptions

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\subcaptionsize}
% \begin{macro}{\@subcapsize}
% \begin{macro}{\subcaptionlabelfont}
% \begin{macro}{\@subcaplabelfont}
% \begin{macro}{\subcaptionfont}
% \begin{macro}{\@subcapfont}
% These macros set the size and fonts for the subcaptions. Set the defaults
% to |\footnotesize|, and the normal roman font.
%    \begin{macrocode}
\newcommand*{\subcaptionsize}[1]{\def\@subcapsize{#1}}
\newcommand*{\subcaptionlabelfont}[1]{\def\@subcaplabelfont{#1}}
\newcommand*{\subcaptionfont}[1]{\def\@subcapfont{#1}}
\subcaptionsize{\footnotesize}
\subcaptionlabelfont{\normalfont}
\subcaptionfont{\normalfont}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@contkeep}
% \begin{macro}{\@contset}
% \begin{macro}{\subconcluded}
% |\@contkeep| stores
% the current subfloat number in counter |@contsubnum| and 
% |\@contset| sets the subfloat number to the value of |@contsubnum|.
% |\subconcluded| sets the subfloat number to zero. 
%    \begin{macrocode}
  \newcounter{@contsubnum}
  \newcommand{\@contkeep}{%
    \setcounter{@contsubnum}{\value{sub\@captype}}}
  \newcommand{\@contset}{%
    \setcounter{sub\@captype}{\value{@contsubnum}}}
  \newcommand{\subconcluded}{%
    \setcounter{sub\@captype}{0}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
%
% \begin{macro}{\if@contbotsub}
% A flag indicating whether the subcaption is to be at the bottom or
% top of the subfloat; TRUE for the subcaption at the bottom.
%    \begin{macrocode}
\newif\if@contbotsub
  \@contbotsubtrue

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\subcaption}
% |\subcaption[|\meta{list-entry}|]{|\meta{caption}|}| is a generic
% subcaption. There is no \Lpack{subfigure} equivalent.
%    \begin{macrocode}
\newcommand{\subcaption}{%
  \bgroup
    \let\label=\memsub@label
    \ifdonemaincaption\else
      \advance\csname c@\@captype\endcsname\@ne
    \fi
    \refstepcounter{sub\@captype}\@contkeep
    \@ifnextchar [%
      {\@memsubcap{sub\@captype}}%
      {\@memsubcap{sub\@captype}[\@empty]}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@memsubcap}
% This handles the optional argument to |\subcaption|. It sets |\@tempdima|
% to |\hsize| as later on |\@makesubfloatcaption| uses this.
% \changes{v1.1a}{2002/04/28}{Added \cs{@tempdima} to \cs{@memsubcap}}
%    \begin{macrocode}
\long\def\@memsubcap#1[#2]#3{%
  \@tempdima=\hsize
  \vskip\subfloatcapskip
  \ifx \@empty #2
    \@memsubcaption{#1}{#3}{#3}%
  \else
    \@memsubcaption{#1}{#2}{#3}%
  \fi
  \vskip\subfloatcapskip
  \egroup}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@memsubcaption}
% |\@memsubcaption{|\meta{type}|}{|\meta{list-entry}|}{|\meta{caption}|}|
% typesets a subcaption. This is a copy of the \Lpack{subfigure} 
% |\@subcaption| macro.
%    \begin{macrocode}
\newcommand{\@memsubcaption}[3]{%
  \ifx \relax#2\relax \else
    \bgroup
      \let\label\@gobble
      \let\protect\string
      \def\@memsubcaplabel{\@nameuse{@@the#1}}%
      \xdef\@memsubfigcaptionlist{%
        \@memsubfigcaptionlist,%
  {\protect\numberline{\@memsubcaplabel}\noexpand{\ignorespaces #2}}}%
    \egroup
  \fi
  \@makesubfloatcaption{\@nameuse{@the#1}}{#3}}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\contsubcaption}
% |\contsubcaption[|\meta{caption}|]| is the continued version of
% |\subcaption|.
%    \begin{macrocode}
\newcommand{\contsubcaption}{%
  \bgroup
    \let\label=\memsub@label
    \@contset
    \refstepcounter{sub\@captype}\@contkeep
    \@ifnextchar [%
      {\@memsubcap{sub\@captype}}%
      {\@memsubcap{sub\@captype}[\@empty]}}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{environment}{subfloat}
% The \Lpack{subfigure} documentation suggests a way of defining a |subfloat|
% environment. This is a trivial implementation because the |\subcaption|
% and |\contsubcaption| commands are provided by the class, and can be used
% within a |subfloat|.
%    \begin{macrocode}
\newenvironment{subfloat}{}{}

%    \end{macrocode}
% \end{environment}
%
%
%
% \begin{macro}{\subbottom}
% \begin{macro}{\@memsubbody}
%    |\subbottom[|\meta{list-entry}|][|\meta{subcaption}|]{|\meta{text}|}|
% typesets a subcaption below the \meta{text}. Most of the  work is
% performed by the |\@memsubbody| macro.
%    \begin{macrocode}
\newcommand{\subbottom}{%
  \@contbotsubtrue
  \@memsubbody}

\newcommand{\@memsubbody}{%
  \bgroup
  \let\label=\memsub@label
  \ifdonemaincaption\else
    \advance\csname c@\@captype\endcsname\@ne
  \fi
  \refstepcounter{sub\@captype}\@contkeep%
  \leavevmode
  \@ifnextchar [%
    {\@memsubfig}%
    {\@memsubfig[\@empty]}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\contsubbottom}
% \begin{macro}{\@memcontsubbody}
%     These are the continued versions of |\subbottom| and |\@memsubbody|.
% \begin{macrocode}
\newcommand{\contsubbottom}{%
  \@contbotsubtrue
  \@memcontsubbody}

\newcommand{\@memcontsubbody}{%
  \bgroup
  \let\label=\memsub@label
  \@contset
  \refstepcounter{sub\@captype}\@contkeep%
  \leavevmode
  \@ifnextchar [%
    {\@memsubfig}%
    {\@memsubfig[\@empty]}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\subtop}
% \begin{macro}{\contsubtop}
%  These are similar to |\subbottom| and |\contsubbottom| except that they
% put the subcaption on top of the \meta{text}.
%    \begin{macrocode}
\newcommand{\subtop}{%
  \@contbotsubfalse
  \@memsubbody}

\newcommand{\contsubtop}{%
  \@contbotsubfalse
  \@memcontsubbody}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \changes{v1.1a}{2002/04/28}{Deleted \cs{@contsubfloat} as it was not used}
%
% \begin{macro}{\@memsubfig}
% This is a revised version of the \Lpack{subfigure} |\@subfigure| command
% --- just the called macro names are changed.
%    \begin{macrocode}
\def\@memsubfig[#1]{%
  \@ifnextchar [%
    {\@memsubfloat{sub\@captype}[#1]}%
    {\@memsubfloat{sub\@captype}[\@empty #1][#1]}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@memsubfloat}
%    This is a modified version of the \Lpack{subfigure} |\@subfloat|
% command Essentially the |\csname if#1topcap\endcsname| constructs are
% replaced by |\if@contbotsub|. 
%    \begin{macrocode}
\def\@memsubfloat#1[#2][#3]#4{%
  \@tempcnta=\@ne
  \if@tightsubcap
    \if@minipage
      \@tempcnta=\z@
    \else
      \ifdim\lastskip=\z@
        \@tempcnta=\@ne
      \else
        \@tempcnta=\tw@
      \fi
    \fi
  \fi
  \if@contbotsub
    \def\subfig@top{\subfloattopskip}%
    \def\subfig@bottom{\subfloatbottomskip}%
  \else
    \def\subfig@top{\subfloatbottomskip}%
    \def\subfig@bottom{\subfloattopskip}%
  \fi
  \setbox\@tempboxa \hbox{#4}%
  \@tempdima=\wd\@tempboxa
  \vtop\bgroup
    \vbox\bgroup
    \ifcase\@tempcnta
      \@minipagefalse
    \or
      \vspace{\subfig@top}
    \or
      \ifdim \lastskip=\z@ \else
        \@tempskipb\subfig@top\@xaddvskip
      \fi
    \fi
    \if@contbotsub
      \box\@tempboxa\egroup
      \ifx \@empty#3\relax \else
        \vskip\subfloatcapskip
        \@memsubcaption{#1}{#2}{#3}%
      \fi
    \else
      \ifx \@empty#3\relax \else
        \@memsubcaption{#1}{#2}{#3}%
        \vskip\subfloatcapskip
        \vskip\subfloatcaptopadj
      \fi\egroup
      \box\@tempboxa
    \fi
    \vspace{\subfig@bottom}
  \egroup
\egroup}
  
%    \end{macrocode}
% \end{macro}
%
%
% The following series of macros, from \Lpack{subfigure}, control the 
% typesetting of the subcaptions.
%
% \begin{macro}{\@memsubfigcaptionlist}
% A copy of |\@subfigcaptionlist|.
%    \begin{macrocode}
\newcommand*{\@memsubfigcaptionlist}{}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\memlistsubcaptions}
% A copy of |\listsubcaptions|.
%    \begin{macrocode}
\newcommand*{\memlistsubcaptions}{%
  \@ifstar
    {\gdef\@memsubfigcaptionlist{}}%
    {\@memlistsubcaptions{\@captype}}}

%    \end{macrocode}
% \end{macro}
% 
%
% \begin{macro}{\@memlistsubcaptions}
% A copy of |\@listsubcaptions|.
%    \begin{macrocode}
\newcommand*{\@memlistsubcaptions}[1]{%
  \@ifundefined{@captype}{}{%
    \@ifundefined{ext@sub#1}{}{%
      \@for \@tempa:=\@memsubfigcaptionlist \do {%
        \ifx \@empty\@tempa\relax \else
          \addcontentsline{\@nameuse{ext@sub#1}}{sub#1}{\@tempa}%
        \fi}}}%
  \gdef\@memsubfigcaptionlist{}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@makesubfloatcaption}
%  This is a copy of |\@makesubfigurecaption|.
%    \begin{macrocode}
\newcommand{\@makesubfloatcaption}[2]{%
  \setbox\@tempboxa\hbox{%
    \@subcapsize
    {\@subcaplabelfont #1}{\@subcapfont\ignorespaces #2}}%
  \@tempdimb=-\subfloatcapmargin
  \multiply\@tempdimb\tw@
  \advance\@tempdimb\@tempdima
  \hb@xt@\@tempdima{%
    \hss
    \ifdim \wd\@tempboxa >\@tempdimb
      \memsubfig@caption{#1}{#2}%
    \else
      \if@shortsubcap
        \memsubfig@caption{#1}{#2}%
      \else
        \box\@tempboxa
      \fi
    \fi
    \hss}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\memsubfig@caption}
% This is a copy of |\subfig@caption|.
%    \begin{macrocode}
\newcommand{\memsubfig@caption}[2]{%
  \if@hangsubcap
    \sbox{\@tempboxa}{\@subcapsize\@subcaplabelfont #1}%
    \addtolength{\@tempdimb}{-\wd\@tempboxa}%
    \usebox{\@tempboxa}%
    \memsubfig@captionpar{\@tempdimb}{%
      {\@subcapfont\ignorespaces #2}}%
  \else
    \memsubfig@captionpar{\@tempdimb}{{\@subcaplabelfont #1}%
      {\@subcapfont\ignorespaces #2}}%
  \fi}

%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\memsubfig@captionpar}
% This replaces |\subfig@captionpar|.
%    \begin{macrocode}
\newcommand{\memsubfig@captionpar}[2]{%
  \parbox[t]{#1}{\@subcapsize\@contsubcstyle #2}}

%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\memsub@label}
% These are copies of |\sub@label| and |\subref|.
%    \begin{macrocode}
\newcommand{\memsub@label}{%
  \@ifnextchar (%
    {\sf@memsub@label}%
    {\sf@memsub@label(Sub\@captype\space
                     \@nameuse{p@sub\@captype}%
                     \@nameuse{thesub\@captype})}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sf@memsub@label}
% This is a copy of |\sf@sub@label|.
%    \begin{macrocode}
\def\sf@memsub@label(#1)#2{%
  \protected@edef\mem@currentlabelname{#1}%
  \sf@@memsub@label{#2}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sf@@memsub@label}
% This is an expanded copy of |\sf@@sub@label| processed after any packages may
% have been loaded.
%    \begin{macrocode}
\AtBeginDocument{%
  \@ifpackageloaded{nameref}{%
%    \end{macrocode}
% The \Lpack{nameref} package is loaded.
%    \begin{macrocode}
    \newcommand*{\sf@@memsub@label}[1]{%
      \@bsphack
      \protected@write\@auxout{}{%
        \string\newlabel{#1}%
          {{\@nameuse{p@sub\@captype}\@nameuse{@@thesub\@captype}}%
          {\thepage}%
          {\mem@currentlabelname\relax}%
          {\@currentHref}{}}}%
      \protected@write\@auxout{}{%
        \string\newlabel{sub@#1}%
          {{\@nameuse{@@thesub\@captype}}%
          {\thepage}%
          {\mem@currentlabelname\relax}%
          {\@currentHref}{}}}%
  \@esphack}
  }{\@ifpackageloaded{hyperref}{%
%    \end{macrocode}
% The \Lpack{hyperref} package is loaded, but not \Lpack{nameref}.
%    \begin{macrocode}
    \newcommand*{\sf@@memsub@label}[1]{%
      \@bsphack
      \protected@write\@auxout{}{%
        \string\newlabel{#1}%
          {{\@nameuse{p@sub\@captype}\@nameuse{@@thesub\@captype}}%
          {\thepage}%
          {\mem@currentlabelname\relax}%
          {\@currentHref}{}}}%
      \protected@write\@auxout{}{%
        \string\newlabel{sub@#1}%
          {{\@nameuse{@@thesub\@captype}}%
          {\thepage}%
          {\mem@currentlabelname\relax}%
          {\@currentHref}{}}}%
  \@esphack}
  }{%
%    \end{macrocode}
% Neither the \Lpack{hyperref} nor the \Lpack{nameref}
% package is loaded.
%    \begin{macrocode}
    \let\@memoldlabel\label
    \newcommand*{\sf@@memsub@label}[1]{%
      \@bsphack
      \@memoldlabel{#1}%
      \protected@write\@auxout{}{%
        \string\newlabel{sub@#1}%
          {{\@nameuse{@@thesub\@captype}}%
          {\thepage}}}%
  \@esphack}
  }{}%
  }
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\subcaptionref}
% This is a copy of the |\subref| macro.
%    \begin{macrocode}
\newcommand*{\subcaptionref}{%
  \@ifstar{\ssc@ref}{\sc@ref}}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\ssc@ref}
% \begin{macro}{\sc@ref}
% The implementation of the starred and unstarred forms of |\subcaptionref|.
%    \begin{macrocode}
\newcommand*{\ssc@ref}[1]{\ref{sub@#1}}
\newcommand*{\sc@ref}[1]{{\@subcaplabelfont\ref{sub@#1}}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% 
%
%
%
% \section{Epigraphs}
%
% This code comes from the \Lpack{epigraph} package~\cite{EPIGRAPH}.
%
% \begin{macro}{\beforeepigraphskip}
% \begin{macro}{\afterepigraphskip}
% \begin{macro}{\epigraphwidth}
% \begin{macro}{\epigraphrule}
%    The several length commands, which can be changed by the user with
% |\setlength|.
%    \begin{macrocode}
\newlength{\beforeepigraphskip}
  \setlength{\beforeepigraphskip}{.5\baselineskip}
\newlength{\afterepigraphskip}
  \setlength{\afterepigraphskip}{.5\baselineskip}
\newlength{\epigraphwidth}
  \setlength{\epigraphwidth}{.4\textwidth}
\newlength{\epigraphrule}
  \setlength{\epigraphrule}{.4\p@}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\epigraphsize}
%    The size of the font to be used.
%    \begin{macrocode}
\newcommand{\epigraphsize}{\small}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\epigraphflush}
% \begin{macro}{\textflush}
% \begin{macro}{\sourceflush}
%  The three commands to position epigraphs in the textblock and to position
%  the components of the epigraph.
%    \begin{macrocode}
\newcommand{\epigraphflush}{flushright}
\newcommand{\textflush}{flushleft}
\newcommand{\sourceflush}{flushright}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\epigraphfontsize}
% \begin{macro}{\epigraphposition}
% \begin{macro}{\epigraphtextposition}
% \begin{macro}{\epigraphsourceposition}
% These are declarative forms of the above. It's a bit late now, but
% the previous macros should have been internal.
% \changes{v1.2}{2002/08/10}{Added some declarations for epigraphs.}
%    \begin{macrocode}
\newcommand{\epigraphfontsize}[1]{\def\epigraphsize{#1}}
\newcommand{\epigraphposition}[1]{\long\def\epigraphflush{#1}}
\newcommand{\epigraphtextposition}[1]{\def\textflush{#1}}
\newcommand{\epigraphsourceposition}[1]{\def\sourceflush{#1}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
%
%
% \begin{macro}{\@epirule}
%  The internal command to draw a rule between text and source.
%    \begin{macrocode}
\newcommand{\@epirule}{\rule[.5ex]{\epigraphwidth}{\epigraphrule}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@epitext}
% The internal command to typeset the \meta{text}. Put it into a minipage of the
% right size and typeset per |\textflush|.
%    \begin{macrocode}
\newcommand{\@epitext}[1]{%
  \begin{minipage}{\epigraphwidth}\begin{\textflush} #1\\
%    \end{macrocode}
%  Draw a rule if it will be visible, otherwise add some extra vertical space.
%    \begin{macrocode}
    \ifdim\epigraphrule>\z@ \@epirule \else \vspace*{1ex} \fi
  \end{\textflush}\end{minipage}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@episource}
%    The internal command for typesetting the \meta{source}, which is put 
% into a minipage and typeset according to |\sourceflush|.
%    \begin{macrocode}
\newcommand{\@episource}[1]{%
  \begin{minipage}{\epigraphwidth}
    \begin{\sourceflush} #1\end{\sourceflush}
  \end{minipage}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\epigraph}
%    Having got the preliminaries out of the way, here's the user command
% for a single epigraph. This is set in a minipage to prevent breaking
% across a page. Position it according to |\epigraphflush|.
%    \begin{macrocode}
\newcommand{\epigraph}[2]{\vspace{\beforeepigraphskip}
  {\epigraphsize\begin{\epigraphflush}\begin{minipage}{\epigraphwidth}
    \@epitext{#1}\\ \@episource{#2}
    \end{minipage}\end{\epigraphflush}
    \vspace{\afterepigraphskip}}}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\qitem}
% \begin{macro}{\qitemlabel}
%    |\qitem| is the epigraph list version of |\item|. 
% Set everything inside a minipage.
%    \begin{macrocode}
\newcommand{\qitem}[2]{{%
  \raggedright\item \begin{minipage}{\epigraphwidth}
    \@epitext{#1}\\ \@episource{#2}
  \end{minipage}}}
%    \end{macrocode}
%  |\qitemlabel| is needed for a list as well. It is not going to 
% typeset anything.
%    \begin{macrocode}
\newcommand{\qitemlabel}[1]{\hfill}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{environment}{epigraphs}
%  Now for the epigraph list. This is defined in terms of a |list|
% environment. 
%    \begin{macrocode}
\newenvironment{epigraphs}{%
%    \end{macrocode}
% Do the vertical space, set the font size, position according to 
% |\epigraphflush|, and put everything into a minipage.
%    \begin{macrocode}
  \vspace{\beforeepigraphskip}\begin{\epigraphflush}
  \epigraphsize
  \begin{minipage}{\epigraphwidth}
   \list{}%
%    \end{macrocode}
%  Make the list just fit the minipage (i.e., no indents).
%    \begin{macrocode}
    {\itemindent\z@ \labelwidth\z@ \labelsep\z@
     \leftmargin\z@ \rightmargin\z@
     \let\makelabel\qitemlabel}}%
  {\endlist\end{minipage}\end{\epigraphflush}
   \vspace{\afterepigraphskip}}
%    \end{macrocode}
% \end{environment}
%
% \subsection{Epigraphs before a chapter title}
%
%
% \begin{macro}{\@epichapapp}
% \begin{macro}{\dropchapter}
% \begin{macro}{\undodrop}
%    Commands to drop and restore positions of chapter titles. Dropping is
% accomplished by inserting vertical space before the |\@chapapp| command.
%    \begin{macrocode}
\newcommand{\dropchapter}[1]{%
  \let\@epichapapp\@chapapp
  \renewcommand{\@chapapp}{\vspace*{#1}\@epichapapp}}
\newcommand{\undodrop}{\let\@chapapp\@epichapapp}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% Placing an epigraph before a chapter title uses the scheme outlined
% by Piet van Oostrum~\cite{FANCYHDR}. This is to put a zero sized
% picture into the page header. 
%
% \begin{macro}{\if@epirhs}
% \begin{macro}{\if@epicenter}
%  Two booleans for testing whether an epigraph is to be at the RH margin,
% centered, or at the LH margin. The default is RH margin.
%    \begin{macrocode}
\newif\if@epirhs     \@epirhstrue
\newif\if@epicenter  \@epicentertrue
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@epipos}
%    This routine sets the |\if@epi...| booleans according to the value of
% |\epigraphflush|. If |\epigraphflush| is neither |center| nor |flushleft|
% then it defaults to |flushright|. We have to use this to be upward 
% compatible with |\epigraphflush| being set by the user with |\renewcommand|.
%    \begin{macrocode}
\newcommand{\@epipos}{
  \long\def\@ept{flushleft}
  \ifx\epigraphflush\@ept
    \@epirhsfalse \@epicenterfalse
  \else
    \long\def\@ept{center}
    \ifx\epigraphflush\@ept
      \@epirhsfalse \@epicentertrue
    \else
      \@epirhstrue  \@epicenterfalse
    \fi
  \fi}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\epigraphhead}
% |\epigraphhead[|\meta{distance}|]{|\meta{text}|}| puts \meta{text} at
% \meta{distance} (a number, not a length) below the header at the 
% page position specified by |\epigraphflush|.
%    \begin{macrocode}
\newcommand{\epigraphhead}[2][95]{%
%    \end{macrocode}
% We have to use |\def| instead of the normal \LaTeX{} definition commands
% as we will keep on
% (re)defining things. For reasons that are not fully clear to me \LaTeX{}
% doesn't seem to like me using a |\savebox| for storing the epigraph text,
% so I'll use a command instead.
%    \begin{macrocode}
  \def\@epitemp{\begin{minipage}{\epigraphwidth}#2\end{minipage}}
%    \end{macrocode}
% Define an |epigraph| page style.
%    \begin{macrocode}
  \def\ps@epigraph{\let\@mkboth\@gobbletwo
%    \end{macrocode}
% There are three possible definitions for |\@oddhead| depending on the
% value of |\epigraphflush|. We call |\@epipos| to decide which one to do.
%    \begin{macrocode}
    \@epipos
    \if@epirhs
      \def\@oddhead{\hfil\begin{picture}(0,0)
                         \put(0,-#1){\makebox(0,0)[r]{\@epitemp}}
                         \end{picture}}
    \else
      \if@epicenter
        \def\@oddhead{\hfil\begin{picture}(0,0)
                           \put(0,-#1){\makebox(0,0)[b]{\@epitemp}}
                           \end{picture}\hfil}
      \else
        \def\@oddhead{\begin{picture}(0,0)
                           \put(0,-#1){\makebox(0,0)[l]{\@epitemp}}
                           \end{picture}\hfil}
      \fi
    \fi
    \let\@evenhead\@oddhead
    \def\@oddfoot{\reset@font\hfil\thepage\hfil}
    \let\@evenfoot\@oddfoot}
%    \end{macrocode}
% Make |epigraph| be the page style for this page.
%    \begin{macrocode}
  \thispagestyle{epigraph}}

%    \end{macrocode}
% \end{macro}
%
%    The above produces a |plain| pagestyle with the epigraph. Life is
% more complex if someone wants a fancy style with the epigraph. They
% will have to do some work, though.
%
% \begin{macro}{\the@epigraph}
% \begin{macro}{\@epidrop}
% \begin{macro}{\epigraphforheader}
% |\the@pigraph| is a macro to store the contents of an epigraph, and |\@pidrop|
% stores the \meta{distance} number.
% |\epigraphforheader|\oarg{distance}\marg{text} defines the internal macros
% appropriately.
% \changes{v1.2}{2002/08/10}{Added \cs{the@epigraph}, \cs{@epidrop}}
% \changes{v1.2}{2002/08/10}{Added \cs{epigraphforheader}}
%    \begin{macrocode}
\newcommand{\the@epigraph}{}
\newcommand{\@epidrop}{95}
\newcommand{\epigraphforheader}[2][95]{%
  \def\@epidrop{#1}\long\def\the@epigraph{#2}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\epigraphpicture}
% |\epigraphpicture| puts |\the@epigraph| into a zero-sized picture at location
% |(0,-\@epidrop)|. This can then be used as part of a fancy chapter header.
% The coding is similar to |\epigraphhead|.
% \changes{v1.2}{2002/08/10}{Added \cs{epigraphpicture}}
%    \begin{macrocode}
\newcommand{\epigraphpicture}{%
  \def\@epitemp{%
    \begin{minipage}{\epigraphwidth}\the@epigraph\end{minipage}}%
  \@epipos
  \if@epirhs
    \begin{picture}(0,0)%
      \put(0,-\@epidrop){\makebox(0,0)[r]{\@epitemp}}%
    \end{picture}%
  \else
    \if@epicenter
      \begin{picture}(0,0)%
        \put(0,-\@epidrop){\makebox(0,0)[b]{\@epitemp}}%
      \end{picture}%
    \else
      \begin{picture}(0,0)%
        \put(0,-\@epidrop){\makebox(0,0)[l]{\@epitemp}}%
      \end{picture}%
    \fi
  \fi}

%    \end{macrocode}
% \end{macro}
%
%
%
% \section{The deprecated font commands}
%
% The class does not support the old font changing commands unless the
% \Lopt{oldfontcommands} option is used.
%
% \begin{macro}{\@memoldfonterr}
% \begin{macro}{\@memoldfontwarn}
% Macros for old font class error and warning. E.g., \\
% |\@memoldfonterr{\tt}{\ttfamily}{\texttt}|
% \changes{v1.4}{2003/02/27}{Added \cs{@memoldfonterr} and \cs{@memoldfontwarn}}
% \changes{v1.618}{2005/09/03}{Fiddled with the braces in \cs{@memoldfontwarn} (mempatch v3.0)}
%    \begin{macrocode}
\newcommand*{\@memoldfonterr}[3]{%
  \@memerror{Font command \protect#1\space is not supported}{%
    Use \protect#2, or \protect#3{...}, or the oldfontcommands option}}
\newcommand*{\@memoldfontwarn}[3]{%
  \@memwarn{The \protect#1\space font command is deprecated.
    \MessageBreak Use \protect#2{...} or {\protect#3... } instead}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%  Just give a single warning when an old font command is used following the
% \Lopt{oldfontcommands} option.
% \changes{v1.4}{2003/11/22}{Just issue a single warning for an old font
%                            (from patch v1.9)}
%
% \begin{macro}{\@mem@rmwarn}
% \begin{macro}{\rm}
% The old command for roman font.
% \changes{v1.0a}{2002/01/29}{Changed definition of \cs{rm}}
% \changes{v1.4}{2003/02/27}{More efficient coding of all the old font commands}
%    \begin{macrocode}
\if@memoldfont
  \def\@mem@rmwarn{\@memoldfontwarn{\rm}{\textrm}{\rmfamily}}
  \DeclareOldFontCommand{\rm}{\@mem@rmwarn\gdef\@mem@rmwarn{}%
    \normalfont\rmfamily}{\mathrm}
\else
  \def\rm{\@memoldfonterr{\rm}{\textrm}{\rmfamily}}
\fi

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@mem@sfwarn}
% \begin{macro}{\sf}
% The old sans font command.
% \changes{v1.0a}{2002/01/29}{Changed definition of \cs{sf}}
%    \begin{macrocode}
\if@memoldfont
  \def\@mem@sfwarn{\@memoldfontwarn{\sf}{\textsf}{\sffamily}}
  \DeclareOldFontCommand{\sf}{\@mem@sfwarn\gdef\@mem@sfwarn{}%
    \normalfont\sffamily}{\mathsf}
\else
  \def\sf{\@memoldfonterr{\sf}{\textsf}{\sffamily}}
\fi

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@mem@ttwarn}
% \begin{macro}{\tt}
% The old typewriter font command.
% \changes{v1.0a}{2002/01/29}{Changed definition of \cs{tt}}
%    \begin{macrocode}
\if@memoldfont
  \def\@mem@ttwarn{\@memoldfontwarn{\tt}{\texttt}{\ttfamily}}
  \DeclareOldFontCommand{\tt}{\@mem@ttwarn\gdef\@mem@ttwarn{}%
    \normalfont\ttfamily}{\mathtt}
\else
  \def\tt{\@memoldfonterr{\tt}{\texttt}{\ttfamily}}
\fi

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@mem@bfwarn}
% \begin{macro}{\bf}
% The old bold font command.
% \changes{v1.0a}{2002/01/29}{Changed definition of \cs{bf}}
%    \begin{macrocode}
\if@memoldfont
  \def\@mem@bfwarn{\@memoldfontwarn{\bf}{\textbf}{\bfseries}}
  \DeclareOldFontCommand{\bf}{\@mem@bfwarn\gdef\@mem@bfwarn{}%
    \normalfont\bfseries}{\mathbf}
\else
  \def\bf{\@memoldfonterr{\bf}{\textbf}{\bfseries}}
\fi

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@mem@itwarn}
% \begin{macro}{\it}
% The old italic font command.
% \changes{v1.0a}{2002/01/29}{Changed definition of \cs{it}}
%    \begin{macrocode}
\if@memoldfont
  \def\@mem@itwarn{\@memoldfontwarn{\it}{\textit}{\itshape}}
  \DeclareOldFontCommand{\it}{\@mem@itwarn\gdef\@mem@itwarn{}%
    \normalfont\itshape}{\mathit}
\else
  \def\it{\@memoldfonterr{\it}{\textit}{\itshape}}
\fi

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@mem@slwarn}
% \begin{macro}{\sl}
% The old slanted font command.
% \changes{v1.0a}{2002/01/29}{Changed definition of \cs{sl}}
%    \begin{macrocode}
\if@memoldfont
  \def\@mem@slwarn{\@memoldfontwarn{\sl}{\textsl}{\slshape}}
  \DeclareOldFontCommand{\sl}{\@mem@slwarn\gdef\@mem@slwarn{}%
    \normalfont\slshape}{\@nomath\sl}
\else
  \def\sl{\@memoldfonterr{\sl}{\textsl}{\slshape}}
\fi

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@mem@scwarn}
% \begin{macro}{\sc}
% The old small caps font command.
% \changes{v1.0a}{2002/01/29}{Changed definition of \cs{sc}}
%    \begin{macrocode}
\if@memoldfont
  \def\@mem@scwarn{\@memoldfontwarn{\sc}{\textsc}{\scshape}}
  \DeclareOldFontCommand{\sc}{\@mem@scwarn\gdef\@mem@scwarn{}%
    \normalfont\scshape}{\@nomath\sc}
\else
  \def\sc{\@memoldfonterr{\sc}{\textsc}{\scshape}}
\fi

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@mem@calwarn}
% \begin{macro}{\cal}
% The old calligraphic font command.
% \changes{v1.0a}{2002/01/29}{Added definition of \cs{cal}}
%    \begin{macrocode}
\if@memoldfont
  \def\@mem@calwarn{%
    \@memwarn{The \protect\cal\space font command is deprecated.
    \MessageBreak Try to use \protect\mathcal\space instead}}
  \DeclareRobustCommand*\cal{\@mem@calwarn\gdef\@mem@calwarn{}%
    \@fontswitch\relax\mathcal}
\else
  \def\cal{%
    \@memerror{Font command \protect\cal\space is not supported}{%
    Use \protect\mathcal, or the oldfontcommands option}}
\fi

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@mem@mitwarn}
% \begin{macro}{\mit}
% The old math italic font command.
% \changes{v1.0a}{2002/01/29}{Added definition of \cs{mit}}
%    \begin{macrocode}
\if@memoldfont
  \def\@mem@mitwarn{%
    \@memwarn{The \protect\mit\space font command is deprecated.
    \MessageBreak Try to use \protect\mathnormal\space instead}}
  \DeclareRobustCommand*\mit{\@mem@mitwarn\gdef\@mem@mitwarn{}%
    \@fontswitch\relax\mathnormal}
\else
  \def\mit{%
  \@memerror{Font command \protect\mit\space is not supported}{%
             Use \protect\mathnormal, or the oldfontcommands option}}
\fi

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\em}
% \begin{macro}{\eminnershape}
% \begin{macro}{\emph}
% The old emphasis font command (the original \cs{em} is defined in the
% kernel file \file{ltfssini.dtx}, coded here as \cs{@m@m@m}).
% \changes{v0.2}{2001/06/03}{Redid coding for \cs{emph} and \cs{em}}
% \changes{v1.618}{2005/09/03}{Dont' deprecate \cs{em} and deleted \cs{@mem@emwarn} (mempatch v3.0)}
% \changes{v1.618}{2005/09/03}{Completely redid \cs{em} and \cs{emph}, matching latest fixltx2e package (mempatch v3.3)}
%    \begin{macrocode}
\DeclareRobustCommand{\em}{%
  \@nomath\em
  \ifdim\fontdimen\@ne\font > \z@
    \eminnershape
  \else
    \itshape
  \fi}
\providecommand{\eminnershape}{\upshape}
\DeclareTextFontCommand{\emph}{\em}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
%
%
% \section{Cross Referencing} \label{sec:xref}
%
% \subsection{Label referencing}
%
% \begin{macro}{\fref}
% \begin{macro}{\tref}
% \begin{macro}{\pref}
%   These are named references to labeled figures, tables and pages. 
% I find these
% useful to ensure consistency throughout the document --- I don't have
% to remember whether it is `see Figure ...' or `figure' or `Fig.' or \ldots.
%    \begin{macrocode}
\newcommand{\fref}[1]{\figurerefname~\ref{#1}}
\newcommand{\tref}[1]{\tablerefname~\ref{#1}}
\newcommand{\pref}[1]{\pagerefname~\pageref{#1}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\Pref}
% \begin{macro}{\Cref}
% \begin{macro}{\Sref}
%   These are named references to labeled Part, Chapter and Sectional
% divisions.
% \changes{v1.2}{2002/07/27}{Added \cs{Pref}, \cs{Cref} and \cs{Sref}}
%    \begin{macrocode}
\newcommand{\Pref}[1]{\partrefname\ref{#1}}
\newcommand{\Cref}[1]{\chapterrefname\ref{#1}}
\newcommand{\Sref}[1]{\sectionrefname\ref{#1}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsection{Title referencing}
%
% This is based on DA's \Lpack{titleref} package~\cite{TITLEREF}. 
% The following 
% remarks are taken from that package.
%
%
%  Titles for numbered sectioning units and for floats with captions are
%  the same as the respective TOC, LOF, or LOT entry (even when no TOC is
%  printed).  When a short title is provided (|\section[short]{long}|)
%  it will be used for the |\titleref|.  This is especially useful for
%  figure captions.  Unnumbered sections take their title reference from
%  the printed title.  Beware!  This turns the title into a moving argument
%  when it normally is not, and will cause weird errors if there are
%  `fragile' commands present.
%
%  Enumerated lists do not have titles and simply inherit the title of
%  their section.
%
%  The format of the title reference is controlled by the command
%  |\theTitleReference|, which can be redefined with |\renewcommand|.
%  It takes two parameters: the number and the title. The number is
%  just the regular |\ref| and it is WRONG in unnumbered sections.
%  Beware!  The default definition is the unadorned title.  You could
%  do, for example, \\
%  |\renewcommand{\theTitleReference}[2]{#1\ \emph{#2}}|.
%
%  The title of the current section is also available without |\label|:
%  Use the command |\currenttitle| to generate a |\titleref| to the current
%  section.  If you have redefined |\theTitleReference| to print the number
%  with the title, be aware that the two may not correspond:  In a numbered
%  list the number will show the current item but the title will show the
%  current section, but in an unnumbered section the number will show
%  some previous section number.
%
%  The big problem with the |[usetoc]| method is that |\titleref| will
%  not refer to a |\label| which was given in the title or caption itself;
%  the label must be placed after the sectioning command or the caption.
%  If you make a title-reference to a label given in a title, you will get
%  a warning message.
%
% \changes{v1.4}{2003/11/16}{Added title referencing (from patch v1.4)}
%
% \begin{macro}{\ifheadnameref}
% |\ifheadnameref|: FALSE use ToC entry, TRUE use header entry
%    \begin{macrocode}
\newif\ifheadnameref
  \headnamereffalse

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\theTitleReference}
% |\theTitleReference|\marg{num}\marg{title} is the style for typesetting
%  a referenced (number and) title.
%    \begin{macrocode}
\newcommand{\theTitleReference}[2]{#2}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\label}
% This redefinition of |\label| is intended to work with other redirections
% of |\label|, if they record extra information in a similar way.
%    \begin{macrocode}
\let\@mem@old@label\label
\def\label#1{\@bsphack\begingroup
  \protected@edef\@currentlabel{\protect\M@TitleReference
      {\@currentlabel}{\M@currentTitle}}%
  \@mem@old@label{#1}%
  \endgroup \@esphack}%

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@mem@@gettitle}
% |@mem@@gettitle|\marg{title} grabs a title text.
%    \begin{macrocode}
\def\@mem@@gettitle#1{\begingroup \let\protect\@unexpandable@protect
    \let\label\@mem@nestwarn 
    \let\index\@gobble \let\glossary\@gobble
    \let\markboth\@gobbletwo \let\@mkboth\@gobbletwo 
    \let\markright\@gobble
    \edef\@tempa{\noexpand\def\noexpand\M@currentTitle{#1}}%
  \expandafter\endgroup\@tempa}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@mem@nestwarn}
% \begin{macro}{\M@TitleReference}
%    \begin{macrocode}
\let\@mem@nestwarn\@gobble
\let\M@TitleReference\@firstoftwo

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\titleref}
% \begin{macro}{\currenttitle}
% |\titleref|\marg{key} prints the title corresponding to |\label{key}|.
% |\currenttitle| prints the latest title.
%    \begin{macrocode}
\DeclareRobustCommand{\titleref}[1]{\begingroup \let\numberline\@gobble
  \let\M@TitleReference\@mem@theTR % interrupt recursion of \ref
  \ref{#1}\endgroup}
\DeclareRobustCommand{\currenttitle}{\begingroup \let\numberline\@gobble
  \theTitleReference\@currentlabel\M@currentTitle\endgroup}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{verbatim}
% % \DeclareRobustCommand\@mem@nestwarn[1]{\@memwarn%
% %  {Label \string"#1\string" was put in a title,\MessageBreak
% %   so the \noexpand\titleref is incorrect}}
% \end{verbatim}
%
% \begin{macro}{\M@currentTitle}
%    \begin{macrocode}
\let\M@currentTitle\@empty

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@mem@theTR}
% proper definition:
%    \begin{macrocode}
\def\@mem@theTR{\let\M@TitleReference\@firstoftwo \theTitleReference}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\namerefon}
% \begin{macro}{\namerefoff}
% Named references has turned some arguments (e.g., |\legend{text}|) into
%  moving ones (Sven.Hartrumpf@FernUni-Hagen.de reported a problem but 
%  not a cause, March 2003). Not everyone needs named references.
%    \begin{macrocode}
\newcommand*{\namerefon}{\let\M@gettitle\@mem@@gettitle}
\newcommand*{\namerefoff}{\let\M@gettitle\@gobble}
  \namerefon
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@caption}
% add hooks to |\@caption|
%    \begin{macrocode}
\let\M@old@caption\@caption 
\long\def\@caption#1[#2]#3{\M@gettitle{#2}\M@old@caption{#1}[{#2}]{#3}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\legend}
% \begin{macro}{\@legend}
% add hooks to |\legend|
%    \begin{macrocode}
\let\M@oldlegend\legend
\long\def\legend#1{\M@gettitle{#1}\M@oldlegend{#1}}
\let\M@old@legend\@legend
\long\def\@legend#1[#2]#3{\M@gettitle{#2}\M@old@legend{#1}[{#2}]{#3}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@vsptitle}
% \begin{macro}{\@vssptitle}
% add hooks to |\poemtitle|
%    \begin{macrocode}
\let\M@old@vsptitle\@vsptitle
\long\def\@vsptitle[#1]#2{\M@gettitle{#1}\M@old@vsptitle[{#1}]{#2}}
\let\M@old@vssptitle\@vssptitle
\long\def\@vssptitle#1{\M@gettitle{#1}\M@old@vssptitle{#1}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@spart}
% \begin{macro}{\@part}
% add hooks to |\part(*)|
%    \begin{macrocode}
%%%\let\M@old@spart\@spart
%%%\def\@spart#1{\M@gettitle{#1}\M@old@spart{#1}}
%%%\let\M@old@part\@part
%%%\def\@part[#1]{\M@gettitle{#1}\M@old@part[{#1}]}

%    \end{macrocode}
% \end{macro}
% \end{macro}

%
% \section{Table of Contents, etc.}
%
%     A |\section| command writes a
%     |\contentsline{section}{|\meta{title}|}{|\meta{page}|}| command
%     on the \file{.toc} file, where \meta{title} contains the
%     contents of the entry and \meta{page} is the page number. If
%     sections are being numbered, then \meta{title} will be of the
%     form |\numberline{|\meta{num}|}{|\meta{heading}|}| where
%     \meta{num} is the number produced by |\thesection|.  Other
%     sectioning commands work similarly.
%
%     A |\caption| command in a `figure' environment writes
%
%     |\contentsline{figure}{\numberline{|\meta{num}|}{|%
%                    \meta{caption}|}}{|\meta{page}|}|
%
%     on the \file{.lof} file, where \meta{num} is the number produced
%     by |\thefigure| and \meta{caption} is the figure caption.  It
%     works similarly for a `table' environment.
%
%    The command |\contentsline{|\meta{name}|}| expands to
%    |\l@|\meta{name}.  So, to specify the table of contents, we must
%    define |\l@chapter|, |\l@section|, |\l@subsection|, ... ; to
%    specify the list of figures, we must define |\l@figure|; and so
%    on.  Most of these can be defined with the |\@dottedtocline|
%    command, which works as follows.
%
%    |\@dottedtocline{|\meta{level}|}{|\meta{indent}|}{|^^A
%                      \meta{numwidth}|}{|^^A
%                      \meta{title}|}{|\meta{page}|}|
%
%    \begin{description}
%    \item[\meta{level}] An entry is produced only if\meta{ level}
%        $<=$ value of the \Lcount{tocdepth} counter.  Note,
%        |\chapter| is level 0, |\section| is level 1, etc.
%    \item[\meta{indent}] The indentation from the outer left margin
%        of the start   of the contents line.
%    \item[\meta{numwidth}] The width of a box in which the section
%        number is to go, if \meta{title} includes a |\numberline|
%        command.
%    \end{description}
%
% \begin{macro}{\@pnumwidth}
% \begin{macro}{\@tocrmarg}
% \begin{macro}{\@dotsep}
%    This command uses the following three parameters, which are set
%    with a |\newcommand| (so em's can be used to make them depend upon
%    the font).
%    \begin{description}
%    \item[\texttt{\bslash @pnumwidth}] The width of a box in which the
%        page number is put.
%    \item[\texttt{\bslash @tocrmarg}] The right margin for multiple
%        line entries.  One wants |\@tocrmarg| $\ge$ |\@pnumwidth|
%    \item[\texttt{\bslash @dotsep}] Separation between dots, in mu
%        units. Should be defined as a number like 2 or 1.7
%    \end{description}
%
%    \begin{macrocode}
\newcommand{\@pnumwidth}{1.55em}
\newcommand{\@tocrmarg} {2.55em}
\newcommand{\@dotsep}{4.5}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\tocentryskip}
% \begin{macro}{\tocbaseline}
% \begin{macro}{\tocskip}
%    We define two lengths and a utility command.
%    \begin{macrocode}
\newlength{\tocentryskip} \setlength{\tocentryskip}{1em}
\newlength{\tocbaseline} \setlength{\tocbaseline}{20pt}
\newcommand{\tocskip}[1]{%
    \addtocontents{toc}{\protect\vspace{#1}}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsection{New List of \ldots}
%
% It is apparent that users want to do at least two things that are not
% readily provided by the standard classes: (a) change the appearance
% of the Table of Contents, etc., headings, and (b) create new List of\ldots.
% This class provides a means of creating new Lists whose headings are
% parameterized, thereby killing two birds with one stone.
%
% In this class the tables of
%    contents, figures etc. are always set in single-column style.
% The titles are added to the ToC, unless the starred versions of the 
% commands are used.
%
% \begin{macro}{\ensureonecol}
% \begin{macro}{\restorefromonecol}
%  The ToC, LoF, etc are always set in a single column. These two
% macros cooperate to swith from two-columns to one column, and back again.
%    \begin{macrocode}
\newcommand*{\ensureonecol}{%
  \if@twocolumn
    \@restonecoltrue\onecolumn
  \else
    \@restonecolfalse
  \fi}
\newcommand*{\restorefromonecol}{\if@restonecol\twocolumn\fi}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \changes{v1.0a}{2002/01/24}{Deleted \cs{ensuretwocol} and \cs{restorefromtwocol}
%                             as they were unused}
% 
% \begin{macro}{\cftparskip}
% The |\parskip| local to the ToC, etc, is set to the length |\cftparskip|.
%    \begin{macrocode}
\newlength{\cftparskip}
\setlength{\cftparskip}{0pt}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\newlistof}
% |\newlistof{|\meta{listofcmmd}|}{|\meta{ext}|}{|\meta{listofname}|}|
% creates the command |\listofcmmd| to typeset a new List of, where the
% external file has the extension |.ext| and the heading title is
% \meta{listofname}. The code for this is a heavily modified part
% of the \Lpack{tocloft} package.
%
%    \begin{macrocode}
\newcommand{\newlistof}[3]{%
%    \end{macrocode}
%
% In the following, |X| stands for the value of \meta{listofcmmd} and
% |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}
      \@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 |\Zheadstart| and |\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 |\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 |\printZtitle{|\meta{title}|}|
% which defaults to |\printchaptertitle|.
%    \begin{macrocode}
  \@namedef{print#2title}##1{\printchaptertitle{##1}}
%    \end{macrocode}
% \end{macro}
%
% This is the end of the definition of |\newlistof|.
%    \begin{macrocode}
} % end \newlistof

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@starttoc}
% A list of macro calls |\@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}
%
%
% \subsection{Table of Contents}
%
% \begin{macro}{\tableofcontents}
% \begin{macro}{\tableofcontents*}
%    These macros request that \LaTeX{} produces a table of
%    contents. The ToC heading is added to the ToC unless the starred
% version is used.
%
%    \begin{macrocode}
\newlistof{tableofcontents}{toc}{\contentsname}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%    The List of Figures and List of Tables are defined later.
%
%
%
% \subsection{List entries}
%
%    Each command in the body of the text that makes an entry in the ToC,
% or LoF or LoT needs an addtional macro to format the entry, as described
% above. Users often want to change the formatting of the entries but
% it is not immediately obvious how to do that.
%
%    I have borrowed and modified more of the code from the \Lpack{tocloft}
% package to enable easy creation and modification the formatting of the
% entries.
%
%
% \begin{macro}{\setpnumwidth}
% \begin{macro}{\setrmarg}
%  User commands for setting |\@pnumwidth| and |\@tocrmarg|.
%    \begin{macrocode}
\newcommand{\setpnumwidth}[1]{\renewcommand{\@pnumwidth}{#1}}
\newcommand{\setrmarg}[1]{\renewcommand{\@tocrmarg}{#1}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\cftdot}
% \begin{macro}{\cftdotfill}
% In the default ToC, a dotted line can be used to provide a leader between
% a title and the page number. The definition of this leader is buried
% in the |\@dottedtocline| command. The |\cftdotfill{|\meta{sep}|}|
% command provides a parameterised version of the leader code, where
% \meta{sep} is the separation between the dots in mu units.
% The symbol used for the `dots' in the leader is given by the value
% of |\cftdot|. 
%    \begin{macrocode}
\providecommand{\cftdot}{.}
\providecommand{\cftdotfill}[1]{%
  \leaders\hbox{$\m@th\mkern #1 mu\hbox{\cftdot}\mkern #1 mu$}\hfill}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\cftdotsep}
% \begin{macro}{\cftnodots}
% |\cftdotsep| holds the default dot separation. 
% If the kerns in |\cftdotfill| are large enough, then no dots will
% be printed. |\cftnodots| should be `large enough'.
% \changes{v1.2}{2002/07/07}{Changed \cs{cftnodots} from 10,000 to 2,000 for 
%                17pt option and pdflatex}
%    \begin{macrocode}
\providecommand{\cftdotsep}{4.5}
\newcommand{\cftnodots}{2000}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%     Now for the trickier bits regarding the typesetting of the ToC
% entries.
%
%     A \file{.toc} (also \file{.lof} and \file{.lot}) file consists
% of a list of 
% |\contentsline{|\meta{kind}|}{|\meta{title}|}{|\meta{page}|}|
% commands, where \meta{kind} is the kind of heading (e.g., |part| or
% |section| or |figure|), \meta{title} is the title text (including the number),
% and \meta{page} is the page number. The entries are inserted into the
% file by calling the 
% |\addcontentsline{|\meta{file}|}{|\meta{kind}|}{|\meta{title}|}|
% command, where \meta{file} is the file extension (e.g., |toc|, |lot|)
% and the other arguments are the same as for the |\contentsline|
% command. (Arbitrary stuff may also be put into the file via the
% |\addtocontents{|\meta{file}|}{|\meta{text}|}| command).
% The typesetting of the |\contentsline| entries is performed by 
% commands of the form |\l@kind|. The sectioning and captioning commands
% call |\addcontentsline| to insert their titles into the \file{.toc}
% etc., files.
%
%     For the purposes at hand it is generally impossible to treat 
% the typesetting
% of a title and its number separately, as both are bundled into the
% \meta{title} argument within |\contentsline|. They could be handled
% separately if the |\contentsline| command was suitably modified. If
% this was done, then the |\addtocontentsline| command would also need
% to be changed which would then require the sectioning and captioning
% commands to be modified as well. This is certainly possible, but would
% cause problems if any other package also modified the sectioning or
% captioning commands, and there are several packages which do this.
%
%  I provide modified versions of the |\l@kind| commands. 
% Essentially, my new definitions
% consist of inlined versions of the code for |\@dottedtocline|.
%
%
% \begin{macro}{\cftparfillskip}
% The |\l@kind| commands modify (locally) the value of |\parfillskip|.
% |\cftparfillskip| is a copy of the default \theTeXbook{}
% |\parfillskip| definition.
%    \begin{macrocode}
\newcommand{\cftparfillskip}{\parfillskip=0pt plus1fil}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\numberline}
% The purpose of the |\numberline{|\meta{secnum}|}| command is to typeset
% \meta{secnum} left justified in a box of width |\@tempdima|. I redefine
% it to add three additional parameters, namely |\@cftbsnum|, 
% |\@cftasnum| and |\@cftasnumb| 
% (see \file{ltsect.dtx} for the original
% definition).
%    \begin{macrocode}
\renewcommand{\numberline}[1]{% 
  \hb@xt@\@tempdima{\@cftbsnum #1\@cftasnum\hfil}\@cftasnumb}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@cftbsnum}
% \begin{macro}{\@cftasnum}
% \begin{macro}{\@cftasnumb}
%    \begin{macrocode}
\newcommand{\@cftbsnum}{}
\newcommand{\@cftasnum}{}
\newcommand{\@cftasnumb}{}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% 
%
% \begin{macro}{\newlistentry}
% |\newlistentry[|\meta{within}|]{|\meta{counter}|}{|\meta{ext}|}{|\meta{level-1}|}| 
% creates a set of commands for typesetting a new kind of entry in a List of.
% \meta{counter} is the name of the counter for the entry and must be 
% the same as the name
% of the entry (e.g., |subsection|). The optional \meta{within} is the
% name of a counter within which |counter| is defined. The file extension
% for the List of is \meta{ext} and \meta{level-1} is one less than the 
% level of the entry in the List of.
%    \begin{macrocode}
\newcommand{\newlistentry}[4][\@empty]{%
%    \end{macrocode}
%
%    In the following, |X| is used as the value of \meta{counter} and
% |Z| as the value of \meta{ext}.
%
% \begin{macro}{\c@X}
% \begin{macro}{\theX}
% Check if \meta{within} and \meta{counter} have been defined. It is
% an error if \meta{within} has not been defined. \meta{counter}
% will be created if it has not been previously defined.
% Set the default counter values.
%    \begin{macrocode}
  \@ifundefined{c@#2}{%    check & set the counter
    \ifx \@empty#1\relax
      \newcounter{#2}
    \else
      \newcounter{#2}[#1]%
      \expandafter\edef\csname the#2\endcsname{%
  \expandafter\noexpand\csname the#1\endcsname.\noexpand\arabic{#2}}
    \fi}{}
  \setcounter{#2}{0}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% That finishes off the error checking. No matter what the result, the
% rest of the new commands are defined.
%
% \begin{macro}{\l@X}
% |\l@X{|\meta{title}|}{|\meta{page}|}| typesets the entry.
%    \begin{macrocode}
  \@namedef{l@#2}##1##2{%
%    \end{macrocode}
% Only typeset if the |\Zdepth| is greater than \meta{level-1}.
%    \begin{macrocode}
    \ifnum \@nameuse{c@#3depth} > #4\relax
%    \end{macrocode}
% Add some vertical space.
%    \begin{macrocode}
      \vskip \@nameuse{cftbefore#2skip}
%    \end{macrocode}
% Start a group to keep paragraphing changes local. Set the |\leftskip|
% to the entry's indentation.
%    \begin{macrocode}
      {\leftskip \@nameuse{cft#2indent}\relax
%    \end{macrocode}
% Set the |\rightskip| to |\@tocrmarg| to leave room for the page number.
%    \begin{macrocode}
       \rightskip \@tocrmarg
%    \end{macrocode}
% Ensure that the last line of an entry will be filled. Setting |\parfillskip|
% to a negative value prevents `overfull box' messages.
%    \begin{macrocode}
       \parfillskip -\rightskip
%    \end{macrocode}
% Set the paragraph's indentation to the entry's indentation.
%    \begin{macrocode}
       \parindent \@nameuse{cft#2indent}\relax\@afterindenttrue
%    \end{macrocode}
% Try and prevent breaks between lines in a multiline entry.
%    \begin{macrocode}
       \interlinepenalty\@M
%    \end{macrocode}
% Make sure we have left vertical mode.
%    \begin{macrocode}
       \leavevmode
%    \end{macrocode}
% Our version of |\numberline| expects that the width of the number box
% is in |\@tempdima|, and that the three macros |\@cftbsnum|, |\@cftaqsnum|,
% and |\@cftasnumb| are defined. We set all these to the values for this entry.
%    \begin{macrocode}
       \@tempdima \@nameuse{cft#2numwidth}\relax
 \expandafter\let\expandafter\@cftbsnum\csname cft#2presnum\endcsname
 \expandafter\let\expandafter\@cftasnum\csname cft#2aftersnum\endcsname
 \expandafter\let\expandafter\@cftasnumb\csname cft#2aftersnumb\endcsname
%    \end{macrocode}
% Arrange that the (entry number and) first line of the title is set at
% the current indent, and that any subsequent lines will be further
% indented.
%    \begin{macrocode}
       \advance\leftskip\@tempdima \null\nobreak\hskip -\leftskip
%    \end{macrocode}
% Print the (number and) title, prohibiting any breqaking.
%    \begin{macrocode}
       {\@nameuse{cft#2font}##1}\nobreak
%    \end{macrocode}
% Print the leader and the page number, and then close the group.
%    \begin{macrocode}
       \@nameuse{cft#2fillnum}{##2}}
    \fi
  }  % end of \l@#2

%    \end{macrocode}
% \end{macro}
%
% Now define all the layout commands used by |\l@X|. The default
% values of these print the entry in a normal font with a dotted
% line between the title and the page number.
% \begin{macro}{\cftbeforeXskip}
% The skip before the title.
%    \begin{macrocode}
  \expandafter\newlength\csname cftbefore#2skip\endcsname
    \setlength{\@nameuse{cftbefore#2skip}}{\z@ \@plus .2\p@}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\cftXindent}
% \begin{macro}{\cftXnumwidth}
% The indent and width for the number.
%    \begin{macrocode}
  \expandafter\newlength\csname cft#2indent\endcsname
  \expandafter\newlength\csname cft#2numwidth\endcsname
%    \end{macrocode}
% Set the default values for the indent and numwidth depending on
% the entry's level. A level of 1 corresponds to a figure entry
% (no indent, and space for a number like N.N).
%    \begin{macrocode}
  \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
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\cftXfont}
% \begin{macro}{\cftXpresnum}
% \begin{macro}{\cftXaftersnum}
% \begin{macro}{\cftXaftersnumb}
% \begin{macro}{\cftXdotsep}
% \begin{macro}{\cftXleader}
% \begin{macro}{\cftXpagefont}
% \begin{macro}{\cftXafterpnum}
% And the remaining commands; the only ones that are not null are for the
% dotsep, the font and the leader
%    \begin{macrocode}
  \@namedef{cft#2font}{\normalfont}
  \@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}{}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\toclevel@X}
% The hyperref package needs a command |\toclevel@X|, holding
% the \meta{level-1} value.
%    \begin{macrocode}
  \@namedef{toclevel@#2}{#4}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\cftXfillnum}
% Typeset the leader and page number.
%    \begin{macrocode}
  \@namedef{cft#2fillnum}##1{%
    {\@nameuse{cft#2leader}}\nobreak
    \hb@xt@\@pnumwidth{%
      \hfil\@nameuse{cft#2pagefont}##1}\@nameuse{cft#2afterpnum}\par}
%    \end{macrocode}
% \end{macro}
% This ends the definition of |\newlistentry|.
%    \begin{macrocode}
} % end \newlistentry

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\cftsetindents}
% |\cftsetindents{|\meta{entry}|}{|\meta{indent}|}{|\meta{numwidth}|}| sets
% the \textit{indent} and \textit{numwidth} for entry \meta{entry}. 
%    \begin{macrocode}
\newcommand{\cftsetindents}[3]{%
  \setlength{\@nameuse{cft#1indent}}{#2}
  \setlength{\@nameuse{cft#1numwidth}}{#3}
}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\cftpartname}
% If you want a part entry in the ToC like: \\
% PART I Title ... \\
% then \verb?\renewcommand*{\cftpartname}{PART~}? \\
% \changes{v1.618}{2005/09/03}{Added \cs{cftpartname}, courtesy Danie Els (mempatch v3.12)}
%    \begin{macrocode}
\newcommand*{\cftpartname}{}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\l@part}
% Can't use |\newlistentry| for this because of the initial penalty
% and the final |\nobreak| code.
%    \begin{macrocode}
\newcommand*{\l@part}[2]{%
  \ifnum \c@tocdepth >-2\relax
    \addpenalty{-\@highpenalty}%
    \addvspace{\cftbeforepartskip}%
    \begingroup
      {\leftskip \cftpartindent\relax
       \rightskip \@tocrmarg
       \parfillskip -\rightskip
       \parindent \cftpartindent\relax\@afterindenttrue
       \interlinepenalty\@M
       \leavevmode    
       \settowidth{\@tempdima}{\cftpartfont\cftpartname}%
       \addtolength{\@tempdima}{\cftpartnumwidth}%
       \let\@cftbsnum \cftpartpresnum
       \let\@cftasnum \cftpartaftersnum
       \let\@cftasnumb \cftpartaftersnumb
       \advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
       {\cftpartfont #1}%
       \cftpartfillnum{#2}}
      \nobreak
        \global\@nobreaktrue
        \everypar{\global\@nobreakfalse\everypar{}}%
    \endgroup
  \fi}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\partnumberline}
% |\partnumberline| is a special version of |\numberline| output by |\part|.
% It's default definition uis identical to |\numberline|.
% \changes{v1.3}{2002/11/14}{Added \cs{partnumberline}}
%    \begin{macrocode}
\newcommand{\partnumberline}[1]{%
  \hb@xt@\@tempdima{%
    \cftpartname\@cftbsnum #1\@cftasnum\hfil}\@cftasnumb}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\cftbeforepartskip}
% \begin{macro}{\cftpartnumwidth}
% \begin{macro}{\cftpartfont}
% \begin{macro}{\cftpartpresnum}
% \begin{macro}{\cftpartaftersnum}
% \begin{macro}{\cftpartaftersnumb}
% \begin{macro}{\cftpartleader}
% \begin{macro}{\cftpartdotsep}
% \begin{macro}{\cftpartpagefont}
% \begin{macro}{\cftpartafterpnum}
% \begin{macro}{\cftpartindent}
% \begin{macro}{\cftpartfillnum}
%  These are the user commands to control the typesetting of Part entries.
%  They are initialised to give the standard appearance.
% \changes{v1.3}{2002/10/10}{Changed \cs{cftpartnumwidth} to cater for
%                            use of \cs{numberline} in the ToC}
%    \begin{macrocode}
  \newlength{\cftbeforepartskip}
    \setlength{\cftbeforepartskip}{2.25em \@plus\p@}
  \newlength{\cftpartindent}
    \setlength{\cftpartindent}{0em}
  \newlength{\cftpartnumwidth}
    \setlength{\cftpartnumwidth}{1.5em}
  \newcommand{\cftpartfont}{\large\bfseries}
  \newcommand{\cftpartpresnum}{}
  \newcommand{\cftpartaftersnum}{}
  \newcommand{\cftpartaftersnumb}{}
  \newcommand{\cftpartleader}{%
              \large\bfseries\cftdotfill{\cftpartdotsep}}
  \newcommand{\cftpartdotsep}{\cftnodots}
  \newcommand{\cftpartpagefont}{\large\bfseries}
  \newcommand{\cftpartafterpnum}{}
  \newcommand{\cftpartfillnum}[1]{%
    {\cftpartleader}%
    {\hb@xt@\@pnumwidth{\hss {\cftpartpagefont #1}}}%
     \cftpartafterpnum\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}{\cftchaptername}
% If you want a chapter entry in the ToC like: \\
% Chapter 1. A title ... \\
% then \verb?\renewcommand*{\cftchaptername}{\chaptername~}? \\
% \verb?\renewcommand*{\cftchapteraftersnum}{.}? \\
% 
% \changes{v1.618}{2005/09/03}{Added \cs{cftchaptername}, courtesy Danie Els (mempatch v3.12)}
%    \begin{macrocode}
\newcommand*{\cftchaptername}{}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\l@chapapp}
% Generic chapter/appendix ToC entry typesetting.
% \changes{v1.618}{2005/09/03}{Added \cs{l@chapapp}, courtesy Danie Els (mempatch v3.12)}
%    \begin{macrocode}
\newcommand*{\l@chapapp}[3]{%
  \ifnum \c@tocdepth >\m@ne
    \cftchapterbreak
    \vskip \cftbeforechapterskip
    {\leftskip \cftchapterindent\relax
     \rightskip \@tocrmarg
     \parfillskip -\rightskip
     \parindent \cftchapterindent\relax
     \@afterindenttrue
     \interlinepenalty\@M
     \leavevmode
     \let\@cftbsnum \cftchapterpresnum
     \let\@cftasnum \cftchapteraftersnum
     \let\@cftasnumb \cftchapteraftersnumb
     \def\@chapapp@head{#3}%
     \settowidth{\@tempdima}{\cftchapterfont\@chapapp@head}%
     \addtolength{\@tempdima}{\cftchapternumwidth}%
     \advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
     {\cftchapterfont #1}\nobreak
     \cftchapterfillnum{#2}}
  \fi}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\l@chapter}
%  |\l@chapter{|\meta{title}|}{|\meta{page}|}| typesets the ToC entry for
% a |chapter| heading. It is a parameterised copy of the default |\l@chapter|
% (see \file{classes.dtx} for the original definition). Can't use
% |\newlistentry| for this because of the intial penalty.
% \changes{v1.3}{2002/11/14}{Added \cs{cftchapterbreak} to \cs{l@chapter}}
% \changes{v1.618}{2005/09/03}{Defined \cs{l@chapter} in terms of \cs{l@chapapp} (mempatch v3.12)}
%    \begin{macrocode}
\newcommand*{\l@chapter}[2]{%
  \l@chapapp{#1}{#2}{\cftchaptername}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\cftappendixname}
% If you want a appendixed chapter entry in the ToC like: \\
% Appendix A A title ... \\
% then \verb?\renewcommand*{\cftappendixname}{\appendixname~}? \\
% 
% \changes{v1.618}{2005/09/03}{Added \cs{cftappendixname}, courtesy Danie Els (mempatch v3.12)}
%    \begin{macrocode}
\newcommand*{\cftappendixname}{}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\l@appendix}
% \begin{macro}{\toclevel@appendix}
% Similar to \cs{l@chapter} but for an appendix.
% \changes{v1.618}{2005/09/03}{Added \cs{l@apendix} (mempatch v3.12)}
%    \begin{macrocode}
\newcommand*{\l@appendix}[2]{%
  \l@chapapp{#1}{#2}{\cftappendixname}}
\newcommand{\toclevel@appendix}{0}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\chapternumberline}
% |\chapternumberline| is a special version of |\numberline| output by 
% |\chapter|.
% It's default definition is identical to |\numberline|.
% \changes{v1.3}{2002/11/14}{Added \cs{chapternumberline}}
% \changes{v1.618}{2005/09/03}{Added \cs{@chapapp@head} to \cs{chapternumberline} (mempatch v3.12)}
%    \begin{macrocode}
\newcommand{\chapternumberline}[1]{%
  \hb@xt@\@tempdima{\@chapapp@head\@cftbsnum #1\@cftasnum\hfil}%
  \@cftasnumb}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\cftbeforechapterskip}
% \begin{macro}{\cftchapterindent}
% \begin{macro}{\cftchapternumwidth}
% \begin{macro}{\cftchapterfont}
% \begin{macro}{\cftchapterpresnum}
% \begin{macro}{\cftchapteraftersnum}
% \begin{macro}{\cftchapteraftersnumb}
% \begin{macro}{\cftchapterleader}
% \begin{macro}{\cftchapterdotsep}
% \begin{macro}{\cftchapterpagefont}
% \begin{macro}{\cftchapterafterpnum}
% \begin{macro}{\cftchapterfillnum}
%  These are the user commands to control the typesetting of Chapter entries.
%  They are initialised to give the standard appearance.
%    \begin{macrocode}
  \newlength{\cftbeforechapterskip}
    \setlength{\cftbeforechapterskip}{1.0em \@plus\p@}
  \newlength{\cftchapterindent}
    \setlength{\cftchapterindent}{0em}
  \newlength{\cftchapternumwidth}
    \setlength{\cftchapternumwidth}{1.5em}
  \newcommand{\cftchapterfont}{\bfseries}
  \newcommand{\cftchapterpresnum}{}
  \newcommand{\cftchapteraftersnum}{}
  \newcommand{\cftchapteraftersnumb}{}
  \newcommand{\cftchapterleader}{%
              \bfseries\cftdotfill{\cftchapterdotsep}}
  \newcommand{\cftchapterdotsep}{\cftnodots}
  \newcommand{\cftchapterpagefont}{\bfseries}
  \newcommand{\cftchapterafterpnum}{}
  \newcommand{\cftchapterfillnum}[1]{
    {\cftchapterleader}\nobreak
    \hb@xt@\@pnumwidth{\hfil\cftchapterpagefont #1}%
    \cftchapterafterpnum\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}{\cftchapterbreak}
% Another parameter for |\l@chapter|.
% \changes{v1.3}{2002/11/14}{Added \cs{cftchapterbreak}}
%    \begin{macrocode}
  \newcommand{\cftchapterbreak}{\addpenalty{-\@highpenalty}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\l@section}
% \begin{macro}{\l@subsection}
% \begin{macro}{\l@subsubsection}
% \begin{macro}{\l@paragraph}
% \begin{macro}{\l@subparagraph}
%  |\l@section{|\meta{title}|}{|\meta{page}|}| typesets the ToC entry for
% a |section| heading. Similarly |\l@subsection| and |\l@subsubsection|
% for |\subsection| and |\subsubsection| entries, and so on.
% \changes{v1.4}{2003/11/22}{Used \cs{newlistentry} for pararaph and 
%                             subparagraph (from patch v1.7)}
% \changes{v1.618}{2005/09/02}{Corrected (sub)paragraph ToC indents (mempatch v2.3)}
%    \begin{macrocode}
\newlistentry[chapter]{section}{toc}{0}
  \cftsetindents{section}{1.5em}{2.3em}
\newlistentry[section]{subsection}{toc}{1}
  \cftsetindents{subsection}{3.8em}{3.2em}
\newlistentry[subsection]{subsubsection}{toc}{2}
  \cftsetindents{subsubsection}{7.0em}{4.1em}
\newlistentry[subsubsection]{paragraph}{toc}{3}
  \cftsetindents{paragraph}{10.0em}{5.0em}
\newlistentry[paragraph]{subparagraph}{toc}{4}
  \cftsetindents{subparagraph}{12.0em}{6.0em}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
%   The typesetting for Figures and Tables is defined later.
%
%
%
% \subsection{Support for the \Lpack{subfigure} package}
%
%  The code for supporting the \Lpack{subfigure} package is, in all 
% essentials, the same as that for the figure and table captions; only the
% names are changed. However, the code need only be executed if the
% \Lpack{subfigure} package is actually loaded. 
%
% \begin{macro}{\@cftl@subfigtab}
%    This command redefines the |\l@subfigure| and |\l@subtable| commands. 
%    \begin{macrocode}
\newcommand{\@cftl@subfigtab}{
%    \end{macrocode}
% \begin{macro}{\l@subfigure}
% \begin{macro}{\l@subtable}
%  |\l@subfigure{|\meta{title}|}{|\meta{page}|}| typesets the LoF entry for
% a |subfigure| caption heading, and |\l@subtable| does the same for subtables.
%    \begin{macrocode}
\newlistentry[figure]{subfigure}{lof}{1}
  \cftsetindents{subfigure}{2.3em}{2.5em}
\newlistentry[table]{subtable}{lot}{1}
  \cftsetindents{subtable}{2.3em}{2.5em}
}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
%    Call the \Lpack{subfigure} package setup code only if the 
% \Lopt{subfigure} package has been used.
%    \begin{macrocode}
\AtBeginDocument{\@ifpackageloaded{subfigure}{\@cftl@subfigtab}{}}

%    \end{macrocode}
%
%
% \subsection{Switching page numbering}
%
%    It can, at times, be useful to be able to have ToC entries that have
% no printed page numbers.
%
% \begin{macro}{\cftpagenumbersoff}
% The user level command for switching off page numbers is 
% |\cftpagenumbersoff{|\meta{entry}|}| where \meta{entry} is the 
% name of the entry. 
% The macro redefines the |\cftXnumfill| command so that there is no leader
% and the page number is ignored.
%    \begin{macrocode}
\DeclareRobustCommand{\cftpagenumbersoff}[1]{%
  \@namedef{cft#1fillnum}##1{%
    \cftparfillskip\@nameuse{cft#1afterpnum}\par}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\cftpagenumberson}
% |\cftpagenumberson{|\meta{entry}|}| is the user level command for 
% reversing the corresponding |\cftpagenumbersoff|.
% The macro defines the |\cftXnumfill| command to correspond to 
% the default definition.
%    \begin{macrocode}
\DeclareRobustCommand{\cftpagenumberson}[1]{%
  \@namedef{cft#1fillnum}##1{%
    {\@nameuse{cft#1leader}}\nobreak
    \hb@xt@\@pnumwidth{%
      \hfil\@nameuse{cft#1pagefont}##1}\@nameuse{cft#1afterpnum}\par}}

%    \end{macrocode}
% \end{macro}
%
%
%
%
%
%
%
%
% \subsection{Chapter precis}
%
%
% \begin{macro}{\chapterprecis}
% The command |\chapterprecis{|\meta{text}|}| typesets
% \meta{text} at the point where it is called, and also adds \meta{text}
% to the \file{.toc} file. It is expects to be called immediately after
% a |\chapter| command.
%    \begin{macrocode}
\newcommand{\chapterprecis}[1]{%
  \chapterprecishere{#1}
  \chapterprecistoc{#1}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\chapterprecishere}
% |\chapterprecishere{|\meta{text}|}| typesets \meta{text}. It expects
% to be called immediately after a |\chapter| command. 
% \changes{v1.3}{2002/11/14}{Parameterised \cs{chapterprecishere}}
%    \begin{macrocode}
\newcommand{\chapterprecishere}[1]{%
  \prechapterprecis #1\postchapterprecis}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\prechapterprecis}
% \begin{macro}{\postchapterprecis}
% These put code before and after |\chapterprecishere| text. By
% default |\prechapterprecis| adds some negative space and starts a
% quote environment using italics. |\postchapterprecis| ends
% the quote environment.
% \changes{v1.3}{2002/11/14}{Added \cs{prechapterprecis} and 
%                            \cs{postchapterprecis}}
%    \begin{macrocode}
\newcommand{\prechapterprecis}{%
  \vspace*{-2\baselineskip}%
  \begin{quote}\normalfont\itshape}
\newcommand{\postchapterprecis}{\end{quote}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\precistocfont}
% Font for typesetting chapter precis in the ToC.
% \changes{v1.3}{2002/11/14}{Added \cs{precistocfont}}
%    \begin{macrocode}
\newcommand{\precistocfont}{\normalfont\itshape}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\chapterprecistoc}
% \begin{macro}{\precistoctext}
% |\chapterprecistoc{|\meta{text}|}| effectively adds \meta{text} to 
% the \file{.toc}
% file. The \meta{text} will be typeset within the same margins as the
% the title text of a |\chapter| heading, using the |\precistocfont| font.
% \changes{v1.3}{2002/11/14}{Added \cs{precistoctext}}
%    \begin{macrocode}
\newcommand{\chapterprecistoc}[1]{%
  \addtocontents{toc}{\precistoctext{#1}}}
\DeclareRobustCommand{\precistoctext}[1]{%
%    \end{macrocode}
% Start a group to localize changes to the paragraphing. Set the 
% left margin to the chapter indent plus the chapter number width.
%    \begin{macrocode}
  {\leftskip \cftchapterindent\relax
   \advance\leftskip \cftchapternumwidth\relax
%    \end{macrocode}
% Set the right hand margin to |\@tocrmarg|.
%    \begin{macrocode}
   \rightskip \@tocrmarg\relax
%    \end{macrocode}
% Typeset \meta{text} using an italic font, then ensure that the paragraph
% is finished (to use the local skips). Finally close the group and we 
% are done.
%    \begin{macrocode}
   \precistocfont #1\par}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Adding things to the ToC}
%
% \begin{macro}{\cftlocalchange}
% |\cftmakelocalchange{|\meta{file}|}{|\meta{pnumwidth}|}{|\meta{tocrmarg}|}|
% makes an entry into \meta{file} to change the |\@pnumwidth| and
% the |\@tocrmarg| values.
%    \begin{macrocode}
\newcommand{\cftlocalchange}[3]{%
  \addtocontents{#1}{\protect\setpnumwidth{#2} \protect\setrmarg{#3}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\cftaddtitleline}
% |\cftaddtitleline{|\meta{file}|}{|\meta{kind}|}{|\meta{title}|}{|\meta{page}|}|
% adds a |\contentsline| entry to \meta{file} with the given information.
%    \begin{macrocode}
\newcommand{\cftaddtitleline}[4]{%
  \addtocontents{#1}{\protect\contentsline{#2}{#3}{#4}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\cftaddnumtitleline}
% |\cftaddnumtitleline{|\meta{file}|}{|\meta{kind}|}{|\meta{num}|}{|\meta{title}|}{|\meta{page}|}|
% adds a |\contentsline| entry to \meta{file} with the given information.
%    \begin{macrocode}
\newcommand{\cftaddnumtitleline}[5]{%
  \addtocontents{#1}%
    {\protect\contentsline{#2}{\protect\numberline{#3}%
                              {\protect\ignorespaces #4}}{#5}}}
%    \end{macrocode}
% \end{macro}
%
%
% \subsection{ToC and divisional numbering}
%
%  Commands are provided, based on the \Lpack{tocvsec2} package, for changing
% the section numbering level and the ToC entry level.
%
% \begin{macro}{\@setclcnt}
% Helper macro to set a sectioning-related counter. Use as
% |\@setclcnt{|\meta{sec}|}{|\meta{counter}|}| to set \Lcount{counter}
% to the level of \meta{sec}.
%    \begin{macrocode}
\newcommand{\@setclcnt}[2]{
  \@tempswafalse
  \nametest{#1}{none}
  \ifsamename
    \setcounter{#2}{-10}
    \@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}
% |\settocdepth{|\meta{sec}|}| is the user command for setting 
% \Lcount{tocdepth} in the \file{.toc} file to the value corresponding to
% \meta{sec}. 
% \changes{v1.3}{2002/11/14}{Changed \cs{settocdepth} to use \cs{changetocdepth}}
%    \begin{macrocode}
\newcommand{\settocdepth}[1]{%
  \@tempswafalse
  \nametest{#1}{none}
  \ifsamename
    \addtocontents{toc}{\changetocdepth{-10}}
    \@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\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}{\changetocdepth}
% Changes the |tocdepth| counter. Make it robust as it will be written
% to the *.toc file.
% \changes{v1.3}{2002/11/14}{Added \cs{changetocdepth}}
%    \begin{macrocode}
\DeclareRobustCommand{\changetocdepth}[1]{\setcounter{tocdepth}{#1}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\maxtocdepth}
% |\maxtocdepth{|\meta{sec}|}| can be used to initialise \Lcount{tocdepth}
% to the value corresponding to \meta{sec}. This can only be used
% between the end of the preamble and the |\tableofcontents| command.
%    \begin{macrocode}
\newcommand{\maxtocdepth}[1]{%
  \@setclcnt{#1}{tocdepth}}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\setsecnumdepth}
% |\setsecnumdepth{|\meta{sec}|}| is the user command for setting
% \Lcount{secnumdepth} to the value for \meta{sec}. It can only be used after
% the preamble.
%    \begin{macrocode}
\newcommand{\setsecnumdepth}[1]{%\leavevmode%
  \@setclcnt{#1}{secnumdepth}}
%    \end{macrocode}
% \end{macro}
% 
% 
% \begin{macro}{\maxsecnumdepth}
% |\maxsecnumdepth{|\meta{sec}|}| can be used to initialise 
% \Lcount{secnumdepth}
% to the value corresponding to \meta{sec}.
% \changes{v1.1}{2002/03/10}{Added maxsecnumdepth counter and modified \cs{maxsecnumdepth}}
%    \begin{macrocode}
\newcounter{maxsecnumdepth}
\newcommand{\maxsecnumdepth}[1]{%
  \@setclcnt{#1}{secnumdepth}\@setclcnt{#1}{maxsecnumdepth}}

%    \end{macrocode}
% \end{macro}
% 
%
%
% \section{Bibliography}
%
%
% \begin{macro}{\bibindent}
% The \Lopt{open} bibliography uses an indentation of |\bibindent|.
%    \begin{macrocode}
\newdimen\bibindent
  \setlength\bibindent{1.5em}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\bibitemsep}
% The vertical separation between items in the bibliography list.
% \changes{v1.4}{2003/11/22}{Added \cs{bibitemsep} (from patch v1.7)}
%    \begin{macrocode}
\newlength{\bibitemsep}
  \setlength{\bibitemsep}{\itemsep}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\biblistextra}
% A hook into the |bibitemlist|.
% \changes{v1.4}{2003/11/22}{Added \cs{biblistextra} (from patch v1.7)}
%    \begin{macrocode}
\newcommand{\biblistextra}{\itemsep=\bibitemsep}

%    \end{macrocode}
% \end{macro}
%
% \begin{environment}{bibitemlist}
%  The |thebibliography| environment starts a new document division. 
% Internally it tweaks some typesetting aspects; principally it
% uses |\sloppy| because good linebreaking is hard in a bibliography,
% and |\sfcode`\.=1000\relax| causes a full stop not to produce an
% end-of-sentence space. The implementation of the environment is
% based on the generic |list| environment, and uses the |\c@enumiv|
% counter for the labels. The following code is extracted from the
% \Lpack{book} class, plus some additions.
% \changes{v1.4}{2003/11/22}{Added \cs{biblistextra} to bibitemlist (from patch v1.7)}
%    \begin{macrocode}
\newenvironment{bibitemlist}[1]{%
  \typeout{bibitemlist}
  \list{\@biblabel{\@arabic\c@enumiv}}%
       {\settowidth\labelwidth{\@biblabel{#1}}%
        \leftmargin\labelwidth
        \advance\leftmargin\labelsep
        \@openbib@code
        \usecounter{enumiv}%
        \let\p@enumiv\@empty
        \renewcommand\theenumiv{\@arabic\c@enumiv}%
        \biblistextra}%
  \sloppy
  \clubpenalty4000
  \@clubpenalty \clubpenalty
  \widowpenalty4000%
  \sfcode`\.\@m}%
  {\def\@noitemerr
    {\@latex@warning{Empty `thebibliography' environment}}%
    \endlist}

%    \end{macrocode}
% \end{environment}
%
% \begin{macro}{\newblock}
% The default is for |\newblock| to provide a small space.
%    \begin{macrocode}
\newcommand{\newblock}{\hskip .11em\@plus.33em\@minus.07em}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@openbib@code}
% This a an empty hook. It will be modified if the \Lopt{openbib}
% option is used.
%    \begin{macrocode}
\let\@openbib@code\@empty
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\setbiblabel}
% \begin{macro}{\@biblabel}
% This is the user command for setting the label for a |\bibitem|. 
% The following sets the default definition.
%    \begin{macrocode}
\newcommand*{\setbiblabel}[1]{%
  \renewcommand*{\@biblabel}[1]{#1}}
\setbiblabel{[#1]\hfill}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@memb@bchap}
% \begin{macro}{\@memb@bsec}
%  In a \ctt{} thread \textit{memoir, natbib, and chapterbib} in January 2003
% there was a discussion on how to get all three to work together.
% Donald Arseneau suggested that |\bibsection| be used as a `standard'
% way of controlling the sectioning command of a bibliography. Here is
% an implementation of that idea.
%
% |\@memb@bchap| is a copy of memoir's original code for the start
% of the |thebibliography| environment which used a |\chapter*|
% title. |\@memb@bsec| is the much simpler version for a |\section| title.
% \changes{v1.4}{2003/02/27}{Added support code for implementing \cs{bibsection}
%                            (patch 1.3)}
%    \begin{macrocode}
\newcommand{\@memb@bchap}{%
  \chapter*{\bibname}%
  \bibmark
  \ifnobibintoc\else
    \phantomsection
    \addcontentsline{toc}{chapter}{\bibname}%
  \fi
  \prebibhook}
\newcommand{\@memb@bsec}{\section{\bibname}\prebibhook}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\bibsection}
% Normally treat the bibliography heading as a chapter.
% \changes{v1.4}{2003/02/27}{Added \cs{bibsection} to replace memoir's
%                            original code for the bibliography (patch 1.3)}
%    \begin{macrocode}
\newcommand{\bibsection}{\@memb@bchap}

%    \end{macrocode}
% \end{macro}
% 
% \begin{environment}{thebibliography}
% The definition of the |thebibliography| environment in this class is not
% quite the same as in the standard classes!
%
% \changes{v0.2}{2001/06/03}{Deleted starred version of thebibliography}
% \changes{v1.4}{2003/02/27}{Replaced initial code in the bibliography
%                            environment by \cs{bibsection} (patch 1.3)}
%    \begin{macrocode}
\newenvironment{thebibliography}[1]{%
  \bibsection
  \begin{bibitemlist}{#1}}{\end{bibitemlist}\postbibhook}
%    \end{macrocode}
% \end{environment}
%
% \begin{macro}{\ifnobibintoc}
% \begin{macro}{\bibintoc}
% \begin{macro}{\nobibintoc}
% Flag to control whether or not to add the bibliography title to the ToC,
% and declarations to set the flag.
% Default is to put the title into the ToC.
% \changes{v0.2}{2001/06/03}{Added \cs{ifnobibintoc}, etc.}
%    \begin{macrocode}
\newif\ifnobibintoc
\newcommand*{\bibintoc}{\nobibintocfalse}
\newcommand*{\nobibintoc}{\nobibintoctrue}
\bibintoc

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\prebibhook}
% \begin{macro}{\postbibhook}
% These two macros are called just before starting the bib items and just
% after finishing them. By default they do nothing but can be changed
% by the user to give, say, some introductory information.
%    \begin{macrocode}
\newcommand{\prebibhook}{}
\newcommand{\postbibhook}{}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\@cite}
% The output of the |\cite| command is produced by this macro. The default
% is used. The \Lpack{cite} package is a good way of changing this.
%    \begin{macrocode}
% \renewcommand*{\@cite}[1]{[#1]}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Use with the \Lpack{natbib} and \Lpack{chapterbib} packages}
%
% The \Lpack{natbib} package~\cite{NATBIB} uses its own definition for
% the |\thebibliography| environment, which knows nothing about adding
% the Bibliography to the ToC. The following makes appropriate changes
% to \Lpack{natbib} code to support the class.
%
% The \Lpack{chapterbib} package by Donald Arseneau also may make changes
% to the |\thebibliography| environment --- perhaps memoir's version
% or natbib's version.
%
%    As packages get loaded after classes, I have to try and patch any
% non-memoir modifications at begin document time. 
% The fixes have to be firstly for \Lpack{natbib}
% and only after those can \Lpack{chapterbib} be considered.
%
% \changes{v1.1}{2002/03/10}{Added internal support for the natbib package}
% \changes{v1.4}{2003/02/27}{Changed code supporting natbib (patch 1.3)}
% \changes{v1.4}{2003/02/27}{Added code supporting chapterbib (patch 1.3)}
% \begin{macro}{\bibsection}
% \begin{macro}{\endthebibliography}
% Natbib provides |\bibsection| for titling the bibliography. I also have
% to extend the end of the |thebibliography| environment to cater for
% |\postbibhook|.
%    \begin{macrocode}
\AtBeginDocument{%
  \@ifpackageloaded{natbib}{% natbib is loaded
%    \end{macrocode}
% As \Lpack{natbib} is used, change |\endthebibliography| to the class
% definition.
%    \begin{macrocode}
    \addtodef{\endthebibliography}{}{\vskip-\lastskip\postbibhook}
    \@ifpackagewith{natbib}{sectionbib}{% with sectionbib option
%    \end{macrocode}
% \Lpack{natbib}'s |sectionbib| option is used,
%    \begin{macrocode}
      \renewcommand{\bibsection}{\@memb@bsec}}%
%    \end{macrocode}
% The |sectionbib| option is not used, so we have a chapter title.
%    \begin{macrocode}
      {\renewcommand{\bibsection}{\@memb@bchap}}}%
%    \end{macrocode}
% Finished with any \Lpack{natbib} related changes.
%    \begin{macrocode}
  {}
%    \end{macrocode}
%
% Now deal with \Lpack{chapterbib} if necessary.
%    \begin{macrocode}
  \@ifpackagewith{chapterbib}{sectionbib}{%
%    \end{macrocode}
% \Lpack{chapterbib} is used with its |sectionbib| option. This is the
% only case to worry about. Kill \Lpack{chapterbib}'s |\sectionbib| macro
% which it calls at begin document to do its patch, then make sure
% the class definition is used.
%    \begin{macrocode}
    \renewcommand{\sectionbib}[2]{}
    \renewcommand{\bibsection}{\@memb@bsec}}{}
%    \end{macrocode}
% And we've finished with this bunch of |\AtBeginDocument|.
%    \begin{macrocode}
}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
%
%  \section{The index}
%
% I allow for a single column index as well as the default double column.
%
% \begin{macro}{\ifonecolindex}
% TRUE for a one column index.
% \changes{v1.3}{2002/10/10}{Added one column index}
%    \begin{macrocode}
\newif\ifonecolindex
  \onecolindexfalse

%    \end{macrocode}
% \end{macro}
%
% \begin{environment}{theindex}
%    The environment `theindex' can be used for indices. It makes an
%    index with two columns, with each entry a separate paragraph. At
%    the user level the commands |\item|, |\subitem| and |\subsubitem|
%    are used to produce index entries of various levels. When a new
%    letter of the alphabet is encountered an amount of |\indexspace|
%    white space can be added.
%
%
% \changes{v0.2}{2001/06/03}{Deleted starred version of theindex}
% \changes{v0.2}{2001/06/03}{Changed Index pagestyle to chapter}
% \changes{v1.618}{2005/09/03}{Make \cs{indexcolsep} work, courtesy of hazydirk (mempatch v3.11)}
%    \begin{macrocode}
\newenvironment{theindex}{%
  \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@
  \parskip\z@ \@plus .3\p@\relax
  \let\item\@idxitem}%
  {\if@restonecol\onecolumn\else\twocolumn\fi}

%    \end{macrocode}
% \end{environment}
%
% \begin{macro}{\ifnoindexintoc}
% \begin{macro}{\indexintoc}
% \begin{macro}{\noindexintoc}
% Flag to control whether or not to add the index title to the ToC,
% and declarations to set the flag.
% Default is to put the title into the ToC.
% \changes{v0.2}{2001/06/03}{Added \cs{ifnoindexintoc}, etc.}
%    \begin{macrocode}
\newif\ifnoindexintoc
\newcommand*{\indexintoc}{\noindexintocfalse}
\newcommand*{\noindexintoc}{\noindexintoctrue}
\indexintoc

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\indexcolsep}
% \begin{macro}{\indexrule}
% These two lengths control the column separation and the thickness
% of the inter-column rule.
% \changes{v0.2}{2001/06/03}{Added \cs{indexcolsep} and \cs{indexrule}}
%    \begin{macrocode}
\newlength{\indexcolsep} \setlength{\indexcolsep}{35pt}
\newlength{\indexrule}   \setlength{\indexrule}{0pt}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\preindexhook}
% A macro that is called between the index heading and the start of the
% two columns. The user can modify it to add something.
%    \begin{macrocode}
\newcommand{\preindexhook}{}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\l@index}
%   Format the index entry in the table of contents.
%    \begin{macrocode}
\newcommand{\l@index}{\@dottedtocline{1}{0em}{0pt}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@idxitem}
% \begin{macro}{\subitem}
% \begin{macro}{\subsubitem}
%    Thsee macros are used to format the entries in the index. 
%    \begin{macrocode}
\newcommand{\@idxitem}  {\par\hangindent 40\p@}
\newcommand{\subitem}   {\par\hangindent 40\p@ \hspace*{20\p@}}
\newcommand{\subsubitem}{\par\hangindent 40\p@ \hspace*{30\p@}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\indexspace}
%    The amount of white space that is inserted between `letter
%    blocks' in the index.
%    \begin{macrocode}
\newcommand{\indexspace}{\par \vskip 10\p@ \@plus5\p@ \@minus3\p@\relax}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\makeindex}
% \begin{macro}{\index}
% \begin{macro}{\specialindex}
% \begin{macro}{\makememindexhook}
% This is a modified version of the kernel |\makeindex| to allow for
% multiple indexes. It also defines |\index| (which is a modified version
% of the standard |\index|) and |\specialindex|.
%
% |\makememindexhook| is a null op but can be redefined to add extra code
% into |\makeindex|. For example, to incorporate \Lpack{ledmac}'s
% |\edindex| into the scheme:
% \begin{verbatim}
% \renewcommand*{\makememindexhook}{%
%    \def\edindex{\@bsphack%
%      \@ifnextchar [{\l@d@index}{\l@d@index[\jobname]}}}
% \end{verbatim}
% \changes{v1.3}{2002/11/14}{Indexing macros completely redone and extended}
% \changes{v1.61}{2004/03/12}{Added the \cs{makememindexhook} to \cs{makeindex}}
% \changes{v1.618}{2005/09/02}{Modified \cs{makeindex} for \cs{nofiles}}
%    \begin{macrocode}
\newcommand*{\makememindexhook}{}
%    \end{macrocode}
%    \begin{macrocode}
\providecommand*{\makeindex}{}
\renewcommand*{\makeindex}[1][\jobname]{%
  \if@filesw
    \def\index{\@bsphack%
      \@ifnextchar [{\@index}{\@index[\jobname]}}
    \def\specialindex{\@bsphack\@spindex}%
    \makememindexhook
    \expandafter\newwrite\csname #1@idxfile\endcsname
    \expandafter\immediate\openout \csname #1@idxfile\endcsname #1.idx\relax
    \typeout{Writing index file #1.idx }%
  \fi}

%    \end{macrocode}
% Initially define, but emasculate, |\index| and |\specialindex| which are
% defined properly by the user calling |\makeindex|.
%    \begin{macrocode}
\renewcommand{\index}[2][\jobname]{\@bsphack\@esphack}
\newcommand{\specialindex}[3]{\@bsphack\@esphack}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\printindex}
% The command to read an \file{ind} file.
%    \begin{macrocode}
\newcommand{\printindex}[1][\jobname]{\@input@{#1.ind}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ifreportnoidxfile}
% \begin{macro}{\ifshowindexmark}
% Two booleans to control reporting on unknown \file{idx} files and
% displaying indexed items in the margin.
%    \begin{macrocode}
\newif\ifreportnoidxfile
  \reportnoidxfilefalse
\newif\ifshowindexmark
  \showindexmarkfalse

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@index}
% |\@index[file]| is first stage of |\index|, handling the \file{idx} file.
%    \begin{macrocode}
\def\@index[#1]{%
  \@ifundefined{#1@idxfile}%
  {\ifreportnoidxfile
     \@memwarn{Undefined index file #1}%
    \fi
    \begingroup
    \@sanitize
    \@nowrindex}%
  {\def\@idxfile{#1}%
   \begingroup
   \@sanitize
   \@wrindexm@m}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@nowrindex}
% Called when there is no \file{idx} file to throw away the indexed item.
%    \begin{macrocode}
\newcommand{\@nowrindex}[1]{%
  \ifshowindexmark\@showidx{#1}\fi\endgroup\@esphack}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@wrindexm@m}
% \begin{macro}{\@@wrindexhyp}
% The next stage in index processing is |\@wrindexm@m{item}|, 
% which writes the \file{idx} file name
% and indexed item to the \file{aux} file. The |\@@wrindexhyp| macro
% provides hyperlinks in case the \Lpack{hyperref} package is used.
%    \begin{macrocode}
\newcommand{\@wrindexm@m}[1]{\@@wrindexhyp#1||\\}
\def\@@wrindexhyp#1|#2|#3\\{%
  \ifshowindexmark\@showidx{#1}\fi
  \ifx\\#2\\%
    \protected@write\@auxout{}%
      {\string\@@wrindexm@m{\@idxfile}{#1|hyperpage}{\thepage}}%
  \else
    \def\Hy@temp@A{#2}%
    \ifx\Hy@temp@A\HyInd@ParenLeft
      \protected@write\@auxout{}%
        {\string\@@wrindexm@m{\@idxfile}{#1|#2hyperpage}{\thepage}}%
    \else
      \protected@write\@auxout{}%
        {\string\@@wrindexm@m{\@idxfile}{#1|#2}{\thepage}}%
    \fi
  \fi
  \endgroup
  \@esphack}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\hyperpage}
% \begin{macro}{\hyperlink}
% These are defined in the \Lpack{hyperref} package but we need them.
% other hyperstuff is only used if the package itself is used.
%    \begin{macrocode}
\newcommand{\hyperpage}[1]{#1}
\newcommand{\hyperlink}[2]{#2}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@wrindexm@m}
% The macro |\@@wrindexm@m{file}{item}{page}| has been written into
% the \file{aux} file. It is normally defined so that it calls
% |\@@@wrindexm@m{item}{page}| to finally write to the \file{idx} file.
%    \begin{macrocode}
\newcommand{\@@wrindexm@m}[1]{\begingroup
  \def\@idxfile{\@nameuse{#1@idxfile}}
  \@sanitize
  \@@@wrindexm@m}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@@wrindexm@m}
% \cs{@@@wrindexm@m}\marg{item}\marg{page} writes the \cs{indexentry} to 
% the \file{idx} file. This file is read both at the beginning and end of 
% a document so \cs{@@@wrindexm@m} must be disabled for one of these reads
% otherwise the index entries will be doubled up. Initially I disabled it
% after the first read. It eventually dawned on me that this meant two
% LaTeX runs to update the \file{idx}. Enabling it for the second read
% means that the \file{idx} file is updated at the end of each run.
% \changes{v1.618}{2005/09/02}{Made \cs{@@@wrindexm@m} obey \cs{nofiles}}
% \changes{v1.618}{2005/09/25}{\cs{@@@wrindexm@m} now reads the aux
%                  file at the end of the document}
%    \begin{macrocode}
\newcommand{\@@@wrindexm@m}[2]{\endgroup}
\AtBeginDocument{%
  \def\@@@wrindexm@m#1#2{%
    \if@filesw
      \immediate\write \@idxfile{\string\indexentry{#1}{#2}}%
    \fi
    \endgroup}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@spindex}
% Now do similar things for \cs{specialindex}.
%    \begin{macrocode}
\newcommand{\@spindex}[2]{%
  \@ifundefined{#1@idxfile}%
  {\ifreportnoidxfile
     \@memwarn{Undefined index file #1}%
    \fi
    \begingroup
    \@sanitize
    \@nowrindex}%
  {\def\@idxfile{#1}%
   \def\@sptheidx{#2}%
   \begingroup
   \@sanitize
   \@wrspindex}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@wrspindex}
% \begin{macro}{\@@wrspindexhyp}
%    \begin{macrocode}
\newcommand{\@wrspindex}[1]{\@@wrspindexhyp#1||\\}
\def\@@wrspindexhyp#1|#2|#3\\{%
  \ifshowindexmark\@showidx{#1}\fi
  \ifx\\#2\\%
    \protected@write\@auxout{}%
      {\string\@@wrindexm@m{\@idxfile}%
        {#1|hyperspindexpage(\thepage)}%
      {\@nameuse{the\@sptheidx}}}%
  \else
    \def\Hy@temp@A{#2}%
    \ifx\Hy@temp@A\HyInd@ParenLeft
      \protected@write\@auxout{}%
        {\string\@@wrindexm@m{\@idxfile}%
         {#1|#2hyperspindexpage(\thepage)}%
         {\@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}
%
% \begin{macro}{\hyperspindexpage}
%    \begin{macrocode}
\def\hyperspindexpage(#1)#2{\hyperlink{page.#1}{#2}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ifmemhyperindex}
% \begin{macro}{\memhyperindextrue}
% \begin{macro}{\memhyperindexfalse}
% Flag to turn off \cs{hyperindexfalse} hyperrefing the index (apparently
% \Lpack{xindy} can't cope with this. Email from Frederic Connes, 2005/07/13:
% \begin{verbatim}
%  ...You use |hyperspindex(\thepage) which xindy doesn't 
% recognise as a valid markup-locref. And I don't know how 
% to add it because xindy only accepts one argument in 
% markup-locref (if the number is not a page number, it will 
% still point to a page with that number) so replacing it 
% with |hyperpage won't work. ...
% \end{verbatim}
% 
%
% The default is \cs{memhyperindextrue}. Setting \cs{memhyperindexfalse}
% prohibits a hyper index --- setting whatever option in \Lpack{hyperref}
% to disable hyper indexing will (probably) not work as far as \Lpack{xindy}
% is concerned.
% \changes{v1.618}{2005/09/04}{Added \cs{ifmemhyperindex}}
%    \begin{macrocode}
\newif\ifmemhyperindex
  \memhyperindextrue

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@wrindexhyp}
% \begin{macro}{\@@wrspindexhyp}
% If the \Lpack{hyperref} package is not being used, or \cs{ifmemhyperindex}
% is false, there is no need
% to clutter up the index files. 
% \changes{v1.61}{2004/03/28}{Declutter index files if hyperref is not used}
% \changes{v1.618}{2005/09/02}{Fixed spaces in non-hyperref index files (mempatch v2.3).
% Reported by Michael W. Daniels.}
%    \begin{macrocode}
\AtBeginDocument{%
  \@ifpackageloaded{hyperref}{}{\memhyperindexfalse}%
%    \end{macrocode}
% If the \Lpack{hyperref} package is not being used, or a hyperindex
% is not required, simplify!
%    \begin{macrocode}
  \ifmemhyperindex\else
    \def\@@wrindexhyp#1||\\{%
      \ifshowindexmark\@showidx{#1}\fi
      \protected@write\@auxout{}%
        {\string\@@wrindexm@m{\@idxfile}{#1}{\thepage}}%
      \endgroup
      \@esphack}%
    \def\@@wrspindexhyp#1||\\{%
      \ifshowindexmark\@showidx{#1}\fi
      \protected@write\@auxout{}%
        {\string\@@wrindexm@m{\@idxfile}{#1}{\@nameuse{the\@sptheidx}}}%
      \endgroup
      \@esphack}%
  \fi
}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\see}
% \begin{macro}{\seename}
% \begin{macro}{\seealso}
% \begin{macro}{\alsoname}
% These definitions are taken from the \Lpack{makeidx} package.
%    \begin{macrocode}
\newcommand*{\see}[2]{\emph{\seename} #1}
\newcommand*{\seename}{see}
\newcommand*{\seealso}[2]{\emph{\alsoname} #1}
\newcommand*{\alsoname}{see also}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\citeindexfile}
% For the \Lpack{natbib} package, and possibly other packages that do 
% some special indexing.
%    \begin{macrocode}
\newcommand{\citeindexfile}{\jobname}
\AtBeginDocument{\@ifpackageloaded{natbib}{%
  \def\NAT@index{\index[\citeindexfile]{\NAT@idxtxt}}}{}}

%    \end{macrocode}
% \end{macro}
%
% The next part of the code is essentially the \Lpack{showidx} package.
% I tried putting index entries into marginoars but too many on a page
% led to the `too many floats' problem.
%
% \begin{macro}{\indexmarkstyle}
% \begin{macro}{\@indexbox}
%    \begin{macrocode}
\newtoks\indexmarkstyle
  \indexmarkstyle{\normalfont\footnotesize\ttfamily}
\newinsert\@indexbox
  \dimen\@indexbox\maxdimen

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%    \begin{macrocode}
\begingroup
  \catcode`\@\active
  \expandafter\gdef\csname\string @sanitizeat\endcsname
  {\def @{\char`\@}}
\endgroup

%    \end{macrocode}
%
% \begin{macro}{\@showidx}
%    \begin{macrocode}
\newcommand{\@showidx}[1]{%
  \insert\@indexbox{%
    \@sanitizeat
    \the\indexmarkstyle
    \hsize\marginparwidth
    \hangindent\marginparsep \parindent\z@
    \everypar{}\let\par\@@par \parfillskip\@flushglue
    \lineskip\normallineskip
    \baselineskip .8\normalbaselineskip\sloppy
    \raggedright \leavevmode
    \vrule \@height .7\normalbaselineskip \@width \z@\relax
      #1\relax
    \vrule \@height \z@ \@depth .3\normalbaselineskip \@width \z@\relax
  }%
  \ifhmode\penalty\@M \hskip\z@skip\fi
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@leftidx}
% \begin{macro}{\@rightidx}
%    \begin{macrocode}
\newcommand{\@leftidx}{\hskip-\marginparsep \hskip-\marginparwidth}
\newcommand{\@rightidx}{\hskip\columnwidth \hskip\marginparsep}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@mkidx}
%    \begin{macrocode}
\newcommand{\@mkidx}{\vbox to \z@{%
  \rlap{%
    \if@twocolumn
      \if@firstcolumn \@leftidx \else \@rightidx \fi
    \else
      \if@twoside
        \ifodd\c@page \@rightidx \else \@leftidx \fi
      \else
        \@rightidx
      \fi
    \fi
    \box\@indexbox
  }%
  \vss
}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\raggedbottom}
% \begin{macro}{\flushbottom}
% \begin{macro}{\@texttop}
% These kernel macros need changing because of the new marginal insert.
%    \begin{macrocode}
\renewcommand{\raggedbottom}{%
  \def\@textbottom{\vskip\z@ plus.0001fil}%
  \let\@texttop\@mkidx}
\renewcommand{\flushbottom}{%
  \let\@textbottom\relax
  \let\@texttop\@mkidx}
\let\@texttop\@mkidx

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \section{Glossaries}
%
% Standard LaTeX provides little and insufficient support for
% glossaries, just a \cs{makeglossary} and \cs{glossary} commands.
% This class does somewhat better. The code follows along the lines
% of that for indexes.
% \changes{v1.618}{2005/09/25}{Added glossary code}
%
%
% \begin{macro}{\ifonecolglossary}
% \begin{macro}{\onecolglossarytrue}
% \begin{macro}{\onecolglossaryfalse}
% TRUE for a one column glossary, otherwise its two column.
%    \begin{macrocode}
\newif\ifonecolglossary
  \onecolglossarytrue

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% \begin{environment}{theglossary}
% The environment \texttt{theglossary} is used for glossaries.
% It makes a glossary with one or two columns, headed by a chapter-like
% title.
%    \begin{macrocode}
\newenvironment{theglossary}{%
  \if@twocolumn
    \@restonecolfalse
  \else
    \@restonecoltrue
  \fi
  \ifonecolglossary
    \onecolumn
    \chapter*{\glossaryname}
    \preglossaryhook
  \else
    \setlength{\columnseprule}{\glossaryrule}
    \setlength{\columnsep}{\glossarycolsep}
    \twocolumn[\@makeschapterhead{\glossaryname}
               \preglossaryhook]%
  \fi
  \glossarymark
  \ifnoglossaryintoc\else
    \phantomsection
    \addcontentsline{toc}{chapter}{\glossaryname}
  \fi
  \thispagestyle{chapter}\parindent\z@
  \parskip\z@ \@plus .3\p@\relax
  \begintheglossaryhook}%
  {\atendtheglossaryhook\if@restonecol\onecolumn\else\twocolumn\fi}

%    \end{macrocode}
% \end{environment}
%
% \begin{macro}{\begintheglossaryhook}
% \begin{macro}{\atendtheglossaryhook}
% Two vacuous macros called as the last thing by \verb?\begin{theglossary}?
% and the first thing by \verb?\end{theglossary}? These could be used,
% for example, to insert another kind of environment.
%    \begin{macrocode}
\newcommand*{\begintheglossaryhook}{}
\newcommand*{\atendtheglossaryhook}{}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\preglossaryhook}
% A vacuous macro called after the title is set and before the listing
% starts. The user can modify it to, for example, add some explanatory
% text.
%    \begin{macrocode}
\newcommand*{\preglossaryhook}{}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ifnoglossaryintoc}
% \begin{macro}{\glossaryintoc}
% \begin{macro}{\noglossaryintoc}
% Flag and declarations to control whether or not the glossary
% title is added to the ToC. Default is to put the title
% in the ToC.
%    \begin{macrocode}
\newif\ifnoglossaryintoc
\newcommand*{\glossaryintoc}{\noglossaryintocfalse}
\newcommand*{\noglossaryintoc}{\noglossaryintoctrue}
  \glossaryintoc

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\glossarycolsep}
% \begin{macro}{\glossaryrule}
% When the glossary is two column these lengths control the
% column separation and the width of a rule between the columns.
%    \begin{macrocode}
\newdimen\glossarycolsep \glossarycolsep=35\p@
\newdimen\glossaryrule \glossaryrule=0\p@
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\glossaryspace}
% Vertical space between `letter blocks' in the glossary.
% Note that this is a macro, not a length.
%    \begin{macrocode}
\newcommand*{\glossaryspace}{%
    \par \vskip 1.0\onelineskip \@plus 5\p@ \@minus3\p@\relax}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\makeglossary}
% The preamble command to set up a glossary. Output is to the
% \file{\#1.glo} file. We need the \cs{providecommand} in case \cs{nofiles}
% has been called before the class. \cs{makeglossary} sets up several 
% file-specific macros.
%    \begin{macrocode}
\providecommand*{\makeglossary}{}
\renewcommand*{\makeglossary}[1][\jobname]{%
  \makememglossaryhook
  \@namedef{memglsact#1}{@}%           actual
  \@namedef{memglsnx#1}{}%             no ref
  \@namedef{memglsn#1}{\thepage}%      num  by page
  \@namedef{memglsnf#1}{|memjustarg}%  no special number format
  \if@filesw \expandafter\newwrite\csname #1memglofile\endcsname
    \expandafter\immediate\openout \csname #1memglofile\endcsname #1.glo\relax
    \typeout{Writing glossary file #1.glo }%
  \fi}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\makememglossaryhook}
% A vacuous macro called at the start of the \cs{makeglossary} code.
% It is redefinable to do something useful.
%    \begin{macrocode}
\newcommand*{\makememglossaryhook}{}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\glossary}
% The user command for a raw glossary item. The full calling
% sequence looks like: \\
% \cs{glossary}\oarg{file}\parg{key}\marg{term}\marg{desc} \\
% but most is accomplished by lower level macros. It calls
% \cs{@glossary} to handle the first optional argument.
%    \begin{macrocode}
  \def\glossary{\@bsphack%
    \@ifnextchar [{\@glossary}{\@glossary[\jobname]}}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@glossary}
% \cs{@glossary}\oarg[file] handles the first of the \cs{glossary} optional
% arguments, even though the calling sequence looks like: \\
% \cs{@glossary}\oarg{file}\parg{key}\marg{term}\marg{desc}. \\
% It calls \cs{@@glossary} to handle the second optional argument
% and the rest.
%    \begin{macrocode}
\def\@glossary[#1]{%
  \@ifnextchar ({\@@glossary[#1]}{\@@glossary[#1]()}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@glossary}
% The base macro for \cs{glossary}. Its real calling sequence is: \\
% \cs{@@glossary}\oarg{file}\parg{key}\marg{term}\marg{desc}. \\
% Provided the glossary output file has been set up it calls
% \cs{@wrglom@m} to write the data, otherwise it throws away its
% arguments.
%    \begin{macrocode}
\def\@@glossary[#1](#2)#3#4{%
  \@ifundefined{#1memglofile}{%
    \begingroup
    \@sanitize
    \endgroup
    \@esphack%
   }{%
    \def\memglofile{#1}%
    \begingroup
    \@sanitize
%    \end{macrocode}
% Use the correct key, \verb?key? if given, otherwise \verb?term?.
%    \begin{macrocode}
    \ifx\@empty#2\@empty
      \@wrglom@m{#3}{#3}{#4}%
    \else
      \@wrglom@m{#2}{#3}{#4}%
    \fi}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@wrglom@m}
% \cs{@wrglom@m}\marg{key}\marg{term}\marg{desc} writes its three arguments,
% plus the \meta{ref} and the \meta{num} to the \file{aux} file as the
% arguments to the \cs{@@wrglom@m} macro.
%    \begin{macrocode}
\newcommand{\@wrglom@m}[3]{%
  \protected@write\@auxout{}%
    {\string\@@wrglom@m{\memglofile}{#1}{#2}{#3}{\@nameuse{memglsnx\memglofile}}{\@nameuse{memglsn\memglofile}}}%
  \endgroup
  \@esphack}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@wrglom@m}
% The calling sequence looks like: \\
% \cs{@@wrglom@m}\marg{file}\marg{key}\marg{term}\marg{desc}\marg{ref}\marg{num} \\
% but is actually really only \\
% \cs{@@wrglom@m}\marg{file} \\
% It saves the output file identifier in \cs{memglofile} and 
% \meta{file} as \cs{m@mgf}, then calls \cs{memwritetoglo}
% to handle the remaining `arguments'.
%    \begin{macrocode}
\newcommand{\@@wrglom@m}[1]{\begingroup
  \def\memglofile{\@nameuse{#1memglofile}}%
  \def\m@mgf{#1}%
  \@sanitize
  \memwritetoglo}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\memwritetoglo}
% \begin{macro}{\@ctualm@mwritetoglo}
% \cs{memwritetoglo}\marg{key}\marg{name}\marg{desc}\marg{ref}\marg{num}
% will, at an appropriate time write the raw glossary data to the
% appropriate \file{glo} file.
%
%    The \file{aux} file is read twice, once as one of the initial
% actions at the start of the \texttt{document} environment, and again
% at the end of the \texttt{document} environment. The raw glossary
% data must only be written once from the \file{aux} to the \file{glo} file.
% Initially \cs{memwritetoglo} is defined to do nothing, then, after the
% \file{aux} file has been read at the beginning is redefined to do
% its writing. 
%
% The Makeindex program expects its input to be like: \\
% \verb?\glosaryentry{key@data}{num}?
% Very cunningly the raw data is fed to the \file{glo}
% file looking like: \\
% \verb?\glossaryentry{key@{\memgloterm{term} \memglodesc{desc} \memgloref{ref}}{num}? \\
% which as far as Makeindex is concerned is simply: \\
% \verb?\glosaryentry{key@{data}}{num}? \\
% (note where all the braces are).
%
%    \begin{macrocode}
\newcommand{\memwritetoglo}[5]{\endgroup}
\newcommand{\@ctualm@mwritetoglo}[5]{%
  \immediate\write \memglofile{\string\glossaryentry{#1\@nameuse{memglsact\m@mgf}
                    {\string\memgloterm{#2}}{\string\memglodesc{#3}}
                    {\string\memgloref{#4}}\@nameuse{memglsnf\m@mgf}}{#5}}%
  \endgroup}
\AtBeginDocument{%
  \let\memwritetoglo\@ctualm@mwritetoglo}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\changeglossactual}
% \begin{macro}{\changeglossref}
% \begin{macro}{\changeglossnum}
% \begin{macro}{\changeglossnumformat}
% There are internal macros that are file-dependant, their names
% being constructed from the file name plus a fixed name. The
% \cs{makeglossary} command initializes these but the user may well need
% to change them. In the macros below, \texttt{...actual} refers to the
% actual character in a Makeindex configuration file (default \texttt{@}),
% \texttt{...ref} to the \meta{ref} glossary argument, \texttt{...num}
% to the meta{num} argument (default \cs{thepage}) and \texttt{...numformat}
% to the Makeindex encapsulating command. Example uses are:
% \begin{verbatim}
% \changeglossactual[file]{?}
% \changeglossref[file]{\thepage}
% \changeglossnum[file]{\thesection}
% \changeglossnumref[file]{|textbf}
% \end{verbatim}
%    \begin{macrocode}
\newcommand*{\changeglossactual}[2][\jobname]{%
  \@namedef{memglsact#1}{#2}}
\newcommand*{\changeglossref}[2][\jobname]{%
  \@namedef{memglsnx#1}{#2}}
\newcommand*{\changeglossnum}[2][\jobname]{%
  \@namedef{memglsn#1}{#2}}
\newcommand*{\changeglossnumformat}[2][\jobname]{%
  \@namedef{memglsnf#1}{#2}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\glossitem}
% The final sorted glossary data is in the form: \\
% \cs{glossitem}\marg{term}\marg{desc}\marg{ref}\marg{num} \\
% The definition here does little.
%    \begin{macrocode}
\newcommand{\glossitem}[4]{#1 #2 #3 #4\par}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\memgloterm}
% \begin{macro}{\memglodesc}
% \begin{macro}{\memgloref}
% \begin{macro}{\memglonum}
% These are wrappers round the \meta{term}, \meta{desc}, \meta{ref} and
% \meta{num} elements in the final data. They are vacuous to start with.
%    \begin{macrocode}
\newcommand*{\memgloterm}[1]{#1}
\newcommand*{\memglodesc}[1]{#1}
\newcommand*{\memgloref}[1]{#1}
\newcommand*{\memglonum}[1]{#1}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\printglossary}
% \cs{printglossary}\oarg{file} inputs the glossary \file{gst} file.
%    \begin{macrocode}
\newcommand*{\printglossary}[1][\jobname]{\@input@{#1.gls}}

%    \end{macrocode}
% \end{macro}
%
%
%
%      
% \section{Notes}
%
%    
% \subsection{Marginpars}
%
% A |\marginpar| is a kind of floating object --- you can't control
% exactly where it will go. There is one problem with the kernel
% definition of |\marginpar| in that sometimes a |\marginpar| may
% end up on the wrong side of the page. The following is an attempt
% to fix that using the odd/even page check provided as part of
% the class.
% 
% \begin{macro}{\@addmarginpar}
% The part of the code for |\marginpar| that deals with deciding which
% side of the page it should be printed on is |\@addmarginpar|, buried
% away in the |output| routine. A couple of minor changes are made
% to the kernel code. The first is at the beginning where I have added
% the |\checkoddpage| page checking code.
%    \begin{macrocode}
\def\@addmarginpar{%
    \checkoddpage
%    \end{macrocode}
% Continue with the kernel code.
%    \begin{macrocode}
    \@next\@marbox\@currlist{\@cons\@freelist\@marbox
    \@cons\@freelist\@currbox}\@latexbug\@tempcnta\@ne
    \if@twocolumn
        \if@firstcolumn \@tempcnta\m@ne \fi
    \else
      \if@mparswitch
%    \end{macrocode}
% The next line, reading \\
% |\ifodd\c@page \else\@tempcnta\m@ne \fi| \\
% is where the odd/even page checking is done in the kernel code. I
% replace it with my code, and then continue with the kernel.
%    \begin{macrocode}
        \ifoddpage \else \@tempcnta\m@ne \fi
      \fi
    \if@reversemargin \@tempcnta -\@tempcnta \fi
    \fi
    \ifnum\@tempcnta <\z@  \global\setbox\@marbox\box\@currbox \fi
    \@tempdima\@mparbottom
    \advance\@tempdima -\@pageht
    \advance\@tempdima\ht\@marbox
    \ifdim\@tempdima >\z@
%    \end{macrocode}
% The next line in the kernel reads: \\
% |\@latex@warning@no@line {Marginpar on page \thepage\space moved}| \\
% I have changed the warning message to give the user an indication
% of the severity of the move. Then follow the kernel on to the end.
%    \begin{macrocode}
      \@latex@warning@no@line {Marginpar on page 
                               \thepage\space moved by \the\@tempdima}%
    \else
      \@tempdima\z@
    \fi
    \global\@mparbottom\@pageht
    \global\advance\@mparbottom\@tempdima
    \global\advance\@mparbottom\dp\@marbox
    \global\advance\@mparbottom\marginparpush
    \advance\@tempdima -\ht\@marbox
    \global\setbox \@marbox
                   \vbox {\vskip \@tempdima
                          \box \@marbox}%
    \global \ht\@marbox \z@
    \global \dp\@marbox \z@
    \kern -\@pagedp
    \nointerlineskip
    \hb@xt@\columnwidth
      {\ifnum \@tempcnta >\z@
          \hskip\columnwidth \hskip\marginparsep
       \else
          \hskip -\marginparsep \hskip -\marginparwidth
       \fi
       \box\@marbox \hss}%
    \nointerlineskip
    \hbox{\vrule \@height\z@ \@width\z@ \@depth\@pagedp}%
}

%    \end{macrocode}
% \end{macro}
%
% \subsection{A fixed marginpar}
%
%
% Introduce a non-floating marginpar.
% \changes{v1.4}{2003/11/22}{Added \cs{sidepar} and supports for a fixed 
%                            marginpar (from patch v1.8)}
%
% \begin{macro}{\parnopar}
% From \theTeXbook{} Exercise 14.15. It creates an `invisible' end/start paragraph,
% and may be used for getting \TeX{} to try a pagebreak.
%    \begin{macrocode}
\newcommand{\parnopar}{\parfillskip=0pt\par\parskip=0pt\noindent}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sideparvshift}
% Vertical shift for sidepar to align with text line
%    \begin{macrocode}
\newlength{\sideparvshift}
  \setlength{\sideparvshift}{-2.08ex}% seems to work for all font sizes

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ifreversesidepar}
% \begin{macro}{\ifsideparswitch}
% Analogues of \cs{marginpar} controls.
%    \begin{macrocode}
\newif\ifreversesidepar
%  \reversesideparfalse
  \reversesidepartrue
\newif\ifsideparswitch
  \sideparswitchfalse
\if@twoside \sideparswitchtrue \fi

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\sidepar}
% \cs{sidepar}\oarg{left}\marg{right} (per \cs{marginpar})
%    \begin{macrocode}
\newcommand{\sidepar}{\@dblarg{\@sidepar}}
\long\def\@sidepar[#1]#2{\@bsphack\leavevmode\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 % at left
      \else               % put at right
        \kern \columnwidth \kern \marginparsep % at right
      \fi
    \else
      \ifoddpage            % put at right
        \kern \textwidth \kern \marginparsep        % at right
      \else                 % put at left
        \kern -\marginparsep \kern -\marginparwidth % at left
      \fi
    \fi
    \vbox to 0pt{%
     \kern \sideparvshift%  % vertical shift to align top text lines
     \begin{minipage}{\marginparwidth}%
      \ifoddpage #2\else #1\fi%
      \end{minipage}%
  \vss}}}\@esphack}

%    \end{macrocode}
% \end{macro}
%
%
% \subsection{Sidebars}
%
% \changes{v1.1}{2002/03/19}{Added experimental code for sidebars}
%
% On 2002/10/22 Donald Arseneau posted the following code to \ctt{} for adding
% sidebars to plain TeX.
% \begin{verbatim}
% \newinsert\sideins
% \skip\sideins=0pt
% \count\sideins=0
% \dimen\sideins=2in
% 
% \def\sidebarvsep{25pt}
% \def\sidebarhsep{15pt}
% 
% \let\mainpagecontents\pagecontents
% 
% \def\sidecontents{%
%   \ifvoid\sideins\else
%       {\advance\hsize\sidebarhsep
%        \moveright\hsize \vtop to0pt{%
%        \vskip-\sidebarvsep \vskip\topskip % offset by difference
%        \unvbox\sideins \vss}%
%     }\fi}
% \def\pagecontents{\sidecontents\mainpagecontents}
% \long\def\sidebar#1{
%   \insert\sideins{%
%     \splittopskip\sidebarvsep\relax
%     \hsize 1.5in \rightskip=0pt plus 20pt \it
%     \noindent \vbox to \sidebarvsep{}\ignorespaces #1%
%     \ifhmode \unskip\strut\fi \par
%    }%
% }
% 
% \end{verbatim}
%
% The following is my attempt to rewrite the above into a form suitable
% for LaTeX, and this class in particular.
%
% \begin{macro}{\sideins}
% Create a new insert called |\sideins|, which also creates a corresponding
% skip, count, dimen, and box. \\
% |\skip\sideins| is the extra vertical space to allow on the page for 
% the insert, \\
% |\count\sideins| is the magnification factor for page breaking, \\
% |\dimen\sideins| is the maximum insertion size (height) per page, \\
% and the inserted material will be in |\box\sideins| when a page is output.
%    \begin{macrocode}
\newinsert\sideins
  \skip\sideins=0pt
  \count\sideins=0

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\setsidebarheight}
% The macro |\setsidebarheight{|\meta{length}|}| sets the total
% height of sidebars on a page to \meta{length}.
%    \begin{macrocode}
\newcommand{\setsidebarheight}[1]{\dimen\sideins= #1}
  \setsidebarheight{20\onelineskip}

%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\sidebarhsep}
% \begin{macro}{\sidebarvsep}
% \begin{macro}{\sidebarwidth}
% The length |\sidebarhsep| is the gap between the typeblock and the sidebar.
% |\sidebarvsep| is the vertical gap between sidebars on a page.
% The maximum number of sidebar lines on a page is 
% |1 + (\dimen\sideins - \sidebarvsep)|, assuming |\dimen\sideins|
% is defined in terms of |\onelineskip|.
% The width of the sidebar is |\sidebarwidth|.
%    \begin{macrocode}
\newlength{\sidebarhsep} \setlength{\sidebarhsep}{\marginparsep}
\newlength{\sidebarvsep} \setlength{\sidebarvsep}{2\onelineskip}
\newlength{\sidebarwidth} \setlength{\sidebarwidth}{\marginparwidth}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\sidebarfont}
% The font for typesetting the contents of a sidebar.
%    \begin{macrocode}
\newcommand{\sidebarfont}{\normalfont}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sidebarform}
% |\sidebarform| sets the `raggedness' in a sidebar. The current definition
% is less ragged than |\raggedright|. Examples: 
% \begin{verbatim}
%  for flushleftright \renewcommand{\sidebarform}{}
%  for raggedleft     \renewcommand{\sidebarform}{\raggedleft}
% \end{verbatim}
% \changes{v1.4}{2003/11/22}{Added \cs{sidebarform} (from patch v1.5)}
%    \begin{macrocode}
\newcommand{\sidebarform}{\rightskip=\z@ \@plus 2em}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ifsidebaroneside}
% Flag for oneside.
% \changes{v1.61}{2004/04/05}{Added \cs{ifsidebaroneside}}
%    \begin{macrocode}
\newif\ifsidebaroneside
  \if@twoside\sidebaronesidefalse\else\sidebaronesidetrue\fi

%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\sidebarmargin}
% \begin{macro}{\m@msidebar@margin}
% \cs{sidebarmargin}\marg{side} sets the side where sidebars will be printed.
% Valid arguments are: left, right, outer, or inner.
% The command is only effective for onecolumn documents with 
% \verb?\sidebaronesidefalse?
% (\verb?\sidebarmargin{right}? is equivalent to \verb?\sidebaronesidetrue?).
%
% \changes{v1.618}{2005/09/03}{Added \cs{sidebarmargin} (mempatch v3.2)}
%    \begin{macrocode}
\newcommand*{\sidebarmargin}[1]{%
  \def\@tempa{#1}\def\@tempb{left}%
  \ifx\@tempa\@tempb
    \gdef\m@msidebar@margin{0}%
  \else
    \def\@tempb{right}%
    \ifx\@tempa\@tempb
      \gdef\m@msidebar@margin{1}%
    \else
      \def\@tempb{outer}%
      \ifx\@tempa\@tempb
        \gdef\m@msidebar@margin{2}%
      \else
        \def\@tempb{inner}%
        \ifx\@tempa\@tempb
          \gdef\m@msidebar@margin{3}%
        \else
          \@memwarn{Bad \string\sidebarmargin\space argument}%
          \gdef\m@msidebar@margin{1}%
        \fi
       \fi
     \fi
  \fi}
%%%% default outer
\gdef\m@msidebar@margin{2}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\sidecontents}
% This locates the horizontal position of a sidebar. On two column
% pages it is put in the margin next to the column where |\sidebar|
% is called, otherwise it is put in the outer (foredge) margin, except
% for the oneside option when it is put into the right hand margin.
% This can be separately controlled via \verb?\ifsidebaroneside?.
% Emanuele Vicentini\footnote{\texttt{emanuelevicentini@yahoo.it},
% private email, 2004/04/05}
% suggested to code for the oneside option.
% \changes{v1.61}{2004/04/05}{Changed \cs{sidecontents} for oneside option} 
% \changes{v1.618}{2005/09/03}{Changed \cs{sidecontents} for \cs{sidebarmargin}}
%    \begin{macrocode}
\newcommand{\sidecontents}{{%
  \if@twocolumn%    %% put outside nearest column
    \if@firstcolumn%  %% move to left
      \setlength{\@tempdimc}{\sidebarwidth}%
      \advance\@tempdimc\sidebarhsep
      \moveleft\@tempdimc
    \else%            %% move to right
      \setlength{\@tempdimc}{\columnwidth}%
      \advance\@tempdimc\sidebarhsep
      \moveright\@tempdimc
    \fi
  \else%            %% put into foremargin?
    \ifsidebaroneside%  %% put into right hand margin
      \setlength{\@tempdimc}{\hsize}%
      \advance\@tempdimc\sidebarhsep
      \moveright\@tempdimc
    \else%              %% ???
      \ifcase\m@msidebar@margin%   0 to left
        \setlength{\@tempdimc}{\sidebarwidth}%
        \advance\@tempdimc\sidebarhsep
        \moveleft\@tempdimc
      \or%                         1 to right
        \setlength{\@tempdimc}{\hsize}%
        \advance\@tempdimc\sidebarhsep
        \moveright\@tempdimc
      \or%                         2 to outer
        \ifodd\c@page%  %% move to right
          \setlength{\@tempdimc}{\hsize}%
          \advance\@tempdimc\sidebarhsep
          \moveright\@tempdimc 
        \else%          %% move to left
          \setlength{\@tempdimc}{\sidebarwidth}%
          \advance\@tempdimc\sidebarhsep
          \moveleft\@tempdimc
        \fi
      \or%                        3 to inner
        \ifodd\c@page%  %% move to left
          \setlength{\@tempdimc}{\sidebarwidth}%
          \advance\@tempdimc\sidebarhsep
          \moveleft\@tempdimc
        \else%          %% move to right
          \setlength{\@tempdimc}{\hsize}%
          \advance\@tempdimc\sidebarhsep
          \moveright\@tempdimc 
        \fi
      \fi
    \fi
  \fi
  \vtop to0pt{%
    \vskip\onelineskip
    \unvbox\sideins \vss}%
}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sidebar}
% |\sidebar{|\meta{text}|}| puts \meta{text} into a sidebar.
%
% Florence Henry (\texttt{florence.henry@obspm.fr}) in the thread 
% \textit{Of memoir, sidebar, and justification}, 2003/04/02, pointed
% out that |itemize| in a |\sidebar| could overflow. DA gave the
% solution hint.
% \changes{v1.4}{2003/11/22}{Revised \cs{sidebar}}
%    \begin{macrocode}
\newcommand{\sidebar}[1]{%
  \insert\sideins{%
    \splittopskip\sidebarvsep\relax
    \hsize \sidebarwidth 
    \@parboxrestore                % <- added in v1.4
    \sidebarform \sidebarfont
    \noindent\vbox to \sidebarvsep{}\ignorespaces #1%
    \ifhmode\unskip\strut\fi\par}}

%    \end{macrocode}
% \end{macro}
%
% Now for the nasty bits which involve modifying the LaTeX output
% routine --- always a dangerous thing to do!
%
% \begin{macro}{\memold@makecol}
% \begin{macro}{\mem@makecol}
% A modified version of the kernel |\@makecol|.
%    \begin{macrocode}
\let\memold@makecol\@makecol
\gdef\mem@makecol{%
  \ifvoid\footins
    \setbox\@outputbox \box\@cclv
  \else
    \setbox\@outputbox \vbox{%
    \boxmaxdepth\@maxdepth
    \@tempdima\dp\@cclv
    \unvbox\@cclv
    \vskip-\@tempdima
    \vskip \skip\footins
    \color@begingroup
      \normalcolor
      \footnoterule
      \unvbox \footins
    \color@endgroup
    }
  \fi
  \ifvoid\sideins\else    %%% start of change
    \setbox\@outputbox \vbox{%
      \sidecontents
      \unvbox\@outputbox
    }
  \fi                     %%% end of change
  \xdef\@freelist{\@freelist\@midlist}%
  \global\let\@midlist\@empty
  \@combinefloats
  \ifvbox\@kludgeins
    \@makespecialcolbox
  \else
    \setbox\@outputbox \vbox to\@colht{%
      \boxmaxdepth \maxdepth
      \@texttop
      \dimen@ \dp\@outputbox
      \unvbox \@outputbox
      \vskip -\dimen@
      \@textbottom
    }%
  \fi
  \global \maxdepth \@maxdepth
}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\if@mem@nofoot}
% \begin{macro}{\@mem@testifnofoot}
% \begin{macro}{\@mem@extranofoot}
% These are from [RS] as \verb?\@doclearpage? should check for more than
% just \verb?\footins? being void. Note that this covers all current
% class defined inserts, including sidebars.
% \changes{v1.61}{2004/03/14}{Added \cs{if@mem@nofoot}, 
%                              \cs{@mem@testifnofoot}, and
%                              \cs{@mem@extranofoot}}
%    \begin{macrocode}
\newif\if@mem@nofoot
\newcommand*{\@mem@testifnofoot}{%
  \@mem@nofoottrue
  \ifvoid\footins\else\@mem@nofootfalse\fi
  \ifvoid\footinsv@r\else\@mem@nofootfalse\fi
  \ifvoid\sideins\else\@mem@nofootfalse\fi
  \@mem@extranofeet}
\newcommand*{\@mem@extranofeet}{}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\memold@doclearpage}
% \begin{macro}{\mem@doclearpage}
% I thought that I could get away with using the kernel's 
% \verb?\@doclearpage? but [RS] discovered that I couldn't.
% \changes{v1.62}{2004/03/14}{Redid \cs{@doclearpage} using [RS] code}
%    \begin{macrocode}
\let\memold@doclearpage\@doclearpage
\newcommand{\mem@doclearpage}{%
%%%%  \typeout{Redefining @doclearpage}
  \@mem@testifnofoot
  \if@mem@nofoot
    \setbox\@tempboxa\vsplit\@cclv to\z@ \unvbox\@tempboxa
    \setbox\@tempboxa\box\@cclv
    \xdef\@deferlist{\@toplist\@botlist\@deferlist}%
    \global\let\@toplist\@empty
    \global\let\@botlist\@empty
    \global\@colroom\@colht
    \ifx \@currlist\@empty
    \else
      \@latexerr{Float(s) lost}\@ehb
      \global\let\@currlist\@empty
    \fi
    \@makefcolumn\@deferlist
    \@whilesw\if@fcolmade \fi{\@opcol\@makefcolumn\@deferlist}%
    \if@twocolumn
      \if@firstcolumn
        \xdef\@dbldeferlist{\@dbltoplist\@dbldeferlist}%
        \global\let\@dbltoplist\@empty
        \global\@colht\textheight
        \begingroup
          \@dblfloatplacement
          \@makefcolumn\@dbldeferlist
          \@whilesw\if@fcolmade \fi{\@outputpage
                                    \@makefcolumn\@dbldeferlist}%
        \endgroup
      \else
        \vbox{}\clearpage
      \fi
    \fi
  \else
    \setbox\@cclv\vbox{\box\@cclv\vfil}%
    \@makecol\@opcol
    \clearpage
  \fi
}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\memold@reinserts}
% \begin{macro}{\mem@reinserts}
% Almost a copy of the kernel |\@reinserts|.
%    \begin{macrocode}
\let\memold@reinserts\@reinserts
\gdef\mem@reinserts{%
  \ifvoid\footins\else\insert\footins{\unvbox\footins}\fi
  \ifvbox\@kludgeins\insert\@kludegins{\unvbox\@kludgeins}\fi
  \ifvoid\sideins\else\sidecontents\fi
}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@makecol}
% \begin{macro}{\@doclearpage}
% Replace the kernel version of |\@makecol| and \verb?\@doclearpage?
% with the memoir versions.
%    \begin{macrocode}
\gdef\@makecol{\mem@makecol}
\gdef\@doclearpage{\mem@doclearpage}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% I think that the original kernel version of |\@reinserts| 
% might be alright, but you never know. If it is not, 
% then we can treat it like |\@makecol|, for example.
%
%
% \subsection{Footnotes}
%
%    For my reference, in case I want to fiddle with it even more
% than I have already done so, here is the kernel code (from \file{ltfloat.dtx}) 
% for footnotes.
% \DescribeMacro{\footins}
% The insert for footnotes.
% \begin{verbatim}
% \newinsert\footins
%   \skip\footins=\bigskipamount
%   \count\footins=1000
%   \dimen\footins=8in
% \end{verbatim}
%
% \DescribeMacro{\footnoterule}
% Draws the rule separating footnotes from the main text. It is executed
% after a \cs{vspace} of \verb?\skip\footins? and should take no vertical
% space.
% \begin{verbatim}
% \def\footnoterule{\kern-3\p@ \hrule \@width 2in \kern 2.6\p@}
%
% \@definecounter{footnote}
%   \def\thefootnote{\@arabic\c@footnote}
% \@definecounter{mpfootnote}
%   \def\thempfootnote{\itshape\@alph\c@mpfootnote}
% \end{verbatim}
%
% \DescribeMacro{\@makefnmark}
% Generates the footnote marker from \cs{@thefnmark}.
% \begin{verbatim}
% \def\@makefnmark{\hbox{\@textsuperscript{\normalfont\@thefnmark}}}
%
% \DeclareRobustCommand*\textsuperscript[1]{%
%    \@textsuperscript{\selectfont#1}}
% \def\@textsuperscript#1{%
%    {\m@th\ensuremath{^{\mbox{\fontsize\sf@size\z@#1}}}}}
%
% \newdimen\footnotesep
% \end{verbatim}
%
% \DescribeMacro{\footnote}
% \cs{footnote}\oarg{num}\marg{text} is what the user uses for footnoting.
% It defines \cs{@thefnmark} and calls \cs{@footnotemark} and 
% \cs{@footnotetext} to do the work.
% \begin{verbatim}
% \def\footnote{\@ifnextchar[\@xfootnote{\stepcounter\@mpfn
%   \protected@xdef\@thefnmark{\thempfn}%
%   \@footnotemark\@footnotetext}}
% \end{verbatim}
%
% \DescribeMacro{\@xfootnote}
% Handles the optional \meta{num} argument to \cs{footnote}.
% It defines \cs{@thefnmark} and calls \cs{@footnotemark} and 
% \cs{@footnotetext} to do the work.
% \begin{verbatim}
% \def\@xfootnote[#1]{%
%   \begingroup
%     \csname c@\@mpfn\endcsname #1\relax
%     \unrestored@protected@xdef\@thefnmark{\thempfn}%
%   \endgroup
%   \@footnotemark\@footnotetext}
% \end{verbatim}
%
% \DescribeMacro{\@footnotetext}
% \cs{@footnotetext}\marg{text} sets up for typeseting the footnote 
% (at the bottom of the page). It calls \cs{@makefntext}\marg{text}
% to actually do the typesetting (\cs{@makefntext} has to be supplied
% by a class or package).
% \begin{verbatim}
% \long\def\@footnotetext#1{%
%   \insert\footins{%
%     \reset@font\footnotesize
%     \interlinepenalty\interfootnotelinepenalty
%     \splittopskip\footnotesep
%     \splitmaxdepth \dp\strutbox \floatingpenalty \@MM
%     \hsize\columnwidth \@parboxrestore
%     \protected@edef\@currentlabel{%
%       \csname p@footnote\endcsname\@thefnmark}%
%     \color@begingroup
%       \@makefntext{%
%         \rule\z@\footnotesep\ignorespaces#1\@finalstrut\strutbox}%
%     \color@endgroup}}
% \end{verbatim}
%
% \DescribeMacro{\footnotemark}
% \cs{footnotemark}\oarg{num} the user uses this to produce just a 
% footnote mark in the text.
% \begin{verbatim}
% \def\footnotemark{%
%    \@ifnextchar[\@xfootnotemark
%      {\stepcounter{footnote}%
%       \protected@xdef\@thefnmark{\thefootnote}%
%       \@footnotemark}}
% \end{verbatim}
%
% \DescribeMacro{\@xfootnotemark}
% Handles the optional \meta{num} argument to \cs{footnotemark}.
% \begin{verbatim}
% \def\@xfootnotemark[#1]{%
%    \begingroup
%      \c@footnote #1\relax
%      \unrestored@protected@xdef\@thefnmark{\thefootnote}%
%    \endgroup
%    \@footnotemark}
% \end{verbatim}
%
% \DescribeMacro{\@footnotemark}
% Typesets the mark in the main text, via \cs{@makefnmark}.
% \begin{verbatim}
% \def\@footnotemark{%
%    \leavevmode
%    \ifhmode\edef\@x@sf{\the\spacefactor}\nobreak\fi
%    \@makefnmark
%    \ifhmode\spacefactor\@x@sf\fi
%    \relax}
% \end{verbatim}
%
% \DescribeMacro{\footnotetext}
% \cs{footnotetext}\oarg{num}\marg{text} is user view for creating a footnote
% without a marker in the main text.
% \begin{verbatim}
% \def\footnotetext{%
%    \@ifnextchar [\@xfootnotenext
%       {\protected@xdef\@thefnmark{\thempfn}%
%       \@footnotetext}}
% \end{verbatim}
%
% \DescribeMacro{\@xfootnotetext}
% Handles the optional \meta{num} argument to \cs{footnotetext}.
% \begin{verbatim}
% \def\@xfootnotenext[#1]{%
%   \begingroup
%     \csname c@\@mpfn\endcsname #1\relax
%     \unrestored@protected@xdef\@thefnmark{\thempfn}%
%   \endgroup
%   \@footnotetext}
% \end{verbatim}
%
% \DescribeMacro{\@mpfn}
% \DescribeMacro{\thempfn}
% \begin{verbatim}
% \def\@mpfn{footnote}
% \def\thempfn{\thefootnote}
% \end{verbatim}
%
% And from \file{ltboxes.dtx} for the |minipage| environment. 
%
% \DescribeMacro{\@mpfn}
% \DescribeMacro{\thempfn}
% \DescribeMacro{\@footnotetext}
% When setting up
% the environment, it includes:
% \begin{verbatim}
% \def\@mpfn{mpfootnote}\def\thempfn{\thempfootnote}\c@mpfootnote\z@
% \let\@footnotetext\@mpfootnotetext
% \end{verbatim}
%
% \DescribeMacro{\@mpfootnotetext}
% Later it defines \cs{@mpfootnotetext}\marg{text} as the minipage version
% of \cs{@footnotetext}.
% \begin{verbatim}
% \long\def\@mpfootnotetext#1{%
%   \global\setbox\@mpfootins\vbox{%
%     \unvbox\@mpfootins
%     \reset@font\footnotesize
%     \hsize\columnwidth
%     \@parboxrestore
%     \protected@edef\@currentlabel{%
%       \csname p@mpfootnote\endcsname\@thefnmark}%
%     \color@begingroup
%       \@makefntext{%
%         \rule\z@\footnotesep\ignorespaces#1\@finalstrut\strutbox}%
%     \color@endgroup}}
% \end{verbatim}
%
% That completes the kernel extracts.
%
% \begin{macro}{\footnoterule}
%    Usually, footnotes are separated from the main body of the text
%    by a small rule. This rule is drawn by the macro |\footnoterule|.
%    We have to make sure that the rule takes no vertical space (see
%    \file{plain.tex}) so we compensate for the natural heigth of the
%    rule of 0.4pt by adding the right amount of vertical skip.
%
%    To prevent the rule from colliding with the footnote we first add
%    a little negative vertical skip, then we put the rule and make
%    sure we end up at the same point where we begun this operation.
%    \begin{macrocode}
\renewcommand{\footnoterule}{%
  \kern-3\p@
  \hrule width .4\columnwidth
  \kern 2.6\p@}
%    \end{macrocode}
% And just to make sure that we are using memoir's font-dependent
% skips:
%    \begin{macrocode}
\skip\footins=\bigskipamount
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\c@footnote}
%    Footnotes are numbered per chapter. The counter is predefined.
%    \begin{macrocode}
\@addtoreset{footnote}{chapter}
%    \end{macrocode}
% \end{macro}
%
%    The footnote mechanism of \LaTeX{} calls the macro |\@makefntext|
%    to produce the actual footnote. The macro gets the text of the
%    footnote as its argument and should use |\@thefnmark| as the mark
%    of the footnote. The macro |\@makefntext| is called when
%    effectively inside a |\parbox| of width |\columnwidth| (i.e.,
%    with |\hsize| = |\columnwidth|).
%
%   An example of what can be achieved is given by the following piece
%   of \TeX{} code.
% \begin{verbatim}
%          \long\def\@makefntext#1{%
%             \@setpar{\@@par
%                      \@tempdima = \hsize
%                      \advance\@tempdima-10pt
%                      \parshape \@ne 10pt \@tempdima}%
%             \par
%             \parindent 1em\noindent
%             \hbox to \z@{\hss\@makefnmark}#1}
% \end{verbatim}
%    The effect of this definition is that all lines of the footnote
%    are indented by 10pt, while the first line of a new paragraph is
%    indented by 1em. To change these dimensions, just substitute the
%    desired value for `10pt' (in both places) or `1em'.  The mark is
%    flushright against the footnote.
%
%    The standard classes use a simpler macro, in which the
%    footnote text is set like an ordinary text paragraph, with no
%    indentation except on the first line of a paragraph, and the
%    first line of the footnote. Thus, all the macro must do is set
%    |\parindent| to the appropriate value for succeeding paragraphs
%    and put the proper indentation before the mark.
% \begin{verbatim}
% \newcommand{\@makefntext}[1]{%
%     \parindent 1em%
%     \noindent
%     \hb@xt@1.8em{\hss\@makefnmark}#1}
% \end{verbatim}
%
% This class provides a configurable specification.
%
% Normally, if two or more footnotes are sequentially applied: \\
% |...text\footnote{first}\footnote{second}...| \\
% the markers in the text run together 
% (e.g., \ldots text\textsuperscript{12}). 
% The class provides a separator between the markers
% (e.g., \ldots text\textsuperscript{1,2}).
% The underlying ideas for this are from the \Lpack{ledmac} 
% package~\cite{LEDMAC},
% which in turn got them from the \Lpack{footmisc} package~\cite{FOOTMISC}.
% \changes{v1.4}{2003/11/22}{Added footnote separator (from patch v1.5)}
%
% \begin{macro}{\multfootsep}
% The separator between multiple footnote markers.
%    \begin{macrocode}
\newcommand*{\multfootsep}{\textsuperscript{\normalfont,}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\multiplefootnotemarker}
% \begin{macro}{\m@mmf@prepare}
% \cs{m@mmf@prepare} is a pair of self-cancelling kerns.
%    \begin{macrocode}
\newcommand*{\multiplefootnotemarker}{3sp}
\newcommand*{\m@mmf@prepare}{%
  \kern-\multiplefootnotemarker
  \kern\multiplefootnotemarker\relax}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\m@mmf@check}
% If \cs{m@mmf@check} recognises the last kern as \cs{multiplefootnotemarker}
% it typesets \cs{multfootsep}.
%    \begin{macrocode}
\newcommand*{\m@mmf@check}{%
  \ifdim\lastkern=\multiplefootnotemarker\relax
    \edef\@x@sf{\the\spacefactor}%
    \unkern
    \multfootsep
    \spacefactor\@x@sf\relax
  \fi}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@footnotetext}
% \begin{macro}{\@footnotemark}
% We have to modify the kernel's \cs{@footnotetext} and \cs{@footnotemark} 
% to implement the separator.
% \changes{v1.618}{2005/09/03}{Stop body font changes leaking into footnotes (mempatch v3.7)}
%    \begin{macrocode}
\let\m@mold@footnotetext\@footnotetext
\renewcommand{\@footnotetext}[1]{%
  \rest@font\m@mold@footnotetext{#1}\m@mmf@prepare}
\renewcommand*{\@footnotemark}{%
  \leavevmode
  \ifhmode
    \edef\@x@sf{\the\spacefactor}%
    \m@mmf@check
    \nobreak
  \fi
  \@makefnmark
  \m@mmf@prepare
  \ifhmode\spacefactor\@x@sf\fi
  \relax}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\footmarkwidth}
% \begin{macro}{\footmarksep}
% \begin{macro}{\footparindent}
% The mark is typset right justified in a box with width |\footmarkwidth|.
% Second and later lines of the text are offset |\footmarksep| from the
% end of the box. Paragraphs in footnotes are indented by |\parindent|.
%    \begin{macrocode}
\newlength{\footmarkwidth}
\newlength{\footmarksep}
\newlength{\footparindent}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\footmarkstyle}
% \begin{macro}{\footscript}
% The marker is typeset according to |\footscript|\marg{marker}. This
% can be specified by the user via |\footmarkstyle|.
%    \begin{macrocode}
\newcommand*{\footmarkstyle}[1]{\def\footscript##1{#1}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\makefootmarkhook}
% A vacuous macro that the user can redefine to do something useful?
%    \begin{macrocode}
\newcommand{\makefootmarkhook}{}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\footfootmark}
% This macro typesets the footnote marker.
%    \begin{macrocode}
\newcommand{\footfootmark}{%
  \ifdim\footmarkwidth < \z@
%    \end{macrocode}
% Negative width, mark is in the margin.
%    \begin{macrocode}
    \llap{\hb@xt@ -\footmarkwidth{%
            \hss\normalfont\footscript{\@thefnmark}}%
          \hspace*{-\footmarkwidth}}%
  \else
    \ifdim\footmarkwidth = \z@
%    \end{macrocode}
% Zero width, mark is at (inside) the margin.
%    \begin{macrocode}
      {\normalfont\footscript{\@thefnmark}}%
    \else
%    \end{macrocode}
% Positive width.
%    \begin{macrocode}
      \hb@xt@\footmarkwidth{\hss\normalfont\footscript{\@thefnmark}}%
    \fi
  \fi}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\makefootmark}
% \begin{macro}{\@makefntext}
% The class version of |\@makefntext|.
%    \begin{macrocode}
\newcommand{\makefootmark}[1]{%
  \leavevmode
  \parindent \footparindent\noindent
  \leftskip\footmarksep\relax
  \advance\leftskip \footmarkwidth \null\nobreak\hskip -\leftskip\relax
  \makefootmarkhook\relax
  \footfootmark #1}
%    \end{macrocode}
% In the thread \textit{memoir and footnote.sty trouble}, 2003/04/02,
% Patrik Nyman (\texttt{patrik.nyman@orient.su.se}) noted that in order
% to stop \file{footnote.sty} (from \texttt{mdwtools}) barfing my original \\
% |\newcommand{\@makefntext}[1]{\makefootmark{#1}}| \\
% had to be changed.
% \changes{v1.4}{2004/11/22}{Revised \cs{@makefntext}}
%    \begin{macrocode}
\newcommand{\@makefntext}[1]{\makefootmark #1}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% All that now remains for the footer part is to set the defaults.
%    \begin{macrocode}
\footmarkstyle{\textsuperscript{#1}}
\setlength{\footmarkwidth}{1.8em}
\setlength{\footmarksep}{-1.8em}
\setlength{\footparindent}{1em}

%    \end{macrocode}
%
% \begin{macro}{\@makefnmark}
%    The footnote markers that are printed in the text to point to the
%    footnotes should be produced by the macro |\@makefnmark|. We use
%    the default definition for it.
%    \begin{macrocode}
%\renewcommand\@makefnmark{\hbox{\@textsuperscript{%
%                                \normalfont\@thefnmark}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\footref}
%    Sometimes it is desireable to reference a footnote more than
% once.
%    If a footnote is labelled (e.g., |\footnote{text\label{fn}}|) then
% the macro |\footref|\marg{fn} will print the footnote marker.
% The code is essentially a much simplified version of |\footnotemark|
% from \file{ltfloat.dtx}.
% \changes{v1.0}{2001/10/30}{Added \cs{footref}}
%    \begin{macrocode}
\newcommand{\footref}[1]{%
  \begingroup
    \unrestored@protected@xdef\@thefnmark{\ref{#1}}%
  \endgroup
  \@footnotemark}

%    \end{macrocode}
% \end{macro}
%
% The following code for footnotes that can include verbatims is based
% on Jeremy Gibbons \textit{`Footnotes with verbatim material'}
% in the column \textit{`Hey --- it works'} (edited by Jeremy Gibbons), 
% TeX and TUG NEWS, vol 2 no. 4, p 9, October 1993. I have updated
% the basic code so that it works for LaTeX2e.
%
% \begin{macro}{\verbfootnote}
% \begin{macro}{\@xverbfootnote}
% |\verbfootnote| is just like |\footnote| except that it can contain
% verbatim material.
%    \begin{macrocode}
\def\verbfootnote{\@ifnextchar[\@xverbfootnote{\stepcounter\@mpfn
  \protected@xdef\@thefnmark{\thempfn}%
  \@footnotemark\@verbfootnotetext}}

\def\@xverbfootnote[#1]{%
  \begingroup
    \csname c@\@mpfn\endcsname #1\relax
    \unrestored@protected@xdef\@thefnmark{\thempfn}%
  \endgroup
  \@footnotemark\@verbfootnotetext}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@verbfootnotetext}
% This is the secret ingredient. It is based on the \cs{footnote} macro
% in \theTeXbook{} page 363, which somehow manages to read an argument 
% with verbatims. As Knuth says that it is subtle and involves trickery,
% don't expect me to even try to explain anything. 
%
% I'm not at all sure about the color bits, though!
% \changes{v1.618}{2005/09/02}{Fixed colors in \cs{verbfootnote} (mempatch v2.3)}
%    \begin{macrocode}
\long\def\@verbfootnotetext{%
  \insert\footins\bgroup
    \footnotesize
    \interlinepenalty\interfootnotelinepenalty
    \splittopskip\footnotesep
    \splitmaxdepth \dp\strutbox \floatingpenalty \@MM
    \hsize\columnwidth \@parboxrestore
    \edef\@currentlabel{\csname p@footnote\endcsname\@thefnmark}%
%%%%    \color@begingroup    
    \@makefntext{\rule{\z@}{\footnotesep}\ignorespaces}%
    \futurelet\next\fo@t
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\fo@t}
% \begin{macro}{\f@@t}
% \begin{macro}{\f@t}
% \begin{macro}{\@foot}
% Guess what these do.
%    \begin{macrocode}
\def\fo@t{\ifcat\bgroup\noexpand\next \let\next\f@@t
          \else \let\next\f@t\fi \next}
\def\f@@t{\bgroup\aftergroup\@foot\let\next}
\def\f@t#1{%
  \color@begingroup
  #1\@foot
  \color@endgroup}
\def\@foot{%
  \strut\egroup
%%%%  \color@endgroup
  }

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@verbmpfootnotetext}
%    Footnotes in minipages are a little different, so another
% version of |\...footnotetext|. 
%    \begin{macrocode}
\long\def\@verbmpfootnotetext{%
  \global\setbox\@mpfootins\vbox{%
    \reset@font\footnotesize
    \unvbox\@mpfootins
    \bgroup
    \hsize\columnwidth
    \@parboxrestore
    \edef\@currentlabel{\csname p@mpfootnote\endcsname\@thefnmark}%
    \color@begingroup
    \@makefntext{\rule{\z@}{\footnotesep}\ignorespaces}%
    }
    \futurelet\next\fo@t
} 

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@minipagerestore}
% To get the |\verbfootnote| to work in a |minipage| we have to use
% |\@verbmpfootnotetext| instead of |\\@verbfootnotetext|. There is
% a nice hook in the kernel |minipage| code for this.
%    \begin{macrocode}
\def\@minipagerestore{\let\@verbfootnotetext\@verbmpfootnotetext}

%    \end{macrocode}
% \end{macro}                      
%
%
% \subsection{Major extensions for footnotes}
%
%    \begin{macrocode}
%%%%%%%%%%%%%% major extension to footnoting 

%    \end{macrocode}
%
% This all requires modifications to the output routine's \cs{@makecol}
%
% \begin{macro}{\m@m@makecolfloats}
% \begin{macro}{\m@m@makecoltext}
% These two macros contain code common to both versions of \cs{@makecol}
%    \begin{macrocode}
\newcommand{\m@m@makecolfloats}{%
  \xdef\@freelist{\@freelist\@midlist}%
  \global\let\@midlist\@empty
  \@combinefloats}
\newcommand{\m@m@makecoltext}{%
  \ifvbox\@kludgeins
    \@makespecialcolbox
  \else
    \setbox\@outputbox \vbox to\@colht{%
      \@texttop
      \dimen@ \dp\@outputbox
      \unvbox \@outputbox
      \vskip -\dimen@
      \@textbottom}%
  \fi}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\m@m@makecolintro}
% A hook into the revised \cs{@makecol}
%    \begin{macrocode}
\newcommand{\m@m@makecolintro}{}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\mem@makecol}
% A version \cs{@makecol} giving the standard footnote order (bottom
% floats after footnotes).
%    \begin{macrocode}
\gdef\mem@makecol{%
  \m@m@makecolintro
  \ifvoid\footins
    \setbox\@outputbox \box\@cclv
  \else
    \setbox\@outputbox \vbox{%
    \boxmaxdepth\@maxdepth
    \@tempdima\dp\@cclv
    \unvbox\@cclv
    \vskip \skip\footins
    \color@begingroup
      \normalcolor
      \footnoterule
      \unvbox \footins
    \color@endgroup
    }%
  \fi
  \m@mdoextrafeet
  \ifvoid\sideins\else
    \setbox\@outputbox \vbox{%
      \sidecontents
      \unvbox\@outputbox}%
  \fi
  \m@m@makecolfloats
  \m@m@makecoltext
  \global \maxdepth \@maxdepth}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\mem@makecolbf}
% A version of \cs{@makecol} which puts footnotes at the bottom of the page
% (after any bottom floats).
%    \begin{macrocode}
\gdef\mem@makecolbf{%
  \m@m@makecolintro
  \setbox\@outputbox \box\@cclv
  \m@m@makecolfloats
  \ifvoid\footins
  \else
    \setbox\@outputbox \vbox{%
    \boxmaxdepth\@maxdepth
    \unvbox\@outputbox
    \vskip \skip\footins
    \color@begingroup
      \normalcolor
      \footnoterule
      \unvbox \footins
    \color@endgroup
    }%
  \fi
  \m@mdoextrafeet
  \ifvoid\sideins\else
    \setbox\@outputbox \vbox{%
      \sidecontents
      \unvbox\@outputbox}%
  \fi
  \m@m@makecoltext
  \global \maxdepth \@maxdepth}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\feetabovefloat}
% \begin{macro}{\feetbelowfloat}
% declarations to put footnotes above bottom floats (standard \LaTeX)
% or at the bottom of the page
%    \begin{macrocode}
\newcommand{\feetabovefloat}{\gdef\@makecol{\mem@makecol}}
\newcommand{\feetbelowfloat}{\gdef\@makecol{\mem@makecolbf}}
\feetabovefloat

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% All sorts of extra kinds of footnotes.
%
% \begin{macro}{\m@mdoextrafeet}
% \begin{macro}{\extrafeetins}
% \begin{macro}{\extrafeetinshook}
% These macros are hooks into \cs{@makecol}.
% \changes{v1.618}{2005/09/02}{Fixed memoir/listings spacing problem (from Cartsten Heinz, mempatch v2.3)}
% \changes{v1.618}{2005/09/03}{Set \cs{boxmaxdepth} in \cs{extrafeetins} (mmempatch v3.1)}
%    \begin{macrocode}
\newcommand{\m@mdoextrafeet}{\extrafeetins}
\newcommand*{\extrafeetins}{%
  \setbox\@outputbox \vbox{%
    \boxmaxdepth \@maxdepth
    \unvbox\@outputbox
    \ifvoid\footinsv@r\else\@footstartv@r\@footgroupv@r\fi
    \extrafeetinshook}}
\newcommand{\extrafeetinshook}{}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@reinserts}
% \begin{macro}{\m@mdodoreinextrafeet}
% \begin{macro}{\extrafeetreinshook}
% Redefine \cs{@reinserts} with hooks for multiple footnote kinds.
%    \begin{macrocode}
\gdef \@reinserts{%
  \ifvoid\footins\else\insert\footins{\unvbox\footins}\fi
  \m@mdodoreinextrafeet
  \ifvoid\sideins\else\sidecontents\fi
  \ifvbox\@kludgeins\insert\@kludgeins{\unvbox\@kludgeins}\fi}
\newcommand{\m@mdodoreinextrafeet}{%
  \ifvoid\footinsv@r\else\insert\footinsv@r{\unvbox\footinsv@r}\fi
  \extrafeetreinshook}
\newcommand{\extrafeetreinshook}{}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
%
% \begin{macro}{\foottextfont}
% \begin{macro}{\footinsdim}
% \begin{macro}{\@preamfntext}
% General macros.
%    \begin{macrocode}
\newcommand{\foottextfont}{\footnotesize}
\newlength{\footinsdim}
  \setlength{\footinsdim}{8in}   % standard for \dimen\footins
\newcommand{\@preamfntext}{%
  \interlinepenalty\interfootnotelinepenalty
  \floatingpenalty \@MM
  \splittopskip=\footnotesep
  \splitmaxdepth=\dp\strutbox
  \@parboxrestore}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% We are going to change some of the kernel footnote macros
%
% \begin{macro}{\@footnotetext}
% \begin{macro}{\@mpfootnotetext}
% \begin{macro}{\m@mold@footnotetext}
% \begin{macro}{\m@mold@mpfootnotetext}
%    \begin{macrocode}
\renewcommand{\@footnotetext}[1]{\insert\footins{%
  \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
    \foottextfont \hsize\columnwidth \@parboxrestore
    \protected@edef\@currentlabel{%
      \csname p@mpfootnote\endcsname\@thefnmark}%
  \color@begingroup
    \@makefntext{%
      \rule\z@\footnotesep\ignorespaces#1\@finalstrut\strutbox}%
  \color@endgroup}}

%    \end{macrocode}
% Save our revised versions of \cs{@footnotetext} and \cs{@mpfootnotetext}
%    \begin{macrocode}
\let\m@mold@footnotetext\@footnotetext
\let\m@mold@mpfootnotetext\@mpfootnotetext

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% We also to patch the minipage environment. We can use 
% \cs{@minipagerestore} for the \texttt{begin} part, but have to 
% modify \cs{endminipage}
%
% \begin{macro}{\m@mdoextrafeetmini}
% \begin{macro}{\extrafeetminihook}
% \begin{macro}{\@minipagerestore}
%    \begin{macrocode}
\newcommand{\m@mdoextrafeetmini}{%
  \extrafeetminihook}
\newcommand{\extrafeetminihook}{}
\renewcommand{\@minipagerestore}{\m@mdoextrafeetmini}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\extrafeetendmini}
% \begin{macro}{\extrafeetendminihook}
%    \begin{macrocode}
\newcommand{\extrafeetendmini}{%
  \ifvoid\@mpfootinsv@r\else
    \vskip\skip\@mpfootins
    \normalcolor\footnoterule\mp@footgroupv@r
  \fi
  \extrafeetendminihook}
\newcommand{\extrafeetendminihook}{}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\m@mdoextrafeetendmini}
% \begin{macro}{\endminipage}
% This is our patched version of \cs{endminipage}.
%    \begin{macrocode}
\newcommand{\m@mdoextrafeetendmini}{\extrafeetendmini}
\def\endminipage{%
  \par
  \unskip
  \ifvoid\@mpfootins\else
    \vskip\skip\@mpfootins
    \normalcolor
    \footnoterule
    \unvbox\@mpfootins
  \fi
  \m@mdoextrafeetendmini
  \@minipagefalse
  \color@endgroup
  \egroup
  \expandafter\@iiiparbox\@mpargs{\unvbox\@tempboxa}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\plainfootnotes}
% The declaration for standard footnotes; easy, just use the saved
% versions of \cs{@footnotetext} and \cs{@mpfootnotetext}.
%    \begin{macrocode}
\newcommand{\plainfootnotes}{%
  \let\@footnotetext\m@mold@footnotetext
  \let\@mpfootnotetext\m@mold@mpfootnotetext}

%    \end{macrocode}
% \end{macro}
%
% Now for lots of somewhat tedious code, interspersed with complex
% `Dirty Tricks' type things.
%
%     Robert Schlicht~\footnote{Private email, 2004/03/12, 
% (\texttt{w.m.l@gmx.net})} [RS] has provided valuable help in tuning
% up the code.
% 
%
% \begin{macro}{\newfootnoteseries}
% \cs{newfootnoteseries}\marg{series} creates the set of macros required
% for footnote code (see the kernel code presented earlier).
% The created macros have \meta{series} appended to their name.
% \changes{v1.61}{2004/03/14}{Added skip setup to \cs{newfootnoteseries}}
% \changes{v1.61}{2004/03/24}{Use \cs{newcounter} instead of \cs{newcount}
%                 in \cs{newfootnoteseries}}
%    \begin{macrocode}
\newcommand{\newfootnoteseries}[1]{%
  \expandafter\newinsert\csname footins#1\endcsname%  -> \footins#1
  \expandafter\skip\csname footins#1\endcsname \bigskipamount%
%%%                                      - > \skip\footins#1 % [RS]
  \newcounter{footnote#1}%                       -> \c@footnote#1
  \@nameuse{c@footnote#1} \z@%                   -> \c@footnote#1=0
  \global\@namelet{p@footnote#1} \@empty%        -> \p@footnote#1 
  \@namedef{thefootnote#1}{\arabic{footnote#1}}% -> \thefootnote#1
  \@namedef{foottextfont#1}{\foottextfont}%      -> \foottextfont#1
  \m@makefootnote{#1}%                           -> \footnote#1
  \m@make@xfootnote{#1}%                         -> \@xfootnote#1
  \m@make@footnotetext{#1}%                      -> \@footnotetext#1
  \m@makefootnotemark{#1}%                       -> \footnotemark#1
  \m@make@xfootnotemark{#1}%                     -> \@xfootnotemark#1
  \m@make@footnotemark{#1}%                      -> \@footnotemark#1
  \m@makefootnotetext{#1}%                       -> \footnotetext#1
  \m@make@xfootnotenext{#1}%                     -> \@xfootnotenext#1
  \m@make@mpfn{#1}%                              -> \@mpfn#1
  \m@makethempfn{#1}%                            -> \thempfn#1
  \m@make@makefnmark{#1}%                        -> \@makefnmark#1
  \m@makefootref{#1}%                            -> \footref#1
  \m@makefootfootmark{#1}%                       -> \footfootmark#1
  \m@makemakefootmark{#1}%                       -> \makefootmark#1
  \m@makefootmarkstyle{#1}%                      -> \footmarkstyle#1
  \@namedef{@makefntext#1}##1{\@nameuse{makefootmark#1} ##1}%
  \m@make@footstart{#1}%                         -> \@footstart#1
  \m@make@footgroup{#1}%                         -> \@footgroup#1
  \expandafter\newinsert\csname @mpfootins#1\endcsname% -> \@mpfootins#1
  \newcounter{mpfootnote#1}%                     -> \c@mpfootnote#1
  \global\@namelet{p@mpfootnote#1}\@empty
  \@namedef{thempfootnote#1}{\itshape\alph{mpfootnote#1}}%
  \m@make@mpfootnotetext{#1}%                    -> \@mpfootnotetext#1
%    \end{macrocode}
% Reset the counter per chapter, except for articles.
% \changes{v1.6}{2004/03/14}{Added chapter reset for footnote series [RS]}
%    \begin{macrocode}
  \ifartopt\else%  [RS]
    \expandafter\@cons\csname cl@chapter\endcsname {{footnote#1}}%
  \fi
%    \end{macrocode}
% Add the footnote to the (re)insert hooks.
%    \begin{macrocode}
  \g@addto@macro{\extrafeetinshook}{%
    \ifvoid\@nameuse{footins#1}\else
      \@nameuse{@footstart#1}\@nameuse{@footgroup#1}\fi}
  \g@addto@macro{\extrafeetreinshook}{%
    \ifvoid\@nameuse{footins#1}\else
      \insert\@nameuse{footins#1}{\unvbox\@nameuse{footins#1}}\fi}
  \g@addto@macro{\extrafeetendminihook}{%
    \ifvoid\@nameuse{@mpfootins#1}\else
      \vskip\skip\@mpfootins
      \normalcolor\footnoterule\@nameuse{mp@footgroup#1}\fi}
  \g@addto@macro{\extrafeetminihook}{%
    \@namedef{@mpfn#1}{mpfootnote#1}
    \@namedef{thempfn#1}{\@nameuse{thempfootnote#1}}
    \csname c@mpfootnote#1\endcsname\z@
     \expandafter\let\expandafter\@t@mp \csname @mpfootnotetext#1\endcsname
     \expandafter\let \csname @footnotetext#1\endcsname \@t@mp}
  \g@addto@macro{\@mem@extranofeet}{%  % [RS]
    \ifvoid\@nameuse{footins#1}\else\@mem@nofootfalse\fi}
  \plainfootstyle{#1}%
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@makefootnote}
% \cs{m@makefootnote}\marg{series} creates  \cs{footnote<series>}
% \changes{v1.4}{2004/01/21}{In \cs{m@makefootnote} changed \cs{advance}
% to \cs{stepcounter} to make change global}
%    \begin{macrocode}
\newcommand{\m@makefootnote}[1]{
  \@namedef{footnote#1}{\@ifnextchar[
    {\@nameuse{@xfootnote#1}}{%\advance \@nameuse{c@\@mpfn#1} by \@ne
                              \stepcounter{\@mpfn#1}%
      \@name@p@xdef{@thefnmark#1}{\@nameuse{thempfn#1}}%
      \@nameuse{@footnotemark#1}\@nameuse{@footnotetext#1}}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@make@xfootnote}
% \cs{m@make@xfootnote}\marg{series} creates \cs{@xfootnote<series>}
%    \begin{macrocode}
\newcommand{\m@make@xfootnote}[1]{
  \@namedef{@xfootnote#1}[##1]{%
  \begingroup
    \csname c@\@mpfn#1\endcsname ##1\relax
    \@name@unresp@xdef{@thefnmark#1}{\@nameuse{thempfn#1}}%
  \endgroup
  \@nameuse{@footnotemark#1}\@nameuse{@footnotetext#1}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@make@footnotetext}
% \cs{m@make@footnotetext}\marg{series} creates \cs{@footnotetext<series>}
%    \begin{macrocode}
\newcommand{\m@make@footnotetext}[1]{%
  \@namelongdef{@footnotetext#1}##1{%
  \insert\@nameuse{footins#1}{%
  \@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##1\@finalstrut\strutbox}%
  \color@endgroup}%
  \m@mmf@prepare}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@make@mpfootnotetext}
% \cs{m@make@mpfootnotetext}\marg{series} creates \cs{@mpfootnotetext<series>}
%    \begin{macrocode}
\newcommand{\m@make@mpfootnotetext}[1]{%
  \@namelongdef{@mpfootnotetext#1}##1{%
    \global\setbox\@nameuse{@mpfootins#1}\vbox{%
      \unvbox\@nameuse{@mpfootins#1}%
      \@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##1\@finalstrut\strutbox}%
    \color@endgroup}%
    \m@mmf@prepare}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@makefootnotemark}
% \changes{v1.61}{2004/03/24}{Changed \cs{advance} to \cs{stepcounter}
%                 in \cs{m@makefootnotemark}}
% \cs{m@makefootnotemark}\marg{series} creates \cs{footnotemark<series>}
%    \begin{macrocode}
\newcommand{\m@makefootnotemark}[1]{
\@namedef{footnotemark#1}{%
  \@ifnextchar[ {\@nameuse{@xfootnotemark#1}}
    {%\advance\@nameuse{c@footnote#1} by \@ne%
     \stepcounter{footnote#1}%
     \@name@p@xdef{@thefnmark#1}{\@nameuse{thefootnote#1}}%
     \@nameuse{@footnotemark#1}}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@make@xfootnotemark}
% \cs{m@make@xfootnotemark}\marg{series} creates \cs{@xfootnotemark<series>}
%    \begin{macrocode}
\newcommand{\m@make@xfootnotemark}[1]{%
  \@namedef{@xfootnotemark#1}[##1]{%
  \begingroup
    \@nameuse{c@footnote#1} ##1\relax
    \@name@unresp@xdef{@thefnmark#1}{\@nameuse{thefootnote#1}}%
  \endgroup
  \@nameuse{@footnotemark#1}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@make@footnotemark}
% \cs{m@make@footnotemark}\marg{series} creates \cs{@footnotemark<series>}
%    \begin{macrocode}
\newcommand{\m@make@footnotemark}[1]{%
\@namedef{@footnotemark#1}{%
  \leavevmode
  \ifhmode
    \edef\@x@sf{\the\spacefactor}%
    \m@mmf@check
    \nobreak
  \fi
  \@nameuse{@makefnmark#1}%
  \m@mmf@prepare
  \ifhmode\spacefactor\@x@sf\fi
  \relax}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@makefootmarkstyle}
% \cs{m@makefootmarkstyle}\marg{series} creates \cs{footmarkstyle<series>}
%    \begin{macrocode}
\newcommand{\m@makefootmarkstyle}[1]{%
 \@namedef{footmarkstyle#1}##1{%
    \@namedef{footscript#1}####1{##1}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@makefootnotetext}
% \cs{m@makefootnotetext}\marg{series} creates \cs{footnotetext<series>}
%    \begin{macrocode}
\newcommand{\m@makefootnotetext}[1]{%
\@namedef{footnotetext#1}{%
  \@ifnextchar[  {\@nameuse{@xfootnotenext#1}}%
  {\@name@p@xdef{@thefnmark#1}{\@nameuse{thempfn#1}}%
  \@nameuse{@footnotetext#1}}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@make@xfootnotenext}
% \cs{m@make@xfootnotenext}\marg{series} creates \cs{@xfootnotenext<series>}
%    \begin{macrocode}
\newcommand{\m@make@xfootnotenext}[1]{
\@namedef{@xfootnotenext#1}[##1]{%
  \begingroup
    \csname c@\@mpfn#1\endcsname ##1\relax
    \@name@unresp@xdef{@thefnmark#1}{\@nameuse{thempfn#1}}%
  \endgroup
  \@nameuse{@footnotetext#1}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@make@mpfn}
% \cs{m@make@mpfn}\marg{series} creates \cs{@mpfn<series>}
%    \begin{macrocode}
\newcommand{\m@make@mpfn}[1]{%
  \@namedef{@mpfn#1}{\@nameuse{footnote#1}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@makethempfn}
% \cs{m@makethempfn}\marg{series} creates \cs{thempfn<series>}
%    \begin{macrocode}
\newcommand{\m@makethempfn}[1]{%
  \@namedef{thempfn#1}{\@nameuse{thefootnote#1}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@make@makefnmark}
% \cs{m@make@makefnmark}\marg{series} creates \cs{@makefnmark<series>}
%    \begin{macrocode}
\newcommand{\m@make@makefnmark}[1]{%
  \@namedef{@makefnmark#1}{%
    \hbox{\@textsuperscript{\normalfont\@nameuse{@thefnmark#1}}}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@makefootref}
% \cs{m@makefootref}\marg{series} creates \cs{footref<series>}
%    \begin{macrocode}
\newcommand{\m@makefootref}[1]{%
  \@namedef{footref#1}##1{%
    \begingroup
      \@name@unresp@xdef{@thefnmark#1}{\ref{##1}}%
    \endgroup
    \@nameuse{@footnotemark#1}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@makefootfootmark}
% \cs{m@makefootfootmark}\marg{series} creates \cs{footfootmark<series>}
%    \begin{macrocode}
\newcommand{\m@makefootfootmark}[1]{%
  \@namedef{footfootmark#1}{%
    \ifdim\footmarkwidth < \z@
      \llap{\hb@xt@ -\footmarkwidth{%
            \hss\normalfont\@nameuse{footscript#1}%
            {\@nameuse{@thefnmark#1}}}%
            \hspace*{-\footmarkwidth}}%
    \else
      \ifdim\footmarkwidth = \z@
        {\normalfont\@nameuse{footscript#1}{\@nameuse{@thefnmark#1}}}%
      \else
        \hb@xt@\footmarkwidth{%
            \hss\normalfont\@nameuse{footscript#1}%
            {\@nameuse{@thefnmark#1}}}%
      \fi
    \fi}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@makemakefootmark}
% \changes{v1.61}{2004/03/23}{Removed extraneous space from \cs{m@makemakefootmark}}
% \cs{m@makemakefootmark}\marg{series} creates \cs{makefootmark<series>}
%    \begin{macrocode}
\newcommand{\m@makemakefootmark}[1]{%
  \@namedef{makefootmark#1}##1{%
    \leavevmode
    \parindent \footparindent\noindent
    \leftskip\footmarksep\relax
    \advance\leftskip \footmarkwidth 
    \null\nobreak\hskip -\leftskip\relax
    \makefootmarkhook\relax
    \@nameuse{footfootmark#1}##1}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@make@footgroup}
% \cs{m@make@footgroup}\marg{series} creates \cs{@footgroup<series>}
%    \begin{macrocode}
\newcommand{\m@make@footgroup}[1]{%
  \@namedef{@footgroup#1}{\unvbox\@nameuse{footins#1}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@makemp@footgroup}
% \cs{m@makemp@footgroup}\marg{series} creates \cs{mp@footgroup<series>}
%    \begin{macrocode}
\newcommand{\m@makemp@footgroup}[1]{%
  \@namedef{mp@footgroup#1}{\unvbox\@nameuse{@mpfootins#1}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@make@footstart}
% \cs{m@make@footstart}\marg{series} creates \cs{@footstart<series>}
%    \begin{macrocode}
\newcommand{\m@make@footstart}[1]{%
  \@namedef{@footstart#1}{%
    \vskip\bigskipamount
    \leftskip=\z@
    \rightskip=\z@
    \footnoterule}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\plainfootstyle}
% \cs{plainfootstyle}\marg{series} specifies a plain (normal) footnote
% style for \meta{series}.
%    \begin{macrocode}
\newcommand{\plainfootstyle}[1]{%
  \m@make@footnotetext{#1}%
  \m@make@footgroup{#1}%
  \m@make@footstart{#1}%
  \m@make@mpfootnotetext{#1}%
  \m@makemp@footgroup{#1}%
  \@nameuse{footmarkstyle#1}{\textsuperscript{##1}}
  \expandafter\dimen\csname footins#1\endcsname=\footinsdim
  \expandafter\count\csname footins#1\endcsname=1000\relax}

%    \end{macrocode}
% \end{macro}
%
% Now the basic code for footnote declarations (we will be
% using \texttt{footnotev@r} instead of the regular \texttt{footnote}).
%
% \begin{macro}{\footinsv@r}
% Define the new \cs{footinsv@r} insert. 
%    \begin{macrocode}
\newinsert\footinsv@r
  \skip\footinsv@r\bigskipamount
  \count\footinsv@r=1000 % no magnifcation
  \dimen\footinsv@r=\footinsdim
%    \end{macrocode}
% \end{macro}
% Create a new \verb?v@r? footnote series.
%    \begin{macrocode}
\m@make@footstart{v@r}
%    \end{macrocode}
% 
% \begin{macro}{\@footgroupv@r}
% Initialise the \texttt{footgroup} for the series to do nothing.
%    \begin{macrocode}
\newcommand{\@footgroupv@r}{}

%    \end{macrocode}
% \end{macro}
%
% And also for minipages:
% \begin{macro}{\@mpfootinsv@r}
% \begin{macro}{\mp@footgroupv@r}
%    \begin{macrocode}
\newinsert\@mpfootinsv@r
\newcommand{\mp@footgroupv@r}{}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \Lpack{ledmac} package style footnotes (see \file{ledmac.dtx} for 
% more detailed explanations).
%
% The two- and three-column notes use a macro \cs{m@mrigidbalance}
% to split text into a number of columns. This is based on \theTeXbook,
% page 397.
% \begin{macro}{\m@m@k}
% \begin{macro}{\m@m@h}
%    \begin{macrocode}
\newcount\m@m@k \newdimen\m@m@h
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\m@mrigidbalance}
% \begin{macro}{\m@mdosplits}
% \begin{macro}{\m@msplitoff}
% \cs{m@mrigidbalance}\marg{box}\marg{num}\marg{length} splits a
% \meta{box} (of text) into \meta{num} columns with \meta{length}
% space between the top baseline and the top of the \cs{vbox}.
%    \begin{macrocode}
\newcommand*{\m@mrigidbalance}[3]{\setbox0=\box#1 \m@m@k=#2 \m@m@h=#3
  \@@line{\splittopskip=\m@m@h \vbadness=\@M \hfilneg
  \valign{##\vfill\cr\m@mdosplits}}}
\newcommand*{\m@mdosplits}{\ifnum\m@m@k>0 \noalign{\hfil}\m@msplitoff
  \global\advance\m@m@k-1\cr\m@mdosplits\fi}
\newcommand*{\m@msplitoff}{\dimen0=\ht0
  \divide\dimen0 by\m@m@k \advance\dimen0 by\m@m@h
  \setbox2 \vsplit0 to \dimen0
  \unvbox2 }

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% \subsubsection{Two column footnotes}
%
% \begin{macro}{\twocolumnfootnotes}
% Declaration for two column footnotes. This causes the standard
% \cs{footnote} and friends to internally use the \verb?v@r? series,
% which here produces two column footnotes.
% \changes{v1.61}{2004/03/14}{Made [RS] change to \cs{twocolumnfootnotes}}
%    \begin{macrocode}
\newcommand{\twocolumnfootnotes}{%
  \@namedef{foottextfontv@r}{\foottextfont}%  % [RS]
  \let\@footnotetext\@twocolfootnotetext
  \dimen\footinsv@r=2\footinsdim
  \count\footinsv@r=500\relax
  \m@make@twocol@footgroup{v@r}%
  \let\@footgroupv@r\@twocol@footgroupv@r
  \let\@mpfootnotetext\@mptwocolfootnotetext 
  \m@make@mptwocol@footgroup{v@r}%
  \let\mp@footgroupv@r\@mptwocol@footgroupv@r}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@twocolfootnotetext}
% \cs{@twocolfootnotetext}\marg{text} is the two column version of
% \cs{@footnotetext} for the \verb?v@r? series.
% \changes{v1.618}{2005/09/03}{Added missing percent to \cs{@twocolfootnotetext} (mempatch v3.1)}
%    \begin{macrocode}
\newcommand{\@twocolfootnotetext}[1]{\insert\footinsv@r{%
  \foottextfont
  \@preamfntext
  \protected@edef\@currentlabel{%
    \csname p@footnote\endcsname\@thefnmark}%
  \color@begingroup
    \@twocolfootfmt{#1}%
  \color@endgroup}%
  \m@mmf@prepare}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@preamtwofmt}
% Give each column 0.45 of the textwidth.
%    \begin{macrocode}
\newcommand{\@preamtwofmt}{%
  \hsize .45\hsize
  \parindent=\z@
  \tolerance=5000\relax
  \raggedright
  \leavevmode}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@twocolfootfmt}
% \cs{@twocolfootfmt}
%    \begin{macrocode}
\newcommand{\@twocolfootfmt}[1]{%
  \@preamtwofmt
  {\footfootmark\strut {\foottextfont #1}\strut\par}\allowbreak}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@mptwocolfootnotetext}
% \cs{@mptwocolfootnotetext}\marg{text} is the two column version
% of \cs{@mpfootnotetext} for the \verb?v@r? series for minipages.
%    \begin{macrocode}
\newcommand{\@mptwocolfootnotetext}[1]{%
  \global\setbox\@mpfootinsv@r\vbox{%
    \unvbox\@mpfootinsv@r
    \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}
%
% \begin{macro}{\twocolumnfootstyle}
% \cs{twocolumnfootstyle}\marg{series} specifies a two column footnote 
% style for \meta{series}.
%    \begin{macrocode}
\newcommand{\twocolumnfootstyle}[1]{%
  \m@make@twocolfootnotetext{#1}%
  \m@make@mptwocolfootnotetext{#1}%
  \m@make@twocolfootfmt{#1}%
  \m@make@twocol@footgroup{#1}%
  \m@make@mptwocol@footgroup{#1}%
  \m@make@footstart{#1}%
  \@namelongdef{@footnotetext#1}##1{%
     \@nameuse{@twocolfootnotetext#1}{##1}}%
  \@namelongdef{@mpfootnotetext#1}##1{%
     \@nameuse{@mptwocolfootnotetext#1}{##1}}%
  \@namedef{@footgroup#1}{\@nameuse{@twocol@footgroup#1}}%
  \@namedef{mp@footgroup#1}{\@nameuse{@mptwocol@footgroup#1}}%
  \expandafter\dimen\csname footins#1\endcsname=2\footinsdim
  \expandafter\count\csname footins#1\endcsname=500\relax}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@make@twocolfootnotetext}
% \cs{m@make@twocolfootnotetext}\marg{series} creates 
% \cs{@twocolfootnotetext}\meta{series}
%    \begin{macrocode}
\newcommand{\m@make@twocolfootnotetext}[1]{%
  \@namelongdef{@twocolfootnotetext#1}##1{%
    \insert\@nameuse{footins#1}{
    \@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}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@make@mptwocolfootnotetext}
% \cs{m@make@mptwocolfootnotetext}\marg{series} creates
% \cs{@mptwocolfootnotetext}\meta{series}
%    \begin{macrocode}
\newcommand{\m@make@mptwocolfootnotetext}[1]{%
\@namelongdef{@mptwocolfootnotetext#1}##1{%
  \global\setbox\@nameuse{@mpfootins#1}\vbox{%
    \unvbox\@nameuse{@mpfootins#1}
    \@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}
%
% \begin{macro}{\m@make@twocolfootfmt}
% \cs{m@make@twocolfootfmt}\marg{series} creates
% \cs{@twocolfootfmt}\meta{series} 
% \changes{v1.61}{2004/03/14}{Removed space from \cs{m@make@twocolfootfmt} [RS]}
%    \begin{macrocode}
\newcommand{\m@make@twocolfootfmt}[1]{%
  \@namedef{@twocolfootfmt#1}##1{%
    \@preamtwofmt
    {\@nameuse{footfootmark#1}\strut
     {\@nameuse{foottextfont#1}##1}\strut\par}\allowbreak}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@make@twocol@footgroup}
% \cs{m@make@twocol@footgroup}\marg{series} creates
% \cs{@twocol@footgroup}\meta{series} 
%    \begin{macrocode}
\newcommand{\m@make@twocol@footgroup}[1]{%
  \@namedef{@twocol@footgroup#1}{{%
    \@nameuse{foottextfont#1} \splittopskip=\ht\strutbox
     \m@mrigidbalance{\@nameuse{footins#1}}{\tw@}{\splittopskip}}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@make@mptwocol@footgroup}
% \cs{m@make@mptwocol@footgroup}\marg{series} creates
% \cs{@mptwocol@footgroup}\meta{series}
%    \begin{macrocode}
\newcommand{\m@make@mptwocol@footgroup}[1]{%
\@namedef{@mptwocol@footgroup#1}{{%
  \@nameuse{foottextfont#1} \splittopskip=\ht\strutbox
  \m@mrigidbalance{\@nameuse{@mpfootins#1}}{\tw@}{\splittopskip}}}}

%    \end{macrocode}
% \end{macro}
%
%
% \subsubsection{Three column footnotes}
%
% \begin{macro}{\threecolumnfootnotes}
% Declaration for three column footnotres.
% \changes{v1.61}{2004/03/14}{Made [RS] change to \cs{threecolumnfootnotes}}
%    \begin{macrocode}
\newcommand{\threecolumnfootnotes}{%
  \@namedef{foottextfontv@r}{\foottextfont}%  % [RS]
  \let\@footnotetext\@threecolfootnotetext
  \dimen\footinsv@r=3\footinsdim
  \count\footinsv@r=333\relax
  \m@make@threecol@footgroup{v@r}%
  \let\@footgroupv@r\@threecol@footgroupv@r
  \let\@mpfootnotetext\@mpthreecolfootnotetext 
  \m@make@mpthreecol@footgroup{v@r}%
  \let\mp@footgroupv@r\@mpthreecol@footgroupv@r}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@threecolfootnotetext}
% \cs{@threecolfootnotetext}\marg{text} is the three column version of
% \cs{@footnotetext}
%    \begin{macrocode}
\newcommand{\@threecolfootnotetext}[1]{\insert\footinsv@r{%
  \foottextfont
  \@preamfntext
  \protected@edef\@currentlabel{%
    \csname p@footnote\endcsname\@thefnmark}%
  \color@begingroup
    \@threecolfootfmt{#1}%
  \color@endgroup}%
  \m@mmf@prepare}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@preamthreefmt}
% Give each column 0.3 of the text width.
%    \begin{macrocode}
\newcommand{\@preamthreefmt}{%
  \hsize .3\hsize
  \parindent=\z@
  \tolerance=5000\relax
  \raggedright
  \leavevmode}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@threecolfootfmt}
%
%    \begin{macrocode}
\newcommand{\@threecolfootfmt}[1]{%
  \@preamthreefmt
  {\footfootmark\strut {\foottextfont #1}\strut\par}\allowbreak}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@mpthreecolfootnotetext}
% \cs{@mpthreecolfootnotetext}\marg{text} is the three column version of
% \cs{@mpfootnotetext}
%    \begin{macrocode}
\newcommand{\@mpthreecolfootnotetext}[1]{%
  \global\setbox\@mpfootinsv@r\vbox{%
    \unvbox\@mpfootinsv@r
    \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}
%
%
%
% \begin{macro}{\threecolumnfootstyle}
% \cs{threecolumnfootstyle}\marg{series} specifies three column footnote style
% for \meta{series}
%    \begin{macrocode}
\newcommand{\threecolumnfootstyle}[1]{%
  \m@make@threecolfootnotetext{#1}%
  \m@make@mpthreecolfootnotetext{#1}%
  \m@make@threecolfootfmt{#1}%
  \m@make@threecol@footgroup{#1}%
  \m@make@mpthreecol@footgroup{#1}%
  \m@make@footstart{#1}%
  \@namelongdef{@footnotetext#1}##1{%
    \@nameuse{@threecolfootnotetext#1}{##1}}%
  \@namelongdef{@mpfootnotetext#1}##1{%
    \@nameuse{@mpthreecolfootnotetext#1}{##1}}%
  \@namedef{@footgroup#1}{\@nameuse{@threecol@footgroup#1}}%
  \@namedef{mp@footgroup#1}{\@nameuse{@mpthreecol@footgroup#1}}%
  \expandafter\dimen\csname footins#1\endcsname=3\footinsdim
  \expandafter\count\csname footins#1\endcsname=333\relax}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@make@threecolfootnotetext}
% \cs{m@make@threecolfootnotetext}\marg{series} creates
% \cs{@threecolfootnotetext}\meta{series}
%    \begin{macrocode}
\newcommand{\m@make@threecolfootnotetext}[1]{%
\@namelongdef{@threecolfootnotetext#1}##1{%
  \insert\@nameuse{footins#1}{
  \@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}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@make@mpthreecolfootnotetext}
% \cs{m@make@mpthreecolfootnotetext}\marg{series} creates
% \cs{@mpthreecolfootnotetext}\meta{series}
%    \begin{macrocode}
\newcommand{\m@make@mpthreecolfootnotetext}[1]{%
\@namelongdef{@mpthreecolfootnotetext#1}##1{%
  \global\setbox\@nameuse{@mpfootins#1}\vbox{%
    \unvbox\@nameuse{@mpfootins#1}
    \@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}
%
% \begin{macro}{\m@make@threecolfootfmt}
% \cs{m@make@threecolfootfmt}\marg{series} creates
% \cs{@threecolfootfmt}\meta{series}
% \changes{v1.61}{2004/03/14}{Removed space from \cs{m@make@threecolfootfmt} [RS]}
%    \begin{macrocode}
\newcommand{\m@make@threecolfootfmt}[1]{%
\@namelongdef{@threecolfootfmt#1}##1{%
  \@preamthreefmt
  {\@nameuse{footfootmark#1}\strut 
   {\@nameuse{foottextfont#1}##1}\strut\par}\allowbreak}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@make@threecol@footgroup}
% \cs{m@make@threecol@footgroup}\marg{series} creates
% \cs{@threecol@footgroup}\meta{series} 
%    \begin{macrocode}
\newcommand{\m@make@threecol@footgroup}[1]{%
\@namedef{@threecol@footgroup#1}{{%
  \@nameuse{foottextfont#1} \splittopskip=\ht\strutbox
  \m@mrigidbalance{\@nameuse{footins#1}}{\thr@@}{\splittopskip}}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@make@mpthreecol@footgroup}
% \cs{m@make@mpthreecol@footgroup}\marg{series} creates
% \cs{@mpthreecol@footgroup}\meta{series} 
%    \begin{macrocode}
\newcommand{\m@make@mpthreecol@footgroup}[1]{%
\@namedef{@mpthreecol@footgroup#1}{{%
  \@nameuse{foottextfont#1} \splittopskip=\ht\strutbox
  \m@mrigidbalance{\@nameuse{@mpfootins#1}}{\thr@@}{\splittopskip}}}}

%    \end{macrocode}
% \end{macro}
%
%
% \subsubsection{Paragraphed footnotes}
%
% Paragraphed footnotes are based on \theTeXbook, page 398ff, in the 
% Dirty Tricks appendix. It does a lot of box manipulations.
%
% \begin{macro}{\m@munvxh}
% \cs{m@munvxh}\marg{vbox}: unvbox, extract the last line, and unhbox it.
%    \begin{macrocode}
\newcommand{\m@munvxh}[1]{%
  \setbox0=\vbox{\unvbox#1%
    \global\setbox1=\lastbox}%
  \unhbox1
  \unskip
  \unskip
  \unpenalty
  \hskip\m@mipn@skip}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@mungebox}
%
%    \begin{macrocode}
\newcommand{\m@mungebox}{%
  \setbox0=\hbox{\m@munvxh0}%
  \dp0=\z@
  \ht0=\footfudgefactor\wd0
  \box0
  \penalty0}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@mipn@skip}
% \begin{macro}{\m@minterparanoteglue}
% These are `inter-para-note-skip' and `inter-parafootnote' glue, for 
% paragraphed footnotes.
%    \begin{macrocode}
\newskip\m@mipn@skip
\newcommand*{\m@minterparanoteglue}[1]{%
  {\foottextfont\global\m@mipn@skip=#1\relax}}
\m@minterparanoteglue{1em plus.4em minus.4em}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\m@mmakehboxofhboxes}
% Make an hbox of hboxes.
%    \begin{macrocode}
\newcommand*{\m@mmakehboxofhboxes}{\setbox0=\hbox{}%
  \loop
    \unpenalty
    \setbox2=\lastbox
  \ifhbox2
    \setbox0=\hbox{\box2\unhbox0}
  \repeat}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@mremovehboxes}
%    \begin{macrocode}
\newcommand*{\m@mremovehboxes}{\setbox0=\lastbox
  \ifhbox0{\m@mremovehboxes}\unhbox0 \fi}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\footfudgefiddle}
% TeX uses \verb?\footfufgefactor? to estimate the space required for
% paragraphed footnotes. If it underestimates then the notes approach,
% or cover, the footer. \verb?\footfudgefiddle? can be changed (upward) from
% its default to improve matters.
%    \begin{macrocode}
\newcommand*{\footfudgefiddle}{64}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\paragraphfootnotes}
% Declaration for paragraphed footnotes.
% \changes{v1.61}{2004/03/14}{Made [RS] change to \cs{paragraphfootnotes}}
%    \begin{macrocode}
\newcommand{\paragraphfootnotes}{%
  \@namedef{foottextfontv@r}{\foottextfont}%  % [RS]
  \let\@footnotetext\@parafootnotetext
  \dimen\footinsv@r=\footinsdim
  \count\footinsv@r=1000\relax
  \m@make@para@footgroup{v@r}%
  \let\@footgroupv@r\@para@footgroupv@r
  \let\@mpfootnotetext\@mpparafootnotetext 
  \m@make@mppara@footgroup{v@r}%
  \let\mp@footgroupv@r\@mppara@footgroupv@r
  {\foottextfont
   \dimen0=\baselineskip
   \multiply\dimen0 by 1024
   \divide\dimen0 by \hsize \multiply\dimen0 by \footfudgefiddle
   \xdef\footfudgefactor{\expandafter\strip@pt\dimen0 }}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@parafootnotetext}
% \cs{@parafootnotetext}\marg{text} is a paragraph version of
% \cs{@footnotetext}
%    \begin{macrocode}
\newcommand{\@parafootnotetext}[1]{\insert\footinsv@r{
  \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}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@parafootfmt}
% \cs{@parafootfmt}\marg{text} is a paragraph version of
% \cs{@footfmt}
%    \begin{macrocode}
\newcommand{\@parafootfmt}[1]{%
  \parindent=\z@
  \parfillskip=0pt \@plus 1fil
  {\footfootmark\strut {\foottextfont #1}\penalty-10}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@mpparafootnotetext}
% \cs{@mpparafootnotetext}\marg{text} is a paragraph version of
% \cs{@mpfootnotetext}
%    \begin{macrocode}
\newcommand{\@mpparafootnotetext}[1]{%
  \global\setbox\@mpfootinsv@r\vbox{%
    \unvbox\@mpfootinsv@r
    \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}
%
% \begin{macro}{\paragraphfootstyle}
% \cs{paragraphfootstyle}\marg{series} specifies paragraphed footnote style
% for \meta{series}.
%    \begin{macrocode}
\newcommand{\paragraphfootstyle}[1]{%
  \m@make@parafootnotetext{#1}%
  \m@make@mpparafootnotetext{#1}%
  \m@make@parafootfmt{#1}%
  \m@make@para@footgroup{#1}%
  \m@make@mppara@footgroup{#1}%
  \m@make@para@footstart{#1}%
  \@namelongdef{@footnotetext#1}##1{%
    \@nameuse{@parafootnotetext#1}{##1}}%
  \@namelongdef{@mpfootnotetext#1}##1{%
    \@nameuse{@mpparafootnotetext#1}{##1}}%
  \@namedef{@footgroup#1}{\@nameuse{@para@footgroup#1}}%
  \@namedef{mp@footgroup#1}{\@nameuse{@mppara@footgroup#1}}%
  \@namedef{@footstart#1}{\@nameuse{@para@footstart#1}}%
  \expandafter\dimen\csname footins#1\endcsname=\footinsdim
  \expandafter\count\csname footins#1\endcsname=1000\relax
  {\@nameuse{foottextfont#1}%
   \dimen0=\baselineskip
   \multiply\dimen0 by 1024
   \divide\dimen0 by \hsize \multiply\dimen0 by 64
   \xdef\footfudgefactor{\expandafter\strip@pt\dimen0 }}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@make@parafootnotetext}
% \cs{m@make@parafootnotetext}\marg{series} creates
% \cs{@parafootnotetext}\meta{series} 
%    \begin{macrocode}
\newcommand{\m@make@parafootnotetext}[1]{%
\@namelongdef{@parafootnotetext#1}##1{%
  \insert\@nameuse{footins#1}{
  \@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}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@make@mpparafootnotetext}
% \cs{m@make@mpparafootnotetext}\marg{series} creates
% \cs{@mpparafootnotetext}\meta{series} 
%    \begin{macrocode}
\newcommand{\m@make@mpparafootnotetext}[1]{%
\@namelongdef{@mpparafootnotetext#1}##1{%
  \global\setbox\@nameuse{@mpfootins#1}\vbox{%
    \unvbox\@nameuse{@mpfootins#1}
    \@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}
%
% \begin{macro}{\m@make@parafootfmt}
% \cs{m@make@parafootfmt}\marg{series} creates
% \cs{@parafootfmt}\meta{series} 
% \changes{v1.61}{2004/03/14}{Removed space from \cs{m@make@parafootfmt} [RS]}
%    \begin{macrocode}
\newcommand{\m@make@parafootfmt}[1]{%
\@namelongdef{@parafootfmt#1}##1{%
  \parindent=\z@
  \parfillskip=0pt \@plus 1fil
  {\@nameuse{footfootmark#1}\strut 
   {\@nameuse{foottextfont#1}##1}\penalty-10}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@make@para@footgroup}\marg{series} creates
% \cs{@para@footgroup}\meta{series}
%    \begin{macrocode}
\newcommand{\m@make@para@footgroup}[1]{%
\@namedef{@para@footgroup#1}{%
  \unvbox\@nameuse{footins#1}
  \m@mmakehboxofhboxes
  \setbox0=\hbox{\unhbox0 \m@mremovehboxes}%
  \@nameuse{foottextfont#1}%
  \noindent\unhbox0\par}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@make@mppara@footgroup}
% \cs{m@make@mppara@footgroup}\marg{series} creates
% \cs{@mppara@footgroup}\meta{series} 
%    \begin{macrocode}
\newcommand{\m@make@mppara@footgroup}[1]{%
\@namedef{@mppara@footgroup#1}{%
  \unvbox\@nameuse{@mpfootins#1}
  \m@mmakehboxofhboxes
  \setbox0=\hbox{\unhbox0 \m@mremovehboxes}%
  \@nameuse{foottextfont#1}%
  \noindent\unhbox0\par}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@make@para@footstart}
% \cs{m@make@para@footstart}\marg{series} creates
% \cs{@para@footstart}\meta{series} 
%    \begin{macrocode}
\newcommand{\m@make@para@footstart}[1]{%
\@namedef{@para@footstart#1}{%
  \vskip\bigskipamount
  \leftskip=\z@
  \rightskip=\z@
  \parindent=\z@
  \vskip\skip\@nameuse{footins#1}%
  \footnoterule}}

%    \end{macrocode}
% \end{macro}
%
%
%
%
% \section{Change marks} \label{sec:vct}
%
%    When preparing a manuscript it normally goes through
%    several iterations. The commands provided may be used to identify
%    changes made to a document during its life cycle.
%
% The code for this part of the class is based on the version
% controls in the \Lpack{iso} class~\cite{ISOCLASS}.
%
% \subsection{Print control}
%
%    Members of the development group often need to see the changes
%    between document versions, while the general public does not.
% \begin{macro}{\ifchangemarks}
%    This controls the appearence of the version controls defined
%    below.
%    \begin{macrocode}
\newif\ifchangemarks\changemarksfalse
%    \end{macrocode}
%   The marks only work properly when the |draft| option
%   is in effect. Also, the command |\changemarkstrue| must be
%   put in the document preamble.
% \end{macro}
%
% \begin{macro}{\v@rid}
%    This acts as an alias for |\marginpar| when both |changemarks| is
%    true and the |draft| option is in effect, otherwise it throws
%    away its two arguments.
% \changes{v1.2}{2002/07/27}{Added \cs{@bsphack} and \cs{@esphack} to \cs{v@rid}}
%    \begin{macrocode}
\newcommand{\v@rid}[2]{%
  \@bsphack
  \ifchangemarks
     \ifdraftdoc
       \marginpar[#1]{#2}%
  \fi\fi
  \@esphack}

%    \end{macrocode}
% \end{macro}
%
% \subsection{Change marking}
%
%    The following commands flag changes in the typeset document. Each of
%    the commands takes one parameter which is intended to be a 
%    `change number' or comment for tracking purposes. A symbol and
% the \meta{change-id} is put into the margin near where the command 
% is given. The marking commands should be attached to some word or 
% punctuation mark in the text otherwise extraneous spaces may creep
% into the final document.
% \changes{v1.2}{2002/07/27}{Added \cs{@bsphack} and \cs{@esphack} to changemarks}
% \begin{macro}{\added}
%    |\added{|\meta{change-id}|}|
%    Flags, with the symbol $\oplus$, that something has been added 
% to the manuscript.
%    \begin{macrocode}
\newcommand{\added}[1]{%
  \@bsphack
  \ifchangemarks
    \v@rid{\small$\oplus$ #1}{\small$\oplus$ #1}%
  \fi
  \@esphack}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\deleted}
%    |\deleted{|\meta{change-id}|}|
%    Flags, with the symbol $\neq$, that something has been deleted 
% from the manuscript.
%    \begin{macrocode}
\newcommand{\deleted}[1]{%
  \@bsphack
  \ifchangemarks
    \v@rid{\small$\neq$ #1}{\small$\neq$ #1}%
  \fi
  \@esphack}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\changed}
%    |\changed{|\meta{change-id}|}|
%    Flags, with the symbol $\Leftrightarrow$,  that something
% has been changed in the text.
%    \begin{macrocode}
\newcommand{\changed}[1]{%
  \@bsphack
  \ifchangemarks
    \v@rid{\small$\Leftrightarrow$ #1}{\small$\Leftrightarrow$ #1}%
  \fi
  \@esphack}

%    \end{macrocode}
% \end{macro}
%
% \section{Trimming marks}
%
% The \Lopt{showtrims} options prints trimming marks at the corners
% of the logical page. The code for this comes from ideas gleaned
% from Martin Schr\"{o}der's \Lpack{everyshi} package~\cite{EVERYSHI}
% and Melchior Franz's \Lpack{crop} package~\cite{CROP}. 
% The implementation and any errors are mine.
%
%    The implementation up to October 2002 was limited to putting a cross
% at the page corners. The manual directed users to the \Lpack{crop}
% packge if they wanted anything more. In October 2002 the implementation
% was substantially extended. The background to this is below.
%
%    Before the release of version 1.7 of \Lpack{crop} in May 2002 its
% author asked me to make some changes to \Lpack{memoir} that would be
% helpful to him, and I did so. Later he told me that the changes were
% unnecessary and I reverted to the original \Lpack{memoir} code.
%
%    On 2002/10/06 Peter Heslin (\url{peter.heslin@ucd.ie}) started a
% thread on \url{comp.text.tex} titled `Incompatibility of memoir.cls 
% and crop.sty' in which he said that \Lpack{memoir} and \Lpack{crop}
% did not seem to work together. The following are some snippets from
% that thread, identified by the various proponents.
% \begin{description}
% \item[crop] Ah, yes. It cannot work with all those |\settrims| etc.
%             crop.sty does only respect |\stock{width,height}|,
%             but none of the other memoir specific trim marks stuff \ldots
%
% \item[crop] \ldots I'm not a memoir expert. crop.sty respects all the
%  usual LaTeX paper dimensions and simply puts the marks around the
% page. It works with all standard classes and the KOMA classes. memoir
% seems to do things completely differently \ldots
%
%    Sorry, but I'm afraid I have to pass the problem to the memoir
% author. \ldots
%
% \item[memoir] I will try and take a look at the problem but I'm
%  not a crop expert --- I've never used it. However it seems odd to
% me that crop doesn't work with memopir. Memoir provides a different
% interface for specifying the page layout but then translates everything
% to the standard length variables. \ldots
%
% \item[crop] You've just taken code from it |...:->|
%
%    crop doesn't have its own idea at all. It just uses that of DEK
% and LaTeX, which says, that the reference point of the output box
% in the output routine is assumed 1 inch to the left [later corrected 
% to right] and 1 inch down
% from the upper left (virtual) paper corner.
%
% \item[crop] With other words: crop changes |\hoffset| and |\voffset|,
% in order to center the logical (virtual) page on the physical sheet
% of paper, in the middle of the crop marks. The length macros 
% |\evensidemargin| and |\oddsidemargin| are unchanged and refer still 
% to the logical page (modulo 1 inch).
%
%    The memoir class, in contrast (mis)uses the LaTeX margins
% |\even(odd)sidemargin| for that purpose.
%
%    Thus, the meaning of |\even(odd)sidemargin| is different
% in both packages.
% \end{description}
%
%     Given all the above I concluded that \Lpack{crop} would remain
% as it was, and that in order to satisfy Peter Heslin I would have
% to extend the trimming marks. The following is limited to extensions
% asked for by Peter Heslin.
%
% \begin{macro}{\trimmark}
% This is a cross 
% in a zero sized picture for marking the corner of a logical page. Up to 
% version 1.0 the macro used |\setlength| for adjusting the |\unitlength|
% but Henrik Holm\footnote{Message to \ctt{} on 2002/01/04 
% (\texttt{h.holm@spray.no})} discovered that if the \Lpack{calc} package
% is used then LaTeX complains about a missing number.
% \changes{v1.0a}{2002/01/04}{Removed \cs{setlength} from \cs{trimmark}. Stops
%                 problem with the calc package.}
%    \begin{macrocode}
\newcommand*{\trimmark}{%
  \begin{picture}(0,0)
    \unitlength 1cm
    \thinlines
    \put(-2,0){\line(1,0){4}}
    \put(0,-2){\line(0,1){4}}
  \end{picture}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\Ltrimpictl}
% \begin{macro}{\Ltrimpictr}
% \begin{macro}{\Ltrimpicbl}
% \begin{macro}{\Ltrimpicbr}
%  `L' shaped trim marks for four corners.
% \changes{v1.3}{2002/10/10}{Added \cs{Ltrimpictl}, \cs{Ltrimpictr}, \cs{Ltrimpicbl}
%                            and \cs{Ltrimpicbr}}
%    \begin{macrocode}
\newcommand*{\Ltrimpictl}{%
  \begin{picture}(0,0)
    \unitlength 1mm
    \thinlines
    \put(-2,0){\line(-1,0){18}}
    \put(0,2){\line(0,1){18}}
  \end{picture}}
\newcommand*{\Ltrimpictr}{%
  \begin{picture}(0,0)
    \unitlength 1mm
    \thinlines
    \put(2,0){\line(1,0){18}}
    \put(0,2){\line(0,1){18}}
  \end{picture}}
\newcommand*{\Ltrimpicbl}{%
  \begin{picture}(0,0)
    \unitlength 1mm
    \thinlines
    \put(-2,0){\line(-1,0){18}}
    \put(0,-2){\line(0,-1){18}}
  \end{picture}}
\newcommand*{\Ltrimpicbr}{%
  \begin{picture}(0,0)
    \unitlength 1mm
    \thinlines
    \put(2,0){\line(1,0){18}}
    \put(0,-2){\line(0,-1){18}}
  \end{picture}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\Ftrimpicbl}
%    Frame the page.
% \changes{v1.3}{2002/10/10}{Added \cs{Ftrimpictl}}
%    \begin{macrocode}
\newcommand*{\Ftrimpicbl}{%
  \begin{picture}(0,0)
    \unitlength 1pt
    \thinlines
    \put(0,0){\framebox(\strip@pt\paperwidth,\strip@pt\paperheight){}}
  \end{picture}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\tmarktl}
% \begin{macro}{\tmarktr}
% \begin{macro}{\tmarkbl}
% \begin{macro}{\tmarkbr}
% The trimming marks used for corner display.
% \changes{v1.3}{2002/10/10}{Added \cs{tmarktl}, \cs{tmarktr}, \cs{tmarkbl}
%                            and \cs{tmarkbr}}
%    \begin{macrocode}
\newcommand*{\tmarktl}{\trimmark} 
\newcommand*{\tmarktr}{\trimmark} 
\newcommand*{\tmarkbl}{\trimmark} 
\newcommand*{\tmarkbr}{\trimmark} 

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\tmarktm}
% \begin{macro}{\tmarkml}
% \begin{macro}{\tmarkmr}
% \begin{macro}{\tmarkbm}
% The trimming marks used for mid-side display.
% \changes{v1.3}{2002/10/10}{Added \cs{tmarktm}, \cs{tmarkml}, \cs{tmarkmr}
%                            and \cs{tmarkbm}}
%    \begin{macrocode}
\newcommand*{\tmarktm}{}
\newcommand*{\tmarkml}{}
\newcommand*{\tmarkmr}{}
\newcommand*{\tmarkbm}{}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\trimXmarks}
% \begin{macro}{\trimLmarks}
% \begin{macro}{\trimFrame}
% \begin{macro}{\trimNone}
% These are declarations for the different kinds of trimming marks.
% \changes{v1.3}{2002/10/10}{Added \cs{trimXmarks}, \cs{trimLmarks}, 
%                            \cs{trimFrame} and \cs{trimNone}} 
%    \begin{macrocode}
\newcommand*{\trimXmarks}{%
  \let\tmarktl\trimmark
  \let\tmarktr\trimmark
  \let\tmarkbl\trimmark
  \let\tmarkbr\trimmark
}
\newcommand*{\trimLmarks}{%
  \let\tmarktl\Ltrimpictl
  \let\tmarktr\Ltrimpictr
  \let\tmarkbl\Ltrimpicbl
  \let\tmarkbr\Ltrimpicbr
}
\newcommand*{\trimFrame}{%
  \let\tmarktl\null
  \let\tmarktr\null
  \let\tmarkbl\Ftrimpicbl
  \let\tmarkbr\null
}
\newcommand*{\trimNone}{%
  \let\tmarktl\relax
  \let\tmarktr\relax
  \let\tmarkbl\relax
  \let\tmarkbr\relax
  \let\tmarktm\relax
  \let\tmarkml\relax
  \let\tmarkmr\relax
  \let\tmarkbm\relax
}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\trimmarks}
%    This positions four marks (|\trimmark|) at the corners of a
% logical page. It is basically a |\vbox| with zero height and width.
%
% Bastiaan Niels Veelo reported (2003/07/11) odd things (e.g., some
% macros ignored) when changing trim marks. Turns out to be related
% to the use of |\protect|.
% \changes{v1.1}{2002/03/10}{Changed \cs{trimmarks} to work properly with the oneside option}
% \changes{v1.3}{2002/10/10}{Changed \cs{trimmarks} to use \cs{tmarktl}, etc.,
%                            instead of four \cs{trimmark}s}
% \changes{v1.4}{2003/11/22}{Fiddled \cs{protect} in \cs{trimmaks}
%                            (from patch v1.7)}              
%    \begin{macrocode}
\newcommand*{\trimmarks}{%   
  \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 \hskip-\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}
%
% \begin{macro}{\c@sheetsequence}
% Peter Heslin asked for the ability to add a sequential page number
% (1 for the first page, N for the last page, no matter what value
% the page counter has) to the trimming marks.
%
%    |\c@sheetsequence| is a new counter for pages starting at the 
% beginning and independent
% of the standard page counter. This should not be reset by anything.
% The counter increment has to be added to the output routine.
%
%     This may also be useful for page N of M numbering.
% \changes{v1.3}{2002/10/10}{Added sheetsequence counter.}
%    \begin{macrocode}
\newcounter{sheetsequence}
  \setcounter{sheetsequence}{1}
  \renewcommand{\thesheetsequence}{\@arabic\c@sheetsequence}
\g@addto@macro{\@outputpage}{\stepcounter{sheetsequence}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\c@lastsheet}
% \begin{macro}{\c@lastpage}
% While we're at it, might as well provide for lastpage and lastsheet counters
% \changes{v1.3}{2002/11/14}{Added lastsheet and lastpage functions.}
%    \begin{macrocode}
\newcounter{lastsheet}
  \setcounter{lastsheet}{0}
\newcounter{lastpage}
  \setcounter{lastpage}{0}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\dol@stsheet}
% \begin{macro}{\dol@stpage}
% These two macros write the values of lastsheet and lastpage to the
% \file{aux} file. They have to be called at the end of the document
% after a |\clearpage| to flush out any floats.
% \changes{v1.4}{2003/02/27}{In \cs{dol@stpage} use the page counter value,
%                            not \cs{thepage} otherwise problems with
%                            non-arabic page numbers (patch 1.1)}
% \changes{v1.61}{2004/03/21}{In \cs{dol@stpage} use the sheet counter value,
%                              not \cs{thesheetsequence} otherwise problems
%                              if (e.g., hangul.sty) \cs{@arabic} is redefined}
% \changes{v1.618}{2005/09/02}{Made \cs{dol@stsheet} and \cs{dol@stpage}
%  obey \cs{nofiles}}
%    \begin{macrocode}
\newcommand{\dol@stsheet}{%
  \if@filesw
    \addtocounter{sheetsequence}{-1}%
    \immediate\write\@auxout%
      {\string\setcounter{lastsheet}{\the\c@sheetsequence}}%
    \stepcounter{sheetsequence}%
  \fi}
\newcommand{\dol@stpage}{%
  \if@filesw
    \addtocounter{page}{-1}%
    \immediate\write\@auxout%
      {\string\setcounter{lastpage}{\the\c@page}}%
    \stepcounter{page}%
  \fi}
%    \end{macrocode}
% I originally used this: \\
% \verb?\AtEndDocument{\clearpage\dol@stsheet\dol@stpage}?
% but following the CTT thread \textit{AtEndDocument produces unwanted page
% break}, 2004/03/11, and in particular Dan Luecking's response I now
% try as hard as possible to do it right at the end, but even this is not
% 100\% reliable. The only way to ensure reliability is to modify the kernel
% \verb?\enddocument? which doesn't seem to be a particularly wise thing
% to do.
% \changes{v1.61}{2004/03/14}{Changed location of calls to \cs{dol@stsheet}
%                             and \cs{dol@stpage}}
%    \begin{macrocode}
\AtBeginDocument{\AtEndDocument{\clearpage\dol@stsheet\dol@stpage}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%    Any marks are put onto the pages by adding to the |\shipout|
% routine.
%
% \begin{macro}{\mem@oldshipout}
% Keep a copy of the current version of |\shipout| in |\mem@oldshipout|.
%    \begin{macrocode}
\let\mem@oldshipout\shipout
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\mem@shipi}
% \begin{macro}{\mem@shipii}
% Effectively these will add the |\trimmarks| to the box holding
% the contents of the page. Note that any \cs{makeindex} must come \emph{before} \Lpack{pagesel} (and \Lpack{selectp}) package.
% \changes{v1.618}{2005/09/03}{Changed \cs{mem@shipii} for the pagesel package, courtesy James Szinger (mempatch v311)}
%    \begin{macrocode}
\newcommand*{\mem@shipi}{%
  \ifvoid\@cclv\expandafter\aftergroup\fi\mem@shipii}
\newcommand*\mem@shipii{%
  \ifvoid\@cclv
    \mem@oldshipout\box\@cclv
  \else
    \mem@oldshipout\vbox{\trimmarks\unvbox\@cclv}%
  \fi}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\shipout}
% Our new version of |\shipout|, which is only needed for the 
% \Lopt{showtrims} option. This adds |\mem@shipi| to the page box
% which then calls the original version of |\shipout|.
%    \begin{macrocode}
\ifshowtrims
  \renewcommand*{\shipout}{\afterassignment\mem@shipi\setbox\@cclv=}
\fi

%    \end{macrocode}
% \end{macro}
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% \section{Verbatims, boxes, and files}
%
% All the code in this section was added for version 1.2 of the class.
% \changes{v1.2}{2002/07/27}{Section `Verbatims, boxes and files' added}
%
% \subsection{Modified version of the verbatim package}
%
% Much of this is from the \Lpack{verbatim} package code~\cite{VERBATIM}.
% Unless indicated otherwise, the code and commentary is from that
% package.
%
%
% \subsubsection{Preliminaries}
%
% \begin{macro}{\every@verbatim}
% \begin{macro}{\afterevery@verbatim}
%    The hook (i.e.\ token register) |\every@verbatim|
%    is initialized to \meta{empty}.
%
% PW added the |\afterevery@verbatim| hook.
%    \begin{macrocode}
\newtoks\every@verbatim
  \every@verbatim={}
\newtoks\afterevery@verbatim
  \afterevery@verbatim={}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\@makeother}
%    |\@makeother| takes as argument a character and changes
%    its category code to $12$ (other).
%    \begin{macrocode}
\def\@makeother#1{\catcode`#112\relax}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@vobeyspaces}
%    The macro |\@vobeyspaces| causes spaces in the input
%    to be printed as spaces in the output.
%    \begin{macrocode}
\begingroup
 \catcode`\ =\active%
 \def\x{\def\@vobeyspaces{\catcode`\ \active\let \@xobeysp}}
 \expandafter\endgroup\x
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@xobeysp}
%    The macro |\@xobeysp| produces exactly one space in
%    the output, protected against breaking just before it.
%    (|\@M| is an abbreviation for the number $10000$.)
%    \begin{macrocode}
\def\@xobeysp{\leavevmode\penalty\@M\ }
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\verbatim@line}
%    We use a newly defined token register called |\verbatim@line|
%    that will be used as the character buffer.
%    \begin{macrocode}
\newtoks\verbatim@line
%    \end{macrocode}
% \end{macro}
%
% PW. I have extended the original \Lpack{verbatim} package code to handle
% TABs within verbatims. Normally TeX replaces a TAB by either a single space or
% ignores it altogether. For this purpose I have bits of code from the \Lpack{moreverb}
% package~\cite{MOREVERB} for handling TABs.
%
% \textit{Code and commentary from moreverb.}
%
% We define a few auxiliary macros and counters for expanding tabs.
%    \begin{macrocode}
\newcount\tab@position 
%    \end{macrocode}
%
% \begin{macro}{\@xobeytab}
% |\@xobeytab| puts enough spaces in to get to the next nominal
% tab stop
%    \begin{macrocode}
\def\@xobeytab{%
  \loop
    \toks@\expandafter{\the\toks@\@xobeysp}%
    \advance\tab@position-1
  \ifnum\tab@position>0 \repeat
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@vobeytabs}
% |\@vobeytabs| initialises use of \cs{@xobeytab}.  Needs to be
% executed within a group, as mustn't be allowed to leak out into the
% wide world.
%
%    \begin{macrocode}
\begingroup
  \catcode`\^^I=\active
  \gdef\@vobeytabs{\catcode`\^^I\active\let^^I\@xobeytab}%
\endgroup
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\verbatim@tabexpand}
% |\verbatim@tabexpand|\meta{body of line}|\@nil| processes every
% character of a line by tail recursion, counting the characters and
% juggling things when a tab is encountered.
%    \begin{macrocode}
\def\verbatim@tabexpand#1{%
  \ifx#1\@nil
    \the\toks@
    \expandafter\par
  \else
    \ifx#1\@xobeytab
      \@xobeytab
    \else
%    \end{macrocode}
%
% We can safely put |\@xobeysp| into the token register, since it
% does precisely what we need
%    \begin{macrocode}
      \toks@\expandafter{\the\toks@#1}%
      \advance\tab@position\m@ne
    \fi
    \ifnum\tab@position=0 \tab@position\tab@size \fi
    \expandafter\verbatim@tabexpand
  \fi
}

%    \end{macrocode}
% \end{macro}
%
% \textit{End of code and commentary from moreverb.}
%
% PW. Some macros for turning tabbing on and off.
%
% \begin{macro}{\tabson}
% \begin{macro}{\tabsoff}
% \begin{macro}{\@maybeobeytabs}
% |\tabson| turns tabbing on, and |\tabsoff| turns it off. Default
% is no tabbing.
%    \begin{macrocode}
\newif\ift@bs
\newcommand{\tabson}[1][4]{%
  \ifnum\@ne > #1\relax
    \tabsoff
  \else
    \t@bstrue
    \def\tab@size{#1\relax}%
    \def\@maybeobeytabs{\@vobeytabs}%
  \fi
}
\newcommand{\tabsoff}{%
  \t@bsfalse
  \def\tab@size{\z@}%
  \def\@maybeobeytabs{}%
}
\tabsoff

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\tabverbatim@processline}
% Process a line with TABs (extracted from \Lpack{moreverb}).
%    \begin{macrocode}
\def\tabverbatim@processline{\tab@position\tab@size
  \toks@{}%
  \expandafter\verbatim@tabexpand\the\verbatim@line\@nil}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\notabverbatim@processline}
% Processes a line ignoring TABs (this is the original \Lpack{verbatim}
% package definition of |\verbatim@processline|).
%    \begin{macrocode}
\def\notabverbatim@processline{\the\verbatim@line\par}

%    \end{macrocode}
% \end{macro}
%
% \textit{We are now back to the \Lpack{verbatim} code.}
%
% The following four macros are defined globally in a way suitable for
% the \texttt{verbatim} and \texttt{verbatim*} environments.
% \begin{macro}{\verbatim@startline}
% \begin{macro}{\verbatim@addtoline}
% \begin{macro}{\verbatim@processline}
%    |\verbatim@startline| initializes processing of a line
%    by emptying the character buffer (|\verbatim@line|).
%    \begin{macrocode}
\def\verbatim@startline{\verbatim@line{}}
%    \end{macrocode}
%    |\verbatim@addtoline| adds the tokens in its argument
%    to our buffer register |\verbatim@line| without expanding
%    them.
%    \begin{macrocode}
\def\verbatim@addtoline#1{%
  \verbatim@line\expandafter{\the\verbatim@line#1}}
%    \end{macrocode}
%    Processing a line inside a \texttt{verbatim} or \texttt{verbatim*}
%    environment means printing it.
%    Ending the line means that we have to begin a new paragraph.
%    We use |\par| for this purpose.  Note that |\par|
%    is redefined in |\@verbatim| to force \TeX{} into horizontal
%    mode and to insert an empty box so that empty lines in the input
%    do appear in the output.
% (PW changed next line from \\
% |\def\verbatim@processline{\the\verbatim@line\par}|
%    \begin{macrocode}
\def\verbatim@processline{\notabverbatim@processline}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\verbatim@finish}
%    As a default, |\verbatim@finish| processes the remaining
%    characters.
%    When this macro is called we are facing the following problem:
%    when the |\end{verbatim}|
%    command is encountered |\verbatim@processline| is called
%    to process the characters preceding the command on the same
%    line.  If there are none, an empty line would be output if we
%    did not check for this case.
%
%    If the line is empty |\the\verbatim@line| expands to
%    nothing.  To test this we use a trick similar to that on p.\ 376
%    of the \TeX{}book, but with |$|\ldots|$| instead of
%    the |!| tokens.  These |$| tokens can never have the same
%    category code as a |$| token that might possibly appear in the
%    token register |\verbatim@line|, as such a token will always have
%    been read with category code $12$ (other).
%    Note that |\ifcat| expands the following tokens so that
%    |\the\verbatim@line| is replaced by the accumulated
%    characters
%    \begin{macrocode}
\def\verbatim@finish{\ifcat$\the\verbatim@line$\else
  \verbatim@processline\fi}
%    \end{macrocode}
% \end{macro}
%
%
% \subsubsection{The \texttt{verbatim} and \texttt{verbatim*} environments}
%
% \begin{macro}{\verbatim@font}
%    We start by defining the macro |\verbatim@font| that is
%    to select the font and to set font-dependent parameters.
%    Then we go through |\verbatim@nolig@list| to avoid
%    certain ligatures.
%    |\verbatim@nolig@list| is a macro defined in the \LaTeXe{} kernel
%    to expand to
% \begin{verbatim}
%    \do\`\do\<\do\>\do\,\do\'\do\-
% \end{verbatim}
%    All the characters in this list can be part of a ligature in some
%    font or other.
% 
% PW. This is the original version which I'm going to replace.
% \begin{verbatim}
% \def\verbatim@font{\normalfont\ttfamily
%                    \hyphenchar\font\m@ne
%                    \let\do\do@noligs
%                    \verbatim@nolig@list}
% \end{verbatim}
% \end{macro}
%
% \begin{macro}{\setverbatimfont}
% \begin{macro}{\m@mverbfont}
% \begin{macro}{\verbatim@font}
% User level handle for changing the font used for verbatim text.
%    \begin{macrocode}
\newcommand{\setverbatimfont}[1]{\def\m@mverbfont{#1}}
\setverbatimfont{\normalfont\ttfamily}

\def\verbatim@font{\m@mverbfont
                   \hyphenchar\font\m@ne
                   \let\do\do@noligs
                   \verbatim@nolig@list}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\@verbatim}
%    The macro |\@verbatim| sets up things properly.
%    First of all, the tokens of the |\every@verbatim| hook
%    are inserted.
%    Then a \texttt{trivlist} environment is started and its first
%    |\item| command inserted.
%    Each line of the \texttt{verbatim} or \texttt{verbatim*}
%    environment will be treated as a separate paragraph.
%    \begin{macrocode}
\def\@verbatim{\the\every@verbatim
  \trivlist \item \relax
%    \end{macrocode}
%    The following extra vertical space is for compatibility with the
%    \LaTeX kernel: otherwise, using the |verbatim| package changes
%    the vertical spacing of a |verbatim| environment nested within a
%    |quote| environment.
%    \begin{macrocode}
  \if@minipage\else\vskip\parskip\fi
%    \end{macrocode}
%    The paragraph parameters are set appropriately:
%    the penalty at the beginning of the environment,
%    left and right margins, paragraph indentation, the glue to
%    fill the last line, and the vertical space between paragraphs.
%    The latter space has to be zero since we do not want to add
%    extra space between lines.
%    \begin{macrocode}
  \@beginparpenalty \predisplaypenalty 
  \leftskip\@totalleftmargin\rightskip\z@
  \parindent\z@\parfillskip\@flushglue\parskip\z@
%    \end{macrocode}
%    There's one point to make here:
%    the \texttt{list} environment uses \TeX's |\parshape|
%    primitive to get a special indentation for the first line
%    of the  list.
%    If the list begins with a \texttt{verbatim} environment
%    this |\parshape| is still in effect.
%    Therefore we have to reset this internal parameter explicitly.
%    We could do this by assigning $0$ to |\parshape|.
%    However, there is a simpler way to achieve this:
%    we simply tell \TeX{} to start a new paragraph.
%    As is explained on p.~103 of the \TeX{}book, this resets
%    |\parshape| to zero.
%    \begin{macrocode}
  \@@par
%    \end{macrocode}
%    We now ensure that |\par| has the correct definition,
%    namely to force \TeX{} into horizontal mode
%    and to include an empty box.
%    This is to ensure that empty lines do appear in the output.
%    Afterwards, we insert the |\interlinepenalty| since \TeX{}
%    does not add a penalty between paragraphs (here: lines)
%    by its own initiative. Otherwise a |verbatim| environment
%    could be broken across pages even if a |\samepage|
%    declaration were present.
%
%    However, in a top-aligned minipage, this will result in an extra
%    empty line added at the top. Therefore, a slightly more
%    complicated construct is necessary.
%    One of the important things here is the inclusion of
%    |\leavevmode| as the first macro in the first line, for example,
%    a blank verbatim line is the first thing in a list item.
%    \begin{macrocode}
  \def\par{%
    \if@tempswa
      \leavevmode\null\@@par\penalty\interlinepenalty
    \else
      \@tempswatrue
      \ifhmode\@@par\penalty\interlinepenalty\fi
    \fi}%
%    \end{macrocode}
%    But to avoid an error message when the environment
%    doesn't contain any text, we redefine |\@noitemerr|
%    which will in this case be called by |\endtrivlist|.
%    \begin{macrocode}
  \def\@noitemerr{\@warning{No verbatim text}}%
%    \end{macrocode}
%    Now we call |\obeylines| to make the end of line character
%    active,
%    \begin{macrocode}
  \obeylines
%    \end{macrocode}
%    change the category code of all special characters,
%    to $12$ (other).
%    \begin{macrocode}
  \let\do\@makeother \dospecials
%    \end{macrocode}
%    and switch to the font to be used.
%    \begin{macrocode}
  \verbatim@font
%    \end{macrocode}
%    To avoid a breakpoint after the labels box, we remove the penalty
%    put there by the list macros: another use of |\unpenalty|!
%    \begin{macrocode}
  \everypar \expandafter{\the\everypar \unpenalty}%
%    \end{macrocode}
% PW added next code at end of |\@verbatim|.
%    \begin{macrocode}
  \wrapright\the\afterevery@verbatim}
%    \end{macrocode}   
% \end{macro}
%
%
% \begin{macro}{\verbatim}
% \begin{macro}{\verbatim*}
%    Now we define the toplevel macros.
%    |\verbatim| is slightly changed:
%    after setting up things properly it calls
%    |\verbatim@start|.
%    This is done inside a group, so that |\verbatim| can be used
%    directly, without |\begin|.
%
% PW. The following is the original code, but I want a version of
% |verbatim| that recognises TABs.
% \begin{verbatim}
%    \begin{macrocode}
% \def\verbatim{%
%    \begingroup\@verbatim \frenchspacing\@vobeyspaces 
%    \verbatim@start}
%    \end{macrocode}
%    |\verbatim*| is defined accordingly.
%    \begin{macrocode}
% \@namedef{verbatim*}{\begingroup\@verbatim\verbatim@start}
% \def\endverbatim{\endtrivlist\endgroup}
% \expandafter\let\csname endverbatim*\endcsname=\endverbatim
%    \end{macrocode}
% \end{verbatim}
%
% PW. My code for these is a modified version of the original \Lpack{verbatim}
% code.
%    \begin{macrocode}
\def\verbatim{\begingroup
  \ift@bs
    \def\verbatim@processline{\tabverbatim@processline}%
  \fi
  \@verbatim \frenchspacing\@vobeyspaces\@maybeobeytabs\verbatim@start}
\@namedef{verbatim*}{\begingroup
  \ift@bs
    \def\verbatim@processline{\tabverbatim@processline}%
  \fi
  \@verbatim\@maybeobeytabs\verbatim@start}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\endverbatim}
% \begin{macro}{\endverbatim*}
%    To end the \texttt{verbatim} and \texttt{verbatim*}
%    environments it is only necessary to finish the
%    \texttt{trivlist} environment started in |\@verbatim| and
%    close the corresponding group, and handle\footnote{Noted by
% Zarko Cucej (\url{zarko.cucej@uni-mb.si}).} following (non-) paragraph,
% by using \cs{@doendpe}.
% \changes{v1.4}{2003/11/22}{Added \cs{@doendpe} to \cs{endverbatim} 
%                            (from patch v1.8)}
% \changes{v2.0a}{2004/03/01}{Refixed \cs{endverbatim}}
%    \begin{macrocode}
\def\endverbatim{\endtrivlist\endgroup\@doendpe}
\@namelet{endverbatim*}\endverbatim

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \subsubsection{The \texttt{comment} environment}
%
%    The |\comment| macro is similar to |\verbatim*|.
%    However, we do not need to switch fonts or set special
%    formatting parameters such as |\parindent| or |\parskip|.
%    We need only set the category code of all special characters
%    to $12$ (other) and that of |^^M| (the end of line character)
%    to $13$ (active).
%    The latter is needed for macro parameter delimiter matching in
%    the internal macros defined below.
%    In contrast to the default definitions used by the
%    |\verbatim| and |\verbatim*| macros,
%    we define |\verbatim@addtoline| to throw away its argument
%    and |\verbatim@processline|, |\verbatim@startline|,
%    and |\verbatim@finish| to act as no-ops.
%    Then we call |\verbatim@|.
%    But the first thing we do is to call |\@bsphack| so that
%    this environment has no influence whatsoever upon the spacing.
%
% PW: This is the original code for the |comment| environment, 
% which I'm going to change.
% \begin{verbatim}
% \def\comment{\@bsphack
%              \let\do\@makeother\dospecials\catcode`\^^M\active
%              \let\verbatim@startline\relax
%              \let\verbatim@addtoline\@gobble
%              \let\verbatim@processline\relax
%              \let\verbatim@finish\relax
%              \verbatim@}
% \end{verbatim}
%    |\endcomment| is very simple: it only calls
%    |\@esphack| to take care of the spacing.
%    The |\end| macro closes the group and therefore takes care
%    of restoring everything we changed.
% \begin{verbatim}
% \let\endcomment=\@esphack
% \end{verbatim}
%
% PW: The remainder of this section is my code.
%
% \begin{macro}{\setupcomment}
% |\setupcomment| does all the |\let|s in the original |\comment| code.
%    \begin{macrocode}
\newcommand{\setupcomment}{%
  \let\do\@makeother\dospecials\catcode`\^^M\active
  \let\verbatim@startline\relax
  \let\verbatim@addtoline\@gobble
  \let\verbatim@processline\relax
  \let\verbatim@finish\relax}
%    \end{macrocode}
% \end{macro}
%
% The macros below do no checking to see if something has (not) been defined
% previously. It's `user beware' time.
%
% \begin{macro}{\newcomment}
% |\newcomment|\marg{name} creates a new comment environment called 
% \meta{name}. This is a generalisation of the original comment code.
%    \begin{macrocode}
\newcommand{\newcomment}[1]{%
  \expandafter\def\csname #1\endcsname{\@bsphack\setupcomment\verbatim@}%
  \expandafter\let\csname end#1\endcsname=\@esphack}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\commentsoff}
% |\commentsoff|\marg{name} switches off the \meta{name} comment 
% environment by defining the relevent macros to do nothing.
%    \begin{macrocode}
\newcommand{\commentsoff}[1]{%
  \expandafter\def\csname #1\endcsname{}%
  \expandafter\def\csname end#1\endcsname{}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\commentson}
% |\commentson|\marg{name} switches on the \meta{name} comment 
% environment. It has to do the same things as |\newcomment| does,
% so let |\newcomment| do the work.
%    \begin{macrocode}
\newcommand{\commentson}[1]{\newcomment{#1}}

%    \end{macrocode}
% \end{macro}
%
% We had better supply the |comment| environment, as promised.
%    \begin{macrocode}
\newcomment{comment}

%    \end{macrocode}
%
% PW: That is the end of my changes and extensions to the original 
% |comment| environment code.
%
%
% \subsubsection{The main loop}
%
% Here comes the tricky part:
% During the definition of the macros we need to use the special
% characters |\|, |{|, and |}| not only with their
% normal category codes,
% but also with category code $12$ (other).
% We achieve this by the following trick:
% first we tell \TeX{} that |\|, |{|, and |}|
% are the lowercase versions of |!|, |[|, and |]|.
% Then we replace every occurrence of |\|, |{|, and |}|
% that should be read with category code $12$ by |!|, |[|,
% and |]|, respectively,
% and give the whole list of tokens to |\lowercase|,
% knowing that category codes are not altered by this primitive!
%
% But first we have ensure that
% |!|, |[|, and |]| themselves have
% the correct category code!
% To allow special settings of these codes we hide their setting in
% the macro |\vrb@catcodes|.  If it is already defined our new
% definition is skipped.
%    \begin{macrocode}
\@ifundefined{vrb@catcodes}%
  {\def\vrb@catcodes{%
     \catcode`\!12\catcode`\[12\catcode`\]12}}{}
%    \end{macrocode}
% This trick allows us to use this code for applications where other
% category codes are in effect.
%
% We start a group to keep the category code changes local.
%    \begin{macrocode}
\begingroup
 \vrb@catcodes
 \lccode`\!=`\\ \lccode`\[=`\{ \lccode`\]=`\}
%    \end{macrocode}
%    We also need the end-of-line character |^^M|,
%    as an active character.
%    If we were to simply write |\catcode`\^^M=\active|
%    then we would get an unwanted active end of line character
%    at the end of every line of the following macro definitions.
%    Therefore we use the same trick as above:
%    we write a tilde |~| instead of |^^M| and
%    pretend that the
%    latter is the lowercase variant of the former.
%    Thus we have to ensure now that the tilde character has
%    category code $13$ (active).
%    \begin{macrocode}
 \catcode`\~=\active \lccode`\~=`\^^M
%    \end{macrocode}
%    The use of the |\lowercase| primitive leads to one problem:
%    the uppercase character `|C|' needs to be used in the
%    code below and its case must be preserved.
%    So we add the command:
%    \begin{macrocode}
 \lccode`\C=`\C
%    \end{macrocode}
%    Now we start the token list passed to |\lowercase|.
%    We use the following little trick (proposed by Bernd Raichle):
%    The very first token in the token list we give to |\lowercase| is
%    the |\endgroup| primitive. This means that it is processed by
%    \TeX{} immediately after |\lowercase| has finished its operation,
%    thus ending the group started by |\begingroup| above. This avoids
%    the global definition of all macros.
%    \begin{macrocode}
 \lowercase{\endgroup
%    \end{macrocode}
% \begin{macro}{\verbatim@start}
%    The purpose of |\verbatim@start| is to check whether there
%    are any characters on the same line as the |\begin{verbatim}|
%    and to pretend that they were on a line by themselves.
%    On the other hand, if there are no characters remaining
%    on the current line we shall just find an end of line character.
%    |\verbatim@start| performs its task by first grabbing the
%    following character (its argument).
%    This argument is then compared to an active |^^M|,
%    the end of line character.
%    \begin{macrocode}
    \def\verbatim@start#1{%
      \verbatim@startline
      \if\noexpand#1\noexpand~%
%    \end{macrocode}
%    If this is true we transfer control to |\verbatim@|
%    to process the next line.  We use
%    |\next| as the macro which will continue the work.
%    \begin{macrocode}
        \let\next\verbatim@
%    \end{macrocode}
%    Otherwise, we define |\next| to expand to a call
%    to |\verbatim@| followed by the character just
%    read so that it is reinserted into the text.
%    This means that those characters remaining on this line
%    are handled as if they formed a line by themselves.
%    \begin{macrocode}
      \else \def\next{\verbatim@#1}\fi
%    \end{macrocode}
%    Finally we call |\next|.
%    \begin{macrocode}
      \next}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\verbatim@}
%    The three macros |\verbatim@|, |\verbatim@@|,
%    and |\verbatim@@@| form the ``main loop'' of the
%    \texttt{verbatim} environment.
%    The purpose of |\verbatim@| is to read exactly one line
%    of input.
%    |\verbatim@@| and |\verbatim@@@| work together to
%    find out whether the four characters
%    |\end| (all with category code $12$ (other)) occur in that
%    line.
%    If so, |\verbatim@@@| will call
%    |\verbatim@test| to check whether this |\end| is
%    part of |\end{verbatim}| and will terminate the environment
%    if this is the case.
%    Otherwise we continue as if nothing had happened.
%    So let's have a look at the definition of |\verbatim@|:
%    \begin{macrocode}
    \def\verbatim@#1~{\verbatim@@#1!end\@nil}%
%    \end{macrocode}
%    Note that the |!| character will have been replaced by a
%    |\| with category code $12$ (other) by the |\lowercase|
%    primitive governing this code before the definition of this
%    macro actually takes place.
%    That means that
%    it takes the line, puts |\end| (four character tokens)
%    and |\@nil| (one control sequence token) as a
%    delimiter behind it, and
%    then calls |\verbatim@@|.
% \end{macro}
%
% \begin{macro}{\verbatim@@}
%    |\verbatim@@| takes everything up to the next occurrence of
%    the four characters |\end| as its argument.
%    \begin{macrocode}
    \def\verbatim@@#1!end{%
%    \end{macrocode}
%    That means: if they do not occur in the original line, then
%    argument |#1| is the
%    whole input line, and |\@nil| is the next token
%    to be processed.
%    However, if the four characters |\end| are part of the
%    original line, then
%    |#1| consists of the characters in front of |\end|,
%    and the next token is the following character (always remember
%    that the line was lengthened by five tokens).
%    Whatever |#1| may be, it is verbatim text,
%    so |#1| is added to the line currently built.
%    \begin{macrocode}
       \verbatim@addtoline{#1}%
%    \end{macrocode}
%    The next token in the input stream
%    is of special interest to us.
%    Therefore |\futurelet| defines |\next| to be equal
%    to it before calling |\verbatim@@@|.
%    \begin{macrocode}
       \futurelet\next\verbatim@@@}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\verbatim@@@}
%    |\verbatim@@@| will now read the rest of the tokens on
%    the current line,
%    up to the final |\@nil| token.
%    \begin{macrocode}
    \def\verbatim@@@#1\@nil{%
%    \end{macrocode}
%    If the first of the above two cases occurred, i.e.\ no
%    |\end| characters were on that line, |#1| is empty
%    and |\next| is equal to |\@nil|.
%    This is easily checked.
%    \begin{macrocode}
       \ifx\next\@nil
%    \end{macrocode}
%    If so, this was a simple line.
%    We finish it by processing the line we accumulated so far.
%    Then we prepare to read the next line.
%    \begin{macrocode}
         \verbatim@processline
         \verbatim@startline
         \let\next\verbatim@
%    \end{macrocode}
%    Otherwise we have to check what follows these |\end|
%    tokens.
%    \begin{macrocode}
       \else
%    \end{macrocode}
%    Before we continue, it's a good idea to stop for a moment
%    and remember where we are:
%    We have just read the four character tokens |\end|
%    and must now check whether the name of the environment (surrounded
%    by braces) follows.
%    To this end we define a macro called |\@tempa|
%    that reads exactly one character and decides what to do next.
%    This macro should do the following: skip spaces until
%    it encounters either a left brace or the end of the line.
%    But it is important to remember which characters are skipped.
%    The |\end|\meta{optional spaces}|{| characters
%    may be part of the verbatim text, i.e.\ these characters
%    must be printed.
%
%    Assume for example that the current line contains
%    \begin{verbatim*}
%      \end {AVeryLongEnvironmentName}
%\end{verbatim*}
%    As we shall soon see, the scanning mechanism implemented here
%    will not find out that this is text to be printed until
%    it has read the right brace.
%    Therefore we need a way to accumulate the characters read
%    so that we can reinsert them if necessary.
%    The token register |\@temptokena| is used for this purpose.
%
%    Before we do this we have to get rid of the superfluous
%    |\end| tokens at the end of the line.
%    To this end we define a temporary macro whose argument
%    is delimited by |\end\@nil| (four character tokens
%    and one control sequence token) to be used below
%    on the rest of the line, after appending a |\@nil| token to it.
%    (Note that this token can never appear in |#1|.)
%    We use the following definition of
%    |\@tempa| to get the rest of the line (after the first
%    |\end|).
%    \begin{macrocode}
         \def\@tempa##1!end\@nil{##1}%
%    \end{macrocode}
%    We mentioned already that we use token register
%    |\@temptokena|
%    to remember the characters we skip, in case we need them again.
%    We initialize this with the |\end| we have thrown away
%    in the call to |\@tempa|.
%    \begin{macrocode}
         \@temptokena{!end}%
%    \end{macrocode}
%    We shall now call |\verbatim@test|
%    to process the characters
%    remaining on the current line.
%    But wait a moment: we cannot simply call this macro
%    since we have already read the whole line.
%    Therefore we have to first expand the macro |\@tempa| to insert
%    them again after the |\verbatim@test| token.
%    A |^^M| character is appended to denote the end of the line.
%    (Remember that this character comes disguised as a tilde.)
%    \begin{macrocode}
         \def\next{\expandafter\verbatim@test\@tempa#1\@nil~}%
%    \end{macrocode}
%    That's almost all, but we still have to
%    now call |\next| to do the work.
%    \begin{macrocode}
       \fi \next}%
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\verbatim@test}
%    We define |\verbatim@test| to investigate every token
%    in turn.
%    \begin{macrocode}
    \def\verbatim@test#1{%
%    \end{macrocode}
%    First of all we set |\next| equal to |\verbatim@test|
%    in case this macro must call itself recursively in order to
%    skip spaces.
%    \begin{macrocode}
           \let\next\verbatim@test
%    \end{macrocode}
%    We have to distinguish four cases:
%    \begin{enumerate}
%      \item The next token is a |^^M|, i.e.\ we reached
%            the end of the line.  That means that nothing
%            special was found.
%            Note that we use |\if| for the following
%            comparisons so that the category code of the
%            characters is irrelevant.
%    \begin{macrocode}
           \if\noexpand#1\noexpand~%
%    \end{macrocode}
%            We add the characters accumulated in token register
%            |\@temptokena| to the current line.  Since
%            |\verbatim@addtoline| does not expand its argument,
%            we have to do the expansion at this point.  Then we
%            |\let| |\next| equal to |\verbatim@|
%            to prepare to read the next line.
%    \begin{macrocode}
             \expandafter\verbatim@addtoline
               \expandafter{\the\@temptokena}%
             \verbatim@processline
             \verbatim@startline
             \let\next\verbatim@
%    \end{macrocode}
%      \item A space character follows.
%            This is allowed, so we add it to |\@temptokena|
%            and continue.
%    \begin{macrocode}
           \else \if\noexpand#1
             \@temptokena\expandafter{\the\@temptokena#1}%
%    \end{macrocode}
%      \item An open brace follows.
%            This is the most interesting case.
%            We must now collect characters until we read the closing
%            brace and check whether they form the environment name.
%            This will be done by |\verbatim@testend|, so here
%            we let |\next| equal this macro.
%            Again we will process the rest of the line, character
%            by character.
%            The characters forming the name of the environment will
%            be accumulated in |\@tempc|.
%            We initialize this macro to expand to nothing.
%    \begin{macrocode}
           \else \if\noexpand#1\noexpand[%
             \let\@tempc\@empty
             \let\next\verbatim@testend
%    \end{macrocode}
%            Note that the |[| character will be a |{| when
%            this macro is defined.
%      \item Any other character means that the |\end| was part
%            of the verbatim text.
%            Add the characters to the current line and prepare to call
%            |\verbatim@| to process the rest of the line.
%    \begin{macrocode}
           \else
             \expandafter\verbatim@addtoline
               \expandafter{\the\@temptokena}%
             \def\next{\verbatim@#1}%
           \fi\fi\fi
%    \end{macrocode}
%    \end{enumerate}
%    The last thing this macro does is to call |\next|
%    to continue processing.
%    \begin{macrocode}
           \next}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\verbatim@testend}
%    |\verbatim@testend| is called when
%    |\end|\meta{optional spaces}|{| was seen.
%    Its task is to scan everything up to the next |}|
%    and to call |\verbatim@@testend|.
%    If no |}| is found it must reinsert the characters it read
%    and return to |\verbatim@|.
%    The following definition is similar to that of
%    |\verbatim@test|:
%    it takes the next character and decides what to do.
%    \begin{macrocode}
    \def\verbatim@testend#1{%
%    \end{macrocode}
%    Again, we have four cases:
%    \begin{enumerate}
%      \item |^^M|: As no |}| is found in the current line,
%            add the characters to the buffer.  To avoid a
%            complicated construction for expanding
%            |\@temptokena|
%            and |\@tempc| we do it in two steps.  Then we
%            continue with |\verbatim@| to process the
%            next line.
%    \begin{macrocode}
         \if\noexpand#1\noexpand~%
           \expandafter\verbatim@addtoline
             \expandafter{\the\@temptokena[}%
           \expandafter\verbatim@addtoline
             \expandafter{\@tempc}%
           \verbatim@processline
           \verbatim@startline
           \let\next\verbatim@
%    \end{macrocode}
%      \item |}|: Call |\verbatim@@testend| to check
%            if this is the right environment name.
%    \begin{macrocode}
         \else\if\noexpand#1\noexpand]%
           \let\next\verbatim@@testend
%    \end{macrocode}
%      \item |\|: This character must not occur in the name of
%            an environment.  Thus we stop collecting characters.
%            In principle, the same argument would apply to other
%            characters as well, e.g., |{|.
%            However, |\| is a special case, since it may be
%            the first character of |\end|.  This means that
%            we have to look again for
%            |\end{|\meta{environment name}|}|.
%            Note that we prefixed the |!| by a |\noexpand|
%            primitive, to protect ourselves against it being an
%            active character.
%    \begin{macrocode}
         \else\if\noexpand#1\noexpand!%
           \expandafter\verbatim@addtoline
             \expandafter{\the\@temptokena[}%
           \expandafter\verbatim@addtoline
             \expandafter{\@tempc}%
           \def\next{\verbatim@!}%
%    \end{macrocode}
%      \item Any other character: collect it and continue.
%            We cannot use |\edef| to define |\@tempc|
%            since its replacement text might contain active
%            character tokens.
%    \begin{macrocode}
         \else \expandafter\def\expandafter\@tempc\expandafter
           {\@tempc#1}\fi\fi\fi
%    \end{macrocode}
%    \end{enumerate}
%    As before, the macro ends by calling itself, to
%    process the next character if appropriate.
%    \begin{macrocode}
         \next}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\verbatim@@testend}
%    Unlike the previous macros |\verbatim@@testend| is simple:
%    it has only to check if the |\end{|\ldots|}|
%    matches the corresponding |\begin{|\ldots|}|.
%    \begin{macrocode}
    \def\verbatim@@testend{%
%    \end{macrocode}
%    We use |\next| again to define the things that are
%    to be done.
%    Remember that the name of the current environment is
%    held in |\@currenvir|, the characters accumulated
%    by |\verbatim@testend| are in |\@tempc|.
%    So we simply compare these and prepare to execute
%    |\end{|\meta{current environment}|}|
%    macro if they match.
%    Before we do this we call |\verbatim@finish| to process
%    the last line.
%    We define |\next| via |\edef| so that
%    |\@currenvir| is replaced by its expansion.
%    Therefore we need |\noexpand| to inhibit the expansion
%    of |\end| at this point.
%    \begin{macrocode}
       \ifx\@tempc\@currenvir
         \verbatim@finish
         \edef\next{\noexpand\end{\@currenvir}%
%    \end{macrocode}
%    Without this trick the |\end| command would not be able
%    to correctly check whether its argument matches the name of
%    the current environment and you'd get an
%    interesting \LaTeX{} error message such as:
%    \begin{verbatim}
%! \begin{verbatim*} ended by \end{verbatim*}.
%\end{verbatim}
%    But what do we do with the rest of the characters, those
%    that remain on that line?
%    We call |\verbatim@rescan| to take care of that.
%    Its first argument is the name of the environment just
%    ended, in case we need it again.
%    |\verbatim@rescan| takes the list of characters to be
%    reprocessed as its second argument.
%    (This token list was inserted after the current macro
%    by |\verbatim@@@|.)
%    Since we are still in an |\edef| we protect it
%    by means of|\noexpand|.
%    \begin{macrocode}
                    \noexpand\verbatim@rescan{\@currenvir}}%
%    \end{macrocode}
%    If the names do not match, we reinsert everything read up
%    to now and prepare to call |\verbatim@| to process
%    the rest of the line.
%    \begin{macrocode}
       \else
         \expandafter\verbatim@addtoline
           \expandafter{\the\@temptokena[}%
           \expandafter\verbatim@addtoline
             \expandafter{\@tempc]}%
         \let\next\verbatim@
       \fi
%    \end{macrocode}
%    Finally we call |\next|.
%    \begin{macrocode}
       \next}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\verbatim@rescan}
%    In principle |\verbatim@rescan| could be used to
%    analyse the characters remaining after the |\end{...}|
%    command and pretend that these were read
%    ``properly'', assuming ``standard'' category codes are in
%    force.\footnote{Remember that they were all read with
%          category codes $11$ (letter) and $12$ (other) so
%          that control sequences are not recognized as such.}
%    But this is not always possible (when there are unmatched
%    curly braces in the rest of the line).
%    Besides, we think that this is not worth the effort:
%    After a \texttt{verbatim} or \texttt{verbatim*} environment
%    a new line in the output is begun anyway,
%    and an |\end{comment}| can easily be put on a line by itself.
%    So there is no reason why there should be any text here.
%    For the benefit of the user who did put something there
%    (a comment, perhaps)
%    we simply issue a warning and drop them.
%    The method of testing is explained in Appendix~D, p.\ 376 of
%    the \TeX{}book. We use |^^M| instead of the |!|
%    character used there
%    since this is a character that cannot appear in |#1|.
%    The two |\noexpand| primitives are necessary to avoid
%    expansion of active characters and macros.
%
%    One extra subtlety should be noted here: remember that
%    the token list we are currently building will first be
%    processed by the |\lowercase| primitive before \TeX{}
%    carries out the definitions.
%    This means that the `|C|' character in the
%    argument to the |\@warning| macro must be protected against
%    being changed to `|c|'.  That's the reason why we added the
%    |\lccode`\C=`\C| assignment above.
%    We can now finish the argument to |\lowercase| as well as the
%    group in which the category codes were changed.
%    \begin{macrocode}
    \def\verbatim@rescan#1#2~{\if\noexpand~\noexpand#2~\else
        \@warning{Characters dropped after `\string\end{#1}'}\fi}}
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{The \cs{verbatiminput} command}
%
% \begin{macro}{\verbatim@in@stream}
%    We begin by allocating an input stream (out of the 16 available
%    input streams).
%    \begin{macrocode}
\newread\verbatim@in@stream
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\verbatim@readfile}
%    The macro |\verbatim@readfile| encloses the main loop by calls to
%    the macros |\verbatim@startline| and |\verbatim@finish|,
%    respectively.  This makes sure
%    that the user can initialize and finish the command when the file
%    is empty or doesn't exist.  The \texttt{verbatim} environment has a
%    similar behaviour when called with an empty text.
%    \begin{macrocode}
\def\verbatim@readfile#1{%
  \verbatim@startline
%    \end{macrocode}
%    When the file is not found we issue a warning.
%    \begin{macrocode}
  \openin\verbatim@in@stream #1\relax
  \ifeof\verbatim@in@stream
    \typeout{No file #1.}%
  \else
%    \end{macrocode}
%    At this point we pass the name of the file to |\@addtofilelist|
%    so that its appears appears in the output of a |\listfiles|
%    command.
%    In addition, we use |\ProvidesFile| to make a log entry in the
%    transcript file and to distinguish files read in via
%    |\verbatiminput| from others.
%    \begin{macrocode}
    \@addtofilelist{#1}%
    \ProvidesFile{#1}[(verbatim)]%
%    \end{macrocode}
%    While reading from the file it is useful to switch off the
%    recognition of the end-of-line character.  This saves us stripping
%    off spaces from the contents of the line.
%    \begin{macrocode}
    \expandafter\endlinechar\expandafter\m@ne
    \expandafter\verbatim@read@file
    \expandafter\endlinechar\the\endlinechar\relax
    \closein\verbatim@in@stream
  \fi
  \verbatim@finish
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\verbatim@read@file}
%    All the work is done in |\verbatim@read@file|.  It reads the input
%    file line by line and recursively calls itself until the end of
%    the file.
%    \begin{macrocode}
\def\verbatim@read@file{%
  \read\verbatim@in@stream to\next
  \ifeof\verbatim@in@stream
  \else
%    \end{macrocode}
%    For each line we call |\verbatim@addtoline| with the contents of
%    the line. \hskip0pt plus 3cm\penalty0\hskip0pt plus -3cm
%    |\verbatim@processline| is called next.
%    \begin{macrocode}
    \expandafter\verbatim@addtoline\expandafter{\next}%
    \verbatim@processline
%    \end{macrocode}
%    After processing the line we call |\verbatim@startline| to
%    initialize all before we read the next line.
%    \begin{macrocode}
    \verbatim@startline
%    \end{macrocode}
%    Without |\expandafter| each call of |\verbatim@read@file| uses
%    space in \TeX's input stack.\footnote{A standard \TeX\ would
%    report an overflow error if you try to read a file with more than
%    ca.\ 200~lines.  The same error occurs if the first line of code
%    in \S 390 of \textsl{``TeX: The Program''\/} is missing.}
%    \begin{macrocode}
    \expandafter\verbatim@read@file
  \fi
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\verbatiminput}
%    |\verbatiminput| first starts a group to keep font and category
%    changes local.
%    Then it calls the macro |\verbatim@input| with additional
%    arguments, depending on whether an asterisk follows.
%
% PW. I added the TAB checking code.
%    \begin{macrocode}
\def\verbatiminput{\begingroup
  \ift@bs
    \def\verbatim@processline{\tabverbatim@processline}%
  \fi
  \@ifstar{\verbatim@input{\@maybeobeytabs}}%
          {\verbatim@input{\frenchspacing\@vobeyspaces\@maybeobeytabs}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\verbatim@input}
%    |\verbatim@input| first checks whether the file exists, using
%    the standard macro |\IfFileExists| which leaves the name of the
%    file found in |\@filef@und|.
%    Then everything is set up as in the |\verbatim| macro.
%    \begin{macrocode}
\def\verbatim@input#1#2{%
   \IfFileExists {#2}{\@verbatim #1\relax
%    \end{macrocode}
%    Then it reads in the file, finishes off the \texttt{trivlist}
%    environment started by |\@verbatim| and closes the group.
%    This restores everything to its normal settings.
%    \begin{macrocode}
    \verbatim@readfile{\@filef@und}\endtrivlist\endgroup\@doendpe}%
%    \end{macrocode}
%   If the file is not found a more or less helpful message is
%    printed. The final |\endgroup| is  needed to close the group
%    started in |\verbatiminput| above.
%    \begin{macrocode}
   {\typeout {No file #2.}\endgroup}}
%    \end{macrocode}
% \end{macro}
%
% \textit{That completes my borrowings from \Lpack{verbatim}.}
%
% The next bunch of code implements wrapping verbatim lines so they, hopefully,
% stay within the typeblock.
%
% \begin{macro}{\verbatimindent}
% \begin{macro}{\verbatimbreakchar}
% \begin{macro}{\setverbatimbreak}
% The length |\verbatimindent| is the distance continuation lines are indented
% from the left margin. |\verbatimbreakchar| is the character to indicate
% a wrapped line.
%    \begin{macrocode}
\newlength{\verbatimindent}
  \setlength{\verbatimindent}{3em}
\newcommand*{\verbatimbreakchar}{\char`\%}
\newcommand*{\setverbatimbreak}{%
  \vspace*{-\baselineskip}%
  \def\@xobeysp{~\discretionary{\verbatimbreakchar}%
    {\kern\verbatimindent}{}}%
}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\wrappingon}
% \begin{macro}{\wrapright}
% \begin{macro}{\wrappingoff}
% |\wrappingon| and |\wrappingoff| enable/prohibit wrapping. The
% default is |\wrappingoff|.
%
% The macro
% |\wrapright|  is used to set paragraph skips; without |\raggedright| lines
% may break at the first space \emph{outside} the text area
%    \begin{macrocode}
\newcommand{\wrappingon}{%
  \def\@xobeysp{~\discretionary{\verbatimbreakchar}%
    {\kern\verbatimindent}{}}%
  \def\wrapright{\raggedright}%
}
\newcommand{\wrappingoff}{%
  \def\@xobeysp{\leavevmode\penalty\@M\ }%
  \def\wrapright{}%
}
\wrappingoff

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% \subsection{Writing and boxing verbatim}
%
%    This bunch of code is from the \Lpack{moreverb} package.
%
% \begin{environment}{verbatimoutput}
% \verb?\begin{verbatimoutput}?\marg{filename} writes all text in its body to a
% file, the name of which it is given as an argument. (The code was
% written by Rainer Sch\"opf but the environment was called 
% \verb?verbatimwrite?). In the code below, uncommenting \\
% \verb?\catcode`\^^I=12? \\
% will result in TABs being written as \verb?^^I?.
% \changes{v1.3}{2002/09/27}{Changed name verbatimwrite to 
%               verbatimoutput (but neither were mentioned in the manual)}
%    \begin{macrocode}
\newwrite \verbatim@out
\def\verbatimoutput#1{%
  \@bsphack
  \immediate\openout \verbatim@out #1
  \let\do\@makeother\dospecials
  \catcode`\^^M\active %% \catcode`\^^I=12  
  \def\verbatim@processline{%
    \immediate\write\verbatim@out
      {\the\verbatim@line}}%
  \verbatim@start}
%    \end{macrocode}
%    \begin{macrocode}
\def\endverbatimoutput{%
  \immediate\closeout\verbatim@out
  \@esphack}
%    \end{macrocode}
% \end{environment}
%
%
%
% \begin{environment}{fboxverbatim}
% \texttt{fboxverbatim} puts the contents of a verbatim environment
% in a framing box. (PW: This was originally called |boxedverbatim|).
%
% (Written by Victor Eijkhout.)
%
% Bug fix (supplied by David Carlisle) 1995/12/28, marked
% \verb+%%%DPC%%%+
%
% First, redefine `processline' to produce only a line as wide
% as the natural width of the line
%
%    \begin{macrocode}
\def\fboxverbatim{\begingroup%
  \tabsoff %% PW otherwise box fills the width
  \def\verbatim@processline{%
    {\setbox0=\hbox{\the\verbatim@line}%
    \hsize=\wd0 \the\verbatim@line\par}}%
%    \end{macrocode}
%
% Now save the verbatim code in a box
%
%    \begin{macrocode}
  \@minipagetrue%%%DPC%%%
  \@tempswatrue%%%DPC%%%
  \setbox0=\vbox\bgroup \verbatim
}
%    \end{macrocode}
%
% At the end of the environment, we (umm) simply have to stick the
% results into a frame.
%
%    \begin{macrocode}
\def\endfboxverbatim{%
  \endverbatim
  \unskip\setbox0=\lastbox %%%DPC%%%
%    \end{macrocode}
%
% Now everything's in the box, so we can close it\dots
%
%    \begin{macrocode}
  \egroup
%    \end{macrocode}
%
% To change the code for centring, the next line needs a spot of
% hacking.
%
%    \begin{macrocode}
  \fbox{\box0}% <<<=== change here for centering,...
\endgroup}
%    \end{macrocode}
% \end{environment}
%
%
%
% \subsection{The shortvrb package}
%
% The following is the \Lpack{shortvrb} package code (from |doc.dtx|
% by Frank Mittelbach). It has been so useful to me that I wanted
% to include it in the class.
%
% CODE AND COMMENTARY IS BY FRANK MITTELBACH
%
% \begin{macro}{\MakeShortVerb}
%    \begin{macrocode}
\def\MakeShortVerb#1{%
  \expandafter\ifx\csname cc\string#1\endcsname\relax
%    \end{macrocode}
%    \begin{macrocode}
    \@shortvrbinfo{Made }{#1}%
    \add@special{#1}%
%    \end{macrocode}
% Then the character's current catcode is stored in |\cc\|\meta{c}.
%    \begin{macrocode}
    \expandafter
    \xdef\csname cc\string#1\endcsname{\the\catcode`#1}%
%    \end{macrocode}
% The character is spliced into the definition using the same trick as
% used in |\verb| (for instance), having activated |~| in a group.
%    \begin{macrocode}
    \begingroup
      \catcode`\~\active  \lccode`\~`#1%
      \lowercase{%
%    \end{macrocode}
% The character's old meaning is recorded in |\ac\|\meta{c} prior to
% assigning it a new one.
%    \begin{macrocode}
      \global\expandafter\let
         \csname ac\string#1\endcsname~%
      \gdef~{\verb~}}%
    \endgroup
%    \end{macrocode}
% Finally the character is made active.
%    \begin{macrocode}
    \global\catcode`#1\active
%    \end{macrocode}
% If we suspect that \meta{c} is already a short reference, we tell
% the user. Now he or she is responsible if anything goes wrong\,\dots
%    \begin{macrocode}
  \else
%    \end{macrocode}
%    \begin{macrocode}
    \@shortvrbinfo\@empty{#1 already}%
  \fi}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\DeleteShortVerb}
% Here's the means of undoing a |\MakeShortVerb|, for instance in a
% region where you need to use the character outside a verbatim
% environment.  It arranges for |\dospecials| and |\@sanitize| to be
% altered appropriately, restores the saved catcode and, if necessary,
% the character's meaning (as stored by 
% |\MakeShortVerb|).  If the catcode wasn't stored in
% |\cc\|\meta{c} (by |\MakeShortVerb|) the command is silently
% ignored. 
%    \begin{macrocode}
\def\DeleteShortVerb#1{%
  \expandafter\ifx\csname cc\string#1\endcsname\relax
  \else
%    \end{macrocode}
%    \begin{macrocode}
    \@shortvrbinfo{Deleted }{#1 as}%
    \rem@special{#1}%
    \global\catcode`#1\csname cc\string#1\endcsname
%    \end{macrocode}
% We must not forget to reset |\cc\|\meta{c}, otherwise the check in
% |\MakeShortVerb| for a repeated definition will not work.
%    \begin{macrocode}
    \global \expandafter\let \csname cc\string#1\endcsname \relax
    \ifnum\catcode`#1=\active
      \begingroup
        \catcode`\~\active   \lccode`\~`#1%
        \lowercase{%
          \global\expandafter\let\expandafter~%
          \csname ac\string#1\endcsname}%
      \endgroup \fi \fi}
%    \end{macrocode}
% \end{macro}
%
%  \begin{macro}{\@shortvrbinfo}
% Helper function for info messages.
%    \begin{macrocode}
\def\@shortvrbinfo#1#2{%
  \PackageInfo{shortvrb}{%
     #1\expandafter\@gobble\string#2 a short reference
                                          for \string\verb}}
%    \end{macrocode}
%  \end{macro}
%
% \begin{macro}{\add@special}
% This helper macro adds its argument to the
% |\dospecials| macro which is conventionally used by verbatim macros
% to alter the catcodes of the currently active characters.  We need
% to add |\do\|\meta{c} to the expansion of |\dospecials| after
% removing the character if it was already there to avoid multiple
% copies building up should |\MakeShortVerb| not be balanced by
% |\DeleteShortVerb| (in case anything that uses |\dospecials| cares
% about repetitions).
%    \begin{macrocode}
\def\add@special#1{%
  \rem@special{#1}%
  \expandafter\gdef\expandafter\dospecials\expandafter
    {\dospecials \do #1}%
%    \end{macrocode}
% Similarly we have to add |\@makeother\|\meta{c} to |\@sanitize|
% (which is used in things like |\index| to re-catcode all special
% characters except braces).
%    \begin{macrocode}
  \expandafter\gdef\expandafter\@sanitize\expandafter
    {\@sanitize \@makeother #1}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\rem@special}
% The inverse of |\add@special| is slightly trickier.  |\do| is
% re-defined to expand to nothing if its argument is the character of
% interest, otherwise to expand simply to the argument.  We can then
% re-define |\dospecials| to be the expansion of itself.  The space
% after |=`##1| prevents an expansion to |\relax|!
%    \begin{macrocode}
\def\rem@special#1{%
  \def\do##1{%
    \ifnum`#1=`##1 \else \noexpand\do\noexpand##1\fi}%
  \xdef\dospecials{\dospecials}%
%    \end{macrocode}
% Fixing |\@sanitize| is the same except that we need to re-define
% |\@makeother| which obviously needs to be done in a group. 
%    \begin{macrocode}
  \begingroup
    \def\@makeother##1{%
      \ifnum`#1=`##1 \else \noexpand\@makeother\noexpand##1\fi}%
    \xdef\@sanitize{\@sanitize}%
  \endgroup}
%    \end{macrocode}
% \end{macro}
%
% END OF MITTELBACH CODE AND COMMENTARY.
%
% \subsection{General verbatim boxing and line numbering}
%
%    A while ago I wrote a package that I never released. Here it is
% now, updated and improved. The package was based on code originally
% posted to \ctt{}  by Donald Arseneau on 13 July 2000.
%
% This is DA's posted code.
% \begin{verbatim}
% \RequirePackage{verbatim}
% \def\boxverbflag{14 }
%
% \def\boxedverbatim{%
%    \fboxsep=1em
%    \def\verbatim@processline{\leavevmode
%      \vrule\vbox{\advance\hsize-.8p@ \@@line
%        {\strut\kern\fboxsep\the\verbatim@line\hss}%
%         \kern\fbozsep}\vrule\par}%
%    \@verbatim % but make some replacement settings
% %   \ifdim\@totalleftmargin>\fboxsep \fboxsep\@totalleftmargin \fi
%    \leftskip\x@skip \rightskip\z@skip
%    \interlinepenalty\boxverbflag
%    \parfillskip\z@ plus\p@ minus\p@
%    \lineskip-\fboxsep \baselineskip\z@skip
%    \frenchspacing\@vobeyspaces
%    \boxverb@toprule
%    \verbatim@start}
%
% \def\endboxedverbatim{\hrule\endtrivlist}
%
% \@namefdef{boxedverbatim*}{\let\frenchspacing\@gobble \boxedverbatim}
% \@namefdef{endboxedverbatim*}{\hrule\endtrivlist}
%
% \output=\expandafter{\expandafter\boxverb@split \the\output}
%
% \def\boxverb@toprule{\hrule \nobreak \vskip-.1\p@
%    \@@line{\vrule height2\fboxsep \hss \vrule}}
%
% \def\boxverb@split{\ifnum\outputpenalty=\boxverbflag
%    \ifdim\dp\@cclv=\z@
%    \setbox\@cclv\vbox{\unvbox\@cclv\hrule\kern-.4pt}%
%    \null \kern-.7\topskip \boxverb@toprule
%    \fi\fi}
% \end{verbatim}
% That's the end of DA's posted code
%
% I have extended this to provide:
% \begin{itemize}
%  \item A (multipage) boxed verbatim
%  \item A (multipage) boxed verbatim input
%  \item Normally each `page' is boxed, but start/end rules can be switched off
%    at page boundaries
%  \item A heading can be put at the start of `continuation' pages
%  \item Lines of verbatim text can be numbered
%  \item Can be `degraded' to normal verbatim environment
%  \end{itemize}
%
%    Before proceeding with the main, here are some macros for aspects
% of line numbering.
%
% \begin{macro}{\linenumberfont}
% \begin{macro}{\vlvnumfont}
%  Set line numbering font(s).
%    \begin{macrocode}
\newcommand{\linenumberfont}[1]{\def\vlvnumfont{#1}}
%%% \linenumberfont{\small\rmfamily}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\linenumberfrequency}
% \begin{macro}{\linemodnum}
% Set numbering intervals (number modulo). Default is modulus 0.
%    \begin{macrocode}
\newcommand{\linenumberfrequency}[1]{%
  \ifnum #1< \@ne
    \def\linemodnum{0\relax}
    \bvcountlinesfalse
  \else
    \def\linemodnum{#1\relax}
    \bvcountlinestrue
  \fi
}
%%%%\linenumberfrequency{0}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\getthelinenumber}
% |\getthelinenumber|\marg{counter} returns |\thecounter| if it is exactly
% divisible by |\linenumberfrequency|, provided this is not zero.
%    \begin{macrocode}
\newcommand{\getthelinenumber}[1]{%
  \ifnum\@ne=\linemodnum
     \@nameuse{the#1}
  \else
    \ifnum\@ne<\linemodnum
      \@tempcnta=\@nameuse{c@#1}
      \divide\@tempcnta\linemodnum \multiply\@tempcnta\linemodnum
      \ifnum\@tempcnta=\@nameuse{c@#1}\@nameuse{the#1}\fi
    \fi
  \fi
}

%    \end{macrocode}
% \end{macro}
%
%    OK, on to the main code.
%
% \begin{macro}{\boxverbflag}
% \begin{macro}{\bvboxsep}
%    \begin{macrocode}
\def\boxverbflag{14 }
\newlength{\bvboxsep}      % user can change this
\setlength{\bvboxsep}{1em}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%    \begin{macrocode}
\newif\ifbvperpage % start/end lines on every page of multipage verbatim
\bvperpagetrue

%    \end{macrocode}
%
% \begin{macro}{\bvtopofpage}
% \begin{macro}{\b@vtop}
% Can use |\bvtopofpage| to put a heading above continued verbatims.
% For example \\
% |\bvtopofpage{\begin{center}\normalfont (Continued)\end{center}}| \\
% It only works for |\bvperpagetrue|.
%    \begin{macrocode}
\newcommand{\bvtopofpage}[1]{%
  \long\def\b@vtop{#1}
}
\def\b@vtop{}  % used in \boxverb@split for heading 

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\theb@vlinenumber}
% \begin{macro}{\resetbvlinenumber}
% |\resetbvlinenumber| reinitializes the line numbering.
%    \begin{macrocode}
\newcounter{bvlinectr}
\def\theb@vlinenumber{\getthelinenumber{bvlinectr}}
\newcommand{\resetbvlinenumber}{\setcounter{bvlinectr}{0}}
 
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\ifbvcountlines}
% TRUE for verbatim line numbers to be printed. Default is not
% to print them. Note that |\linenumberfrequency| twiddles with this.
%    \begin{macrocode}
\newif\ifbvcountlines   % TRUE to print line numbers of verbatim lines
  \bvcountlinesfalse
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\b@vdocount}
% \begin{macro}{\bvnumlength}
% Increment line number if counting. A line number is printed in
% a space width |\bvnumlength|, which is given a temporary value here.
%    \begin{macrocode}
\def\b@vdocount{\ifbvcountlines\stepcounter{bvlinectr}\fi}
\newlength{\bvnumlength}       
%% \settowidth{\bvnumlength}{\vlvnumfont 9999}
\settowidth{\bvnumlength}{\normalfont 999}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\ifbvcountinside}
% \begin{macro}{\bvnumbersinside}
% \begin{macro}{\bvnumbersoutside}
% Flag and commands for positioning the numbers. Default is to print
% them inside the box.
%    \begin{macrocode}
\newif\ifbvcountinside  % TRUE if line numbers inside box
  \bvcountinsidetrue
\newcommand{\bvnumbersinside}{\bvcountinsidetrue}
\newcommand{\bvnumbersoutside}{\bvcountinsidefalse}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\b@vdoinside}
% \begin{macro}{\b@vdooutside}
% Print numbers inside (outside) the box.
%    \begin{macrocode}
\def\b@vdoinside{%
  \ifbvcountlines\ifbvcountinside%
    \makebox[\bvnumlength][r]{%
      \vlvnumfont \theb@vlinenumber\space}%
  \fi\fi}

\def\b@vdooutside{%
  \ifbvcountlines\ifbvcountinside\else%
    \llap{\makebox[\bvnumlength][r]{%
      \vlvnumfont \theb@vlinenumber\space}}%
  \fi\fi}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\setupboxverb@line}
% Use a macro for the first main part of DA's code for |\boxedverbatim|, 
% adding lots of hooks.
%    \begin{macrocode}
\newcommand{\setupboxverb@line}{%
  \par
  \ifbvperpage
    \output=\expandafter{\expandafter\boxverb@split \the\output}
  \fi
%    \end{macrocode}
% \begin{macro}{\verbatim@processline}
%    \begin{macrocode}
  \def\verbatim@processline{\leavevmode
    \b@vdocount%
    \bvleftsidehook\vbox{\advance\hsize-.8\p@ \@@line
      {\b@vdooutside\strut\kern\bvboxsep%
       \b@vdoinside%
       \ift@bs
         \tabverbatim@processline
       \else
         \the\verbatim@line
       \fi
       \hss}%
     \kern\bvboxsep}\bvrightsidehook\par}%
}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\setupbox@verb}
% Use a macro for the second main part of DA's code, and integrate it
% with the other verbatim codes (e.g., include |\@maybeobeytags|).
%    \begin{macrocode}
\newcommand{\setupbox@verb}{%
  \leftskip\z@skip \rightskip\z@skip
  \interlinepenalty\boxverbflag
  \parfillskip\z@ plus\p@ minus\p@
  \lineskip-\bvboxsep \baselineskip\z@skip
  \frenchspacing\@vobeyspaces\@maybeobeytabs
  \boxverb@toprule
}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\boxedverbatim}
% \begin{macro}{\endboxedverbatim}
% \begin{macro}{\boxedverbatim*}
% \begin{macro}{\endboxedverbatim*}
% Given the two macros above, we can write a briefer version of DA's 
% |\boxedverbatim(*)|. 
% As noted by Zarko Cucej\footnote{\url{zarko.cucej@uni-mb.si}}, have to handle
% a following (no-) paragraph.
% \changes{v1.4}{2003/11/22}{Added \cs{@doendpe} to \cs{endboxedverbatim}
%                            (from patch v1.8)}
%    \begin{macrocode}
\def\boxedverbatim{\begingroup
  \setupboxverb@line
  \@verbatim
  \setupbox@verb
  \verbatim@start
}
\def\endboxedverbatim{\bvendrulehook\endtrivlist\endgroup\@doendpe}

\@namedef{boxedverbatim*}{\let\frenchspacing\@gobble \boxedverbatim}
\@namelet{endboxedverbatim*}\endboxverbatim

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
%    \begin{macrocode}
\def\boxverb@toprule{\bvtoprulehook
  \@@line{\bvleftsidehook \bvtopmidhook \bvrightsidehook}}

%    \end{macrocode}
%
% \begin{macro}{\bvendofpage}
% \cs{bvendofpage}\marg{spec} sets the boxed verbatim `end of page' to \meta{spec}.
% \changes{v1.618}{2005/09/02}{Added \cs{bvendofpage} (mempatch v2.3d)}
%    \begin{macrocode}
\newcommand*{\bvendofpage}[1]{%
  \def\boxverb@botpage{#1}}
\bvendofpage{\hrule\kern-.4pt}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\boxverb@split}
%    \begin{macrocode}
\def\boxverb@split{\ifnum\outputpenalty=\boxverbflag
  \ifdim\dp\@cclv=\z@
%%%%    \setbox\@cclv\vbox{\unvbox\@cclv\hrule\kern-.4pt}%
    \setbox\@cclv\vbox{\unvbox\@cclv\boxverb@botpage}%
    \null \kern-.7\topskip \b@vtop \boxverb@toprule
  \fi
\fi}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\bvtoprulehook}
% \begin{macro}{\bvendrulehook}
% \begin{macro}{\bvleftsidehook}
% \begin{macro}{\bvrightsidehook}
% \begin{macro}{\bvtopmidhook}
% The new hooks, for the top, bottom, left and right of the box.
%    \begin{macrocode}
\def\bvtoprulehook{\hrule \nobreak \vskip-.1\p@}
\def\bvendrulehook{\hrule}
\def\bvleftsidehook{\vrule}
\def\bvrightsidehook{\vrule}
\def\bvtopmidhook{\rule{0\p@}{2\bvboxsep} \hss}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\boxedverbatiminput}
% \begin{macro}{\boxedverbatim@input}
% |\boxedverbatiminput{filename}| read in filename contents as verbatim
%    \begin{macrocode}
\newcommand{\boxedverbatiminput}{\begingroup
  \@ifstar{\let\frenchspacing\@gobble 
           \boxedverbatim@input\relax}%
          {\boxedverbatim@input{\frenchspacing\@vobeyspaces}}}

%    \end{macrocode}
%
%    \begin{macrocode}
\def\boxedverbatim@input#1#2{%
  \setupboxverb@line
  \IfFileExists{#2}{\@verbatim #1\relax
  \setupbox@verb
    \verbatim@readfile{\@filef@und}%
      \bvendrulehook\endtrivlist\endgroup\@doendpe}%
  {\typeout {No file #2.}\endgroup}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% Some prepackaged boxing styles.
%
% \begin{macro}{\bvbox}
% Original (default) boxing.
%    \begin{macrocode}
\newcommand{\bvbox}{%
  \bvperpagetrue%
  \renewcommand{\bvtoprulehook}{\hrule \nobreak \vskip-.1\p@}%
  \renewcommand{\bvleftsidehook}{\vrule}%
  \renewcommand{\bvrightsidehook}{\vrule}%
  \renewcommand{\bvendrulehook}{\hrule}%
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\nobvbox}
% No boxing
%    \begin{macrocode}
\newcommand{\nobvbox}{%
  \bvperpagefalse%
  \renewcommand{\bvtoprulehook}{}%
  \renewcommand{\bvleftsidehook}{}%
  \renewcommand{\bvrightsidehook}{}%
  \renewcommand{\bvendrulehook}{}%
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\bvtopandtail}
% Head and foot horizontal lines only
%    \begin{macrocode}
\newcommand{\bvtopandtail}{%
  \bvperpagefalse%
  \renewcommand{\bvtoprulehook}{\hrule \nobreak \vskip-.1\p@}%
  \renewcommand{\bvleftsidehook}{}%
  \renewcommand{\bvrightsidehook}{}%
  \renewcommand{\bvendrulehook}{\hrule}%
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\bvsides}
% Side vertical lines only
%    \begin{macrocode}
\newcommand{\bvsides}{%
  \bvperpagefalse%
  \renewcommand{\bvtoprulehook}{\vskip 3ex}%
  \renewcommand{\bvleftsidehook}{\vrule}%
  \renewcommand{\bvrightsidehook}{\vrule}%
  \renewcommand{\bvendrulehook}{}%
}

%    \end{macrocode}
% \end{macro}
%
%
%
% \subsection{The framed package}
%
% The following code is the \Lpack{framed} package~\cite{FRAMED}
% by Donald Arseneau.
% \changes{v1.4}{2003/11/22}{Updated framed code to v0.8a of the package}
%
%
% \begin{macro}{\framed}
% \begin{macro}{\endframed}
% \begin{macro}{\shaded}
% \begin{macro}{\endshaded}
%    \begin{macrocode}
\let\framed\relax \let\endframed\relax
\let\shaded\relax \let\endshaded\relax

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% DA sent me a pre-release copy of v0.6 of his \Lpack{framed} 
% package~\cite{FRAMED}.
% We also discussed good ways of embedding it into the class. One result
% being that I defined some some items |\AtBeginDocument|, but this is
% no longer required as DA has changed \Lpack{framed} to cooperate
% with memoir.
%
% Here is a modified version of \file{framed.sty}. In particular
% I deleted the debugging code (lines starting with \verb?|?).
%
% \begin{verbatim}
% framed.sty   v 0.8a   21-Jul-2003
% Copyright (C) 1992-2003 by Donald Arseneau
% These macros may be freely transmitted, reproduced, or modified
% provided that this notice is left intact.
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create framed or shaded regions that can break across pages using 
% \begin{framed} ... \end{framed}    -- ordinary frame box (box at margin)
% \begin{shaded} ... \end{shaded}    -- shaded background (into margin)
%    ... leftbar ...                 -- line on left side
%
% The "framed" environment puts the text into "\fbox" with the
% settings "\fboxrule=\FrameRule" and "\fboxsep=\FrameSep".
% You can change these lengths (using "\setlength") and you
% can even change the definition of "\FrameCommand" to use
% much fancier boxes.
%
% In fact, the "shaded" environment just redefines "\FrameCommand"
% to use "\colorbox{shadecolor}" (and you have to define the
% color "shadecolor").
%
% A page break is allowed, and even encouraged, before the framed
% environment.  If you want to attach some text (a box title) to the
% frame, then the text should be inserted by \FrameCommand
%
% The contents of the framed regions are restricted: 
% Floats, footnotes, marginpars and head-line entries will be lost.
% (Some of these may be handled in a later version.)
% This package will not work with the page breaking of multicol.sty,
% or other systems that perform column-balancing.
%
% The MakeFramed environment does the work. Its "settings" argument
% should contain any adjustments to the text width (applied to \hsize,
% and using the "\width" of the frame itself) as well as a `restore'
% command -- \@parboxrestore or \FrameRestore or something similar.
%
% Expert commands:
% \MakeFramed, \endMakeFramed: the "MakeFramed" environment
% \FrameCommand: command to draw the frame around its argument
% \FrameRestore: restore some text settings, but fewer than \@parboxrestore
% \FrameRule: length register; \fboxrule for default "framed".
% \FrameSep: length register; \fboxsep for default "framed".
% \FrameHeightAdjust: macro; height of frame above baselineskip at 
%                     top of page
%
% This is still a `pre-production' version because I can think of many
% features/improvements that should be made.  Nevertheless, starting
% with version 0.5 it should be bug-free.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \end{verbatim}
%
%    \begin{macrocode}
%%%%%%%%%%%% BEGIN FRAMED %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% PW -- can't have \ProvidesPpackage
%%\ProvidesPackage{framed}[2003/07/21 v 0.8a: 
%%    framed or shaded text with page breaks]

%    \end{macrocode}
%
% \begin{macro}{\ifframeasnormal}
%    \begin{macrocode}
%% PW added \ifframeasnormal % TRUE normal paras, FALSE minipage paras
%% PW default is (FALSE to match DA v0.5, or TRUE to match DA v0.6)
\newif\ifframeasnormal
  \frameasnormaltrue

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\endMakeFramed}
% \begin{environment}{framed}
% \begin{environment}{shaded}
% \begin{environment}{leftbar}
%    \begin{macrocode}
%% PW Define some things AtBeginDocument if they have not already
%%    been defined via someone using the framed package.
\AtBeginDocument{\@ifundefined{framed}{%
\def\endMakeFramed{\par
     \kern\z@ \penalty-100 % put depth into height
 \egroup
 \begingroup \put@frame \endgroup}

\newenvironment{framed}% using default \FrameCommand
  {\MakeFramed {\advance\hsize-\width \FrameRestore}}%
  {\endMakeFramed}

\newenvironment{shaded}{%
  \def\FrameCommand{\colorbox{shadecolor}}%
  \MakeFramed {\FrameRestore}}%
 {\endMakeFramed}

\newenvironment{leftbar}{%
  \def\FrameCommand{\vrule width 3pt \hspace{10pt}}%
  \MakeFramed {\advance\hsize-\width \FrameRestore}}%
 {\endMakeFramed}

}{}} % end of AtBeginDocument

%    \end{macrocode}
% \end{environment}
% \end{environment}
% \end{environment}
% \end{macro}
%
% \begin{macro}{\MakeFramed}
%    \begin{macrocode}
%% \def\MakeFramed#1{\par         %% PW change to \newcommand
\newcommand{\MakeFramed}[1]{\par
 % measure added width and height; call result \width and \height
 \setbox\z@\vbox{\vskip-1in \hbox{\hskip-1in 
   \FrameCommand{\hbox{\vrule \@height .7in \@depth.3in \@width 1in}}}%
   \vskip\z@skip}%
 \def\width{\wd\z@}\def\height{\ht\z@}%
 \edef\fb@frw{\the\width}\edef\fb@frh{\the\height}%
 % insert pre-penalties and skips
 \begingroup
 \skip@\lastskip
 \if@nobreak\else 
    \penalty9999 % updates \page parameters
    \ifdim\pagefilstretch=\z@ \ifdim\pagefillstretch=\z@
       \edef\@tempa{\the\skip@}%
       \ifx\@tempa\zero@glue \penalty-30
       \else \vskip-\skip@ \penalty-30 \vskip\skip@
    \fi\fi\fi
    \penalty\z@
    % Give a stretchy breakpoint that will always be taken in preference
    % to the \penalty 9999 used to update page parameters. The cube root
    % of 10000/100 indicates a multiplier of 0.21545, but the maximum 
    % calculated badness is really 8192, not 10000, so the multiplier
    % is 0.2301.  
    \advance\skip@ \z@ plus-.5\baselineskip
    \advance\skip@ \z@ plus-.231\height
    \advance\skip@ \z@ plus-.231\skip@
    \advance\skip@ \z@ plus-.231\topsep
    \vskip-\skip@ \penalty 1800 \vskip\skip@
 \fi
 \addvspace{\topsep}%
 \endgroup
 % clear out pending page break
 \penalty\@M \vskip 2\baselineskip \vskip\height
 \penalty9999 \vskip -2\baselineskip \vskip-\height
 \penalty9999 % updates \pagetotal
 \fb@adjheight 
 \setbox\@tempboxa\vbox\bgroup
   #1% Modifications to \hsize (can use \width and \height)
   \textwidth\hsize \columnwidth\hsize 
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\put@frame}
% |\put@frame| takes the contents of |\@tempboxa| and puts all, or a piece,
% of it on the page with a frame (|\FrameCommand|).  It recurses until
% all of |\@tempboxa| has been used up. (|\@tempboxa| must have zero depth.)
%    \begin{macrocode}
\def\put@frame{\relax         
 \ifdim\pagegoal=\maxdimen \pagegoal\vsize \fi
 \ifinner \else
    \dimen@\pagegoal \advance\dimen@-\pagetotal % space left on page
  \ifdim\dimen@<2\baselineskip 
    \eject \fb@adjheight \put@frame
  \else % there's appreciable room left on the page
    \begingroup % temporarily set \dimen@ to be...
    \advance\dimen@.8\pageshrink  % maximum space available on page
    \advance\dimen@-\fb@frh\relax % space for frame's contents
    \expandafter\endgroup
    % restore \dimen@ to real room left on page
    \ifdim\dimen@>\ht\@tempboxa % whole box does fit
    \else % box must be split
      \setbox\@tempboxa\vbox{% simulate frame and flexiblity of page:
        \vskip \fb@frh \@plus\pagestretch \@minus.8\pageshrink
        \kern137sp\kern-137sp\penalty-30 
        \unvbox\@tempboxa}%
      \edef\fb@resto@set{\boxmaxdepth\the\boxmaxdepth 
                         \splittopskip\the\splittopskip}%  % v0.8a
      \boxmaxdepth\z@ \splittopskip\z@
      \setbox\tw@\vsplit\@tempboxa to\dimen@
      \setbox\tw@\vbox{\unvbox\tw@}% natural-sized
      % If the split-to size > (\vsize-\topskip),
      % then set box to full size
      \begingroup
        \advance\dimen@\topskip
        \expandafter\endgroup
      \ifdim\dimen@>\pagegoal
        \dimen@ii\pagegoal
        \advance\dimen@ii -\topskip
        \advance\dimen@ii \FrameHeightAdjust\relax
      \else  % suspect this is wrong:
        % If the split-to size > feasible room_on_page, 
        % rebox it smaller.
        \advance\dimen@.8\pageshrink
        \ifdim\ht\tw@>\dimen@
          \dimen@ii\dimen@
        \else % use natural size
          \dimen@ii\ht\tw@
        \fi
      \fi
      % Re-box contents to desired size \dimen@ii
      \advance\dimen@ii -\fb@frh
      \setbox\tw@\vbox to\dimen@ii \bgroup
      % remove simulated frame and page flexibility:
      \vskip -\fb@frh \@plus-\pagestretch \@minus-.8\pageshrink
      \unvbox\tw@ \unpenalty\unpenalty
      \ifdim\lastkern=-137sp % whole box went to next page
        \egroup \fb@resto@set \eject 
%%%              % (\vskip for frame size was discarded) % v 0.8a
        \fb@adjheight
      \else %
        \egroup \fb@resto@set  % v0.8a
        \ifvoid\@tempboxa % it all fit after all
           \setbox\@tempboxa\box\tw@
        \else % it really did split
          \ifdim\wd\tw@>\z@
            \centerline{\FrameCommand{\box\tw@}}% ? \centerline bad idea
           \else
             \box\tw@
           \fi
           \hrule \@height\z@
           \eject
           \fb@adjheight
           \put@frame
  \fi\fi\fi\fi\fi
  \ifvoid\@tempboxa\else
    \centerline{\FrameCommand{\box\@tempboxa}}%
    \nointerlineskip \null %{\showoutput \showlists}
    \penalty-30 \vskip\topsep
  \fi}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\fb@adjheight}
%    \begin{macrocode}
\def\fb@adjheight{%         
  \vbox to\FrameHeightAdjust{}% get proper baseline skip from above.
  \penalty\@M \nointerlineskip
  \vskip-\FrameHeightAdjust
  \penalty\@M} % useful for tops of pages

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\zero@glue}
%    \begin{macrocode}
\edef\zero@glue{\the\z@skip}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\FrameCommand}
% \begin{macro}{\FrameHeightAdjust}
%    \begin{macrocode}
%% Define configuration commands:
\def\FrameCommand{\fboxrule=\FrameRule \fboxsep=\FrameSep \fbox}
\newdimen\FrameRule \FrameRule= \fboxrule
\newdimen\FrameSep  \FrameSep = 3\fboxsep

%% Height of frame above first baseline when frame starts a page:
%%\def\FrameHeightAdjust{6pt} % PW changed the value
\def\FrameHeightAdjust{0.6em}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\FrameRestore}
% |\FrameRestore| has parts of |\@parboxrestore|. See how it is used in the
% `settings' argument of |\MakeFrame|. Previous behaviour can be restored by 
% using |\@parboxrestore| there, or redefining: 
% \begin{verbatim}
% \makeatletter 
%   \renewcommand{\FrameRestore}{\@parboxrestore} 
% \makeatother
% \end{verbatim}
% However, PW's |\ifframeasnormal| obviates the need for this.
% \changes{v1.618}{2005/09/03}{Corrected spelling of \cs{@dischyph} in \cs{FrameRestore} (mempatch v3.5)}
%    \begin{macrocode}
%% PW: I have added my \ifframeasnormal to the original \FrameRestore
%%     The commented out lines are commented out by DA in the 
%%     framed package

%\def\FrameRestore{%         %% PW change to \newcommand
\newcommand{\FrameRestore}{%
  \let\if@nobreak\iffalse
  \let\if@noskipsec\iffalse
  \let\-\@dischyph
  \let\'\@acci\let\`\@accii\let\=\@acciii
  \linewidth\hsize
  \sloppy
\ifframeasnormal\else
  \let\par\@@par
  \parindent\z@ \parskip\z@skip  
  \everypar{}%
  \@totalleftmargin\z@
  \leftskip\z@skip \rightskip\z@skip \@rightskip\z@skip
  \parfillskip\@flushglue \lineskip\normallineskip
  \baselineskip\normalbaselineskip
  \let\\\@normalcr
\fi
}

%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
% Compatability with previous versions (temporary!):
%%\let\fram@d=\MakeFramed  \let\endfram@d=\endMakeFramed

%%%%%%%%%%%% END FRAMED %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%    \end{macrocode}
%
% \subsection{The newfile package}
%
% The following code is from the \Lpack{newfile} package.
% To try and avoid name clashes with other packages, each internal macro
% in this package includes the character string `|stre@m|'.
%
% \begin{macro}{\newoutputstream}
% |\newoutputam|\marg{stream} creates a new output stream called \meta{stream}.
% Different files may be associated with the \meta{stream}. Note that
% TeX permits no more than 16 output streams.
%    \begin{macrocode}
\newcommand{\newoutputstream}[1]{%
  \@ifundefined{#1outstre@m}%
    {\expandafter\newwrite\csname #1outstre@m\endcsname
     \csname newif\expandafter\endcsname
       \csname ifstre@m#1open\endcsname
     \global\csname stre@m#1openfalse\endcsname
     \expandafter\ifx\csname atstreamopen#1\endcsname\relax
       \global\@namedef{atstreamopen#1}{}%
     \fi
     \expandafter\ifx\csname atstreamclose#1\endcsname\relax
       \global\@namedef{atstreamclose#1}{}%
     \fi
    }%
    {\@memwarn{Output stream #1\space is already defined}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\newinputstream}
% |\newinputstream|\marg{stream} creates a new input stream called \meta{stream}.
% Different files may be associated with the \meta{stream}. Note that
% TeX permits no more than 16 input streams.
%    \begin{macrocode}
\newcommand{\newinputstream}[1]{%
  \@ifundefined{#1instre@m}%
    {\expandafter\newread\csname #1instre@m\endcsname
     \csname newif\expandafter\endcsname
       \csname ifstre@m#1open\endcsname
     \global\csname stre@m#1openfalse\endcsname
     \expandafter\ifx\csname atstreamopen#1\endcsname\relax
       \global\@namedef{atstreamopen#1}{}%
     \fi
     \expandafter\ifx\csname atstreamclose#1\endcsname\relax
       \global\@namedef{atstreamclose#1}{}%
     \fi
    }%
    {\@memwarn{Input stream #1\space is already defined}}}

%    \end{macrocode}
% \end{macro}
%
%    Some checking macros will be useful as some of the checks occur in
% multiple places.
%
% \begin{macro}{\IfStreamOpen}
% |\IfStreamOpen|\marg{stream}\marg{TRUE code}\marg{FALSE code}
% checks if stream \meta{stream} is currently open.
%    \begin{macrocode}
\newcommand{\IfStreamOpen}[3]{%
  \csname ifstre@m#1open\endcsname#2\else#3\fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\instre@mandopen}
% |\instre@mandopen|\marg{stream}\marg{TRUE code} checks if \meta{stream}
% is an input stream and is open. If so, it executes \meta{TRUE code}.
%    \begin{macrocode}
\newcommand{\instre@mandopen}[2]{%
  \@ifundefined{#1instre@m}{%
    \@memwarn{#1\space is not an input stream}}%
  {\IfStreamOpen{#1}{#2}{%
    \@memwarn{Input stream #1\space is not open}}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\instre@mandclosed}
% |\instre@mandclosed|\marg{stream}\marg{TRUE code} checks if \meta{stream}
% is an input stream and is closed (not open). 
% If so, it executes \meta{TRUE code}.
%    \begin{macrocode}
\newcommand{\instre@mandclosed}[2]{%
  \@ifundefined{#1instre@m}{%
    \@memwarn{#1\space is not an input stream}}%
  {\IfStreamOpen{#1}{%
    \@memwarn{Input stream #1\space is open}}{#2}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\outstre@mandopen}
% |\outstre@mandopen|\marg{stream}\marg{TRUE code} checks if \meta{stream}
% is an output stream and is open. If so, it executes \meta{TRUE code}.
%    \begin{macrocode}
\newcommand{\outstre@mandopen}[2]{%
  \@ifundefined{#1outstre@m}{%
    \@memwarn{#1\space is not an output stream}}%
  {\IfStreamOpen{#1}{#2}{%
    \@memwarn{Output stream #1\space is not open}}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\outstre@mandclosed}
% |\outstre@mandclosed|\marg{stream}\marg{TRUE code} checks if \meta{stream}
% is an output stream and is closed (not open). 
% If so, it executes \meta{TRUE code}.
%    \begin{macrocode}
\newcommand{\outstre@mandclosed}[2]{%
  \@ifundefined{#1outstre@m}{%
    \@memwarn{#1\space is not an output stream}}%
  {\IfStreamOpen{#1}{%
    \@memwarn{Output stream #1\space is open}}{#2}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\openoutputfile}
% |\openoutputfile|\marg{filename}\marg{stream} opens the file called 
% \meta{filename} and attaches it to the stream \meta{stream} for
% writing. However, if the |\nofiles| command has been given the
% file is \emph{not} attached to the stream. No more than one file can be 
% attached to a stream at any given time.
%    \begin{macrocode}
\newcommand{\openoutputfile}[2]{%
  \outstre@mandclosed{#2}{%
    \global\@namedef{#1@filename}{#1}%
    \if@filesw 
      \immediate\openout\@nameuse{#2outstre@m}=\@nameuse{#1@filename}%
    \fi
    \global\csname stre@m#2opentrue\endcsname%
    \@nameuse{atstreamopen#2}%
  }%
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\closeoutputstream}
% |\closeoutputstream|\marg{stream} closes the stream \meta{stream}.
%    \begin{macrocode}
\newcommand{\closeoutputstream}[1]{%
  \outstre@mandopen{#1}{%
    \@nameuse{atstreamclose#1}%
    \immediate\closeout\@nameuse{#1outstre@m}%
    \global\csname stre@m#1openfalse\endcsname}%
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\openinputfile}
% |\openinputfile|\marg{filename}\marg{stream} opens the file called 
% \meta{filename} and attaches it to the stream \meta{stream} for
% reading. The file is added to the list of files. 
% No more than one file can be attached to a stream at
% any given time.
%    \begin{macrocode}
\newcommand{\openinputfile}[2]{%
  \IfFileExists{#1}{%                   file exists
    \instre@mandclosed{#2}{% 
      \@addtofilelist{#1}%
      \global\@namedef{#1@filename}{#1}%
      \immediate\openin\@nameuse{#2instre@m}=\@nameuse{#1@filename}%
      \global\csname stre@m#2opentrue\endcsname%
      \@nameuse{atstreamopen#2}}}%
  {%                                    file not found
    \typeout{No file #1.}
  }%
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\closeinputstream}
% |\closeinputstream|\marg{stream} closes the stream \meta{stream}.
%    \begin{macrocode}
\newcommand{\closeinputstream}[1]{%
  \instre@mandopen{#1}{%
     \@nameuse{atstreamclose#1}%
     \immediate\closein\@nameuse{#1instre@m}%
     \global\csname stre@m#1openfalse\endcsname}%
}

%    \end{macrocode}
% \end{macro}
%
% \begin{environment}{writeverbatim}
% |\begin{writeverbatim}|\marg{stream} writes the contents of
% the environment as verbatim text to the given \meta{stream}.
%    \begin{macrocode}
\def\writeverbatim#1{%
  \@bsphack
  \let\do\@makeother\dospecials
  \catcode`\^^M\active
  \def\verbatim@processline{%
    \immediate\write\@nameuse{#1outstre@m}{\the\verbatim@line}}%
  \verbatim@start}
\def\endwriteverbatim{\@esphack}

%    \end{macrocode}
% \end{environment}
%
% \begin{macro}{\addtostream}
% |\addtostream|\marg{stream}\marg{text} writes \meta{text} to the
% given \meta{stream}.
%    \begin{macrocode}
\newcommand{\addtostream}[2]{%
  \@bsphack
  \outstre@mandopen{#1}{%
    {\let\protect\string
     \immediate\write\@nameuse{#1outstre@m}{#2}%
    }}%
  \@esphack
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ifstre@mnoteof}
% \begin{macro}{\checkstre@mnoteof}
% |\checkstre@mnoteof|\marg{stream} sets |\ifstre@mnoteof| to TRUE if
% \meta{stream} is not at the end of the file (i.e., it is the opposite
% of |\ifeof|).
%    \begin{macrocode}
\newif\ifstre@mnoteof
\newcommand{\checkstre@meof}[1]{%
  \stre@mnoteoftrue\ifeof\@nameuse{#1instre@m}\stre@mnoteoffalse\fi}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\readstream}
% |\readstream|\marg{stream} reads the contents of
% the given \meta{stream} as |\input| text.
%    \begin{macrocode}
\def\readstream#1{
  \instre@mandopen{#1}{%
    \loop \checkstre@meof{#1} \ifstre@mnoteof
      \read\@nameuse{#1instre@m} to\temptokstre@m
     \temptokstre@m
    \repeat
    }%
}
  
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\readaline}
% |\readaline|\marg{stream} reads what TeX considers to be one line
% from the given \meta{stream} as |\input| text.
%    \begin{macrocode}
\def\readaline#1{
  \instre@mandopen{#1}{%
    \ifeof\@nameuse{#1instre@m}
      \@memwarn{No more to read from stream #1}
    \else
      \read\@nameuse{#1instre@m} to\temptokstre@m
      \temptokstre@m
    \fi
    }%
}
  
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\readverbatim}
% |\readverbatim|\marg{stream} reads the contents of
% the given \meta{stream} as verbatim text.
%
% The read verbatim code is a slight variation on code from the
% \Lpack{verbatim} package. Most of the setup is done by the macros
% |\stre@mverb@input|\marg{setup}\marg{stream} and
% |\verbatim@readstre@m|\marg{stream}. Finally, |\verbatim@read@file|
% is a \Lpack{verbatim} package macro.
%    \begin{macrocode}
\def\readverbatim{\begingroup
  \ift@bs
    \def\verbatim@processline{\tabverbatim@processline}%
  \fi
  \@ifstar{\stre@mverb@input{\@maybeobeytabs}}%
     {\stre@mverb@input{\frenchspacing\@vobeyspaces\@maybeobeytabs}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\stre@mverb@input}
% \begin{macro}{\@verbinstre@m}
% |\stre@mverb@input|\marg{setup}\marg{stream} is a stream
% version of |\verbatim@input|. It defines |\@verbinstre@m|
% to be \meta{stream}.
%
%    \begin{macrocode}
\newcommand{\stre@mverb@input}[2]{%
  \IfStreamOpen{#2}%
    {\@verbatim #1\relax
     \def\@verbinstre@m{\@nameuse{#2instre@m}}
     \verb@readstre@m\endtrivlist\endgroup\@doendpe}%
    {\@memwarn{Stream #2\space is not open}\endgroup}%
}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\verb@readstre@m}
% |\verb@readstre@m| is the stream analogue to |\verbatim@readfile|.
%    \begin{macrocode}
\newcommand{\verb@readstre@m}{%
  \verbatim@startline
  \expandafter\endlinechar\expandafter\m@ne
  \expandafter\verbatim@read@stre@m
  \expandafter\endlinechar\the\endlinechar\relax
  \verbatim@finish
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\verbatim@read@stre@m}
% |\verbatim@read@stre@m| is the analogue to |\verbatim@readfile|.
% It gets its input from the stream |\@verbinstre@m|, which is defined
% in the |\stre@mverb@input| macro.
%    \begin{macrocode}
\newcommand{\verbatim@read@stre@m}{%
  \read\@verbinstre@m to\next
  \ifeof\@verbinstre@m
  \else
    \expandafter\verbatim@addtoline\expandafter{\next}%
    \verbatim@processline
    \verbatim@startline
    \expandafter\verbatim@read@stre@m
  \fi
}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\readboxedverbatim}
% |\readboxedverbatim|\marg{stream} is the equivalent of
% |\readverbtim| except that it generates a boxed verbatim.
%    \begin{macrocode}
\newcommand{\readboxedverbatim}{\begingroup
  \@ifstar{\stre@mbvin\relax}%
          {\stre@mbvin{\frenchspacing\@vobeyspaces}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\stre@mbvin}
% |\stre@mbvin|\marg{setup}\marg{stream} is the workhorse for
% |\readboxedverbatim|, and in its turn it uses |\verb@readstre@m|.
%    \begin{macrocode}
\newcommand{\stre@mbvin}[2]{%
  \IfStreamOpen{#2}%
    {\setupboxverb@line
     \@verbatim #1\relax
     \def\@verbinstre@m{\@nameuse{#2instre@m}}%
     \setupbox@verb
     \verb@readstre@m\bvendrulehook\endtrivlist\endgroup\@doendpe}%
    {\@memwarn{Stream #2\space is not open}\endgroup}%
}

%    \end{macrocode}
% \end{macro}
%
%
%
% \section{Utilities}
%
%
% \subsection{Extra `provide' commands}
%
% \begin{macro}{\provideenvironment}
% Works like |\providecommand| but for environments.
% \changes{v1.2}{2002/08/04}{Added \cs{provideenvironment} and supports}
%    \begin{macrocode}
\newcommand{\provideenvironment}{\@star@or@long\m@mprovenv}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@mprovenv}
% This checks if the environment has been defined. If not it calls
% the kernel code for |\newenvironment| otherwise it calls code to
% discard arguments.
%    \begin{macrocode}
\newcommand{\m@mprovenv}[1]{\@ifundefined{#1}%
  {\new@environment{#1}}%       % create new environment
  {\@memwarn{Environment `#1' already defined}%
   \m@mgobbleoptsandtwo}}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\m@mgobbleoptsandtwo}
% \begin{macro}{\m@mgobbleoptandtwo}
% |\m@mgobbleoptsandtwo| gobbles the |[opt][opt]{begin}{end}| arguments to 
% |\provideenvironment|. It is actually more general than that as it will 
% gobble any sequence of any number of optional arguments followed by
% two required arguments. If there are no optional arguments it disposes
% of the two required ones, other it calls a recursive procedure to
% discard any number of optionals, then two required ones.
%
% |\m@mgobbleoptandtwo| recursively discards optional arguments, then finally
% two required arguments.
%    \begin{macrocode}
\newcommand{\m@mgobbleoptsandtwo}{%
  \@ifnextchar [{\m@mgobbleoptandtwo}{\@gobbletwo}}
\def\m@mgobbleoptandtwo[#1]{%
  \@ifnextchar [{\m@mgobbleoptandtwo}{\@gobbletwo}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
%
%
% \begin{macro}{\providecounter}
% Works like the other |\provide...| commands, but for counters.
% Code is based on |\newcounter| in |ltcounts.dtx|.
% \changes{v1.2}{2002/07/17}{Added \cs{providecounter}}
%    \begin{macrocode}
\newcommand*{\providecounter}[1]{%
  \@ifundefined{c@#1}%
    {\newcounter{#1}}%
    {\@memwarn{Counter `#1' already defined}%
      \@ifnextchar[{\m@mgobbleopt}{}}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@mgobbleopt}
% Gobble an optional argument.
% \changes{v1.2}{2002/07/07}{Added \cs{m@mgobbleopt}}
%    \begin{macrocode}
\def\m@mgobbleopt[#1]{}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\providelength}
% Works like the other |\provide...| commands, but for lengths.
% Code is based on |\provide@command| in |ltdefns.dtx|.
% \changes{v1.2}{2002/07/17}{Added \cs{providelength}}
%    \begin{macrocode}
\newcommand*{\providelength}[1]{%
  \begingroup
    \escapechar\m@ne\xdef\@gtempa{{\string#1}}%
  \endgroup
  \expandafter\@ifundefined\@gtempa
    {\newlength{#1}}%
    {\@memwarn{Length #1 already defined}}%
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\newloglike}
% \begin{macro}{\m@mnewlog}
% \begin{macro}{\m@mnewlogs}
% For defining a new log-like math function.
% |\newloglike{\fun}{string}| or |\newloglike*{\fun}{string}|.
% The starred version is for functions with limits (like an integral sign).
% \changes{v1.6}{2004/01/28}{Added \cs{newloglike}}
%    \begin{macrocode}
\newcommand*{\newloglike}{\@ifstar{\m@mnewlogs}{\m@mnewlog}}
\newcommand*{\m@mnewlogs}[2]{%
  \newcommand*{#1}{\mathop{\operator@font #2}}}
\newcommand*{\m@mnewlog}[2]{%
  \newcommand*{#1}{\mathop{\operator@font #2}\nolimits}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% 
% \begin{macro}{\provideloglike}
% \begin{macro}{\m@mprovlogs}
% \begin{macro}{\m@mprovlog}
%  The provide version of |\newloglike|.
% \changes{v1.6}{2004/01/28}{Added \cs{provideloglike}}
%    \begin{macrocode}
\newcommand*{\provideloglike}{\@ifstar{\m@mprovlogs}{\m@mprovlog}}
\newcommand*{\m@mprovlogs}[2]{%
  \providecommand*{#1}{\mathop{\operator@font #2}}}
\newcommand*{\m@mprovlog}[2]{%
  \providecommand*{#1}{\mathop{\operator@font #2}\nolimits}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% 
%
% \subsection{Changing counters}
%
%     In LaTeX, a new counter called, say `ctr', is created by the command
% |\newcounter{ctr}[within]|. If the optional within argument is given
% the the counter `ctr' is reset to zero each time the counter `within'
% changes. The command |\thectr| typesets the value of the counter ctr.
% This is automatically defined by |\newcounter| and is initialised
% to typeset arabic numerals. Sometimes it may be desireable to change
% the counter definitions.
%
% The following code is based on the \Lpack{chngcntr} package~\cite{CHNGCNTR}.
%
% \begin{macro}{\@removefromreset}
% This code uses David Carlisle's |\@removefromreset| command as
% specified in the remreset package~\cite{REMRESET}.
% It is provided here as a convenience to the user, and with
% David Carlisle's permission.
%
% START OF DAVID CARLISLE'S CODE
%    \begin{macrocode}
\providecommand{\@removefromreset}[2]{{%
  \expandafter\let\csname c@#1\endcsname\@removefromreset
  \def\@elt##1{%
    \expandafter\ifx\csname c@##1\endcsname\@removefromreset
    \else
      \noexpand\@elt{##1}%
    \fi}%
  \expandafter\xdef\csname cl@#2\endcsname{%
    \csname cl@#2\endcsname}}}

%    \end{macrocode}
% END OF DAVID CARLISLE'S CODE
% \end{macro}
%
% \begin{macro}{\@ifbothcntrs}
% This is called as \\
% |\@ifbothcntrs{|\meta{cntr}|}{|\meta{within}|}{<code when both are counters>}|. 
% This tests if both \meta{cntr} and \meta{within} are counters.
%    \begin{macrocode} 
\newcommand{\@ifbothcntrs}[3]{%
  \@ifundefined{c@#1}{% counter undefined
    \@memerror{#1 is not a counter}{\@eha}}%
  {% else counter is defined
    \@ifundefined{c@#2}{% within undefined
      \@memerror{#2 is not a counter}{\@eha}}%
    {% else both counter and within  are defined
     #3}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\counterwithin}
% \begin{macro}{\counterwithin*}
% \begin{macro}{\@csinstar}
% \begin{macro}{\@csin}
%    It is sometimes desireable to change a counter that has been defined
% by |\newcounter{ctr}| to act as though it had been defined as 
% |\newcounter{ctr}[within]|. The command
% |\counterwithin{ctr}{within}| accomplishes this. By default,
% it also redefines the |\thectr| command so that it typesets values
% in the style |\thewithin.\arabic{ctr}|. The starred version of the
% command suppresses the redefinition of |\thectr|. 
%
%    \begin{macrocode}
\newcommand{\counterwithin}{\@ifstar{\@csinstar}{\@csin}}
\newcommand{\@csinstar}[2]{%
  \@ifbothcntrs{#1}{#2}{\@addtoreset{#1}{#2}}}
\newcommand{\@csin}[2]{%
  \@ifbothcntrs{#1}{#2}{\@addtoreset{#1}{#2}%
                        \@namedef{the#1}{\@nameuse{the#2}.\arabic{#1}}}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\counterwithout}
% \begin{macro}{\counterwithout*}
% \begin{macro}{\@csoutstar}
% \begin{macro}{\@csout}
%    Likewise, the command |\counterwithout{ctr}{within}| changes a
% counter that has been created by |\newcounter{ctr}[within]| to act
% as though it had been created by |\newcounter{ctr}|. By default it
% also redefines the |\thectr| command so that it just typesets an arabic
% numeral. The starred version of the command suppresses the redefinition
% of |\thectr|.
% \changes{v1.618}{2005/09/03}{Deleted chngctr from \cs{@csoutstar} (mempatch v3.4)}
%    \begin{macrocode}
\newcommand{\counterwithout}{\@ifstar{\@csoutstar}{\@csout}}
\newcommand{\@csoutstar}[2]{%
  \@ifbothcntrs{#1}{#2}{\@removefromreset{#1}{#2}}}
\newcommand{\@csout}[2]{%
  \@ifbothcntrs{#1}{#2}{\@removefromreset{#1}{#2}%
                        \@namedef{the#1}{\arabic{#1}}}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
%    Any number of |\counterwithin{ctr}{...}| and |\counterwithout{ctr}{...}|
% commands can be issued for a given counter, ctr, if you wish to toggle
% between the two styles. The current value of ctr is unaffected by
% |\counterwithin| and |\counterwithout|. If you want to change the value
% after one of these commands, use |\setcounter{ctr}{...}|, and to change
% the typeseting style use |\renewcommand{\thectr}{...}|.
%
% \subsection{Odd/even page checking}
%
% It is difficult to check robustly if the current page is odd or even.
% This code, which is based on the \Lpack{chngpage} package~\cite{CHNGPAGE}, 
% provides a method that has proved (so far) to be robust.
%    It works by writing out a label and on the next LaTeX run checks
% the page reference for the label.
%
% \begin{macro}{\ifoddpage}
% \begin{macro}{\ifstrictpagecheck}
% \begin{macro}{\c@cp@cntr}
% \begin{macro}{\cplabel}
% The boolean |\ifoddpage| is TRUE if the checked page is odd. The
% boolean |\ifstrictpagecheck| is for turning on (TRUE) and off (FALSE)
% the strictest method of page checking. The counter |cp@cntr| is used
% to make unique labels, which start with |\cplabel|.
%    \begin{macrocode}
\newif\ifoddpage
\newif\ifstrictpagecheck
  \strictpagecheckfalse
\newcounter{cp@cntr}
\newcommand{\cplabel}{^_}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\checkoddpage}
% This is the user level command to check for odd/even page. It does
% a robust check for |\strictpagecheck| otherwise the simple minded
% check. It sets |\ifoddpage| to TRUE if the page is odd, otherwise
% it sets it to FALSE.
%
%    This is now fixed so that it should work for non-arabic page numbering.
% It uses a new label/pageref variant based on the page counter value, not
% its printed representation. This also gets rid of worrying about hyperref!!
% The problem was discovered by Bastiaan Niels Veelo
%
% \changes{v1.4}{2003/11/22}{Complete rewrite of odd page checking 
%                            (from patch v1.6)}
%    \begin{macrocode}
\DeclareRobustCommand{\checkoddpage}{%
  \oddpagefalse%
  \ifstrictpagecheck%
    \stepcounter{cp@cntr}\pmemlabel{\cplabel\thecp@cntr}%
    \@memcnta=\pmemlabelref{\cplabel\thecp@cntr}\relax
    \ifodd\@memcnta\oddpagetrue\fi
  \else
    \ifodd\c@page\oddpagetrue\fi
  \fi}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\thepmemc@@page}
% The value of the page counter.
%    \begin{macrocode}
\gdef\thepmemc@@page{\the\c@page}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\pmemprotected@write}
% |\pmemprotected@write| is a modified version of the kernel's
% |\protected@write|.
%
%  This version of the macro was provided by Romano Giannetti
% (\url{romano@dea.icai.upco.es}) on 15 July 2003. 
%    \begin{macrocode}
\long\def\pmemprotected@write#1#2#3{%
  \begingroup
  \let\thepmemc@@page\relax
  #2%
  \let\protect\@unexpandable@protect
  \edef\reserved@a{\write#1{#3}}%
  \reserved@a
  \endgroup
  \if@nobreak\ifvmode\nobreak\fi\fi}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\pmemlabel}
% A version of |\label| that uses |\pmemprotected@write|.
%    \begin{macrocode}
\newcommand{\pmemlabel}[1]{\@bsphack
  \pmemprotected@write\@auxout{}%
    {\string\newpmemlabel{#1}{\thepmemc@@page}}%
  \@esphack}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\newpmemlabel}
% \begin{macro}{\pmemlabelref}
% We have to be able to cope with a particular label
% not (yet) being in the the \file{.aux} file when we come to check 
% the page number.
%    \begin{macrocode}
\newcommand{\newpmemlabel}[2]{{\global\@namedef{m@#1}{#2}}}
\newcommand{\pmemlabelref}[1]{%
  \expandafter\ifx\csname m@#1\endcsname\relax
    0%
  \else
    \csname m@#1\endcsname
  \fi}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%  \subsection{Checking for empty arguments}
%
%  Like page checking, testing for an empty macro argument is more
% difficult than it might appear at first sight.
%
%    The following code is from the \Lpack{ifmtarg} package~\cite{IFMTARG}.
%
% \begin{macro}{\@ifmtarg}
% \begin{macro}{\@ifnotmtarg}
% |\@ifmtarg{|\meta{arg}|}{<code when empty>}{<code when arg not empty>}| \\
% |\@ifnotmtarg{|\meta{arg}|}{<code when arg not empty>}| \\
%    \begin{macrocode}
\begingroup
\catcode`\Q=3
\long\gdef\@ifmtarg#1{\@xifmtarg#1QQ\@secondoftwo\@firstoftwo\@nil}
\long\gdef\@xifmtarg#1#2Q#3#4#5\@nil{#4}
\long\gdef\@ifnotmtarg#1{\@xifmtarg#1QQ\@firstofone\@gobble\@nil}
\endgroup

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%    Some example uses:
% \begin{verbatim}
% \newcommand{\isempty}[1]{%
%    \@ifmtarg{#1}{\typeout{YES}}{\typeout{NO}}}
% \newcommand{\isnotempty}[1]{%
%    \@ifnotmtarg{#1}{\typeout{YES}}}
%
% \isempty{}    -> YES  \isnotempty{}    -> 
% \isempty{   } -> YES  \isnotempty{   } -> 
% \isempty{A}   -> NO   \isnotempty{A}   -> YES 
% \isempty{ A } -> NO   \isnotempty{ A } -> YES 
% \end{verbatim}
%
%
% \subsection{Changing the page layout in the document}
%
%  You should not do this, but\ldots
%
% The following code is essentially from the \Lpack{chngpage} 
% package~\cite{CHNGPAGE}.
%
% \begin{macro}{\ch@ngetext}
% This macro sets the page output parameters.
%    \begin{macrocode}
\DeclareRobustCommand{\ch@ngetext}{%
  \setlength{\@colht}{\textheight}\setlength{\@colroom}{\textheight}%
  \setlength{\vsize}{\textheight}\setlength{\columnwidth}{\textwidth}%
  \if@twocolumn%
    \advance\columnwidth-\columnsep \divide\columnwidth\tw@%
    \@firstcolumntrue%
  \fi%
  \setlength{\hsize}{\columnwidth}%
  \setlength{\linewidth}{\hsize}%
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\changetext}
% |\changetext{|\meta{H}|}{|\meta{W}|}{|\meta{E}|}{|\meta{O}|}{|\meta{G}|}|
% adds the given lengths to 5 main bits of the page layout. An empty
% argument means `no change'.
%    \begin{macrocode}
\DeclareRobustCommand{\changetext}[5]{%
  \@ifmtarg{#1}{}{\addtolength{\textheight}{#1}}%
  \@ifmtarg{#2}{}{\addtolength{\textwidth}{#2}}%
  \@ifmtarg{#3}{}{\addtolength{\evensidemargin}{#3}}%
  \@ifmtarg{#4}{}{\addtolength{\oddsidemargin}{#4}}%
  \@ifmtarg{#5}{}{\addtolength{\columnsep}{#5}}%
  \ch@ngetext%
}

%    \end{macrocode}
% \end{macro}
%
% Any given change lasts until another |\changetext| command is given.
% A |\changetext| command should only be issued between two paragraphs.
%
% |\changetext| should not be used by anyone unless they really
% know what they are doing. If you really know what you are doing
% then you should know enough not to use it.
%
% \subsection{Temporarily changing the text width}
%
%
% \begin{environment}{adjustwidth}
% \begin{environment}{adjustwidth*}
% |\begin{adjustwidth}{|\meta{left}|}{|\meta{right}|}|
% adds the given lengths to the left and right hand margins. A positive
% value will shorten the text and a negative value will widen it. The
% starred version of the environment will cause the
% margin changes to switch between odd and even pages.
%
% This code is based on the \Lpack{chngpage} package.
% \changes{v0.2}{2001/06/03}{Replaced optional arg of adjustwidth by adjustwidth*}
%    \begin{macrocode}
\newenvironment{adjustwidth}[2]{%
  \begin{list}{}{%
    \topsep\z@%
    \listparindent\parindent%
    \parsep\parskip%
    \@ifmtarg{#1}{\setlength{\leftmargin}{\z@}}%
                 {\setlength{\leftmargin}{#1}}%
    \@ifmtarg{#2}{\setlength{\rightmargin}{\z@}}%
                 {\setlength{\rightmargin}{#2}}%
    }
    \item[]}{\end{list}}

%    \end{macrocode}
%
%    \begin{macrocode}
\newenvironment{adjustwidth*}[2]{%
  \begin{list}{}{%
    \topsep\z@%
    \listparindent\parindent%
    \parsep\parskip%
    \checkoddpage
    \ifoddpage  % odd numbered page
      \@ifmtarg{#1}{\setlength{\leftmargin}{\z@}}%
                   {\setlength{\leftmargin}{#1}}%
      \@ifmtarg{#2}{\setlength{\rightmargin}{\z@}}%
                   {\setlength{\rightmargin}{#2}}%
    \else       % even numbered page
      \@ifmtarg{#2}{\setlength{\leftmargin}{\z@}}%
                   {\setlength{\leftmargin}{#2}}%
      \@ifmtarg{#1}{\setlength{\rightmargin}{\z@}}%
                   {\setlength{\rightmargin}{#1}}%
    \fi
    }
    \item[]}{\end{list}}

%    \end{macrocode}
% \end{environment}
% \end{environment}
%
%  The environments only work for complete paragraphs.
%
% \begin{macro}{\calccentering}
% This macro calculates the amount to be added to the spine margin
% (and subtracted from the foredge margin) 
% in order to center the textblock. Call as \\
% |\calccentering{\length}| and it sets |\length|
% to the required value. Then use as \\
% |\begin{adjustwidth*}{\length}{-\length}...|
%    \begin{macrocode}
\newcommand{\calccentering}[1]{
  #1 = \paperwidth
  \advance #1 by -\textwidth
  \divide #1 by \tw@
  \advance #1 by -\spinemargin
}

%    \end{macrocode}
% \end{macro}
%
%
% \subsection{Moving from the current page}
%
% Much of this code is taken from the \Lpack{nextpage} package~\cite{NEXTPAGE}.
%
% Clear to next page is |\clearpage|.
%
% \begin{macro}{\cleartoevenpage}
%  |\cleartoevenpage[|\meta{text}|}| clears to the next even 
% numbered page, 
%  putting the optional \meta{text} on the skipped page, if any.
%    \begin{macrocode}
\newcommand{\cleartoevenpage}[1][\@empty]{%
  \clearpage%
  \ifodd\c@page\hbox{}#1\clearpage\fi}

%    \end{macrocode}
% \end{macro}
%
% Move to the next page without flushing floats is |\newpage|.
%
% \begin{macro}{\movetoevenpage}
%  |\movetoevenpage[|\meta{text}|}| moves to the next even 
% numbered page without flushing floats, 
%  putting the optional \meta{text} on the skipped page, if any.
%    \begin{macrocode}
\newcommand{\movetoevenpage}[1][\@empty]{%
  \newpage%
  \ifodd\c@page\hbox{}#1\newpage\fi}

%    \end{macrocode}
% \end{macro}
%
% Clear to next odd numbered page is \cs{cleardoublepage}.
%
% \begin{macro}{\cleartooddpage}
%  \cs{cleartooddpage}\oarg{text} clears to the next odd
% numbered page, 
%  putting the optional \meta{text} on the skipped page, if any.
%    \begin{macrocode}
\newcommand{\cleartooddpage}[1][\@empty]{%
  \clearpage%
  \ifodd\c@page\else\hbox{}#1\clearpage\fi}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\movetooddpage}
%  |\movetooddpage[|\meta{text}|}| moves to the next odd
% numbered page without flushing floats, 
%  putting the optional \meta{text} on the skipped page, if any.
%    \begin{macrocode}
\newcommand{\movetooddpage}[1][\@empty]{%
  \newpage%
  \ifodd\c@page\else\hbox{}#1\newpage\fi}

%    \end{macrocode}
% \end{macro}
%
% Example uses: \\
% \begin{verbatim}
% \cleartooddpage                         % same as \cleardouble page
% \cleartooddpage[\thispagestyle{empty}]  % No headings on the skipped page
% \cleartoevenpage                        % go to next even numbered page
% Next example puts text on a skipped page
% \cleartoevenpage[\vspace*{\hfill}THIS PAGE LEFT BLANK\vspace*{\hfill}]
% \end{verbatim}
%
%
%
% \subsection{Needing space at the bottom of a page}
%
% \begin{macro}{\needspace}
% |\needspace{|\meta{length}|}| checks if the is \meta{length} amount
% of vertical space left on the page. If there is not it will start
% a new page. The code is a variant of code thought of for the 
% \Lpack{needspace} package~\cite{NEEDSPACE}.
% \changes{v1.4}{2003/11/16}{Added patch v1.4 versions of needspace macros}
%    \begin{macrocode}
\newcommand{\needspace}[1]{\begingroup\setlength{\dimen@}{#1}%
  \vskip\z@\@plus\dimen@\penalty -100\vskip\z@\@plus-\dimen@
  \vskip\dimen@\penalty 9999\vskip-\dimen@\endgroup}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\Needspace}
% \begin{macro}{\Needspace*}
% |\Needspace| and |\Needspace*| are more robust versions of |\needspace|,
% which depends on penalties. If either |\needspace| or |\Needspace|
% produce a short page it will be ragged bottom, even if |\flushbottom|
% is in effect. The |\Needspace*| version honours the |\...bottom|
% declaration.
%    \begin{macrocode}    
\newcommand{\Needspace}{\@ifstar{\M@sneedsp@}{\M@needsp@}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\M@sneedsp@}
% \begin{macro}{\M@needsp@}
% These implement |\Needspace*| and |\Needspace| respectively.
%    \begin{macrocode}    
\newcommand{\M@sneedsp@}[1]{\par \penalty-100\begingroup
  \setlength{\dimen@}{#1}%
  \dimen@ii\pagegoal \advance\dimen@ii-\pagetotal
  \ifdim \dimen@>\dimen@ii
    \break
  \fi\endgroup}
\newcommand{\M@needsp@}[1]{\par \penalty-100\begingroup
  \setlength{\dimen@}{#1}%
  \dimen@ii\pagegoal \advance\dimen@ii-\pagetotal
  \ifdim \dimen@>\dimen@ii
    \ifdim \dimen@ii>\z@
      \vfil
    \fi
    \break
  \fi\endgroup}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Text spacing commands}
%
% While we're at it, new text spacing commands to supplement
% the kernel's \\
% \verb?\, (\thinspace) = 3/18 em?
% \changes{v1.4}{2003/11/16}{Extra text spacing macros from patch v1.4}
% \begin{macro}{\medspace}
% \begin{macro}{\:}
% Medium space \verb?\: (\medspace) = 4/18 em?
%    \begin{macrocode}    
\newcommand{\medspace}{\kern .22222em }
\DeclareRobustCommand{\:}{%
  \relax\ifmmode\mskip\medmuskip\else\medspace\fi}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\!}
% Negative thin space (- 3/18 em).
% Patrik Nyman (private email 2003/10/06) pointed out I had missed
% the final \verb?\fi? !!!!. 
%    \begin{macrocode}
\DeclareRobustCommand{\!}{%
  \relax\ifmmode\mskip-\thinmuskip\else\negthinspace\fi}

%    \end{macrocode}
% \end{macro}
%
% \subsection{Fractions and subscripts}
%
%
% Styles for fractions like 3/4.
%
% \begin{macro}{\slashfracstyle}
% |\slashfracstyle| based on the kernel |\textsuperscript| macro
% \changes{v1.4}{2003/11/16}{Added \cs{slashfrac} and supports (from patch v1.4)}
%    \begin{macrocode}    
\DeclareRobustCommand*{\slashfracstyle}[1]{%
  {\m@th\ensuremath{\mbox{\fontsize\sf@size\z@\selectfont #1}}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\slashfrac}
% |\slashfrac| based on TeXbook exercise 11.6
%    \begin{macrocode}    
\DeclareRobustCommand*{\slashfrac}[2]{\leavevmode
  \raise.5ex\hbox{\slashfracstyle{#1}}\kern-.13em/%
  \kern-.15em\lower.25ex\hbox{\slashfracstyle{#2}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\textsubscript}
% \begin{macro}{\@textsubscript}
% |\textsubscript|, for text subscripts, based on the kernel 
% |\textsuperscript| macro.
% \changes{v1.4}{2003/11/16}{Added \cs{textsubscript} and supports 
%                (from patch v1.5)}
%    \begin{macrocode}    
\DeclareRobustCommand*{\textsubscript}[1]{%
  \@textsubscript{\selectfont#1}}
\newcommand*{\@textsubscript}[1]{%
  {\m@th\ensuremath{_{\mbox{\fontsize\sf@size\z@#1}}}}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
%  \subsection{Numbers to names}
%
%    \begin{macrocode}
%%%%%%%%%%%%%%%%%%%% number formatting

%    \end{macrocode}
%
% The macros here convert an integer number in the range 0--2147483647
% (TeX's maximum) into
% the natural language name of the number.
% \changes{v1.4}{2003/11/16}{Much extended numbers to names (patch v1.2)}
%
% \begin{macro}{\iflowernumtoname}
% A flag for lowercasing the initial letters.
%    \begin{macrocode}
\newif\iflowernumtoname
  \lowernumtonamefalse
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ifpriornum}
% \begin{macro}{\ifminusnumber}
% \begin{macro}{\ifnotnumtonameallcaps}
% \begin{macro}{\ifmakeordinal}
% Some new booleans
%    \begin{macrocode}
\newif\ifpriornum
\newif\ifminusnumber
\newif\ifnotnumtonameallcaps
\newif\ifmakeordinal

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\namenumberand}
% \begin{macro}{\namenumbercomma}
% \begin{macro}{\lcminusname}
% \begin{macro}{\ucminusname}
% \begin{macro}{\minusname}
% Some macros representing characters/words for number formatting.
%    \begin{macrocode}
\newcommand*{\namenumberand}{ and }
\newcommand*{\namenumbercomma}{, }
\newcommand*{\lcminusname}{minus }
\newcommand*{\ucminusname}{Minus }
\let\minusname\lcminusname
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\fnumbersep}
% \begin{macro}{\tensunitsep}
% \begin{macro}{\nthstring}
% \begin{macro}{\iststring}
% \begin{macro}{\iindstring}
% \begin{macro}{\iiirdstring}
% \begin{macro}{\tiethstring}
% \begin{macro}{\teenstring}
% More characters/words for formatting
%    \begin{macrocode}
\newcommand*{\fnumbersep}{,}
\newcommand*\tensunitsep{-}
\newcommand*{\nthstring}{th}      % nth
\newcommand*{\iststring}{st}      % 1st
\newcommand*{\iindstring}{nd}     % 2nd
\newcommand*{\iiirdstring}{rd}    % 3rd
\newcommand*{\tiethstring}{tieth} % tieth
\newcommand*{\teenstring}{teen}   % teen
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\ordscript}
% How to format the ordinal string
%    \begin{macrocode}
\newcommand{\ordscript}[1]{#1}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\m@mten}
% An internal shorthand for 10
%    \begin{macrocode}
\chardef\m@mten=10 % shorthand for 10

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\c@ism@mctr}
% \begin{macro}{\c@xsm@mctr}
% \begin{macro}{\c@csm@mctr}
% \begin{macro}{\c@ksm@mctr}
% \begin{macro}{\c@xksm@mctr}
% Counters for digits (units to ten thousands) in a number
%    \begin{macrocode}
\newcounter{ism@mctr}  % units
\newcounter{xsm@mctr}  % tens
\newcounter{csm@mctr}  % hundreds
\newcounter{ksm@mctr}  % thousands
\newcounter{xksm@mctr} % ten thousands
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\c@cksm@mctr}
% \begin{macro}{\c@msm@mctr}
% \begin{macro}{\c@xmsm@mctr}
% \begin{macro}{\c@cmsm@mctr}
% \begin{macro}{\c@bsm@mctr}
% Counters for digits (hundred thousands to billions) in a number
%    \begin{macrocode}
\newcounter{cksm@mctr} % hundred thousands
\newcounter{msm@mctr}  % millions
\newcounter{xmsm@mctr} % ten millions
\newcounter{cmsm@mctr} % hundred millions
\newcounter{bsm@mctr}  % billions
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\c@workm@mctr}
% A `work' counter.
%    \begin{macrocode}
\newcounter{workm@mctr}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\numdigits}
% |\numdigits|\marg{number} splits the \meta{number} into individual digits.
% It sets |\minusnumbertrue| if the number is negative, otherwise it is false.
%    \begin{macrocode}
\newcommand*{\numdigits}[1]{%
  \setcounter{ism@mctr}{0}%
  \setcounter{xsm@mctr}{0}%
  \setcounter{csm@mctr}{0}%
  \setcounter{ksm@mctr}{0}%
  \setcounter{xksm@mctr}{0}%
  \setcounter{cksm@mctr}{0}%
  \setcounter{msm@mctr}{0}%
  \setcounter{xmsm@mctr}{0}%
  \setcounter{cmsm@mctr}{0}%
  \setcounter{bsm@mctr}{0}%
  \setcounter{workm@mctr}{#1}%
  \minusnumberfalse
  \ifnum \c@workm@mctr < \z@  % negative
    \minusnumbertrue
    \c@workm@mctr = -\c@workm@mctr
  \fi
  \ifnum \c@workm@mctr > \m@ne     % units
    \c@ism@mctr = \c@workm@mctr
    \divide \c@workm@mctr by \m@mten
    \multiply \c@workm@mctr by \m@mten
    \advance \c@ism@mctr by -\c@workm@mctr
    \divide \c@workm@mctr by \m@mten
  \fi
  \ifnum \c@workm@mctr > \z@    % tens
    \c@xsm@mctr = \c@workm@mctr
    \divide \c@workm@mctr by \m@mten
    \multiply \c@workm@mctr by \m@mten
    \advance \c@xsm@mctr by -\c@workm@mctr
    \divide \c@workm@mctr by \m@mten
  \fi
  \ifnum \c@workm@mctr > \z@   % hundreds
    \c@csm@mctr = \c@workm@mctr
    \divide \c@workm@mctr by \m@mten
    \multiply \c@workm@mctr by \m@mten
    \advance \c@csm@mctr by -\c@workm@mctr
    \divide \c@workm@mctr by \m@mten
  \fi
  \ifnum \c@workm@mctr > \z@   % thousands
    \c@ksm@mctr = \c@workm@mctr
    \divide \c@workm@mctr by \m@mten
    \multiply \c@workm@mctr by \m@mten
    \advance \c@ksm@mctr by -\c@workm@mctr
    \divide \c@workm@mctr by \m@mten
  \fi
  \ifnum \c@workm@mctr > \z@   % ten thousands
    \c@xksm@mctr = \c@workm@mctr
    \divide \c@workm@mctr by \m@mten
    \multiply \c@workm@mctr by \m@mten
    \advance \c@xksm@mctr by -\c@workm@mctr
    \divide \c@workm@mctr by \m@mten
  \fi
  \ifnum \c@workm@mctr > \z@   % hundred thousands
    \c@cksm@mctr = \c@workm@mctr
    \divide \c@workm@mctr by \m@mten
    \multiply \c@workm@mctr by \m@mten
    \advance \c@cksm@mctr by -\c@workm@mctr
    \divide \c@workm@mctr by \m@mten
  \fi
  \ifnum \c@workm@mctr > \z@   % millions
    \c@msm@mctr = \c@workm@mctr
    \divide \c@workm@mctr by \m@mten
    \multiply \c@workm@mctr by \m@mten
    \advance \c@msm@mctr by -\c@workm@mctr
    \divide \c@workm@mctr by \m@mten
  \fi
  \ifnum \c@workm@mctr > \z@  % ten millions
    \c@xmsm@mctr = \c@workm@mctr
    \divide \c@workm@mctr by \m@mten
    \multiply \c@workm@mctr by \m@mten
    \advance \c@xmsm@mctr by -\c@workm@mctr
    \divide \c@workm@mctr by \m@mten
  \fi
  \ifnum \c@workm@mctr > \z@  % hundred millions
    \c@cmsm@mctr = \c@workm@mctr
    \divide \c@workm@mctr by \m@mten
    \multiply \c@workm@mctr by \m@mten
    \advance \c@cmsm@mctr by -\c@workm@mctr
    \divide \c@workm@mctr by \m@mten
  \fi
  \ifnum \c@workm@mctr > \z@% billions
    \c@bsm@mctr = \c@workm@mctr
    \divide \c@workm@mctr by \m@mten
    \multiply \c@workm@mctr by \m@mten
    \advance \c@bsm@mctr by -\c@workm@mctr
  \fi
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\form@tnumber}
% |\form@tnumber|\marg{number} formats \meta{number} as digits.
%    \begin{macrocode}
\newcommand*{\form@tnumber}[1]{%
  \numdigits{#1}%
  \ifminusnumber-\fi
  \priornumfalse
  \ifnum \c@bsm@mctr > \z@ % billions
    \priornumtrue
    \thebsm@mctr\fnumbersep
  \fi
  \ifpriornum                 % hundred millions
    \thecmsm@mctr
  \else
    \ifnum \c@cmsm@mctr > \z@
      \priornumtrue
      \thecmsm@mctr
    \fi
  \fi
  \ifpriornum                 % ten millions
    \thexmsm@mctr
  \else
    \ifnum \c@xmsm@mctr > \z@
      \priornumtrue
      \thexmsm@mctr
    \fi
  \fi
  \ifpriornum                 % millions
    \themsm@mctr\fnumbersep
  \else
    \ifnum \c@msm@mctr > \z@
      \priornumtrue
      \themsm@mctr\fnumbersep
    \fi
  \fi
  \ifpriornum                % hundred thousands
    \thecksm@mctr
  \else
    \ifnum \c@cksm@mctr > \z@
      \priornumtrue
      \thecksm@mctr
    \fi
  \fi
  \ifpriornum                % ten thousands
    \thexksm@mctr
  \else
    \ifnum \c@xksm@mctr > \z@
      \priornumtrue
      \thexksm@mctr
    \fi
  \fi
  \ifpriornum                % thousands
    \theksm@mctr\fnumbersep
  \else
    \ifnum \c@ksm@mctr > \z@
      \priornumtrue
      \theksm@mctr\fnumbersep
    \fi
  \fi
  \ifpriornum                % hundreds
    \thecsm@mctr
  \else
    \ifnum \c@csm@mctr > \z@
      \priornumtrue
      \thecsm@mctr
    \fi
  \fi
  \ifpriornum                % tens
    \thexsm@mctr
  \else
    \ifnum \c@xsm@mctr > \z@
      \priornumtrue
      \thexsm@mctr
    \fi
  \fi
  \theism@mctr              % units
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\cardinal}
% |\cardinal|\marg{number} prints \meta{number} unformatted.
%    \begin{macrocode}
\newcommand*{\cardinal}[1]{%
  \begingroup
  \let\fnumbersep\relax
  \form@tnumber{#1}%
  \endgroup}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\fcardinal}
% |\fcardinal|\marg{number} prints \meta{number} formatted.
%    \begin{macrocode}
\newcommand*{\fcardinal}[1]{%
  \begingroup
  \form@tnumber{#1}%
  \endgroup}
  
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ordinal}
% |\ordinal|\marg{number} prints \meta{number} as an unformatted ordinal.
%    \begin{macrocode}
\newcommand*{\ordinal}[1]{%
  \begingroup
  \let\fnumbersep\relax
  \form@tnumber{#1}%
  \let\ordstring\nthstring
  \ifnum \c@xsm@mctr=\@ne\else
    \ifcase \c@ism@mctr
      \or \let\ordstring\iststring%    1st
      \or \let\ordstring\iindstring%   2nd
      \or \let\ordstring\iiirdstring%  3rd
    \fi
  \fi
  \ordscript{\ordstring}%
  \endgroup
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\fordinal}
% |\fordinal|\marg{number} prints \meta{number} as a formatted ordinal.
%    \begin{macrocode}
\newcommand*{\fordinal}[1]{%
  \begingroup
  \form@tnumber{#1}%
  \let\ordstring\nthstring
  \ifnum \c@xsm@mctr=\@ne\else
    \ifcase \c@ism@mctr
      \or \let\ordstring\iststring%    1st
      \or \let\ordstring\iindstring%   2nd
      \or \let\ordstring\iiirdstring%  3rd
    \fi
  \fi
  \ordscript{\ordstring}%
  \endgroup
}

%    \end{macrocode}
% \end{macro}
%
% The next, tedious, code is for translating numbers into names.
%
% \begin{macro}{\nNameo}
% \begin{macro}{\nNamec}
% \begin{macro}{\nNamem}
% \begin{macro}{\nNamemm}
% \begin{macro}{\nNamemmm}
% Names of major numbers: $0$, $10^{2}$, $10^{3}$, $10^{6}$, and $10^{9}$.
%    \begin{macrocode}
\newcommand*\nNameo{\iflowernumtoname z\else Z\fi ero}
\newcommand*\nNamec{\iflowernumtoname h\else H\fi undred}
\newcommand*\nNamem{\iflowernumtoname t\else T\fi housand}
\newcommand*\nNamemm{\iflowernumtoname m\else M\fi illion}
\newcommand*\nNamemmm{\iflowernumtoname b\else B\fi illion}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\nNamei}
% \begin{macro}{\nNameii}
% \begin{macro}{\nNameiii}
% \begin{macro}{\nNameiv}
% \begin{macro}{\nNamev}
%    These are the names for numbers 1 to 5.
%    \begin{macrocode}
\newcommand*\nNamei{\iflowernumtoname o\else O\fi ne}
\newcommand*\nNameii{\iflowernumtoname t\else T\fi wo}
\newcommand*\nNameiii{\iflowernumtoname t\else T\fi hree}
\newcommand*\nNameiv{\iflowernumtoname f\else F\fi our}
\newcommand*\nNamev{\iflowernumtoname f\else F\fi ive}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\nNamevi}
% \begin{macro}{\nNamevii}
% \begin{macro}{\nNameviii}
% \begin{macro}{\nNameix}
% \begin{macro}{\nNamex}
%    These are the names for numbers 6 to 10.
%    \begin{macrocode}
\newcommand*\nNamevi{\iflowernumtoname s\else S\fi ix}
\newcommand*\nNamevii{\iflowernumtoname s\else S\fi even}
\newcommand*\nNameviii{\iflowernumtoname e\else E\fi ight}
\newcommand*\nNameix{\iflowernumtoname n\else N\fi ine}
\newcommand*\nNamex{\iflowernumtoname t\else T\fi en}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\nNamexi}
% \begin{macro}{\nNamexii}
% \begin{macro}{\nNamexiii}
% \begin{macro}{\nNamexiv}
% \begin{macro}{\nNamexv}
%    These are the names for numbers 11 to 15.
%    \begin{macrocode}
\newcommand*\nNamexi{\iflowernumtoname e\else E\fi leven}
\newcommand*\nNamexii{\iflowernumtoname t\else T\fi welve}
\newcommand*\nNamexiii{\iflowernumtoname t\else T\fi hir\teenstring}
\newcommand*\nNamexiv{\iflowernumtoname f\else F\fi our\teenstring}
\newcommand*\nNamexv{\iflowernumtoname f\else F\fi if\teenstring}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\nNamexvi}
% \begin{macro}{\nNamexvii}
% \begin{macro}{\nNamexviii}
% \begin{macro}{\nNamexix}
% \begin{macro}{\nNamexx}
%    These are the names for numbers 16 to 20.
%    \begin{macrocode}
\newcommand*\nNamexvi{\iflowernumtoname s\else S\fi ix\teenstring}
\newcommand*\nNamexvii{\iflowernumtoname s\else S\fi even\teenstring}
\newcommand*\nNamexviii{\iflowernumtoname e\else E\fi igh\teenstring}
\newcommand*\nNamexix{\iflowernumtoname n\else N\fi ine\teenstring}
\newcommand*\nNamexx{\iflowernumtoname t\else T\fi wenty}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\nNamexxx}
% \begin{macro}{\nNamexl}
% \begin{macro}{\nNamel}
% \begin{macro}{\nNamelx}
% \begin{macro}{\nNamelxx}
%    These are the names for numbers 30 to 70.
%    \begin{macrocode}
\newcommand*\nNamexxx{\iflowernumtoname t\else T\fi hirty}
\newcommand*\nNamexl{\iflowernumtoname f\else F\fi orty}
\newcommand*\nNamel{\iflowernumtoname f\else F\fi ifty}
\newcommand*\nNamelx{\iflowernumtoname s\else S\fi ixty}
\newcommand*\nNamelxx{\iflowernumtoname s\else S\fi eventy}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\nNamelxxx}
% \begin{macro}{\nNamexc}
%    These are the names for numbers 80 to 90.
%    \begin{macrocode}
\newcommand*\nNamelxxx{\iflowernumtoname e\else E\fi ighty}
\newcommand*\nNamexc{\iflowernumtoname n\else N\fi inety}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
%
% \begin{macro}{\unitnumbername}
% Get the name of a unit (0 -- 9).
%    \begin{macrocode}
\newcommand*{\unitnumbername}[1]{%
  \ifcase #1 \nNameo\or
   \nNamei\or
   \nNameii\or
   \nNameiii\or
   \nNameiv\or
   \nNamev\or
   \nNamevi\or
   \nNamevii\or
   \nNameviii\or
   \nNameix\fi}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\teennumbername}
% Get the name of a 'teen number (10 -- 19)
%    \begin{macrocode}
\newcommand*{\teennumbername}[1]{%
  \ifcase #1 \nNamex\or
   \nNamexi\or
   \nNamexii\or
   \nNamexiii\or
   \nNamexiv\or
   \nNamexv\or
   \nNamexvi\or
   \nNamexvii\or
   \nNamexviii\or
   \nNamexix\fi}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\tensnumbername}
% Get the name of a tens number (20 -- 90)
%    \begin{macrocode}
\newcommand*{\tensnumbername}[2]{%
  \ifnum #1=\@ne
    \teennumbername{#2}\ifnotnumtonameallcaps\lowernumtonametrue\fi
  \else
    \ifcase #1
    \or
    \or \nNamexx
    \or \nNamexxx
    \or \nNamexl
    \or \nNamel
    \or \nNamelx
    \or \nNamelxx
    \or \nNamelxxx
    \or \nNamexc
    \fi
    \ifnotnumtonameallcaps\lowernumtonametrue\fi
    \ifnum #2 > \z@ \tensunitsep\unitnumbername{#2}\fi
  \fi}

%    \end{macrocode}
% \end{macro}
%
% Names of small ordinals. The use of |\nthstring| instead of `th' saves
% some tokens.
%    \begin{macrocode}
\newcommand*\nthNameo{\nNameo\nthstring}
\newcommand*\nthNamei{\iflowernumtoname f\else F\fi irst}
\newcommand*\nthNameii{\iflowernumtoname s\else S\fi econd}
\newcommand*\nthNameiii{\iflowernumtoname t\else T\fi hird}
\newcommand*\nthNameiv{\nNameiv\nthstring}
\newcommand*\nthNamev{\iflowernumtoname f\else F\fi if\nthstring}
\newcommand*\nthNamevi{\nNamevi\nthstring}
\newcommand*\nthNamevii{\nNamevii\nthstring}
\newcommand*\nthNameviii{\iflowernumtoname e\else E\fi igh\nthstring}
\newcommand*\nthNameix{\iflowernumtoname n\else N\fi in\nthstring}
\newcommand*\nthNamexii{\iflowernumtoname t\else T\fi welf\nthstring}

%    \end{macrocode}
%
% \begin{macro}{\unitordinalname}
% Get the ordinal name of a unit (0 -- 9)
%    \begin{macrocode}
\newcommand*{\unitordinalname}[1]{%
  \ifcase #1 \nthNameo\or
  \nthNamei\or
  \nthNameii\or
  \nthNameiii\or
  \nthNameiv\or
  \nthNamev\or
  \nthNamevi\or
  \nthNamevii\or
  \nthNameviii\or
  \nthNameix\fi}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\teenordinalname}
% Get the ordinal name of a 'teen number (10 -- 19). Using |\nthstring|
% instead of `th' to save some tokens.
%    \begin{macrocode}
\newcommand*{\teenordinalname}[1]{%
  \ifcase #1 \nNamex\nthstring\or
  \nNamexi\nthstring\or
  \nthNamexii\or
  \nNamexiii\nthstring\or
  \nNamexiv\nthstring\or
  \nNamexv\nthstring\or
  \nNamexvi\nthstring\or
  \nNamexvii\nthstring\or
  \nNamexviii\nthstring\or
  \nNamexix\nthstring\fi}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\tensordinalname}
% Get the ordinal name of a tens number (20 -- 90)
% (Mathew Dafilis (\texttt{mpd@swin.edu.au}) sent Email on 2003/11/14
% saying that \cs{ordinaltoname} didn't work for 20, 30, etc. He was
% correct. It is now fixed.
%    \begin{macrocode}
\newcommand*{\tensordinalname}[2]{%
  \ifnum #1=\@ne
    \teenordinalname{#2}\ifnotnumtonameallcaps\lowernumtonametrue\fi
  \else
    \ifnum #2> \z@
      \ifcase #1
      \or 
      \or \nNamexx
      \or \nNamexxx
      \or \nNamexl
      \or \nNamel
      \or \nNamelx
      \or \nNamelxx
      \or \nNamelxxx
      \or \nNamexc
      \fi
      \ifnotnumtonameallcaps\lowernumtonametrue\fi
      \tensunitsep\unitordinalname{#2}
    \else
      \ifcase #1
      \or 
      \or \nthNamexx
      \or \nthNamexxx
      \or \nthNamexl
      \or \nthNamel
      \or \nthNamelx
      \or \nthNamelxx
      \or \nthNamelxxx
      \or \nthNamexc
      \fi
      \ifnotnumtonameallcaps\lowernumtonametrue\fi
    \fi
  \fi}
  
%    \end{macrocode}
% \end{macro}
%
%    The names of tens ordinals. The use of |\tiethstring| instead
% of `tieth' saves somes tokens.
%    \begin{macrocode}
\newcommand*\nthNamexx{\iflowernumtoname t\else T\fi wen\tiethstring}
\newcommand*\nthNamexxx{\iflowernumtoname t\else T\fi hir\tiethstring}
\newcommand*\nthNamexl{\iflowernumtoname f\else F\fi or\tiethstring}
\newcommand*\nthNamel{\iflowernumtoname f\else F\fi if\tiethstring}
\newcommand*\nthNamelx{\iflowernumtoname s\else S\fi ix\tiethstring}
\newcommand*\nthNamelxx{\iflowernumtoname s\else S\fi even\tiethstring}
\newcommand*\nthNamelxxx{\iflowernumtoname e\else E\fi igh\tiethstring}
\newcommand*\nthNamexc{\iflowernumtoname n\else N\fi ine\tiethstring}

%    \end{macrocode}
%
% \begin{macro}{\n@me@number}
% |\n@me@number|\meta{number} is an internal macro to convert a 
% \meta{number} to names.
%    \begin{macrocode}
\newcommand*{\n@me@number}[1]{%
\begingroup
    \numdigits{#1}%
    \ifminusnumber\minusname\fi
    \priornumfalse
%% billions
    \ifnum \c@bsm@mctr > \z@
      \unitnumbername{\thebsm@mctr}\space
      \ifnotnumtonameallcaps\lowernumtonametrue\fi\nNamemmm
      \priornumtrue
    \fi
%% hundred millions
    \ifnum \c@cmsm@mctr > \z@
      \ifpriornum\namenumbercomma\fi
      \unitnumbername{\thecmsm@mctr}\space
      \ifnotnumtonameallcaps\lowernumtonametrue\fi\nNamec
      \priornumtrue
    \fi
%% tens/units millions
    \ifnum \c@xmsm@mctr > \z@
      \ifpriornum
        \ifnum\c@cmsm@mctr>\z@\namenumberand\else\namenumbercomma\fi
      \fi
      \tensnumbername{\thexmsm@mctr}{\themsm@mctr}%
      \priornumtrue
    \else
      \ifnum \c@msm@mctr > \z@
        \ifpriornum
          \ifnum\c@cmsm@mctr>\z@\namenumberand\else\namenumbercomma\fi
        \fi
        \unitnumbername{\themsm@mctr}%
        \ifnotnumtonameallcaps\lowernumtonametrue\fi
        \priornumtrue
      \fi
    \fi
    \ifnum \c@cmsm@mctr > \z@
      \ifpriornum\space\fi
      \nNamemm
    \else
      \ifnum \c@xmsm@mctr > \z@
        \ifpriornum\space\fi
        \nNamemm
      \else
        \ifnum \c@msm@mctr > \z@
          \ifpriornum\space\fi
          \nNamemm
        \fi
      \fi
    \fi
%% hundred thousands
    \ifnum \c@cksm@mctr > \z@
      \ifpriornum\namenumbercomma\fi
      \unitnumbername{\thecksm@mctr}\space
        \ifnotnumtonameallcaps\lowernumtonametrue\fi\nNamec
      \priornumtrue
    \fi
%% tens/units thousands
    \ifnum \c@xksm@mctr > \z@
      \ifpriornum
        \ifnum\c@cksm@mctr>\z@\namenumberand\else\namenumbercomma\fi
      \fi
      \tensnumbername{\thexksm@mctr}{\theksm@mctr}%
      \priornumtrue
    \else
      \ifnum \c@ksm@mctr > \z@
        \ifpriornum
          \ifnum\c@cksm@mctr>\z@\namenumberand\else\namenumbercomma\fi
        \fi
        \unitnumbername{\theksm@mctr}%
        \ifnotnumtonameallcaps\lowernumtonametrue\fi
        \priornumtrue
      \fi
    \fi
    \ifnum \c@cksm@mctr > \z@
      \ifpriornum\space\fi
      \nNamem
    \else
      \ifnum \c@xksm@mctr > \z@
        \ifpriornum\space\fi
        \nNamem
      \else
        \ifnum \c@ksm@mctr > \z@
          \ifpriornum\space\fi
          \nNamem
        \fi
      \fi
    \fi
%% hundreds
    \ifnum \c@csm@mctr > \z@
      \ifpriornum\namenumbercomma\fi
      \unitnumbername{\thecsm@mctr}\space
        \ifnotnumtonameallcaps\lowernumtonametrue\fi\nNamec
      \priornumtrue
    \fi
%% tens/units
  \ifmakeordinal
    \ifnum \c@xsm@mctr > \z@
      \ifpriornum\namenumberand\fi
      \tensordinalname{\thexsm@mctr}{\theism@mctr}%
    \else
      \ifnum \c@ism@mctr > \z@
        \ifpriornum\namenumberand\fi
        \unitordinalname{\theism@mctr}%
      \else
        \ifpriornum\nthstring\else\unitordinalname{\theism@mctr}\fi
      \fi
    \fi
  \else  % not ordinal
    \ifnum \c@xsm@mctr > \z@
      \ifpriornum\namenumberand\fi
      \tensnumbername{\thexsm@mctr}{\theism@mctr}%
    \else
      \ifnum \c@ism@mctr > \z@
        \ifpriornum\namenumberand\fi
        \unitnumbername{\theism@mctr}%
      \else
        \ifpriornum\else\unitnumbername{\theism@mctr}\fi
      \fi
    \fi
  \fi % end ifmakeordinal
\endgroup
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\numtoname}
% Lowercase all names
%    \begin{macrocode}
\newcommand*{\numtoname}[1]{%
  \makeordinalfalse
  \notnumtonameallcapstrue%
  \lowernumtonametrue%
  \n@me@number{#1}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\numtoName}
% Uppercase first letter of first name (all else lowercase).
%    \begin{macrocode}
\newcommand*{\numtoName}[1]{%
  \makeordinalfalse
  \notnumtonameallcapstrue%
  \lowernumtonamefalse%
  \n@me@number{#1}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\NumToName}
% Uppercase first letter of all names (all else lowercase).
%    \begin{macrocode}
\newcommand*{\NumToName}[1]{%
  \makeordinalfalse
  \notnumtonameallcapsfalse%
  \lowernumtonamefalse%
  \n@me@number{#1}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ordinaltoname}
% Lowercase all ordinal names
%    \begin{macrocode}
\newcommand*{\ordinaltoname}[1]{%
  \makeordinaltrue
  \notnumtonameallcapstrue%
  \lowernumtonametrue%
  \n@me@number{#1}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ordinaltoName}
% Uppercase first letter of first ordinal name (all else lowercase).
%    \begin{macrocode}
\newcommand*{\ordinaltoName}[1]{%
  \makeordinaltrue
  \notnumtonameallcapstrue%
  \lowernumtonamefalse%
  \n@me@number{#1}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\OrdinalToName}
% Uppercase first letter of all ordinal names (all else lowercase).
%    \begin{macrocode}
\newcommand*{\OrdinalToName}[1]{%
  \makeordinaltrue
  \notnumtonameallcapsfalse%
  \lowernumtonamefalse%
  \n@me@number{#1}}

%    \end{macrocode}
% \end{macro}
%
%
% \subsection{A fix for two column headings}
%
%
% This is from a posting by Donald Arseneau to \ctt{} on 23 April 2001 to fix
% a problem --- `When I use |\onecolumn\chapter...| its headline
% is printed lower on the page than for two-column chapters'
%
% DA and I had previously discussed this in relation to the Index
% and I had put a hack into the \Lpack{tocbibind} package to fix the Index.
%
% Donald posted the following.
%
% |\vspace*| gives bad spacing after a pagebreak and |\@makechapterhead|
% starts with |\vspace*|.
% The biggest problem is the definition of |\@topnewpage|, which is used
% for the two-column spanning text. Here is a redefinition:
% \changes{v0.2}{2001/06/03}{Added DA's redefinition of \cs{@topnewpage}}
% \begin{macro}{\@topnewpage}
%    \begin{macrocode}
\long\def \@topnewpage [#1]{%
  \@nodocument
  \@next\@currbox\@freelist{}{}%
  \global \setbox\@currbox
    \vbox {%
      \break
      \prevdepth\z@
      \begingroup
      \normalcolor
      \hsize\textwidth
      \@parboxrestore
      \col@number \@ne
      #1%
      \vskip -\dbltextfloatsep
      \endgroup
      \null % ordinary \baselineskip
      \vskip -\topskip
  }%
  \begingroup %% \showbox\@currbox
    \splitmaxdepth\maxdepth \splittopskip\topskip
    \setbox\@tempboxa \vsplit\@currbox to \z@
  \endgroup %% \showbox\@currbox
  \ifdim \ht\@currbox>\textheight
    \ht\@currbox \textheight
  \fi
  \global \count\@currbox \tw@
  \@tempdima -\ht\@currbox
  \advance \@tempdima -\dbltextfloatsep
  \global \advance \@colht \@tempdima
  \ifx \@dbltoplist \@empty
  \else
    \@latexerr{Float(s) lost}\@ehb
    \let \@dbltoplist \@empty
  \fi
  \@cons \@dbltoplist \@currbox
  \global \@dbltopnum \m@ne
  \ifdim \@colht<2.5\baselineskip
    \@latex@warning@no@line {Optional argument of \noexpand\twocolumn
        too tall on page \thepage}%
    \@emptycol
    \if@firstcolumn
    \else
      \@emptycol
    \fi
  \else
    \global \vsize \@colht
    \global \@colroom \@colht
    \@floatplacement
  \fi
}

%    \end{macrocode}
% \end{macro}
%
% The original version of |\@topnewpage| is in \file{ltouput.dtx}, line 159.
%
%
%
% \section{Initialization} \label{sec:init}
%
% \subsection{Words and phrases}
% 
% This document class is for documents prepared in the English language.
% To prepare a version for another language, various English words 
% and phrases must
% be replaced.  The English elements that require replacement are
% defined below in command names.
%
% \begin{macro}{\abstractname}
% \begin{macro}{\contentsname}
% \begin{macro}{\listfigurename}
% \begin{macro}{\listtablename}
% \begin{macro}{\partname}
% \begin{macro}{\chaptername}
% \begin{macro}{\appendixname}
% \begin{macro}{\appendixtocname}
% \begin{macro}{\appendixpagename}
% \begin{macro}{\bibname}
% \begin{macro}{\indexname}
% \begin{macro}{\glossaryname}
%
%    This list is for titles of document sections.
%
%    \begin{macrocode}
\newcommand*{\abstractname}{Abstract}
\newcommand*{\contentsname}{Contents}
\newcommand*{\listfigurename}{List of Figures}
\newcommand*{\listtablename}{List of Tables}
\newcommand*{\partname}{Part}
\newcommand*{\chaptername}{Chapter}
\newcommand*{\appendixname}{Appendix}
\newcommand*{\appendixtocname}{Appendices}
\newcommand*{\appendixpagename}{Appendices}
\newcommand*{\bibname}{Bibliography}
\newcommand*{\indexname}{Index}
\newcommand*{\glossaryname}{Glossary}
%    \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}{\figurename}
% \begin{macro}{\tablename}
% \begin{macro}{\figurerefname}
% \begin{macro}{\tablerefname}
% \begin{macro}{\pagename}
% \begin{macro}{\pagrefename}
%
%    These are the names and phrases used for general elements.
%
%    \begin{macrocode}
\newcommand*{\figurename}{Figure}
\newcommand*{\tablename}{Table}
\newcommand*{\figurerefname}{Figure}
\newcommand*{\tablerefname}{Table}
\newcommand*{\pagename}{page}
\newcommand*{\pagerefname}{page}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\partrefname}
% \begin{macro}{\chapterrefname}
% \begin{macro}{\sectionrefname}
% More names for referencing.
% \changes{v1.2}{2002/07/27}{Added \cs{partrefname}, \cs{chapterrefname}
%                            and \cs{sectionrefname}}
%    \begin{macrocode}
\newcommand*{\partrefname}{Part~}
\newcommand*{\chapterrefname}{Chapter~}
\newcommand*{\sectionrefname}{\S}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% \subsection{Date}
%
% \begin{macro}{\today}
%    This macro uses the \TeX\ primitives |\month|, |\day| and |\year|
%    to provide the date of the \LaTeX-run.
%    \begin{macrocode}
\newcommand{\today}{\ifcase\month\or
  January\or February\or March\or April\or May\or June\or
  July\or August\or September\or October\or November\or December\fi
  \space\number\day, \number\year}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Two column mode}
%
% \begin{macro}{\columnsep}
%    This gives the distance between two columns in two column mode.
%    \begin{macrocode}
\setlength\columnsep{10\p@}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\columnseprule}
%    This gives the width of the rule between two columns in two
%    column mode. We have no visible rule.
%    \begin{macrocode}
\setlength\columnseprule{0\p@}
%    \end{macrocode}
% \end{macro}
%
% \subsection{The page style and counters}
%    We use the page style \pstyle{headings} by
%    default and arabic page numbering.
%    \begin{macrocode}
\pagestyle{headings}
\pagenumbering{arabic}

%    \end{macrocode}
%    We set the sectional counters to zero, the |tocdepth| to
%    one (sections and above listed), the |secnumdepth| to
%    two (sections and above numbered), and |\maxsecnumdepth| to
% the same.
% \changes{v1.1a}{2002/04/28}{Added initialisation of \cs{maxsecnumdepth}}
%    \begin{macrocode}
\setcounter{part}{0}
\setcounter{chapter}{0}
\setcounter{tocdepth}{1}
\setcounter{secnumdepth}{2}
\maxsecnumdepth{section}

%    \end{macrocode}
%
% Set the |\linenumberfrequency| to zero to prohibit line numbering
% and also set the font for line numbers. Can now set the final space
% for boxed verbatim line numbers.
%    \begin{macrocode}
\linenumberfrequency{0}
\linenumberfont{\small\rmfamily}
\settowidth{\bvnumlength}{\vlvnumfont 9999}

%    \end{macrocode}
%
%
% \subsection{Single or double sided printing}
%
%
%    Unless the \Lopt{twoside} wasn't specified, We do not try to make 
% each page of equal height.
%    \begin{macrocode}
\if@twoside
\else
  \raggedbottom
\fi
%    \end{macrocode}
%    When the \Lopt{twocolumn} option was specified we call
%    |\twocolumn| to activate this mode. We try to make each column as
%    long as the others, but call |sloppy| to make our life easier.
%    \begin{macrocode}
\if@twocolumn
  \twocolumn
  \sloppy
  \flushbottom
%    \end{macrocode}
%    Normally we call |\onecolumn| to initiate typesetting in one
%    column.
%    \begin{macrocode}
\else
  \onecolumn
\fi

%    \end{macrocode}
%
% \subsection{Floats}
%
%    Here are the implementations of the figure and table environments
% and their accompanying List of\ldots
%
% \begin{environment}{figure}
% \begin{environment}{figure*}
%    This is the definition of the actual environment. The form with the
%    |*| is used for double column figures. We use |\newfloat| to set it.
% In this class figures are numbered per chapter, but we need to change 
% the default definition of |\thefigure| if a figure
% is in a pre-numbered chapter.
% \changes{v1.3a}{2002/11/22}{Added \cs{kill@lastcounter} after new 
%                             figure float}
%    \begin{macrocode}
\newfloat[chapter]{figure}{lof}{\figurename}
  \kill@lastcounter{lofdepth}
\renewcommand{\thefigure}{\thechapter.\@arabic\c@figure}

%    \end{macrocode}
% \end{environment}
% \end{environment}
%
% \begin{macro}{\listoffigures}
% \begin{macro}{\listoffigures*}
% These macros request that LaTeX produces a list of figures.
% The LoF heading is added to the ToC unless the starred
% version is used.
% \changes{v1.3}{2002/11/14}{Added \cs{kill@lastcounter} after new 
%                             list of figures}
%    \begin{macrocode}
\newlistof{listoffigures}{lof}{\listfigurename}
  \kill@lastcounter{lofdepth}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\l@figure}
%  |\l@figure{|\meta{title}|}{|\meta{page}|}| typesets the LoF entry for
% a |figure| caption heading. 
% \changes{v1.3a}{2002/11/22}{Deleted \cs{kill@lastcounter} after new 
%                             figure listentry}
%    \begin{macrocode}
\newlistentry[chapter]{figure}{lof}{0}
  \cftsetindents{figure}{0em}{2.3em}
%%  \kill@lastcounter{lofdepth}

%    \end{macrocode}
% \end{macro}
%
%
% \begin{environment}{table}
% \begin{environment}{table*}
%  The definition for tables is almost identical.
% \changes{v1.3a}{2002/11/22}{Added \cs{kill@lastcounter} after new 
%                             table float}
%    \begin{macrocode}
\newfloat[chapter]{table}{lot}{\tablename}
  \kill@lastcounter{lotdepth}
\renewcommand{\thetable}{\thechapter.\@arabic\c@table}

%    \end{macrocode}
% \end{environment}
% \end{environment}
%
% \begin{macro}{\listoftables}
% \begin{macro}{\listoftables*}
% These macros request that LaTeX produces a list of tables.
% The LoT heading is added to the ToC unless the starred
% version is used.
% \changes{v1.3}{2002/11/14}{Added \cs{kill@lastcounter} after new 
%                             list of tables}
%    \begin{macrocode}
\newlistof{listoftables}{lot}{\listtablename}
  \kill@lastcounter{lotdepth}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\l@table}
%  |\l@table{|\meta{title}|}{|\meta{page}|}| typesets the LoT entry for
% a |table| caption heading. 
% \changes{v1.3a}{2002/11/22}{Deleted \cs{kill@lastcounter} after new 
%                             table listentry}
%    \begin{macrocode}
\newlistentry[chapter]{table}{lot}{0}
  \cftsetindents{table}{0em}{2.3em}
%%  \kill@lastcounter{lotdepth}

%    \end{macrocode}
% \end{macro}
%
% The \Lpack{subfigure} package defines |lofdepth| and |lotdepth|
% counters. If is not used, then we have to define them.
%    \begin{macrocode}
\AtBeginDocument{%
  \@ifundefined{c@lofdepth}%
     {\newcounter{lofdepth}\setcounter{lofdepth}{1}}{}
  \@ifundefined{c@lotdepth}%
     {\newcounter{lotdepth}\setcounter{lotdepth}{1}}{}
}

%    \end{macrocode}
%
% \subsection{The \Lopt{article} option}
%
% The \Lopt{article} option requires changes to the default chapterstyle,
% and the numbering of floats, etc.
%
% Emanuele Vicentini (2003/07/21) suggested making |\maketitle| more
% closely match the real article's appearance.
% Alan Budden\footnote{\texttt{alan.s.budden@bristol.ac.uk}} commented
% on 2003/12/18 that the equation counter should be continuous.
% \changes{v1.0a}{2001/12/07}{Added footnote and chaptername changes to the article option}
% \changes{v1.4}{2003/11/22}{Added some resets of maketitle macros to 
% the article option (from patch v1.7)}
% \changes{v1.4}{2003/12/18}{Added equation counter to article resets}
%    \begin{macrocode}
\ifartopt
  \chapterstyle{article}
  \counterwithout{figure}{chapter}
  \counterwithout{table}{chapter}
  \counterwithout{footnote}{chapter}
  \counterwithout{equation}{chapter}
  \renewcommand{\chaptername}{}
  \renewcommand{\maketitlehookb}{%
    \vskip -1.5\topsep\vskip -1.5\partopsep}
  \renewcommand{\maketitlehookc}{%
    \vskip -1.5\topsep\vskip -1.5\partopsep}
\fi

%    \end{macrocode}
%
%
% \subsection{The \Lopt{ms} option}
%
% This should be done last as it makes various changes to the defaults.
%
% \begin{macro}{\msdoublespacing}
% \begin{macro}{\mssinglespacing}
%  These do nothing unless the \Lopt{ms} option is used; then they change
% the |\baselinestretch|.
%    \begin{macrocode}
\newcommand{\msdoublespacing}{}
\newcommand{\mssinglespacing}{}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%    \begin{macrocode}
\ifmsdoc
  \renewcommand{\msdoublespacing}{%
    \renewcommand{\baselinestretch}{1.6}\large\normalsize}
  \renewcommand{\mssinglespacing}{%
    \renewcommand{\baselinestretch}{1.0}\large\normalsize}
  \renewcommand{\familydefault}{cmtt}
  \renewcommand{\rmdefault}{cmtt}
  \renewcommand{\sfdefault}{cmtt}
  \renewcommand{\bfdefault}{m}
  \renewcommand{\itdefault}{n}
  \renewcommand{\sldefault}{n}
  \renewcommand{\scdefault}{n}
  \renewcommand{\baselinestretch}{1.6}
  \@twocolumnfalse
  \onecolumn
  \sloppy
  \@twosidefalse
  \raggedbottom
  \pagestyle{plain}
\fi

%    \end{macrocode}
%
% \subsection{Emulated packages}
%
%    \begin{macrocode}
\EmulatedPackage{abstract}
\EmulatedPackage{appendix}
\EmulatedPackage{array}
\EmulatedPackage{booktabs}
\EmulatedPackage{ccaption}
\EmulatedPackage{chngcntr}
\EmulatedPackage{chngpage}
\EmulatedPackage{crop}
\EmulatedPackage{dcolumn}
\EmulatedPackage{delarray}
\EmulatedPackage{enumerate}
\EmulatedPackage{epigraph}
\EmulatedPackage{framed}
\EmulatedPackage{ifmtarg}
\EmulatedPackage{ifpdf}
\EmulatedPackage{index}
\EmulatedPackage{makeidx}
\EmulatedPackage{moreverb}
\EmulatedPackage{needspace}
\EmulatedPackage{newfile}
\EmulatedPackage{nextpage}
\EmulatedPackage{patchcmd}
\EmulatedPackage{shortvrb}
\EmulatedPackage{showidx}
\EmulatedPackage{tabularx}
\EmulatedPackage{titleref}
\EmulatedPackage{titling}
\EmulatedPackage{tocbibind}
\EmulatedPackage{tocloft}
\EmulatedPackage{verbatim}
\EmulatedPackage{verse}

%    \end{macrocode}
%
% \subsection{Patch file}
%
% At the suggestion of Danie Els (\texttt{DanieEls@sun.ac.za}), Dan Leucking
% (\texttt{luecking@uark.edu}) and others, input a patch file, if one exists,
% as the final act. (This is preferable to my series of \file{memfixa.sty},
% \file{memfixb.sty} packages, and so on, for each release of the class.)
% \changes{v1.3}{2002/11/14}{Added input of mempatch.sty}
%    \begin{macrocode}
\InputIfFileExists{mempatch.sty} % should be this

%    \end{macrocode}
%
%    The end of the class definitions.
%    \begin{macrocode}
%</class>
%    \end{macrocode}
%
% \section{Glossary Makeindex style file}
%
% Here is the basic style (configuration) file for Makeindex 
% for use with the default glossary setup.
% \changes{v1.618}{2005/09/25}{Added basic.gst file}
%
%    \begin{macrocode}
%<*gst>
%%%%% basic.gst    basic makeindex glossary configuration file for memoir
%%%%% Output style parameters
preamble "\\begin{theglossary}"
postamble "\n\\end{theglossary}\n"
group_skip "\n\\glossaryspace\n"
item_0    "\n\\glossitem"
delim_0   "{\\memglonum{"
encap_suffix "}}}"
%%% Input style parameters
keyword "\\glossaryentry"

%</gst>
%    \end{macrocode}
%
% The end of the configuration file code
%
% \bibliographystyle{alpha}
% \begingroup
% \raggedright
% \begin{thebibliography}{GMSN94A}
%
%
%    \bibitem[ABH90]{bk:Impatient}
%      Paul W.~Abrahams, Karl Berry and Kathryn A.~Hargreaves.
%      \newblock \emph{TeX{} for the Impatient}.
%      \newblock
%       Addison-Wesley, Reading, Massachusetts, 1990.
%      \newblock (Available from CTAN in \texttt{info/impatient})
%
% \bibitem[Ars01a]{TITLEREF}
% Donald Arseneau.
% \newblock \emph{\Lpack{Titleref} package (version 3.1)}.
% \newblock April 2001.
% \newblock (Available from CTN as
%            \texttt{macros/latex/contrib/misc/titleref.sty})
%
% \bibitem[Ars01b]{CHAPTERBIB}
% Donald Arseneau.
% \newblock \emph{\Lpack{Chapterbib} package (version 1.9)}.
% \newblock September 2001.
% \newblock (Available from CTN as
%            \texttt{macros/latex/contrib/misc/chapterbib.sty})
%
% \bibitem[Ars03]{FRAMED}
% Donald Arseneau.
% \newblock \emph{\Lpack{Framed} package (version 0.8a)}.
% \newblock July 2003.
% \newblock (Available from CTAN as
%            \texttt{macros/latex/contrib/misc/framed.sty})
%
% \bibitem[ArWi00]{IFMTARG}
% Donald Arseneau and Peter Wilson.
% \newblock \emph{The ifmtarg package}.
% \newblock March, 2000.
% \newblock (Available from CTAN in 
%            \texttt{/macros/latex/contrib/misc})
%
%
%  \bibitem[Car94]{DELARRAY}
%  David Carlisle.
%  \newblock \emph{The \Lpack{delarray} package}.
%  \newblock March 1994.
%  \newblock (Available from CTAN in
%             \texttt{/macros/latex/required/tools})
%
%
% \bibitem[Car98a]{ENUMERATE}
% David Carlisle.
% \newblock \emph{The enumerate package}.
% \newblock August, 1998.
% \newblock (Available from CTAN in 
%            \texttt{/macros/latex/required/tools})
%
% \bibitem[Car98b]{REMRESET}
% David Carlisle.
% \newblock \emph{The remreset package}.
% \newblock August, 1998.
% \newblock (Available from CTAN in 
%            \texttt{/macros/latex/contrib/carlisle})
%
%  \bibitem[Car99]{TABULARX}
%  David Carlisle.
%  \newblock \emph{The \Lpack{tabularx} package}.
%  \newblock January 1999.
%  \newblock (Available from CTAN in
%             \texttt{/macros/latex/required/tools})
%
%  \bibitem[Car01]{DCOLUMN}
%  David Carlisle.
%  \newblock \emph{The \Lpack{dcolumn} package}.
%  \newblock May 2001.
%  \newblock (Available from CTAN in
%             \texttt{/macros/latex/required/tools})
%
% \bibitem[Coc02]{SUBFIGURE}
% Steven Douglas Cochran.
% \newblock \emph{The subfigure package}.
% \newblock March, 2002.
% \newblock (Available from CTAN in 
%            \texttt{/macros/latex/contrib/subfigure})
%
% \bibitem[Dal99]{NATBIB}
% Patrick W. Daly.
% \newblock \emph{Natural Sciences Citations and References}.
% \newblock May, 1999.
% \newblock (Available from CTAN in 
%            \texttt{/macros/latex/contrib/natbib})
%
% \bibitem[Dow00]{PATCHCMD}
% Michael J. Downes
% \newblock \emph{The patchcmd package}.
% \newblock July 2000.
% \newblock (Available from CTAN in
%            \texttt{/macros/latex/contrib/patchcmd})
%
% \bibitem[Fai98]{MOREVERB}
% Robin Fairbairns.
% \newblock \emph{The moreverb package}.
% \newblock December, 1998.
% \newblock (Available from CTAN in 
%            \texttt{/macros/latex/contrib/moreverb})
%
% \bibitem[Fai03]{FOOTMISC}
% Robin Fairbairns.
% \newblock \emph{\Lpack{footmisc} --- a portmanteau package for 
%            customising footnotes in LaTeX}.
% \newblock February 2003.
% \newblock (Available from CTAN in
%            \texttt{macros/latex/contrib/footmisc})
%
%
%    \bibitem[Fea03]{BOOKTABS}
%      Simon Fear.
%      \newblock \emph{Publication quality tables in \LaTeX}.
%       \newblock March, 2003.
%      \newblock (Available from CTAN in 
%                 \texttt{macros/latex/contrib/booktabs})
%
% \bibitem[Fra00]{CROP}
% Melchior Franz.
% \newblock \emph{The crop package}.
% \newblock February, 2000.
% \newblock (Available from CTAN in 
%            \texttt{/macros/latex/contrib/crop})
%
%
% \bibitem[GMS94]{GOOSSENS94}
% Michel Goossens, Frank Mittelbach, and Alexander Samarin.
% \newblock {\em The LaTeX Companion}.
% \newblock Addison-Wesley Publishing Company, 1994.
%
%
%
%    \bibitem[Knu84]{bk:knuth}  
%       Donald E. Knuth.
%       \newblock  \emph{The \TeX{}book}.
%       \newblock
%       Addison-Wesley, Reading, Massachusetts, 1984.
%
%    \bibitem[Lam94]{bk:lamport} 
%       Leslie Lamport.
%       \newblock  \emph{\LaTeX\ --- A Document Preparation System}.
%       \newblock
%       Addison-Wesley, Reading, Massachusetts, 1994.
%
% \bibitem[LMB99]{CLASSES}
% Leslie Lamport, Frank Mittelbach and Johannes Braams.
% \newblock \emph{Standard Document Classes for LaTeX version 2e}.
% \newblock September, 1999.
% \newblock (Available from CTAN as 
%            \texttt{/macros/latex/base/classes.dtx})
%
%  \bibitem[MC98]{ARRAY}
%  Frank Mittelbach and David Carlisle.
%  \newblock \emph{A new implementation of LaTeX's tabular and array 
%                  environment}
%  \newblock May 1998.
%  \newblock (Available from CTAN in
%             \texttt{/macros/latex/required/tools})
%
% \bibitem[Oos96]{FANCYHDR}
% Piet van Oostrum.
% \newblock \emph{Page layout in LaTeX}.
% \newblock June, 1996.
% \newblock (Available from CTAN in 
%            \texttt{/macros/latex/contrib/fancyhdr})
%
%
% \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[Sch98]{EVERYSHI}
% Martin Schr\"{o}der.
% \newblock \emph{The everyshi package}.
% \newblock August, 1998.
% \newblock (Available from CTAN in 
%            \texttt{/macros/latex/contrib/ms})
%
% \bibitem[SRR01]{VERBATIM}
% Rainer Sch\"{o}pf, Bernd Raichle and Chris Rowley.
% \newblock \emph{A new implementation of LaTeX's verbatim and verbatim*
%                 environments}.
% \newblock March, 2001.
% \newblock (Available from CTAN in 
%            \texttt{/macros/latex/required/tools})
%
% \bibitem[Wil99]{TOCVSEC2}
% Peter Wilson.
% \newblock \emph{The tocvsec2 package}.
% \newblock January, 1999.
% \newblock (Available from CTAN in 
%            \texttt{/macros/latex/contrib/tocvsec2})
%
% \bibitem[Wil00a]{EPIGRAPH}
% Peter Wilson.
% \newblock \emph{The epigraph package}.
% \newblock February, 2000.
% \newblock (Available from CTAN in 
%            \texttt{/macros/latex/contrib/epigraph})
%
% \bibitem[Wil00b]{ISOCLASS}
% Peter Wilson.
% \newblock \emph{LaTeX files for typesetting ISO standards}.
% \newblock February, 2000.
% \newblock (Available from CTAN in 
%            \texttt{/macros/latex/contrib/isostds/iso})
%
% \bibitem[Wil00c]{NEXTPAGE}
% Peter Wilson.
% \newblock \emph{The nextpage package}.
% \newblock February, 2000.
% \newblock (Available from CTAN in 
%            \texttt{/macros/latex/contrib/misc})
%
% \bibitem[Wil00d]{NEEDSPACE}
% Peter Wilson.
% \newblock \emph{The needspace package}.
% \newblock March, 2000.
% \newblock (Available from CTAN in 
%            \texttt{/macros/latex/contrib/misc})
%
% \bibitem[Wil01a]{ABSTRACT}
% Peter Wilson.
% \newblock \emph{The abstract package}.
% \newblock February, 2001.
% \newblock (Available from CTAN in 
%            \texttt{/macros/latex/contrib/abstract})
%
% \bibitem[Wil01b]{CHNGPAGE}
% Peter Wilson.
% \newblock \emph{The chngpage package}.
% \newblock February, 2001.
% \newblock (Available from CTAN in 
%            \texttt{/macros/latex/contrib/misc})
%
% \bibitem[Wil01c]{APPENDIX}
% Peter Wilson.
% \newblock \emph{The appendix package}.
% \newblock March, 2001.
% \newblock (Available from CTAN in 
%            \texttt{/macros/latex/contrib/appendix})
%
% \bibitem[Wil01d]{CCAPTION}
% Peter Wilson.
% \newblock \emph{The ccaption package}.
% \newblock March, 2001.
% \newblock (Available from CTAN in 
%            \texttt{/macros/latex/contrib/ccaption})
%
% \bibitem[Wil01e]{CHNGCNTR}
% Peter Wilson.
% \newblock \emph{The chngcntr package}.
% \newblock March, 2001.
% \newblock (Available from CTAN in 
%            \texttt{/macros/latex/contrib/misc})
%
% \bibitem[Wil01f]{HANGING}
% Peter Wilson.
% \newblock \emph{The hanging package}.
% \newblock March, 2001.
% \newblock (Available from CTAN in 
%            \texttt{/macros/latex/contrib/hanging})
%
% \bibitem[Wil01g]{TITLING}
% Peter Wilson.
% \newblock \emph{The titling package}.
% \newblock March, 2001.
% \newblock (Available from CTAN in 
%            \texttt{/macros/latex/contrib/titling})
%
% \bibitem[Wil01h]{TOCBIBIND}
% Peter Wilson.
% \newblock \emph{The tocbibind package}.
% \newblock April, 2001.
% \newblock (Available from CTAN in 
%            \texttt{/macros/latex/contrib/tocbibind})
%
% \bibitem[Wil01i]{TOCLOFT}
% Peter Wilson.
% \newblock \emph{The tocloft package}.
% \newblock April, 2001.
% \newblock (Available from CTAN in 
%            \texttt{/macros/latex/contrib/tocloft})
%
% \bibitem[Wil01j]{VERSE}
% Peter Wilson.
% \newblock \emph{Typesetting simple verse with LaTeX}.
% \newblock August, 2001.
% \newblock (Available from CTAN in 
%            \texttt{/macros/latex/contrib/verse})
%
%
% \bibitem[Wil03]{LEDMAC}
% Peter Wilson.
% \newblock \emph{\Lpack{ledmac}: A presumptuous attempt to port EDMAC and
%                 TABMAC to LaTeX}. 
% \newblock August 2003.
% \newblock (Available from CTAN in
%            \texttt{macros/latex/contrib/ledmac})
%
%
%
%
%
% \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         \~}
%%

