%%
%% This is file `mempatch.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% mempatch.dtx  (with options: `patch')
%% 
%%   Author: Peter Wilson (Herries Press) herries dot press at earthlink dot net
%%   Copyright 2001 --- 2006 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.
%% 
\ProvidesFile{mempatch.sty}[2006/08/08 v4.5 Patches for memoir class v1.618]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%   Version 4.0 was released simultaneously with version 1.618 of memoir.
%%% By definition there were no patches.
%%% Most of the subsequent additions are noted in the Addendum.
%%%
%%%   Version 4.3 introduces the \reparticle macro which is not in the
%%% Addendum.
%%%
%%%   Version 4.4 introduces several additional commands, in particular
%%% the \book command provides a document division above Part. See the
%%% code (towards the end) for details of the new commands.
%%%
%%%   Version 4.5 introduces commands for switching trim marks off and on
%%% and a \DisemulatePackage macro to negate a prior \EmulatedPackage.
%%% It also provides the functions of the pagenote package for end notes.
%%% See the code (near the end) for details.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Version 4.1 (2005/10/03 and later)

%%%% Use correct number of arguments for \memsecstarinfo (2 not 3).
\def\@ssect#1#2#3#4#5{%
  \M@gettitle{#5}%
  \memsecstarinfo{\m@msecn@mame}{#5}%
  \@mem@old@ssect{#1}{#2}{#3}{#4}{#5}}

%%%%%%%%%%%%
%%%%%%%%%%%% Side captions
%%%%%%%%%%%%

\newsavebox{\m@mscap@capbox}
\newsavebox{\m@mscap@fbox}

\newdimen\sidecapsep
  \sidecapsep=\marginparsep
\newdimen\sidecapwidth
  \sidecapwidth=\marginparwidth

\newdimen\m@m@tempdima
\newdimen\m@mscapraise

\newdimen\sidecapraise
  \sidecapraise \z@

\newcommand*{\setsidecappos}[1]{%
  \def\m@mscappos{#1}\def\@tempb{t}%
  \ifx\@tempb\m@mscappos
  \else
    \def\@tempb{b}%
    \ifx\@tempb\m@mscappos
    \else
      \def\@tempb{c}%
      \ifx\@tempb\m@mscappos
      \else
        \@memerror{Argument to \string\setsidecappos\space is not t or c or b.
                   \MessageBreak Set to c}{\@ehc}%
        \def\m@mscappos{c}%
      \fi
    \fi
  \fi}
\setsidecappos{c}

\newcommand{\sidecapmargin}[1]{%
  \def\@tempa{#1}\def\@tempb{left}%
    \ifx\@tempb\@tempa
      \def\m@mscapmarg{0}%   left
    \else
      \def\@tempb{right}%
      \ifx\@tempb\@tempa
        \def\m@mscapmarg{1}%  right
      \else
        \def\@tempb{outer}%
        \ifx\@tempb\@tempa
          \def\m@mscapmarg{2}%  outer
        \else
          \def\@tempb{inner}%
          \ifx\@tempb\@tempa
            \def\m@mscapmarg{3}%  inner
          \else
            \@memerror{Unrecognized argument for \string\sidecapmargin}%
                      {\@ehc}%
            \def\m@mscapmarg{-1}% error
          \fi
        \fi
      \fi
    \fi}
\sidecapmargin{left}

\newif\ifscapmargleft

\def\sidecapfloatwidth{\linewidth}
\newdimen\m@mscapmainwidth

\newdimen\m@mscaplkern
\newcommand*{\setm@mscaplkern}{%
  \m@mscaplkern=\sidecapwidth
  \advance\m@mscaplkern \sidecapsep
  \advance\m@mscaplkern \m@mscapmainwidth}

\newcommand*{\sidecapstyle}{%
%%%  \captionnamefont{\bfseries}%
  \ifscapmargleft
    \captionstyle{\raggedleft}%
  \else
    \captionstyle{\raggedright}%
  \fi}

\newcommand*{\sidecaption}{%
  \@ifnextchar [{\@sidecaption}{\@sidecaption[]}}
\def\@sidecaption[#1]#2{%
  \@ifnextchar [{\@@sidecaption{#1}{#2}}{\@@sidecaption{#1}{#2}[]}}
\def\@@sidecaption#1#2[#3]{%
  \ifx\@empty#1\@empty
    \def\m@mscap@fortoc{#2}%
  \else
    \def\m@mscap@fortoc{#1}%
  \fi
  \def\m@mscap@forcap{#2}%
  \ifx\@empty#3\@empty
    \def\m@mscaplabel{}%
  \else
    \def\m@mscaplabel{\@bsphack\label{#3}\@esphack}%
  \fi
  \m@mscapstart@fbox}

\newcommand*{\m@mscapstart@fbox}{%
  \setlength{\m@mscapmainwidth}{\sidecapfloatwidth}%
  \setm@mscaplkern
  \begin{lrbox}{\m@mscap@fbox}%
    \begin{minipage}[c]{\m@mscapmainwidth}}
\newcommand*{\m@mscapend@fbox}{%
    \end{minipage}%
  \end{lrbox}}

\def\endsidecaption{%
  \m@mscapend@fbox
  \refstepcounter\@captype
  \m@mscaplabel
  \begin{lrbox}{\m@mscap@capbox}%
    \begin{minipage}[c]{\sidecapwidth}%
      \sidecapstyle
      \@caption\@captype[\m@mscap@fortoc]{\m@mscap@forcap}
    \end{minipage}%
  \end{lrbox}%
  \m@mscapopboxes}
\newcommand*{\m@mscapopboxes}{%
  \m@mcalcscapraise
  \usebox{\m@mscap@fbox}\m@mscapcheckside
  \ifscapmargleft%
    \rlap{\kern-\m@mscaplkern
          \raisebox{\m@mscapraise}{\usebox{\m@mscap@capbox}}}%
  \else%
    \rlap{\kern\sidecapsep
          \raisebox{\m@mscapraise}{\usebox{\m@mscap@capbox}}}%
  \fi
  \gdef\m@mscapthisside{}}

\newcommand*{\m@mcalcscapraise}{%
  \def\@tempb{t}%
  \ifx\m@mscappos\@tempb
    \settoheight{\m@m@tempdima}{\strut\usebox{\m@mscap@capbox}}%
    \settoheight{\m@mscapraise}{\usebox{\m@mscap@fbox}}%
    \advance\m@mscapraise -\m@m@tempdima
    \advance\m@mscapraise  0.5ex
  \else
    \def\@tempb{b}%
    \ifx\m@mscappos\@tempb
      \settodepth{\m@m@tempdima}{\usebox{\m@mscap@fbox}}%
      \settodepth{\m@mscapraise}{\strut\usebox{\m@mscap@capbox}}%
      \advance\m@mscapraise -\m@m@tempdima
    \else
      \m@mscapraise=\z@
      \advance\m@mscapraise 0.25ex
    \fi
  \fi
  \advance\m@mscapraise  \sidecapraise}

\newcommand*{\m@mscapcheckside}{%
  \if@twocolumn
    \ifdim\hsize=\textwidth%  float*
      \m@mscapcheckregside
    \else
      \if@firstcolumn
        \scapmarglefttrue
      \else
        \scapmargleftfalse
      \fi
    \fi
  \else
    \m@mscapcheckregside
  \fi
  \m@mscapthisside}
\newcommand*{\m@mscapcheckregside}{%
    \if@twoside
      \checkoddpage
      \ifnum\m@mscapmarg<\@ne%      % left
        \scapmarglefttrue
      \else
        \ifnum\m@mscapmarg=\@ne%    % right
          \scapmargleftfalse
        \else
          \ifnum\m@mscapmarg=\tw@%  % outer
            \scapmarglefttrue
            \ifoddpage
              \scapmargleftfalse
            \fi
          \else%                  % inner
            \scapmargleftfalse
            \ifoddpage
              \scapmarglefttrue
            \fi
          \fi
        \fi
      \fi
    \else%     oneside
      \scapmarglefttrue
      \ifnum\m@mscapmarg>\@ne
        \ifnum\m@mscapmarg<\thr@@
          \scapmargleftfalse
        \fi
      \fi
    \fi}

\newcommand*{\overridescapmargin}[1]{%
  \def\@tempb{#1}\def\@tempa{left}%
  \ifx\@tempa\@tempb
    \def\m@mscapthisside{\scapmarglefttrue}%
  \else
    \def\@tempa{right}%
    \ifx\@tempa\@tempb
      \def\m@mscapthisside{\scapmargleftfalse}%
    \else
      \@memerror{Argument to \string\overridescapmargin\space neither
                 left nor right}{\@ehc}%
      \def\m@mscapthisside{}%
    \fi
  \fi}
\newcommand*{\m@mscapthisside}{}

\newcommand*{\sidecontcaption}{%
  \@sidecontcaption}
\def\@sidecontcaption#1{%
  \@ifnextchar [{\@@sidecontcaption{#1}}{\@@sidecontcaption{#1}[]}}
\def\@@sidecontcaption#1[#2]{%
  \def\m@mscap@forcap{#1}%
  \ifx\@empty#2\@empty
    \def\m@mscaplabel{}%
  \else
    \def\m@mscaplabel{\@bsphack\label{#2}\@esphack}%
  \fi
  \m@mscapstart@fbox}

\def\endsidecontcaption{%
  \m@mscapend@fbox
  \addtocounter{\@captype}{\m@ne}\refstepcounter\@captype
  \m@mscaplabel
  \begin{lrbox}{\m@mscap@capbox}%
    \begin{minipage}[c]{\sidecapwidth}%
      \sidecapstyle
      \@contcaption\@captype{\m@mscap@forcap}
    \end{minipage}%
  \end{lrbox}%
  \m@mscapopboxes}

\newcommand*{\sidenamedlegend}{%
  \@ifnextchar [{\@sidenamedlegend}{\@sidenamedlegend[]}}
\def\@sidenamedlegend[#1]#2{%
  \@@sidenamedlegend{#1}{#2}}
\def\@@sidenamedlegend#1#2{%
  \ifx\@empty#1\@empty
    \def\m@mscap@fortoc{#2}%
  \else
    \def\m@mscap@fortoc{#1}%
  \fi
  \def\m@mscap@forcap{#2}%
  \def\m@mscaplabel{}%
  \m@mscapstart@fbox}

\def\endsidenamedlegend{%
  \m@mscapend@fbox
  \begin{lrbox}{\m@mscap@capbox}%
    \begin{minipage}[c]{\sidecapwidth}%
      \sidecapstyle
      \@legend\@captype[\m@mscap@fortoc]{\m@mscap@forcap}
    \end{minipage}%
  \end{lrbox}%
  \m@mscapopboxes}

\newcommand*{\sidelegend}{%
  \@@sidelegend}
\def\@@sidelegend#1{%
  \def\m@mscap@forcap{#1}%
  \m@mscapstart@fbox}

\def\endsidelegend{%
  \m@mscapend@fbox
  \begin{lrbox}{\m@mscap@capbox}%
    \begin{minipage}[c]{\sidecapwidth}%
      \sidecapstyle
      \legend{\m@mscap@forcap}
    \end{minipage}%
  \end{lrbox}%
  \m@mscapopboxes}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Version 4.2 (2005/11/21 and later)

%%%%%%%%%%%%
%%%%%%%%%%%% Extensions to \AtBeginDocument
%%%%%%%%%%%%

\renewcommand{\InputIfFileExists}[2]{%
  \IfFileExists{#1}%
    {#2\@addtofilelist{#1}\m@matbeginf{#1}%
     \@@input \@filef@und
     \m@matendf{#1}%
     \killm@matf{#1}}}

\newcommand{\m@matbeginf}[1]{\@ifundefined{#1-m@mfb}{}%
  {\@nameuse{#1-m@mfb}}}
\newcommand{\m@matendf}[1]{\@ifundefined{#1-m@mfe}{}%
  {\@nameuse{#1-m@mfe}}}

\newcommand*{\killm@matf}[1]{%
  \@namelet{#1-m@mfb}\relax
  \@namelet{#1-m@mfe}\relax}

\newcommand{\AtBeginFile}[2]{\@ifundefined{#1-m@mfb}%
  {\@namedef{#1-m@mfb}{#2}}%
  {\expandafter\addtodef\csname #1-m@mfb\endcsname{}{#2}}}
\newcommand{\AtEndFile}[2]{\@ifundefined{#1-m@mfe}%
  {\@namedef{#1-m@mfe}{#2}}%
  {\expandafter\addtodef\csname #1-m@mfe\endcsname{}{#2}}}

\newcommand{\AtBeginPackage}[2]{%
  \AtBeginFile{#1.\@pkgextension}{#2}}
\newcommand{\AtEndPackage}[2]{%
  \AtEndFile{#1.\@pkgextension}{#2}}
\newcommand{\RequireAtEndPackage}[2]{%
  \@ifpackageloaded{#1}{#2}%
  {\AtEndFile{#1.\@pkgextension}{#2}}}

\newcommand{\AtBeginClass}[2]{%
  \AtBeginFile{#1.\@clsextension}{#2}}
\newcommand{\AtEndClass}[2]{%
  \AtEndFile{#1.\@clsextension}{#2}}
\newcommand{\RequireAtEndClass}[2]{%
  \@ifclassloaded{#1}{#2}%
  {\AtEndFile{#1.\@clsextension}{#2}}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Version 4.3 (2005/12/13 and later)

%%% Replicate the appearance of a \section in the article class
\makechapterstyle{reparticle}{%
  \setlength{\beforechapskip}{3.5ex \@plus 1ex \@minus .2ex}%
  \renewcommand*{\chapterheadstart}{\vspace{\beforechapskip}}%
  \setlength{\afterchapskip}{2.3ex \@plus .2ex}%
  \renewcommand*{\printchaptername}{}%
  \renewcommand*{\chapternamenum}{}%
  \renewcommand*{\chaptitlefont}{\normalfont\Large\bfseries}%
  \renewcommand*{\chapnumfont}{\chaptitlefont}%
  \renewcommand*{\printchapternum}{\@hangfrom{\chapnumfont \thechapter\quad}}%
  \renewcommand*{\afterchapternum}{}}

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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Version 4.4 (2006/01/06 and later)

%%% \captiontitlefinal{<stuff>} will put <stuff> immediately at the
%%% end of a \caption's title text and it will not appear in the
%%% LoF/LoT/etc. For example: \captiontitlefinal{.}
%%% (Code supplied by Frederic Connes)
\newcommand*{\captiontitlefinal}[1]{\def\@contfinal{#1}}
\captiontitlefinal{}

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

%%% Use the declaration \xindyindex if you will be using the xindy program
%%% instead of MakeIndex to process index entries. As yet, xindy will not
%%% hyperref any entries.
%%% (Code supplied by Frederic Connes)
\newif\ifm@mxindy
\m@mxindyfalse
\newcommand*{\xindyindex}{\m@mxindytrue}
\def\@@wrspindexhyp#1|#2|#3\\{%
  \ifshowindexmark\@showidx{#1}\fi
  \ifx\\#2\\%
    \protected@write\@auxout{}%
      {\string\@@wrindexm@m{\@idxfile}%
        \ifm@mxindy{#1}\else{#1|hyperspindexpage(\thepage)}\fi
      {\@nameuse{the\@sptheidx}}}%
  \else
    \def\Hy@temp@A{#2}%
    \ifx\Hy@temp@A\HyInd@ParenLeft
      \protected@write\@auxout{}%
        {\string\@@wrindexm@m{\@idxfile}%
         \ifm@mxindy{#1|#2}\else{#1|#2hyperspindexpage(\thepage)}\fi
      {\@nameuse{the\@sptheidx}}}%
    \else
      \protected@write\@auxout{}%
        {\string\@@wrindexm@m{\@idxfile}{#1|#2}%
      {\@nameuse{the\@sptheidx}}}%
    \fi
  \fi
  \endgroup
  \@esphack}

\renewenvironment{theindex}{%
  \clearforchapter%             <- added
  \if@twocolumn
    \@restonecolfalse
  \else
    \@restonecoltrue
  \fi
  \ifonecolindex
    \onecolumn
    \chapter*{\indexname}
    \preindexhook
  \else
    \setlength{\columnseprule}{\indexrule}%
    \setlength{\columnsep}{\indexcolsep}%
    \twocolumn[\@makeschapterhead{\indexname}
               \preindexhook]%
  \fi
  \indexmark
  \ifnoindexintoc\else
    \phantomsection
    \addcontentsline{toc}{chapter}{\indexname}
  \fi
  \thispagestyle{chapter}\parindent\z@
  \parskip\z@ \@plus .3\p@\relax
  \let\item\@idxitem}%
  {\if@restonecol\onecolumn\else\twocolumn\fi}

%%% \ignorespaces fix by Aaron Rendahl to remove extraneous space
%%% with \\>[0pt] ending a verse line
\renewcommand*{\verselinebreak}[1][\z@]{\newline\hspace*{#1}\ignorespaces}

%%% Use the \verselinenumbersleft declaration to set verse line numbers
%%% at the left. To later return to the default of setting them at the right
%%% use the \verselinenumbersright declaration.
\newcommand*{\verselinenumbersright}{\def\@vstypelinenum{\@vslnumright}}
\newcommand*{\verselinenumbersleft}{\def\@vstypelinenum{\@vslnumleft}}
\verselinenumbersright

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

%%% Fix for wrapped verbatims in list environments.
\newcommand*{\raggedwrap}{%
  \@rightskip\@flushglue
  \rightskip\@rightskip
  \leftskip\@totalleftmargin
  \parindent\ragrparindent}
\renewcommand*{\wrappingon}{%
  \def\@xobeysp{~\discretionary{\verbatimbreakchar}%
    {\kern\verbatimindent}{}}%
  \def\wrapright{\raggedwrap}}

%%% Use the \nopartblankpage declaration if you don't want a blank
%%% page after a Part title page. The default is set by \partblankpage.
\newif\ifm@mnopartnewpage
\newcommand*{\partblankpage}{\m@mnopartnewpagefalse}
\newcommand*{\nopartblankpage}{\m@mnopartnewpagetrue}

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

\renewcommand{\partnumberline}[1]{%
  \hb@xt@\@tempdima{%
    \cftpartname\@cftbsnum #1\@cftasnum\hfil}\@cftasnumb\space}

\aliaspagestyle{afterpart}{empty}
\aliaspagestyle{book}{empty}
\aliaspagestyle{afterbook}{empty}

%%% The \book (and \book*) document division is one level higher than
%%% a \cs{part}. It has the same kind of controls over its appearance in
%%% the body of the document and in the ToC as \part has.
\newcommand*{\book}{%
  \@setupbook
  \secdef\@book\@sbook}

\newcommand*{\bookmark}[1]{}
\newcounter{book} \setcounter{book}{0}
\renewcommand*{\thebook}{\Roman{book}}
\newcommand*{\theHbook}{\arabic{book}}
\newcommand*{\toclevel@book}{-2}

\newcommand*{\beforebookskip}{\null\vfil}
\newcommand*{\midbookskip}{\par \vskip 2\baselineskip}
\newcommand*{\afterbookskip}{\vfil\newpage}

\newcommand*{\@setupbook}{%
  \if@openright
    \cleardoublepage
  \else
    \clearpage
  \fi
  \thispagestyle{book}%
  \if@twocolumn
    \@tempswatrue
  \else
    \@tempswafalse
  \fi
  \beforebookskip}

\newcommand*{\bookname}{Book}
\newcommand*{\booknamefont}{\normalfont\huge\bfseries}
\newcommand*{\booknumfont}{\normalfont\huge\bfseries}
\newcommand*{\booktitlefont}{\normalfont\Huge\bfseries}

\newcommand*{\printbookname}{\booknamefont \bookname}
\newcommand*{\booknamenum}{\space}
\newcommand*{\printbooknum}{\booknumfont \thebook}
\newcommand{\printbooktitle}[1]{\booktitlefont #1}

\newcommand*{\membookinfo}[3]{}
\newcommand*{\membookstarinfo}[1]{}

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

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

%%% Use the \nobookblankpage declaration if you don't want a blank
%%% page after a Book title page. The default is set by \bookblankpage.
\newif\ifm@mnobooknewpage
\newcommand*{\bookblankpage}{\m@mnobooknewpagefalse}
\newcommand*{\nobookblankpage}{\m@mnobooknewpagetrue}

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

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

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

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

\renewcommand*{\settocdepth}[1]{%
  \@tempswafalse
  \nametest{#1}{none}%
  \ifsamename
    \addtocontents{toc}{\changetocdepth{-10}}%
    \@tempswatrue
  \fi
  \nametest{#1}{book}%
  \ifsamename
    \addtocontents{toc}{\changetocdepth{-2}}%
    \@tempswatrue
  \fi
  \nametest{#1}{part}%
  \ifsamename
    \addtocontents{toc}{\changetocdepth{-1}}%
    \@tempswatrue
  \fi
  \nametest{#1}{chapter}%
  \ifsamename
    \addtocontents{toc}{\changetocdepth{0}}%
    \@tempswatrue
  \fi
  \nametest{#1}{section}%
  \ifsamename
    \addtocontents{toc}{\changetocdepth{1}}%
    \@tempswatrue
  \fi
  \nametest{#1}{subsection}%
  \ifsamename
    \addtocontents{toc}{\changetocdepth{2}}%
    \@tempswatrue
  \fi
  \nametest{#1}{subsubsection}%
  \ifsamename
    \addtocontents{toc}{\changetocdepth{3}}%
    \@tempswatrue
  \fi
  \nametest{#1}{paragraph}%
  \ifsamename
    \addtocontents{toc}{\changetocdepth{4}}%
    \@tempswatrue
  \fi
  \nametest{#1}{subparagraph}%
  \ifsamename
    \addtocontents{toc}{\changetocdepth{5}}%
    \@tempswatrue
  \fi
  \nametest{#1}{all}%
  \ifsamename
    \addtocontents{toc}{\changetocdepth{50}}%
    \@tempswatrue
  \fi
  \if@tempswa
    \@ifundefined{toclevel@#1}{%
      @memwarn{Unknown toclevel for #1}%
    }{%
      \setcounter{tocdepth}{\@nameuse{toclevel@#1}}%
    }
  \else
    \@memerror{%
      Unknown document division name (#1)
    }{%
     I'll ignore it.
     Type \space <return> and I'll continue.}%
  \fi}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Version 4.5 (2006/02/11 and later)

%%% \DisemulatePackage{pack} undoes a previous \EmulatedPackage{pack}.
%%% For example \DisemulatePackage{index} if you need to use the index package.
\providecommand*{\DisemulatePackage}[1]{%
  \@namelet{ver@#1.\@pkgextension}\relax}

\renewcommand{\@nameedef}[1]{%
  \expandafter\protected@edef\csname #1\endcsname}

%%% change timing of \@mainmattertrue in \@smemmain
\renewcommand*{\@smemmain}{%
  \ifartopt
    \clearpage
  \else
    \cleardoublepage
    \counterwithin{figure}{chapter}%
    \counterwithin{table}{chapter}%
  \fi
  \@mainmattertrue
  \setcounter{secnumdepth}{\value{maxsecnumdepth}}}

\renewcommand{\@footnotetext}[1]{%
  \reset@font\m@mold@footnotetext{#1}\m@mmf@prepare}

\newif\ifm@mpn@new@chap
  \m@mpn@new@chapfalse
\newif\ifm@mpn@new@schap
  \m@mpn@new@schapfalse

%%% Fix for mistiming of \chaptermark with article option
\def\@chapter[#1]#2{%
  \m@mpn@new@chaptrue
  \def\f@rbdy{#2}%
  \ifx\ch@pt@c\@empty % no optional args
    \def\f@rtoc{#2}%
    \def\f@rhdr{#2}%
  \else                  % at least one opt arg
    \let\f@rtoc\ch@pt@c
    \ifx\@empty#1\@empty
      \let\f@rhdr\ch@pt@c
    \else
      \def\f@rhdr{#1}%
    \fi
  \fi
  \m@m@Andfalse
  \ifnum \c@secnumdepth >\m@ne
    \if@mainmatter
      \m@m@Andtrue
    \fi
  \fi
  \ifm@m@And
    \refstepcounter{chapter}%
  \fi
  \ifartopt
    \@makechapterhead{#2}%
    \@afterheading
    \chaptermark{\f@rhdr}%
  \else
    \chaptermark{\f@rhdr}%
    \insertchapterspace
    \if@twocolumn
      \@topnewpage[\@makechapterhead{#2}]%
    \else
      \@makechapterhead{#2}%
    \fi
    \@afterheading
  \fi
  \ifm@m@And
    \ifanappendix
      \addcontentsline{toc}{appendix}{%
        \protect\chapternumberline{\thechapter}\f@rtoc}%
      \memappchapinfo{\thechapter}{\f@rtoc}{\f@rhdr}{#2}%
    \else
      \addcontentsline{toc}{chapter}{%
        \protect\chapternumberline{\thechapter}\f@rtoc}%
      \memchapinfo{\thechapter}{\f@rtoc}{\f@rhdr}{#2}%
    \fi
  \else
    \addcontentsline{toc}{chapter}{\f@rtoc}%
    \ifanappendix
      \memappchapinfo{}{\f@rtoc}{\f@rhdr}{#2}%
    \else
      \memchapinfo{}{\f@rtoc}{\f@rhdr}{#2}%
    \fi
  \fi
  \ifheadnameref\M@gettitle{\f@rhdr}\else\M@gettitle{\f@rtoc}\fi}

\renewcommand{\@schapter}[1]{%
  \m@mpn@new@schaptrue
  \def\f@rbdy{#1}%
  \ifartopt
    \@makeschapterhead{#1}%
  \else
    \if@twocolumn
      \@topnewpage[\@makeschapterhead{#1}]
    \else
      \@makeschapterhead{#1}%
    \fi
  \fi
  \@afterheading}
\renewcommand{\@m@mschapter}[2][\@empty]{%
  \@schapter{#2}%
  \ifx \@empty#1
    \def\f@rhdr{#2}%
  \else
    \def\f@rhdr{#1}%
    \setcounter{secnumdepth}{-10}%
    \chaptermark{#1}%
    \setcounter{secnumdepth}{\value{maxsecnumdepth}}%
  \fi
  \ifanappendix
    \memappchapstarinfo{\f@rhdr}{#2}%
  \else
    \memchapstarinfo{\f@rhdr}{#2}%
  \fi
  \ifheadnameref\M@gettitle{\f@rhdr}\else\M@gettitle{#2}\fi}

%%%% Use \showtrimsoff and \showtrimson to switch trim marks off and on.
%%%% If the showtrims option has not been used, these do nothing.
\newcommand*{\showtrimsoff}{\showtrimsfalse}
\newcommand*{\showtrimson}{\showtrimstrue}
\renewcommand*{\mem@shipii}{%
  \ifvoid\@cclv
    \mem@oldshipout\box\@cclv
  \else
    \ifshowtrims
      \mem@oldshipout\vbox{\trimmarks\unvbox\@cclv}%
    \else
      \mem@oldshipout\box\@cclv
    \fi
  \fi}

%%%% Fix to stop \sidepars moving up or down
\long\def\@sidepar[#1]#2{\@bsphack\strut\vadjust{%
  \checkoddpage
  \ifsideparswitch
    \ifreversesidepar
      \ifoddpage
        \oddpagefalse
      \else
        \oddpagetrue
      \fi
    \fi
  \else
    \oddpagetrue
    \ifreversesidepar
      \oddpagefalse
     \fi
  \fi
  \rlap{\kern-\parindent
    \if@twocolumn
      \if@firstcolumn%              put at left
        \kern -\marginparsep \kern -\marginparwidth
      \else%                        put at right
        \kern \columnwidth \kern \marginparsep
      \fi
    \else
      \ifoddpage%                   put at right
        \kern \textwidth \kern \marginparsep
      \else%                        put at left
        \kern -\marginparsep \kern -\marginparwidth
      \fi
    \fi
    \setbox0=\vtop to 0pt{%
      \begin{minipage}[t]{\marginparwidth}%
        \ifoddpage #2\else #1\fi%
      \end{minipage}%
      \vss}%
    \vtop to 0pt{\kern\sideparvshift%  default should be 0pt
      \kern-\dp\strutbox
      \kern-\ht0
      \box0 \vss}}}%
  \@esphack}
\setlength{\sideparvshift}{0pt}

%%%% Ensure that fancybreaks close the paragraph they start
\renewcommand{\@fbreak}[1]{\par
  \penalty -100
  \noindent\parbox{\linewidth}{\centering #1}%
  \par
  \@afterindentfalse
  \@afterheading}
\renewcommand{\@sfbreak}[1]{\par
  \penalty -100
  \noindent\parbox{\linewidth}{\centering #1}%
  \par
  \@afterindenttrue
  \@afterheading}

%%%% \begin{vplace}[<num>] vertically centered stuff \end{vplace}
%%%% <num> adjusts above space wrt to below space
\newenvironment{vplace}[1][1]{%
  \par\vspace*{\stretch{#1}}%
}{%
  \vspace*{\stretch{1}}%
  \par}

%%%%
%%%% Include the functions of the pagenote package
%%%%

\newif\ifm@mpnpageopt
  \m@mpnpageoptfalse
\newif\ifm@mpncontopt
  \m@mpncontoptfalse
\newcommand*{\notepageref}{\m@mpnpageopttrue}
\newcommand*{\continuousnotenums}{%
  \counterwithout{pagenote}{chapter}
  \renewcommand{\thepagenote}{\arabic{pagenote}}}

  \newcounter{pagenote}[chapter]
\renewcommand{\thepagenote}{\arabic{pagenote}}
\setcounter{pagenote}{0}
\newif\ifmempagenotes
  \mempagenotesfalse

\newcommand*{\makepagenote}{%
  \newwrite\@notefile
  \immediate\openout\@notefile=\jobname.ent
  \mempagenotestrue
  \def\pagenote{\@bsphack\begingroup
    \@sanitize
    \m@m@wrpnote}%
  \typeout{Writing note file \jobname.ent}%
  \let\makepagenote\@empty}

\newcommand{\immediate@protected@write}[3]{%
  \begingroup
  #2%
  \let\protect\@unexpandable@protect
  \edef\reserved@a{\immediate\write#1{#3}}%
  \reserved@a
  \endgroup
  \if@nobreak\ifvmode\nobreak\fi\fi}

\ifm@mpnpageopt
  \let\m@m@pnwrite\protected@write
\else
  \let\m@m@pnwrite\immediate@protected@write
\fi

\newcommand*{\pnchap}{\f@rtoc}
\newcommand*{\pnschap}{\f@rbdy}

\newcommand{\m@m@wrpnote}[2][]{%
  \refstepcounter{pagenote}%
  \notenumintext{\thepagenote}%
  \ifm@mpn@new@chap
    \global\m@mpn@new@chapfalse
    \addtonotes{\string\pagenotesubhead{\@chapapp}{\thechapter}{\pnchap}}%
  \fi
  \ifm@mpn@new@schap
    \global\m@mpn@new@schapfalse
    \addtonotes{\string\pagenotesubhead{\@chapapp}{}{\pnschap}}%
  \fi
  \m@m@pnwrite\@notefile{}
    {\string\noteentry{\thepagenote}{#1}{#2}{\thepage}}%
  \endgroup
  \@esphack}

\def\pagenote{\@bsphack\begingroup \@sanitize\m@m@pagenote}
\newcommand{\m@m@pagenote}[2][]{\endgroup\@esphack}

\newcommand*{\pagetofootnote}{%
  \let\memsavepagenote\pagenote
  \renewcommand{\pagenote}[2][]{\footnote{##2}}}
\newcommand*{\foottopagenote}{%
  \let\memsavefootnote\footnote
  \renewcommand*{\footnote}[2][]{\pagenote{##2}}}

\newcommand{\addtonotes}[1]{%
  \ifmempagenotes
 \IfFileExists{\jobname.ent}{\m@m@pnwrite\@notefile{}{#1}}{\mempnofilewarn}%
\fi}

\newcommand{\notenumintext}[1]{%
  \textsuperscript{#1}}
\newcommand{\notenuminnotes}[1]{%
  {\normalfont #1.}\space}
\newcommand{\noteentry}[4]{%
  \prenoteinnotes
  \noteidinnotes{#1}{#2}\pageinnotes{#4}\noteinnotes{#3}%
  \postnoteinnotes}

\newcommand{\idtextinnotes}[1]{%
  [#1]\space}
\newcommand{\noteidinnotes}[2]{%
  \@ifmtarg{#2}{%
    \notenuminnotes{#1}}{\idtextinnotes{#2}}}
\newcommand{\pageinnotes}[1]{%
  \ifm@mpnpageopt \printpageinnotes{#1}\fi}
\newcommand*{\printpageinnotes}[1]{%
  (\pagerefname\ #1)\space}
\newcommand{\noteinnotes}[1]{#1}

\newcommand{\prenoteinnotes}{\par\noindent}
\newcommand{\postnoteinnotes}{\par}

\providecommand*{\notesname}{Notes}
\newcommand*{\notedivision}{\chapter{\notesname}}

\newcommand*{\printpagenotes}{\@ifstar{\@sprintpagenotes}{\@printpagenotes}}
\newcommand*{\mempnofilewarn}{%
  \ClassWarning{memoir}{There is no .ent file}}

\newcommand*{\@sprintpagenotes}{%
  \ifmempagenotes
  \notedivision
\IfFileExists{\jobname.ent}{%
  \immediate\closeout\@notefile
  \input{\jobname.ent}%
  \immediate\openout\@notefile=\jobname.ent%
  }{%
  \mempnofilewarn
}%
\fi}

\newcommand*{\@printpagenotes}{%
  \ifmempagenotes
    \notedivision
    \IfFileExists{\jobname.ent}{%
      \immediate\closeout\@notefile
      \input{\jobname.ent}%
     }{%
       \mempnofilewarn
     }
  \fi}

\newcommand*{\pagenotesubhead}[3]{%
  \section*{#1 #2 #3}}

\EmulatedPackage{pagenote}

\newcommand*{\@cftn@me}{}
\renewcommand*{\numberline}[1]{%
  \hb@xt@\@tempdima{\@cftn@me\@cftbsnum #1\@cftasnum\hfil}\@cftasnumb}

\renewcommand*{\newlistentry}[4][\@empty]{%
  \@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}
  \@namedef{l@#2}##1##2{%
    \ifnum \@nameuse{c@#3depth} > #4\relax
      \vskip \@nameuse{cftbefore#2skip}%
      {\leftskip \@nameuse{cft#2indent}\relax
       \rightskip \@tocrmarg
       \parfillskip -\rightskip
       \parindent \@nameuse{cft#2indent}\relax\@afterindenttrue
       \interlinepenalty\@M
       \leavevmode
       \settowidth{\@tempdima}{\@nameuse{cft#2font}\@nameuse{cft#2name}}%
       \addtolength{\@tempdima}{\@nameuse{cft#2numwidth}}%
\expandafter\let\expandafter\@cftbsnum\csname cft#2presnum\endcsname
\expandafter\let\expandafter\@cftasnum\csname cft#2aftersnum\endcsname
\expandafter\let\expandafter\@cftasnumb\csname cft#2aftersnumb\endcsname
\expandafter\let\expandafter\@cftn@me\csname cft#2name\endcsname
       \advance\leftskip\@tempdima \null\nobreak\hskip -\leftskip
       {\@nameuse{cft#2font}##1}\nobreak
       \@nameuse{cft#2fillnum}{##2}}
     \fi
  }% end of \l@#2
  \expandafter\newlength\csname cftbefore#2skip\endcsname
    \setlength{\@nameuse{cftbefore#2skip}}{\z@ \@plus .2\p@}
  \expandafter\newlength\csname cft#2indent\endcsname
  \expandafter\newlength\csname cft#2numwidth\endcsname
  \ifcase #4\relax%         0 (level 1)
    \setlength{\@nameuse{cft#2indent}}{0em}
    \setlength{\@nameuse{cft#2numwidth}}{2.3em}
  \or%                      1 (level 2)
    \setlength{\@nameuse{cft#2indent}}{2.3em}
    \setlength{\@nameuse{cft#2numwidth}}{3.2em}
  \or%                      2 (level 3)
    \setlength{\@nameuse{cft#2indent}}{5.5em}
    \setlength{\@nameuse{cft#2numwidth}}{4.1em}
  \or%                      3 (level 4)
    \setlength{\@nameuse{cft#2indent}}{8.5em}
    \setlength{\@nameuse{cft#2numwidth}}{5.0em}
  \else%                    anything else
    \setlength{\@nameuse{cft#2indent}}{10.5em}
    \setlength{\@nameuse{cft#2numwidth}}{6.0em}
  \fi
  \@namedef{cft#2font}{\normalfont}
  \@namedef{cft#2name}{}
  \@namedef{cft#2presnum}{}
  \@namedef{cft#2aftersnum}{}
  \@namedef{cft#2aftersnumb}{}
  \@namedef{cft#2dotsep}{\cftdotsep}
  \@namedef{cft#2leader}{\normalfont\cftdotfill{\@nameuse{cft#2dotsep}}}
  \@namedef{cft#2pagefont}{\normalfont}
  \@namedef{cft#2afterpnum}{}
  \@namedef{cft#2toclevel@#2}{#4}
  \@namedef{cft#2fillnum}##1{%
    {\@nameuse{cft#2leader}}\nobreak
    \hb@xt@\@pnumwidth{%
      \hfil\@nameuse{cft#2pagefont}##1}\@nameuse{cft#2afterpnum}\par}
}% end of \newlistentry

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

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

\renewcommand*{\setrectanglesize}[3]{%
  \nametest{#1}{*}%
  \ifsamename                           % H = *
    \nametest{#2}{*}%
    \ifsamename                         % W = *
      \@memerror{%
        The combination of argument values is ambiguous.\MessageBreak
        The lengths will be set to zero}{\@ehd}
      \setlength{\@tempdima}{0pt}%
      \setlength{\@tempdimb}{0pt}%
    \else                               % W
      \nametest{#3}{*}%
      \ifsamename                       % r = *
        \setlength{\@tempdimb}{#2}%
        \setlength{\@tempdima}{\@tempdimb}%
      \else                             % r
        \setlength{\@tempdimb}{#2}
        \setlength{\@tempdima}{#3\@tempdimb}
      \fi
    \fi
  \else                                 % H
    \nametest{#2}{*}%
    \ifsamename                         % W = *
      \nametest{#3}{*}%
      \ifsamename                       % r = *
        \setlength{\@tempdima}{#1}%
        \setlength{\@tempdimb}{\@tempdima}%
      \else                             % r
        \setlength{\@tempdima}{#1}%
        \setlength{\@tempdimb}{#3\@tempdima}%
      \fi
    \else                               % W
      \setlength{\@tempdima}{#1}%
      \setlength{\@tempdimb}{#2}%
    \fi
  \fi
}

\renewcommand*{\setfillsize}[5]{%
  \nametest{#2}{*}%
  \ifsamename                                % C = *
    \nametest{#3}{*}%
    \ifsamename                              % L = *
      \nametest{#4}{*}%
      \ifsamename                            % R = *
        \@memerror{%
          The combination of argument values is ambiguous.\MessageBreak
          The lengths will be set to zero}{\@ehd}
        \setlength{\@tempdima}{0pt}%
        \setlength{\@tempdimb}{0pt}%
        \setlength{\@tempdimc}{0pt}%
      \else                                  % R
        \nametest{#5}{*}%
        \ifsamename                          % r = *
          \setlength{\@tempdimb}{#4}%
          \setlength{\@tempdima}{\@tempdimb}%
          \setlength{\@tempdimc}{#1}%
          \advance\@tempdimc -\@tempdima
          \advance\@tempdimc -\@tempdimb
        \else                                % r
          \setlength{\@tempdimb}{#4}%
          \setlength{\@tempdima}{#5\@tempdimb}%
          \setlength{\@tempdimc}{#1}%
          \advance\@tempdimc -\@tempdima
          \advance\@tempdimc -\@tempdimb
        \fi
      \fi
    \else                                    % L
      \nametest{#4}{*}%
      \ifsamename                            % R = *
        \nametest{#5}{*}%
        \ifsamename                          % r = *
          \setlength{\@tempdima}{#3}%
          \setlength{\@tempdimb}{\@tempdima}%
          \setlength{\@tempdimc}{#1}%
          \advance\@tempdimc -\@tempdima
          \advance\@tempdimc -\@tempdimb
        \else                                % r
          \setlength{\@tempdima}{#3}%
          \setlength{\@tempdimb}{#5\@tempdima}%
          \setlength{\@tempdimc}{#1}%
          \advance\@tempdimc -\@tempdima
          \advance\@tempdimc -\@tempdimb
        \fi
      \else                                  % R
        \setlength{\@tempdima}{#3}%
        \setlength{\@tempdimb}{#4}%
        \setlength{\@tempdimc}{#1}%
        \advance\@tempdimc -\@tempdima
        \advance\@tempdimc -\@tempdimb
      \fi
    \fi
  \else                                      % C is valued
    \nametest{#3}{*}%
    \ifsamename                              % L = *
      \nametest{#4}{*}%
      \ifsamename                            % R = *
        \nametest{#5}{*}%
        \ifsamename                          % r = *
          \setlength{\@tempdimc}{#2}%
          \setlength{\@tempdima}{#1}%
          \advance\@tempdima -\@tempdimc
          \@tempdima = 0.5\@tempdima
          \@tempdimb = \@tempdima
        \else                                % r (CODE PERHAPS FIXED)
          \setlength{\@tempdimc}{#2}%
          \setlength{\@tempdimb}{#1}%        % T
          \advance\@tempdimb -\@tempdimc     % T - C
          \@tempdima = 1000sp
          \@tempdima = #5\@tempdima          % 1000r sp
          \advance\@tempdima by 1000sp       % 1000(1+r)sp
          \@tempcnta = \@tempdima            % 1000(1+r)
          \@tempdima = \@tempdimb            % T - C
          \divide\@tempdima by \@tempcnta    % (T-C)/1000(1+r) pts
          \@tempdima = 1000\@tempdima        % (T-C)/(1+r)  pts = L
          \advance\@tempdimb by -\@tempdima  % = R
        \fi
      \else                                  % R
        \setlength{\@tempdimc}{#2}%
        \setlength{\@tempdimb}{#4}%
        \setlength{\@tempdima}{#1}%
        \advance\@tempdima -\@tempdimc
        \advance\@tempdima -\@tempdimb
      \fi
    \else                                    % L
      \nametest{#4}{*}%
      \ifsamename                            % R = *
        \setlength{\@tempdimc}{#2}%
        \setlength{\@tempdima}{#3}%
        \setlength{\@tempdimb}{#1}%
        \advance\@tempdimb -\@tempdimc
        \advance\@tempdimb -\@tempdima
      \else                                  % R
        \@memerror{%
          The combination of argument values is ambiguous.\MessageBreak
          The lengths will be set to zero}{\@ehd}%
        \setlength{\@tempdima}{0pt}%
        \setlength{\@tempdimb}{0pt}%
        \setlength{\@tempdimc}{#2}%
      \fi
    \fi
  \fi}

\newcommand*{\m@mclassicht}{%
  \setlength{\@tempdima}{\textheight}%
  \divide\@tempdima \baselineskip
  \@tempcnta=\@tempdima
  \setlength{\textheight}{\@tempcnta\baselineskip}%
  \addtolength{\textheight}{\topskip}}

\newcommand*{\m@mlinesht}{%
  \setlength{\@tempdima}{\textheight}%
  \advance\@tempdima -\baselineskip
  \divide\@tempdima \baselineskip
  \@tempcnta=\@tempdima
  \setlength{\textheight}{\@tempcnta\baselineskip}%
  \addtolength{\textheight}{\topskip}}

\newcommand*{\m@mnearestht}{%
  \setlength{\@tempdima}{\textheight}%
  \advance\@tempdima -\topskip
  \advance\@tempdima 0.5\baselineskip
  \divide\@tempdima \baselineskip
  \@tempcnta=\@tempdima
  \setlength{\textheight}{\@tempcnta\baselineskip}%
  \addtolength{\textheight}{\topskip}}

\renewcommand*{\checkthelayout}[1][classic]{%
  \@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}
  \nametest{#1}{classic}%
  \ifsamename
    \m@mclassicht
  \else
    \nametest{#1}{lines}%
    \ifsamename
      \m@mlinesht
    \else
      \nametest{#1}{nearest}%
      \ifsamename
        \m@mnearestht
      \else
        \nametest{#1}{fixed}
        \ifsamename
        \else%                   not classic, lines, nearest, or fixed
          \@memerror{Optional argument is not one of:\MessageBreak
                     classic, fixed, lines, or nearest. \MessageBreak
                     I will assume the default}%
                    {\@ehc}%
        \fi
      \fi
    \fi
  \fi
  \setulmargins{\uppermargin}{*}{*}
  \@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
}
\renewcommand*{\checkandfixthelayout}[1][classic]{%
  \checkthelayout[#1]%
  \fixthelayout
  \typeoutlayout}

\newcommand*{\midsloppy}{%
  \tolerance 5000%
  \hbadness 4000%
  \emergencystretch 1.5em%
  \hfuzz .1\p@
  \vfuzz\hfuzz}
\newenvironment{midsloppypar}{\par\midsloppy}{\par}

\newdimen\everylistparindent
  \everylistparindent \z@
\renewcommand*{\list}[2]{%
  \ifnum \@listdepth >5\relax
    \@toodeep
  \else
    \global\advance\@listdepth\@ne
  \fi
  \rightmargin\z@
  \listparindent\everylistparindent
  \itemindent\z@
  \csname @list\romannumeral\the\@listdepth\endcsname
  \def\@itemlabel{#1}%
  \let\makelabel\@mklab
  \@nmbrlistfalse
  #2\relax
  \@trivlist
  \parskip\parsep
  \parindent\listparindent
  \advance\linewidth -\rightmargin
  \advance\linewidth -\leftmargin
  \advance\@totalleftmargin \leftmargin
  \parshape \@ne \@totalleftmargin \linewidth
  \ignorespaces}

\renewcommand*{\defaultlists}{%
  \setlength{\partopsep}{0.2\onelineskip \@plus 0.1\onelineskip
                                         \@minus 0.1\onelineskip}%
  \parsepi = 0.3333\onelineskip \@plus 0.1667\onelineskip \@minus \p@
  \itemsepi = \parsepi
  \topsepi = 0.6667\onelineskip \@plus 0.3333\onelineskip
                                \@minus 0.2\onelineskip
  \parsepii = 0.1667\onelineskip \@plus \p@ \@minus \p@
  \topsepii = \parsepi
  \topsepiii = \parsepii
  \everylistparindent \z@}
\defaultlists

\newcommand*{\firmlists}{%
  \@ifstar{\m@msfirmlists}{\m@mfirmlists}}

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

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

\renewcommand*{\tightlists}{%
  \@ifstar{\m@mstightlists}{\m@mtightlists}}

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

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

\endinput
%%
%% End of file `mempatch.sty'.
