%%
%% This is file `memoir.cls',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% memoir.dtx  (with options: `class')
%% 
%%      This program is provided under the terms of the
%%  LaTeX Project Public License distributed from CTAN
%%  archives in directory macros/latex/base/lppl.txt.
%% 
%%  Author: Peter Wilson (CUA)
%%          now at: peter.r.wilson@boeing.com
%% 
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{memoir}[2002/03/28 v1.1 configurable document class]
\newcommand{\@ptsize}{}
\newlength{\onelineskip}
\newlength{\lxvchars}
\newlength{\xlvchars}
\newcount\@memcnta
\newif\if@restonecol
\newif\if@openright
  \@openrighttrue

\newif\if@openleft
  \@openleftfalse

\newif\if@mainmatter
  \@mainmattertrue

\newif\if@memoldfont
  \@memoldfontfalse

\newif\ifsamename
\newcommand{\nametest}[2]{%
  \samenamefalse
  \begingroup
  \def\@memtempa{#1} \def\@memtempb{#2}
  \ifx \@memtempa\@memtempb
    \endgroup
    \samenametrue
  \else
    \endgroup
  \fi
}

\newcommand{\kill@lastcounter}[1]{%
  \advance\count10 \m@ne
  \expandafter\let\csname c@#1\endcsname\relax}

\newif\ifpdf
\ifx\pdfoutput\undefined
\else
  \ifx\pdfoutput\relax
  \else
    \ifcase\pdfoutput
    \else
      \pdftrue
    \fi
  \fi
\fi

\newcommand{\phantomsection}{}

\edef\wo@dmacro{%
  \string m\string a\string c\string r\string o\string :%
}

\def\wo@difmacro@begingroup#1{%
  \begingroup
    \edef\x{%
      \noexpand\wo@dparsemacro\meaning#1\wo@dmacro\string -%
    }%
    \x\@nil{#1}%
}

\begingroup
  \edef\x{\endgroup
    \def\noexpand\wo@dparsemacro##1\wo@dmacro##2\string -}%
\x#3\@nil#4{%
  \ifx\\#3\\%
    \endgroup
    \ClassWarning{memoir}{\string `\string #4\string ' is not a macro}%
    \expandafter\@gobble
  \else
    \expandafter\@firstofone
  \fi
}

\def\addtodef{\@star@or@long\wo@daddtodef}
\long\def\wo@daddtodef#1#2#3{%
  \wo@difmacro@begingroup{#1}{%
    \@temptokena{#2}%
    \toks@\expandafter{#1#3}%
    \edef\x{\endgroup
         \l@ngrel@x\def\noexpand#1{\the\@temptokena \the\toks@}}%
    \x
  }%
}

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

\newlength{\stockheight}
\newlength{\stockwidth}
\newlength{\trimtop}
\newlength{\trimedge}

\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}}
\DeclareOption{landscape}
  {\setlength\@tempdima  {\stockheight}
   \setlength\stockheight{\stockwidth}
   \setlength\stockwidth {\@tempdima}}

\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{twoside}{\@twosidetrue  \@mparswitchtrue}
\DeclareOption{oneside}{\@twosidefalse \@mparswitchfalse}
\DeclareOption{onecolumn}{\@twocolumnfalse}
\DeclareOption{twocolumn}{\@twocolumntrue}
\newif\ifdraftdoc\draftdocfalse
\setlength{\overfullrule}{\z@}
\DeclareOption{final}{\setlength{\overfullrule}{\z@}
                      \draftdocfalse
                      \msdocfalse}
\DeclareOption{draft}{\setlength\overfullrule{5pt}%
                      \draftdoctrue
                      \msdocfalse}
\newif\ifmsdoc
  \msdocfalse
\DeclareOption{ms}{%
  \msdoctrue
  \draftdocfalse
  \setlength\overfullrule{\z@}
}

\newif\ifshowtrims
  \showtrimsfalse
\DeclareOption{showtrims}{\showtrimstrue}

\newif\ifartopt
  \artoptfalse
\DeclareOption{article}{\artopttrue}

\DeclareOption{subfigure}{%
  \ClassWarningNoLine{memoir}{The subfigure option is not required}}

\DeclareOption{openright}{\@openrighttrue}
\DeclareOption{openany}{\@openrightfalse}
\DeclareOption{openleft}{\@openlefttrue}
\newcommand{\openright}{\@openrighttrue\@openleftfalse%
  \gdef\clearforchapter{\cleartorecto}}
\newcommand{\openany}{\@openrightfalse\@openleftfalse%
  \gdef\clearforchapter{\clearpage}}
\newcommand{\openleft}{\@openlefttrue
  \gdef\clearforchapter{\cleartoverso}}

\DeclareOption{leqno}{\input{leqno.clo}}
\DeclareOption{fleqn}{\input{fleqn.clo}}
\DeclareOption{openbib}{%
  \AtEndOfClass{%
    \renewcommand\@openbib@code{%
      \advance\leftmargin\bibindent
      \itemindent -\bibindent
      \listparindent \itemindent
      \parsep \z@
     }%
    \renewcommand\newblock{\par}}%
}

\DeclareOption{oldfontcommands}{%
  \@memoldfonttrue}

\ExecuteOptions{final,letterpaper,10pt,onecolumn,openright,twoside}
\ProcessOptions

\def\cleartorecto{\clearpage\if@twoside \ifodd\c@page\else
  \hbox{}\thispagestyle{cleared}%
  \newpage\if@twocolumn\hbox{}\newpage\fi\fi\fi}

\def\cleartoverso{\clearpage\if@twoside
  \ifodd\c@page\hbox{}\thispagestyle{cleared}%
  \newpage\if@twocolumn\hbox{}\newpage\fi\fi\fi}


\if@openleft
  \openleft
\else
  \if@openright
    \openright
  \else
    \openany
  \fi
\fi

\ifnum\@ptsize = 9\relax
  \input{mem\@ptsize.clo}
\else
  \input{mem1\@ptsize.clo}
\fi
\newcommand{\captionsize}{\normalsize}
\setlength\lineskip{1\p@}
\setlength\normallineskip{1\p@}
\renewcommand{\baselinestretch}{}
\setlength\parskip{0\p@ \@plus \p@}
\@lowpenalty   51
\@medpenalty  151
\@highpenalty 301
\clubpenalty  1000
\widowpenalty 1000
\newcommand{\hangpara}[2]{\hangindent#1\hangafter#2\noindent}
\newenvironment{hangparas}[2]{\setlength{\parindent}{\z@}
  \everypar={\hangpara{#1}{#2}}}{\par}

\newcommand{\setrectanglesize}[3]{%
  \nametest{#1}{*}
  \ifsamename                           % H = *
    \nametest{#2}{*}
    \ifsamename                         % W = *
      \ClassError{memoir}{%
        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
}

\newcommand{\setfillsize}[5]{%
  \nametest{#2}{*}
  \ifsamename                                % C = *
    \nametest{#3}{*}
    \ifsamename                              % L = *
      \nametest{#4}{*}
      \ifsamename                            % R = *
        \ClassError{memoir}{%
          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
        \ClassError{memoir}{%
          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
}

\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}
}
\newcommand{\settypeblocksize}[3]{%
  \setrectanglesize{#1}{#2}{#3}
  \setlength{\textheight}{\@tempdima}
  \setlength{\textwidth}{\@tempdimb}
}

\newlength{\spinemargin}
\newlength{\foremargin}
\newcommand{\setlrmargins}[3]{%
  \setfillsize{\paperwidth}{\textwidth}{#1}{#2}{#3}
  \setlength{\textwidth}{\@tempdimc}
  \setlength{\spinemargin}{\@tempdima}
  \setlength{\foremargin}{\@tempdimb}
}

\newcommand{\setlrmarginsandblock}[3]{%
  \setfillsize{\paperwidth}{*}{#1}{#2}{#3}
  \setlength{\textwidth}{\@tempdimc}
  \setlength{\spinemargin}{\@tempdima}
  \setlength{\foremargin}{\@tempdimb}
}

\newlength{\uppermargin}
\newlength{\lowermargin}
\newcommand{\setulmargins}[3]{%
  \setfillsize{\paperheight}{\textheight}{#1}{#2}{#3}
  \setlength{\textheight}{\@tempdimc}
  \setlength{\uppermargin}{\@tempdima}
  \setlength{\lowermargin}{\@tempdimb}
}

\newcommand{\setulmarginsandblock}[3]{%
  \setfillsize{\paperheight}{*}{#1}{#2}{#3}
  \setlength{\textheight}{\@tempdimc}
  \setlength{\uppermargin}{\@tempdima}
  \setlength{\lowermargin}{\@tempdimb}
}

\newlength{\headdrop}
\newcommand{\setheaderspaces}[3]{%
  \setfillsize{\uppermargin}{\headheight}{#1}{#2}{#3}
  \setlength{\headheight}{\@tempdimc}
  \setlength{\headdrop}{\@tempdima}
  \setlength{\headsep}{\@tempdimb}
}

\newcommand{\setheadfoot}[2]{%
  \setlength{\headheight}{#1}
  \setlength{\footskip}{#2}
}

\newcommand{\setcolsepandrule}[2]{%
  \setlength{\columnsep}{#1}
  \setlength{\columnseprule}{#2}
}

\newcommand{\setmarginnotes}[3]{%
  \setlength{\marginparsep}{#1}
  \setlength{\marginparwidth}{#2}
  \setlength{\marginparpush}{#3}
}

\settrimmedsize{\stockheight}{\stockwidth}{*}
\settrims{\z@}{\z@}

\setlength{\@tempdimb}{1.14\lxvchars}
\setlength\@tempdima{\paperwidth}
  \addtolength\@tempdima{-2in}
\if@twocolumn
  \ifdim\@tempdima>2\@tempdimb\relax
    \setlength\textwidth{2\@tempdimb}
  \else
    \setlength\textwidth{\@tempdima}
  \fi
\else
  \ifdim\@tempdima>\@tempdimb\relax
    \setlength\textwidth{\@tempdimb}
  \else
    \setlength\textwidth{\@tempdima}
  \fi
\fi
\@settopoint\textwidth

\setlength\@tempdima{\paperheight}
  \addtolength\@tempdima{-3.5in}
  \divide\@tempdima\baselineskip
\@tempcnta=\@tempdima
\setlength\textheight{\@tempcnta\baselineskip}
  \addtolength\textheight{\topskip}

\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 is .8 and not .4
\fi
\ifdim\marginparwidth>2in
  \setlength\marginparwidth{2in}
\fi
\@settopoint\oddsidemargin
\@settopoint\marginparwidth

\setlength\evensidemargin  {\paperwidth}
\addtolength\evensidemargin{-2in}
\addtolength\evensidemargin{-\textwidth}
\addtolength\evensidemargin{-\oddsidemargin}
\@settopoint\evensidemargin
\setlength\topmargin  {\paperheight}
\addtolength\topmargin{-2in}
\addtolength\topmargin{-\headheight}
\addtolength\topmargin{-\headsep}
\addtolength\topmargin{-\textheight}
\addtolength\topmargin{-\footskip}
\addtolength\topmargin{-.5\topmargin}
\@settopoint\topmargin

\setlength{\spinemargin}{\oddsidemargin}
\addtolength{\spinemargin}{1in}
\setlrmargins{\spinemargin}{*}{*}

\setlength{\uppermargin}{\topmargin}
\addtolength{\uppermargin}{1in}
\addtolength{\uppermargin}{\headheight}
\addtolength{\uppermargin}{\headsep}
\setulmargins{\uppermargin}{*}{*}

\newcommand{\checkthelayout}{%
  \ifdim\stockwidth>\z@\else
    \ClassError{memoir}{\protect\stockwidth\space is zero or negative}%
                       {\@ehd}
  \fi
  \ifdim\trimedge<\z@
    \ClassError{memoir}{\protect\trimedge\space is negative}%
                       {\@ehd}
  \fi
  \ifdim\paperwidth>\z@\else
    \ClassError{memoir}{\protect\paperwidth\space is zero or negative}%
                       {\@ehd}
  \fi
  \ifdim\textwidth>\z@\else
    \ClassError{memoir}{\protect\textwidth\space is zero or negative}%
                       {\@ehd}
  \fi
  \ifdim\spinemargin>\z@\else
    \ClassError{memoir}{\protect\spinemargin\space is zero or negative}%
                       {\@ehd}
  \fi
  \ifdim\foremargin>\z@\else
    \ClassError{memoir}{\protect\foremargin\space is zero or negative}%
                       {\@ehd}
  \fi
  \ifdim\marginparsep>\z@\else
    \ClassError{memoir}{\protect\marginparsep\space is zero or negative}%
                       {\@ehd}
  \fi
  \ifdim\marginparwidth>\z@\else
    \ClassError{memoir}{\protect\marginparwidth\space is zero or negative}%
                       {\@ehd}
  \fi
  \ifdim\stockheight>\z@\else
    \ClassError{memoir}{\protect\stockheight\space is zero or negative}%
                       {\@ehd}
  \fi
  \ifdim\trimtop<\z@
    \ClassError{memoir}{\protect\trimtop\space is negative}%
                       {\@ehd}
  \fi
  \ifdim\paperheight>\z@\else
    \ClassError{memoir}{\protect\paperheight\space is zero or negative}%
                       {\@ehd}
  \fi
  \ifdim\textheight>\z@\else
    \ClassError{memoir}{\protect\textheight\space is zero or negative}%
                       {\@ehd}
  \fi
  \ifdim\uppermargin>\z@\else
    \ClassError{memoir}{\protect\uppermargin\space is zero or negative}%
                       {\@ehd}
  \fi
  \ifdim\lowermargin>\z@\else
    \ClassError{memoir}{\protect\lowermargin\space is zero or negative}%
                       {\@ehd}
  \fi
  \ifdim\headheight>\z@\else
    \ClassError{memoir}{\protect\headheight\space is zero or negative}%
                       {\@ehd}
  \fi
  \ifdim\headsep>\z@\else
    \ClassError{memoir}{\protect\headsep\space is zero or negative}%
                       {\@ehd}
  \fi
  \ifdim\footskip>\z@\else
    \ClassError{memoir}{\protect\footskip\space is zero or negative}%
                       {\@ehd}
  \fi
  \@tempdima = \textheight
    \divide\@tempdima \baselineskip
  \@tempcnta=\@tempdima
  \setlength{\textheight}{\@tempcnta\baselineskip}
    \addtolength{\textheight}{\topskip}
  \setulmargins{\uppermargin}{*}{*}
  \@tempdimb = -1pt
  \@tempdima=\stockwidth
  \advance\@tempdima -\trimedge
  \advance\@tempdima -\paperwidth
  \ifdim\@tempdima<\@tempdimb
    \@tempdima = -\@tempdima
    \ClassError{memoir}{\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
    \ClassError{memoir}{\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
    \ClassError{memoir}{\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
    \ClassError{memoir}{\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
    \ClassError{memoir}{\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
    \ClassError{memoir}{\protect\footskip
                        is too large for \protect\lowermargin\space
                        by \the\@tempdima}%
                       {\@ehd}
  \fi
}
\newcommand{\fixthelayout}{%
  \topmargin = \trimtop
    \advance\topmargin \uppermargin
    \advance\topmargin -\headsep
    \advance\topmargin -\headheight
    \advance\topmargin -1in
  \oddsidemargin = \stockwidth
    \advance\oddsidemargin -\trimedge
    \advance\oddsidemargin -\paperwidth
    \advance\oddsidemargin \spinemargin
    \advance\oddsidemargin -1in
  \evensidemargin = \trimedge
    \advance\evensidemargin \foremargin
    \advance\evensidemargin -1in
  \@settopoint\textwidth
  \@settopoint\oddsidemargin
  \@settopoint\evensidemargin
}

\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{}
}

\newcommand{\checkandfixthelayout}{%
  \checkthelayout
  \fixthelayout
  \typeoutlayout
}

\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{}
}

\setcounter{topnumber}{3}
\renewcommand{\topfraction}{.85}
\setcounter{bottomnumber}{2}
\renewcommand{\bottomfraction}{.5}
\setcounter{totalnumber}{4}
\renewcommand{\textfraction}{.1}
\renewcommand{\floatpagefraction}{.7}
\setcounter{dbltopnumber}{3}
\renewcommand{\dbltopfraction}{.85}
\renewcommand{\dblfloatpagefraction}{.7}
\newcommand{\makeevenhead}[4]{%
  \@namedef{#1eheadl}{#2}
  \@namedef{#1eheadc}{#3}
  \@namedef{#1eheadr}{#4}
}
\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}
}

\newcommand{\makerunningwidth}[2]{%
  \@namedef{#1runwidth}{#2}}
\newcommand{\normalrulethickness}{0.4pt}
\newcommand{\footruleheight}{\z@skip}
\newcommand{\footruleskip}{0.3\normalbaselineskip}
\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}
}

\newcommand{\makeheadposition}[5]{%
  \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
  \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
}

\newcommand{\makepsmarks}[2]{\@namedef{#1pshook}{#2}}
\newcommand{\makepshook}[2]{%
  \ClassWarning{memoir}{\protect\makepshook\space is deprecated.\MessageBreak
                        Use \protect\makepsmarks\space instead}
  \@namedef{#1pshook}{#2}
}

\newcommand{\makepagestyle}[1]{%
  \@namedef{ps@#1}{%
    \@namedef{#1@evenhead}{%
      \@nameuse{#1evenhpl}\hb@xt@\@nameuse{#1runwidth}{\vbox{\hbox{%
        \rlap{\parbox[b]{\@nameuse{#1runwidth}}{%
          \raggedright\@nameuse{#1eheadl}\strut}}\hfill
              \parbox[b]{\@nameuse{#1runwidth}}{%
          \centering\@nameuse{#1eheadc}\strut}\hfill
        \llap{\parbox[b]{\@nameuse{#1runwidth}}{%
          \raggedleft\@nameuse{#1eheadr}\strut}}}%
        \@nameuse{#1headrule}}}\@nameuse{#1evenhpr}}
    \@namedef{#1@oddhead}{%
      \@nameuse{#1oddhpl}\hb@xt@\@nameuse{#1runwidth}{\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}}
    \@namedef{#1@evenfoot}{%
      \@nameuse{#1evenfpl}\hb@xt@\@nameuse{#1runwidth}{%
        \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}}
    \@namedef{#1@oddfoot}{%
      \@nameuse{#1oddfpl}\hb@xt@\@nameuse{#1runwidth}{%
        \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}}
    \def\@evenhead{\@nameuse{#1@evenhead}}
    \def\@oddhead{\@nameuse{#1@oddhead}}
    \def\@evenfoot{\@nameuse{#1@evenfoot}}
    \def\@oddfoot{\@nameuse{#1@oddfoot}}
    \@nameuse{#1pshook}
  }
  \makeevenhead{#1}{}{}{}
  \makeoddhead{#1}{}{}{}
  \makeevenfoot{#1}{}{}{}
  \makeoddfoot{#1}{}{}{}
  \makerunningwidth{#1}{\textwidth}
  \makeheadposition{#1}{}{}{}{}
  \makeheadrule{#1}{\textwidth}{0pt}
  \makefootrule{#1}{\textwidth}{\footruleheight}{\footruleskip}
  \makepsmarks{#1}{}
}

\newcommand{\aliaspagestyle}[2]{%
  \@namedef{ps@#1}{\@nameuse{ps@#2}}}

\newcommand{\ifonlyfloats}[2]{\if@fcolmade #1\else #2\fi}

\newcommand{\mergepagefloatstyle}[3]{%
  \@nameuse{ps@#3} \@nameuse{ps@#2}
  \@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}}}
  \@namedef{#1pshook}{\@nameuse{#2pshook}}
}}
\makepagestyle{empty}

\makepagestyle{plain}
  \makeevenfoot{plain}{}{\thepage}{}
  \makeoddfoot{plain}{}{\thepage}{}

\if@twoside
  \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
  \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

\makepagestyle{myheadings}
  \makepsmarks{myheadings}{%
    \let\@mkboth\@gobbletwo
    \let\chaptermark\@gobble
    \let\sectionmark\@gobble
    \let\tocmark\@gobble
    \let\lofmark\@gobble
    \let\lotmark\@gobble
    \let\bibmark\@gobble
    \let\indexmark\@gobble
  }
  \makeevenhead{myheadings}{\thepage}{}{\slshape\leftmark}
  \makeoddhead{myheadings}{\slshape\rightmark}{}{\thepage}

\aliaspagestyle{chapter}{plain}
\aliaspagestyle{part}{plain}
\aliaspagestyle{cleared}{empty}

\def\cleardoublepage{\clearpage\if@twoside \ifodd\c@page\else
  \hbox{}\thispagestyle{cleared}%
  \newpage\if@twocolumn\hbox{}\newpage\fi\fi\fi}

\makepagestyle{ruled}
\makeevenfoot{ruled}{\thepage}{}{}
\makeoddfoot{ruled}{}{}{\thepage}
\makeheadrule{ruled}{\textwidth}{\normalrulethickness}
\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}
\makeevenhead{ruled}{\scshape\leftmark}{}{}
\makeoddhead{ruled}{}{}{\rightmark}

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

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

\renewcommand{\pagenumbering}{%
  \@ifstar{\@smempnum}{\@mempnum}}
\newcommand{\@smempnum}[1]{%
  \gdef\thepage{\csname @#1\endcsname \c@page}}
\newcommand{\@mempnum}[1]{%
  \@smempnum{#1}\global\c@page \@ne}

\newcounter{storedpagenumber}
  \setcounter{storedpagenumber}{1}
\newcommand{\savepagenumber}{\global\c@storedpagenumber \c@page}
\newcommand{\restorepagenumber}{\global\c@page \c@storedpagenumber}

\newif\if@titlepage
  \@titlepagefalse

\newcommand*{\partmark}[1]{}
\newcommand*{\chaptermark}[1]{}

\newcommand*{\bibmark}{}
\newcommand*{\indexmark}{}

\setcounter{secnumdepth}{2}
\newcounter{part}
\newcounter{chapter}
\newcounter{section}[chapter]
\newcounter{subsection}[section]
\newcounter{subsubsection}[subsection]
\newcounter{paragraph}[subsubsection]
\newcounter{subparagraph}[paragraph]
\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}
\newcommand{\@chapapp}{\chaptername}

\newcommand{\frontmatter}{%
  \@ifstar{\@smemfront}{\@memfront}}
\newcommand{\@smemfront}{%
  \cleardoublepage
  \@mainmatterfalse
  \setcounter{secnumdepth}{-10}
  \counterwithout{figure}{chapter}
  \counterwithout{table}{chapter}
}
\newcommand{\@memfront}{%
  \@smemfront\pagenumbering{roman}}

\newcommand{\mainmatter}{%
  \@ifstar{\@smemmain}{\@memmain}}
\newcommand{\@smemmain}{
  \cleardoublepage
  \@mainmattertrue
  \setcounter{secnumdepth}{\value{maxsecnumdepth}}
  \counterwithin{figure}{chapter}
  \counterwithin{table}{chapter}
}
\newcommand{\@memmain}{%
  \@smemmain\pagenumbering{arabic}}

\newcommand{\backmatter}{%
  \if@openright
    \cleardoublepage
  \else
    \clearpage
  \fi
  \@mainmatterfalse
  \setcounter{secnumdepth}{-10}
%%  \renewcommand{\thesection}{\@arabic\c@section}
  \counterwithout{figure}{chapter}
  \counterwithout{table}{chapter}
  \setcounter{figure}{0}
  \setcounter{table}{0}
}

\newcommand{\part}{%
  \@setuppart
  \secdef\@part\@spart}
\newcommand{\beforepartskip}{\null\vfil}
\newcommand{\midpartskip}{\par\vskip 20pt}
\newcommand{\afterpartskip}{\vfil\newpage}

\newcommand{\@setuppart}{%
  \if@openright
    \cleardoublepage
  \else
    \clearpage
  \fi
  \thispagestyle{part}%
  \if@twocolumn
    \onecolumn
    \@tempswatrue
  \else
    \@tempswafalse
  \fi
  \beforepartskip}

\newcommand{\partnamefont}{\normalfont\huge\bfseries}
\newcommand{\partnumfont}{\normalfont\huge\bfseries}
\newcommand{\parttitlefont}{\normalfont\Huge\bfseries}

\newcommand{\printpartname}{\partnamefont \partname}
\newcommand{\partnamenum}{\space}
\newcommand{\printpartnum}{\partnumfont \thepart}
\newcommand{\printparttitle}[1]{\parttitlefont #1}

\def\@part[#1]#2{%
  \ifnum \c@secnumdepth >-2\relax
    \refstepcounter{part}%
%    \addcontentsline{toc}{part}{\thepart\hspace{1em}#1}%
    \addtocontents{toc}{\protect\begin{center}{\normalfont\large\bfseries #1}\protect\end{center}}%
  \else
%    \addcontentsline{toc}{part}{#1}%
    \addtocontents{toc}{\protect\begin{center}{\normalfont\large\bfseries #1}\protect\end{center}}%
  \fi
  \markboth{}{}%
  {\centering
   \interlinepenalty \@M
   \normalfont
   \ifnum \c@secnumdepth >-2\relax
     \printpartname \partnamenum \printpartnum
     \midpartskip
   \fi
   \printparttitle{#2}\par}%
  \@endpart}

\def\@spart#1{%
  {\centering
   \interlinepenalty \@M
   \normalfont
   \printparttitle{#1}\par}%
  \@endpart}

\def\@endpart{\afterpartskip
  \if@twoside
    \if@openright
      \null
      \thispagestyle{empty}%
      \newpage
    \fi
  \fi
  \if@tempswa
    \twocolumn
  \fi}

\newcommand\chapter{%
  \ifartopt\else
    \clearforchapter
    \thispagestyle{chapter}
    \global\@topnum\z@
  \fi
  \@afterindentfalse
  \secdef\@chapter\@schapter}

\def\@chapter[#1]#2{%
  \ifnum \c@secnumdepth >\m@ne
    \if@mainmatter
      \refstepcounter{chapter}%
      \typeout{\@chapapp\space\thechapter.}%
      \addcontentsline{toc}{chapter}%
        {\protect\numberline{\thechapter}#1}%
    \else
      \addcontentsline{toc}{chapter}{#1}%
    \fi
  \else
    \addcontentsline{toc}{chapter}{#1}%
  \fi
  \chaptermark{#1}%
  \ifartopt
    \@makechapterhead{#2}%
    \@afterheading
  \else
    \insertchapterspace
    \if@twocolumn
      \@topnewpage[\@makechapterhead{#2}]%
    \else
      \@makechapterhead{#2}%
      \@afterheading
    \fi
  \fi}

\def\@makechapterhead#1{%
  \chapterheadstart%  \vspace*{50\p@}%
  {\parindent \z@ \raggedright \normalfont
   \ifnum \c@secnumdepth >\m@ne
     \if@mainmatter
       \printchaptername \chapternamenum \printchapternum
       \afterchapternum % \par\nobreak \vskip 20\p@
     \fi
   \fi
   \interlinepenalty\@M
   \printchaptertitle{#1} % \Huge \bfseries #1
   \afterchaptertitle % \par\nobreak \vskip 40\p@
  }}

\newcommand{\insertchapterspace}{%
  \addtocontents{lof}{\protect\addvspace{10pt}}%
  \addtocontents{lot}{\protect\addvspace{10pt}}%
}

\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\printchaptertitle##1{\chaptitlefont ##1}
  \def\afterchaptertitle{\par\nobreak\vskip \afterchapskip}
}

\newcommand{\chapnamefont}{\normalfont\huge\bfseries}
\newcommand{\chapnumfont}{\normalfont\huge\bfseries}
\newcommand{\chaptitlefont}{\normalfont\Huge\bfseries}
\newlength{\beforechapskip}\setlength{\beforechapskip}{50pt}
\newlength{\midchapskip}\setlength{\midchapskip}{20pt}
\newlength{\afterchapskip}\setlength{\afterchapskip}{40pt}

\newcommand{\makechapterstyle}[2]{\@namedef{chs@#1}{\@chs@def@ult #2}}
\newcommand{\chapterstyle}[1]{\@nameuse{chs@#1}}

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

\def\@schapter#1{%
  \ifartopt
    \@makeschapterhead{#1}%
    \@afterheading
  \else
    \if@twocolumn
      \@topnewpage[\@makeschapterhead{#1}]%
    \else
      \@makeschapterhead{#1}%
      \@afterheading
    \fi
  \fi}

\def\@makeschapterhead#1{%
  \chapterheadstart
  {\parindent \z@ \raggedright \normalfont
   \interlinepenalty\@M
   \printchaptertitle{#1}
   \afterchaptertitle
  }
}

\makechapterstyle{section}{%
  \renewcommand{\printchaptername}{}
  \renewcommand{\chapternamenum}{}
  \renewcommand{\chapnumfont}{\normalfont\Huge\bfseries}
  \renewcommand{\printchapternum}{\chapnumfont \thechapter\space}
  \renewcommand{\afterchapternum}{}
}

\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\space}
  \renewcommand{\afterchapternum}{}
}

\makechapterstyle{hangnum}{%
  \renewcommand{\chapnumfont}{\chaptitlefont}
  \settowidth{\chapindent}{\chapnumfont 999}
  \renewcommand{\printchaptername}{}
  \renewcommand{\chapternamenum}{}
  \renewcommand{\printchapternum}{%
    \noindent\llap{\makebox[\chapindent][l]{\chapnumfont \thechapter}}}
  \renewcommand{\afterchapternum}{}
}

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

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

\newcommand{\section}{%
  \sechook%
  \@startsection{section}{1}%  level 1
      {\secindent}%            heading indent
      {\beforesecskip}%        skip before the heading
      {\aftersecskip}%         skip after the heading
      {\normalfont\secheadstyle}} % font
\newcommand{\sechook}{}
\newcommand{\setsechook}[1]{\renewcommand{\sechook}{#1}}
\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}

\newcommand{\subsection}{%
  \subsechook%
  \@startsection{subsection}{2}%  level 2
      {\subsecindent}%            heading indent
      {\beforesubsecskip}%        skip before the heading
      {\aftersubsecskip}%         skip after the heading
      {\normalfont\subsecheadstyle}} % font
\newcommand{\subsechook}{}
\newcommand{\setsubsechook}[1]{\renewcommand{\subsechook}{#1}}
\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}

\newcommand{\subsubsection}{%
  \subsubsechook%
  \@startsection{subsubsection}{3}%  level 3
      {\subsubsecindent}%            heading indent
      {\beforesubsubsecskip}%        skip before the heading
      {\aftersubsubsecskip}%         skip after the heading
      {\normalfont\subsubsecheadstyle}} % font
\newcommand{\subsubsechook}{}
\newcommand{\setsubsubsechook}[1]{\renewcommand{\subsubsechook}{#1}}
\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}

\newcommand{\paragraph}{%
  \parahook%
  \@startsection{paragraph}{4}%  level 4
      {\paraindent}%            heading indent
      {\beforeparaskip}%        skip before the heading
      {\afterparaskip}%         skip after the heading
      {\normalfont\paraheadstyle}} % font
\newcommand{\parahook}{}
\newcommand{\setparahook}[1]{\renewcommand{\parahook}{#1}}
\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}

\newcommand{\subparagraph}{%
  \subparahook%
  \@startsection{subparagraph}{5}%  level 5
      {\subparaindent}%            heading indent
      {\beforesubparaskip}%        skip before the heading
      {\aftersubparaskip}%         skip after the heading
      {\normalfont\subparaheadstyle}} % font
\newcommand{\subparahook}{}
\newcommand{\setsubparahook}[1]{\renewcommand{\subparahook}{#1}}
\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}

\newcommand{\sethangfrom}[1]{\renewcommand{\@hangfrom}[1]{#1}}
\newcommand{\setsecnumformat}[1]{\renewcommand{\@seccntformat}[1]{#1}}
\newcommand{\hangsecnum}{%
  \def\@seccntformat##1{\llap{\csname the##1\endcsname\quad}}}
\newcommand{\defaultsecnum}{%
  \def\@seccntformat##1{\csname the##1\endcsname\quad}}

\newcommand{\plainbreak}{\@ifstar{\@spbreak}{\@pbreak}}
\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}

\newcommand{\fancybreak}{\@ifstar{\@sfbreak}{\@fbreak}}
\newcommand{\@fbreak}[1]{\par
  \penalty -100
  \noindent\parbox{\linewidth}{\centering #1}
  \penalty -20
  \vskip -\onelineskip
  \@afterindentfalse
  \@afterheading}
\newcommand{\@sfbreak}[1]{\par
  \penalty -100
  \noindent\parbox{\linewidth}{\centering #1}
  \penalty -20
  \vskip -\onelineskip
  \@afterindenttrue
  \@afterheading}

\newcommand{\plainfancybreak}{\@ifstar{\@spfbreak}{\@pfbreak}}
\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}

\newcommand\appendix{\par
  \setcounter{chapter}{0}%
  \setcounter{section}{0}%
  \gdef\@chapapp{\appendixname}%
  \gdef\thechapter{\@Alph\c@chapter}}

\newcommand{\appendixpage}{%
  \@ifstar{\@sapppage}{\@apppage}}
\def\@apppage{%
  \@setuppart
  \addappheadtotoc
  \markboth{}{}%
  {\centering
   \interlinepenalty \@M
   \normalfont
   \printparttitle{\appendixpagename}\par}%
  \@endpart}
\def\@sapppage{%
  \@setuppart
  \markboth{}{}%
  {\centering
   \interlinepenalty \@M
   \normalfont
   \printparttitle{\appendixpagename}\par}%
  \@endpart}

\def\addappheadtotoc{\addcontentsline{toc}{chapter}{\appendixtocname}}
\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}}}

\newcommand{\@resets@pp}{
  \par
  \@ppsavesec
  \setcounter{section}{0}
  \setcounter{chapter}{0}
  \renewcommand\@chapapp{\appendixname}
  \renewcommand\thechapter{\@Alph\c@chapter}
  \restoreapp
}

\newenvironment{appendices}{\@resets@pp}%
                           {\@ppsaveapp\@pprestoresec}

\newcommand{\setthesection}{\thechapter.\Alph{section}}

\newcommand{\@resets@ppsub}{
  \par
  \setcounter{section}{0}
  \renewcommand{\thesection}{\setthesection}
}

\newif\ifnamesubappendix
  \namesubappendixfalse

\newenvironment{subappendices}{%
  \@resets@ppsub
  \def\addappheadtotoc{\addcontentsline{toc}{section}{\appendixtocname}}
  \ifnamesubappendix
    \def\sectionname{\appendixname}
    \def\@seccntformat##1{\@ifundefined{##1name}{}{\csname ##1name\endcsname\ }%
        \csname the##1\endcsname\quad}
  \fi
  }{}

\newcommand{\@formatsecmark@pp}[1]{%
  \MakeUppercase{\appendixname\space
    \ifnum \c@secnumdepth >\z@
      \thesection\quad
    \fi
    #1}}
\if@twocolumn
  \setlength{\leftmargini}{2em}
\else
  \setlength{\leftmargini}{2.5em}
\fi
\leftmargin \leftmargini
\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
\setlength{\itemindent}{\z@}
\setlength{\labelsep}{0.5em}
\setlength{\labelwidth}{\leftmargini}
  \addtolength{\labelwidth}{-\labelsep}
\@beginparpenalty -\@lowpenalty
\@endparpenalty   -\@lowpenalty
\@itempenalty     -\@lowpenalty

\newlength{\parsepi}
\newlength{\topsepi}
\newlength{\itemsepi}
\newlength{\parsepii}
\newlength{\topsepii}
\newlength{\topsepiii}

\newcommand{\defaultlists}{%
\ifcase\@ptsize  % 0 = 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@
\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
  \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              % 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@
\else            % < 10pt > 14pt
  \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
}

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

\def\@listi{\leftmargin\leftmargini
  \parsep\parsepi
  \topsep\topsepi
  \itemsep\itemsepi}
\let\@listI\@listi
\defaultlists
\@listi

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

\newcommand{\firmlist}{%
  \setlength{\itemsep}{0.5\itemsep}\setlength{\parskip}{0.5\parskip}}
\newcommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}

\renewcommand{\theenumi}{\@arabic\c@enumi}
\renewcommand{\theenumii}{\@alph\c@enumii}
\renewcommand{\theenumiii}{\@roman\c@enumiii}
\renewcommand{\theenumiv}{\@Alph\c@enumiv}
\newcommand{\labelenumi}{\theenumi.}
\newcommand{\labelenumii}{\theenumii)}
\newcommand{\labelenumiii}{\theenumiii.}
\newcommand{\labelenumiv}{\theenumiv.}
\renewcommand{\p@enumii}{\theenumi}
\renewcommand{\p@enumiii}{\theenumi(\theenumii)}
\renewcommand{\p@enumiv}{\p@enumiii\theenumiii}
\newtoks\@enLab
\def\@enQmark{?}
\def\@enLabel#1#2{%
  \edef\@enThe{\noexpand#1{\@enumctr}}%
  \@enLab\expandafter{\the\@enLab\csname the\@enumctr\endcsname}%
  \@enloop}
\def\@enSpace{\afterassignment\@enSp@ce\let\@memtempa= }
\def\@enSp@ce{\@enLab\expandafter{\the\@enLab\space}\@enloop}
\def\@enGroup#1{\@enLab\expandafter{\the\@enLab{#1}}\@enloop}
\def\@enOther#1{\@enLab\expandafter{\the\@enLab#1}\@enloop}
\def\@enloop{\futurelet\@entemp\@enloop@}
\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
                         \@enhook
             \fi\fi\fi\fi\fi\fi\fi\fi
  \@memtempa}
%% \providecommand\@enhook{}
  \newcommand\@enhook{}
\def\enumerate{%
  \ifnum \@enumdepth >3 \@toodeep\else
      \advance\@enumdepth \@ne
      \edef\@enumctr{enum\romannumeral\the\@enumdepth}\fi
  \@ifnextchar[{\@@enum@}{\@enum@}}
\def\@@enum@[#1]{%
  \@enLab{}\let\@enThe\@enQmark
  \@enloop#1\@enum@
  \ifx\@enThe\@enQmark\@warning{The counter will not be printed.%
   ^^J\space\@spaces\@spaces\@spaces The label is: \the\@enLab}\fi
  \expandafter\edef\csname label\@enumctr\endcsname{\the\@enLab}%
  \expandafter\let\csname the\@enumctr\endcsname\@enThe
  \csname c@\@enumctr\endcsname7
  \expandafter\settowidth
            \csname leftmargin\romannumeral\@enumdepth\endcsname
            {\the\@enLab\hspace{\labelsep}}%
  \@enum@}
\def\@enum@{\list{\csname label\@enumctr\endcsname}%
           {\usecounter{\@enumctr}\def\makelabel##1{\hss\llap{##1}}}}

\newcommand{\labelitemi}{\textbullet}
\newcommand{\labelitemii}{\normalfont\bfseries \textendash}
\newcommand{\labelitemiii}{\textasteriskcentered}
\newcommand{\labelitemiv}{\textperiodcentered}
\renewcommand{\itemize}[1][\@empty]{%
  \ifnum \@itemdepth >\thr@@\@toodeep\else
    \advance\@itemdepth\@ne
    \ifx #1\@empty \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

\newenvironment{description}%
               {\list{}{\labelwidth\z@ \itemindent 0.5em \labelsep 0.5em
                        \let\makelabel\descriptionlabel}}%
               {\endlist}
\newcommand*{\descriptionlabel}[1]{\hspace\labelsep
                                   \normalfont\bfseries #1}
\newcounter{vslineno}
\newcounter{poemline}
\newcounter{modulo@vs}

\newcommand{\poemlines}[1]{%
  \ifnum#1>\z@
    \setcounter{modulo@vs}{#1}
  \else
    \setcounter{modulo@vs}{0}
  \fi
}
\poemlines{0}

\newcommand{\getmodulo@vs}{{\@tempcnta\c@poemline
  \ifnum\c@modulo@vs<\@ne    % no line numbers
  \else
    \ifnum\c@modulo@vs<\tw@  % number every line
      \thepoemline
    \else
      \divide\@tempcnta\c@modulo@vs
      \multiply\@tempcnta\c@modulo@vs
      \ifnum\@tempcnta=\c@poemline\thepoemline\fi
    \fi
  \fi
}}

\newif\ifaltindent
  \altindentfalse
\newif\ifpattern
  \patternfalse
\newif\ifstarpattern
  \starpatternfalse

\newlength{\vleftskip}
  \setlength{\vleftskip}{30pt}
\newlength{\vrightskip}
  \setlength{\vrightskip}{10pt}

\newcommand{\flagverse}[1]{\hskip-\vleftskip\llap{#1}\hskip\vleftskip\ignorespaces}

\newlength{\versewidth}
\newlength{\vgap} \setlength{\vgap}{1.5em}
\newcommand{\vin}{\hspace*{\vgap}}
\newlength{\vindent} \setlength{\vindent}{2\vgap}

\newcommand{\verselinebreak}[1][\z@]{\newline\hspace*{#1}% extra verseindent
}
\newcommand{\incr@vsline}{%
  \refstepcounter{poemline}%
  \stepcounter{vslineno}}

\newcommand{\@vsifbang}[1]{\@ifnextchar !{\@firstoftwo{#1}}}
\newcommand{\@vsifgt}[1]{\@ifnextchar >{\@firstoftwo{#1}}}

\newcommand{\@vscentercr}{%
  \ifhmode \unskip\else \@nolnerr\fi
  \hfill\rlap{\kern\vrightskip\kern\rightmargin\getmodulo@vs}%
  \@vsifgt{\verselinebreak}{%
  \incr@vsline
  \par\@ifstar{\nobreak\@vsxcentercr}{%
    \@vsifbang{\@ifnextchar[ {\@vsicentercr}{}}{\@vsxcentercr}}}}
\newcommand{\@vsxcentercr}{\addvspace{-\parskip}%
  \@ifnextchar[ {\@vsicentercr}{\start@vsline}}
\def\@vsicentercr[#1]{\vskip #1\ignorespaces \start@vsline}
\newcommand{\start@vsline}{%
  \ifaltindent\ifodd\thevslineno\else\vin\fi\fi%
  \ifpattern\get@vsindent\fi%
  \ifstarpattern\getstar@vsindent\fi}

\newenvironment{verse}[1][\linewidth]{
  \setcounter{poemline}{0}\refstepcounter{poemline}%
  \setcounter{vslineno}{1}%
  \let\\=\@vscentercr
  \list{}{\itemsep \z@
          \itemindent  -\vindent%
          \listparindent\itemindent
          \parsep       \onelineskip
          \ifdim #1 < \linewidth
            \rightmargin        \z@
            \setlength{\leftmargin}{\linewidth}%
            \addtolength{\leftmargin}{-#1}%
            \addtolength{\leftmargin}{-0.5\leftmargin}%
          \else
            \rightmargin        \leftmargin
          \fi
          \addtolength{\leftmargin}{\vindent}}
  \item[]}{\endlist}

\newenvironment{altverse}%
  {\starpatternfalse\patternfalse\altindenttrue\setcounter{vslineno}{1}}%
  {\altindentfalse}

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

\newif\ifbounderror
  \bounderrorfalse
\newif\ifinteger

\newcounter{chrsinstr}  % CHARactersINSTRing

\newcommand{\newarray}[3]{%
  \@nameedef{#1-low}{#2}%
  \@nameedef{#1-high}{#3}%
  \ifnum #3<#2
    \ClassError{memoir}{Limits for array #1 are in reverse order}{\@ehc}%
  \fi
}

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

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

\newcommand{\setarrayelement}[3]{%
  \checkarrayindex{#1}{#2}%
  \@nameedef{#1-#2}{#3}%
}

\newcommand{\getarrayelement}[3]{%
  \checkarrayindex{#1}{#2}%
  \protected@edef#3{\@nameuse{#1-#2}}%
}

\newcommand{\checkarrayindex}[2]{%
  \bounderrorfalse
  \expandafter\ifx\csname #1-low\endcsname\relax%
    \ifpattern\else
      \ClassError{memoir}{No array called #1}{\@ehc}%
    \fi
    \bounderrortrue
  \fi
  \ifnum #2<\@nameuse{#1-low}\relax%
    \ifpattern\else
      \ClassError{memoir}{Index #2 outside limits for array #1}{\@ehc}%
    \fi
    \bounderrortrue
  \fi
  \ifnum #2>\@nameuse{#1-high}\relax%
    \ifpattern\else
      \ClassError{memoir}{Index #2 outside limits for array #1}{\@ehc}%
    \fi
    \bounderrortrue
  \fi
}

\newcommand{\arraytostring}[2]{%
  \def#2{}%
  \c@chrsinstr = \@nameuse{#1-low}%
  \@vsarraytostring{#1}{#2}%
}

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

\newcommand{\checkifinteger}[1]{%
  \protected@edef\@vsa{#1}%
  \ifcat _\ifnum9<1\gobm{#1} _\else A\fi
    \integertrue%
  \else
    \integerfalse%
  \fi%
}
\newcommand{\gobm}[1]{#1}

\newcommand{\indentpattern}[1]{%
  \stringtoarray{Array@vs}{#1}}

\newcommand{\get@vsindent}{%
  \getarrayelement{Array@vs}{\thevslineno}{\@vspat}%
  \ifbounderror
    \arraytostring{Array@vs}{\@vsp@t}%
    \ClassWarning{memoir}{%
      Index `\thevslineno' for pattern `\@vsp@t' is out of bounds}%
    \def\@vspat{0}%
  \else
    \checkifinteger{\@vspat}%
    \ifinteger\else
      \arraytostring{Array@vs}{\@vsp@t}%
      \ClassWarning{memoir}{%
       `\@vspat' at index `\thevslineno' in pattern `\@vsp@t' is not a digit}%
      \def\@vspat{0}%
    \fi
  \fi
  \ifcase\@vspat\else\hspace*{\@vspat\vgap}\fi}
\newcommand{\getstar@vsindent}{%
  \expandafter\ifx\csname Array@vs-high\endcsname\relax
    \ClassError{memoir}{A pattern has not been specified}{\@ehc}
  \else
    \ifnum\c@vslineno>\@nameuse{Array@vs-high}%
      \setcounter{vslineno}{1}%
     \fi
     \get@vsindent
  \fi}

\newenvironment{patverse}%
  {\starpatternfalse\patterntrue\altindentfalse\setcounter{vslineno}{1}}%
  {\patternfalse}

\newenvironment{patverse*}%
  {\starpatterntrue\patternfalse\altindentfalse\setcounter{vslineno}{1}}%
  {\starpatternfalse}

\newcommand{\poemtitle}{\par%
  \secdef\@vsptitle\@vssptitle}
\newcommand{\poemtoc}{section}

\def\@vsptitle[#1]#2{%
  \addcontentsline{toc}{\poemtoc}{#1}
  \poemtitlemark{#1}%
  \@vstypeptitle{#2}
  \@afterheading}

\def\@vssptitle#1{%
  \@vstypeptitle{#1}
  \@afterheading}

\newcommand{\@vstypeptitle}[1]{%
  \vspace{\beforepoemtitleskip}
  {\poemtitlefont #1\par}
  \vspace{\afterpoemtitleskip}
}

\newcommand{\poemtitlefont}{\normalfont\large\bfseries\centering}
\newcommand{\poemtitlemark}[1]{}

\newlength{\beforepoemtitleskip}
  \setlength{\beforepoemtitleskip}{3.5ex \@plus 1ex \@minus .2ex}
\newlength{\afterpoemtitleskip}
  \setlength{\afterpoemtitleskip}{2.3ex \@plus.2ex}

\newenvironment{quotation}%
               {\list{}{\listparindent 1.5em%
                        \itemindent    \listparindent
                        \rightmargin   \leftmargin
                        \parsep        \z@ \@plus\p@}%
                \item[]}%
               {\endlist}
\newenvironment{quote}%
               {\list{}{\rightmargin\leftmargin}%
                \item[]}%
               {\endlist}
\newcommand{\symbollabel}[1]{{#1 \hfill}}
\newenvironment{symbols}{\list{}%
    {\itemindent 0em \leftmargin 8em
     \labelsep 1em \labelwidth 5em
     \let\makelabel\symbollabel}}%
    {\endlist}
\newcommand{\symboldef}[2]{\item[#1] #2}
\setlength\arraycolsep{5\p@}
\setlength\tabcolsep{6\p@}
\setlength\arrayrulewidth{.4\p@}
\setlength\doublerulesep{2\p@}
\setlength\tabbingsep{\labelsep}
\skip\@mpfootins = \skip\footins
\setlength\fboxsep{3\p@}
\setlength\fboxrule{.4\p@}

\@addtoreset{equation}{chapter}
\renewcommand{\theequation}{%
  \ifnum \c@chapter>\z@ \thechapter.\fi \@arabic\c@equation}
\newcounter{newflo@tctr}
  \setcounter{newflo@tctr}{1}

\newcommand{\newfloat}[4][\@empty]{%
  \@namedef{ftype@#2}{\value{newflo@tctr}}
  \addtocounter{newflo@tctr}{\value{newflo@tctr}}
  \@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}

  \@namedef{ext@#2}{#3}  % file extension
  \@ifundefined{c@#3depth}{\newcounter{#3depth}}{}
  \setcounter{#3depth}{1}

  \@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

  \newenvironment{#2}{\@float{#2}}{\end@float}
  \newenvironment{#2*}{\@dblfloat{#2}}{\end@dblfloat}
} % end \newfloat

\newcommand{\newsubfloat}[1]{%
  \newlistentry[#1]{sub#1}{\@nameuse{ext@#1}}{1}
  \@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}
}

\newif\ifdonemaincaption
  \donemaincaptionfalse

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

\let\@memoldefloat\end@float
\renewcommand{\end@float}{%
  \@memlistsubcaptions{\@captype}\@memoldefloat}
\let\@memoldedblfloat\end@dblfloat
\renewcommand{\end@dblfloat}{%
  \@memlistsubcaptions{\@captype}\@memoldedblfloat}

\newif\if@contcw
\newif\if@conthang
\newif\if@contindent

\newcommand{\captiondelim}[1]{\def\@contdelim{#1}}
\captiondelim{: }

\newcommand{\captionnamefont}[1]{\def\@contnfont{#1}}
\captionnamefont{}

\newcommand{\captiontitlefont}[1]{\def\@conttfont{#1}}
\captiontitlefont{}

\newcommand{\flushleftright}{%
  \leftskip\z@ \rightskip\z@
  \parfillskip=\z@ plus 1fil}
\newcommand{\centerlastline}{%
  \leftskip=\z@ plus 1fil
  \rightskip=\z@ plus -1fil
  \parfillskip=\z@ plus 2fil}

\newcommand{\captionstyle}[1]{\def\@contcstyle{#1}}
\captionstyle{}

\newlength{\@contcwidth}
\newcommand{\captionwidth}[1]{\setlength{\@contcwidth}{#1}}
\captionwidth{\linewidth}
\newcommand{\changecaptionwidth}{\@contcwtrue}
\newcommand{\normalcaptionwidth}{\@contcwfalse}
\normalcaptionwidth

\newlength{\@contindw}
\newcommand{\hangcaption}{\@conthangtrue\@contindentfalse}
\newcommand{\indentcaption}[1]{\setlength{\@contindw}{#1}%
  \@conthangfalse\@contindenttrue}
\newcommand{\normalcaption}{\@conthangfalse\@contindentfalse}
\normalcaption

\newcommand{\precaption}[1]{\def\@contpre{#1}}
\precaption{}
\newcommand{\postcaption}[1]{\def\@contpost{#1}}
\postcaption{}
\newcommand{\midbicaption}[1]{\def\@contmidbi{#1}}
\midbicaption{}

\newlength{\abovecaptionskip}
  \setlength{\abovecaptionskip}{0.5\onelineskip}
\newlength{\belowcaptionskip}
  \setlength{\belowcaptionskip}{0.5\onelineskip}

\let\@memoldcaption\caption
\def\caption{\donemaincaptiontrue\@memoldcaption}

\long\def\@makecaption#1#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}
  \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\par}
  \else
    \if@contindent
      \@contpre%
      {\@contnfont #1\@contdelim}\@memtempa
      {\@contcstyle\hangindent=\@contindw\hangafter=\@ne\@conttfont #2\par}
    \else
      \@contpre%
      {\@contnfont #1\@contdelim}\@memtempa
      {\@contcstyle\@conttfont #2\par}
    \fi
  \fi
  \@contpost
  \if@contcw
    \par
    }  % end of the \parbox
  \fi
  \vskip\belowcaptionskip}

\newcommand{\contcaption}{\@contcaption\@captype}

\long\def\@@contcaption#1#2{%
  \par
  \begingroup
     \@parboxrestore
     \if@minipage
       \@setminipage
     \fi
     \normalsize
     \@makecaption{\csname fnum@#1\endcsname}{\ignorespaces #2}\par
  \endgroup}

\long\def\@contcaption#1#2{%
  \if@contbotsub
    \@memlistsubcaptions{#1}%
    \@@contcaption{#1}{#2}%
  \else
    \@@contcaption{#1}{#2}%
    \@memlistsubcaptions{#1}%
  \fi}

\newcommand{\legend}[1]{%
  \par
  \begingroup
     \@parboxrestore
     \if@minipage
       \@setminipage
     \fi
     \normalsize
     \captiondelim{\mbox{}}
     \@makecaption{}{\ignorespaces #1}\par
  \endgroup}

\newcommand{\namedlegend}{\@dblarg{\@legend\@captype}}

\long\def\@legend#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}

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

\newcommand{\bitwonumcaption}[6][\@empty]{%
  \begingroup
  \@ifmtarg{#2}{\caption{#3}}{\caption[#2]{#3}}
  \ifx #1\@empty\else
    \label{#1}
  \fi
  \setlength{\abovecaptionskip}{0pt}
  \setlength{\belowcaptionskip}{0pt}
  \edef\@memtempc{#4}
  \expandafter \renewcommand \csname \@captype name\endcsname{\@memtempc}
  \addtocounter{\@captype}{-1}
  \@contmidbi
  \@ifmtarg{#5}{\caption{#6}}{\caption[#5]{#6}}
  \endgroup
}

\newcommand{\bionenumcaption}[6][\@empty]{%
  \begingroup
  \@ifmtarg{#2}{\caption{#3}}{\caption[#2]{#3}}
  \ifx #1\@empty\else
    \label{#1}
  \fi
  \setlength{\abovecaptionskip}{0pt}
  \setlength{\belowcaptionskip}{0pt}
  \edef\@memtempc{#4}
  \expandafter \renewcommand \csname \@captype name\endcsname{\@memtempc}
  \@contmidbi
  \contcaption{#6}
  \@ifmtarg{#5}{%
    \addcontentsline{\csname ext@\@captype\endcsname}{\@captype}%
      {\protect\numberline{}{\ignorespaces #6}}}{%
    \addcontentsline{\csname ext@\@captype\endcsname}{\@captype}%
      {\protect\numberline{}{\ignorespaces #5}}}
  \endgroup
}

\newcommand{\bicaption}[5][\@empty]{%
  \begingroup
  \@ifmtarg{#2}{\caption{#3}}{\caption[#2]{#3}}
  \ifx #1\@empty\else
    \label{#1}
  \fi
  \setlength{\abovecaptionskip}{0pt}
  \setlength{\belowcaptionskip}{0pt}
  \edef\@memtempc{#4}
  \expandafter \renewcommand \csname \@captype name\endcsname{\@memtempc}
  \@contmidbi
  \contcaption{#5}
  \endgroup
}

\newcommand{\bicontcaption}[3]{%
  \begingroup
  \contcaption{#1}
  \setlength{\abovecaptionskip}{0pt}
  \setlength{\belowcaptionskip}{0pt}
  \edef\@memtempc{#2}
  \expandafter \renewcommand \csname \@captype name\endcsname{\@memtempc}
  \@contmidbi
  \contcaption{#3}
  \endgroup
}

\newcommand{\subcaptionstyle}[1]{\def\@contsubcstyle{#1}}
\subcaptionstyle{}

\newif\if@shortsubcap
\newif\if@hangsubcap
\newcommand*{\shortsubcaption}{\@shortsubcaptrue}
\newcommand*{\hangsubcaption}{\@hangsubcaptrue}
\newcommand*{\normalsubcaption}{\@shortsubcapfalse\@hangsubcapfalse}
\normalsubcaption

\newskip\subfloattopskip
\newskip\subfloatcapskip
\newskip\subfloatcaptopadj
\newskip\subfloatbottomskip
\newskip\subfloatlabelskip
\newdimen\subfloatcapmargin
\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

\newcommand*{\subcaptionsize}[1]{\def\@subcapsize{#1}}
\newcommand*{\subcaptionlabelfont}[1]{\def\@subcaplabelfont{#1}}
\newcommand*{\subcaptionfont}[1]{\def\@subcapfont{#1}}
\subcaptionsize{\footnotesize}
\subcaptionlabelfont{\normalfont}
\subcaptionfont{\normalfont}

  \newcounter{@contsubnum}
  \newcommand{\@contkeep}{\setcounter{@contsubnum}{\value{sub\@captype}}}
  \newcommand{\@contset}{\setcounter{sub\@captype}{\value{@contsubnum}}}
  \newcommand{\subconcluded}{\setcounter{sub\@captype}{0}}
\newif\if@contbotsub
  \@contbotsubtrue

\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]}}
\long\def\@memsubcap#1[#2]#3{%
  \vskip\subfloatcapskip
  \ifx #2\@empty
    \@memsubcaption{#1}{#3}{#3}%
  \else
    \@memsubcaption{#1}{#2}{#3}%
  \fi
  \vskip\subfloatcapskip
  \egroup}
\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}}

\newcommand{\contsubcaption}{%
  \bgroup
    \let\label=\memsub@label
    \@contset
    \refstepcounter{sub\@captype}\@contkeep
    \@ifnextchar [%
      {\@memsubcap{sub\@captype}}%
      {\@memsubcap{sub\@captype}[\@empty]}}
\newenvironment{subfloat}{}{}

\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]}}

\newcommand{\contsubbottom}{%
  \@contbotsubtrue
  \@memcontsubbody}

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

\newcommand{\subtop}{%
  \@contbotsubfalse
  \@memsubbody}

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


\newcommand{\@contsubfloat}{%
  \bgroup
  \let\label=\memsub@label
  \refstepcounter{sub\@captype}\@contkeep%   % <- change here
  \@ifnextchar [%
    {\@memsubfig}%
    {\@memsubfig[\@empty]}}

\def\@memsubfig[#1]{%
  \@ifnextchar [%
    {\@memsubfloat{sub\@captype}[#1]}%
    {\@memsubfloat{sub\@captype}[\@empty #1][#1]}}

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

\newcommand*{\@memsubfigcaptionlist}{}
\newcommand*{\memlistsubcaptions}{%
  \@ifstar
    {\gdef\@memsubfigcaptionlist{}}%
    {\@memlistsubcaptions{\@captype}}}

\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{}}

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

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

\newcommand{\memsubfig@captionpar}[2]{%
  \parbox[t]{#1}{\@subcapsize\@contsubcstyle #2}}

\newcommand{\memsub@label}{%
  \@ifnextchar (%
    {\sf@memsub@label}%
    {\sf@memsub@label(Sub\@captype\space
                     \@nameuse{p@sub\@captype}%
                     \@nameuse{thesub\@captype})}}
\def\sf@memsub@label(#1)#2{%
  \protected@edef\mem@currentlabelname{#1}%
  \sf@@memsub@label{#2}}

\AtBeginDocument{%
  \@ifpackageloaded{nameref}{%
    \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}{%
    \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}
  }{%
    \let\@memoldlabel\label
    \newcommand*{\sf@@memsub@label}[1]{%
      \@bsphack
      \@memoldlabel{#1}%
      \protected@write\@auxout{}{%
        \string\newlabel{sub@#1}%
          {{\@nameuse{@@thesub\@captype}}%
          {\thepage}}}%
  \@esphack}
  }{}%
  }
}

\newcommand*{\subcaptionref}{%
  \@ifstar{\ssc@ref}{\sc@ref}}
\newcommand*{\ssc@ref}[1]{\ref{sub@#1}}
\newcommand*{\sc@ref}[1]{{\@subcaplabelfont\ref{sub@#1}}}

\def\@addmarginpar{%
    \checkoddpage
    \@next\@marbox\@currlist{\@cons\@freelist\@marbox
    \@cons\@freelist\@currbox}\@latexbug\@tempcnta\@ne
    \if@twocolumn
        \if@firstcolumn \@tempcnta\m@ne \fi
    \else
      \if@mparswitch
        \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@
      \@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}%
}

\newlength{\beforeepigraphskip}
  \setlength{\beforeepigraphskip}{.5\baselineskip}
\newlength{\afterepigraphskip}
  \setlength{\afterepigraphskip}{.5\baselineskip}
\newlength{\epigraphwidth}
  \setlength{\epigraphwidth}{.4\textwidth}
\newlength{\epigraphrule}
  \setlength{\epigraphrule}{.4\p@}
\newcommand{\epigraphsize}{\small}
\newcommand{\epigraphflush}{flushright}
\newcommand{\textflush}{flushleft}
\newcommand{\sourceflush}{flushright}
\newcommand{\@epirule}{\rule[.5ex]{\epigraphwidth}{\epigraphrule}}
\newcommand{\@epitext}[1]{%
  \begin{minipage}{\epigraphwidth}\begin{\textflush} #1\\
    \ifdim\epigraphrule>\z@ \@epirule \else \vspace*{1ex} \fi
  \end{\textflush}\end{minipage}}
\newcommand{\@episource}[1]{%
  \begin{minipage}{\epigraphwidth}\begin{\sourceflush} #1\end{\sourceflush}
  \end{minipage}}
\newcommand{\epigraph}[2]{\vspace{\beforeepigraphskip}
  {\epigraphsize\begin{\epigraphflush}\begin{minipage}{\epigraphwidth}
    \@epitext{#1}\\ \@episource{#2}
    \end{minipage}\end{\epigraphflush}
    \vspace{\afterepigraphskip}}}
\newcommand{\qitem}[2]{{\raggedright\item \begin{minipage}{\epigraphwidth}
  \@epitext{#1}\\ \@episource{#2}
  \end{minipage}}}
\newcommand{\qitemlabel}[1]{\hfill}
\newenvironment{epigraphs}{%
  \vspace{\beforeepigraphskip}\begin{\epigraphflush}
  \epigraphsize
  \begin{minipage}{\epigraphwidth}
   \list{}%
    {\itemindent\z@ \labelwidth\z@ \labelsep\z@
     \leftmargin\z@ \rightmargin\z@
     \let\makelabel\qitemlabel}}%
  {\endlist\end{minipage}\end{\epigraphflush}
   \vspace{\afterepigraphskip}}
\newcommand{\dropchapter}[1]{%
  \let\@epichapapp\@chapapp
  \renewcommand{\@chapapp}{\vspace*{#1}\@epichapapp}}
\newcommand{\undodrop}{\let\@chapapp\@epichapapp}
\newif\if@epirhs     \@epirhstrue
\newif\if@epicenter  \@epicentertrue
\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}
\newcommand{\epigraphhead}[2][95]{%
  \def\@epitemp{\begin{minipage}{\epigraphwidth}#2\end{minipage}}
  \def\ps@epigraph{\let\@mkboth\@gobbletwo
    \@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}
  \thispagestyle{epigraph}}
\if@memoldfont
  \DeclareOldFontCommand{\rm}{%
    \ClassWarning{memoir}{The \protect\rm\space font command is deprecated.
    \MessageBreak Try to use \protect\rmfamily\space
                          or \protect\textrm\space instead}%
    \normalfont\rmfamily}{\mathrm}
\else
  \def\rm{\ClassError{memoir}{Font command \protect\rm\space is not supported}{%
          Use \protect\textrm{...},
          or {\protect\rmfamily\space ...},
          or the oldfontcommands option}}
\fi

\if@memoldfont
  \DeclareOldFontCommand{\sf}{%
    \ClassWarning{memoir}{The \protect\sf\space font command is deprecated.
    \MessageBreak Try to use \protect\sffamily\space
                          or \protect\textsf\space instead}%
    \normalfont\sffamily}{\mathsf}
\else
  \def\sf{\ClassError{memoir}{Font command \protect\sf\space is not supported}{%
          Use \protect\textsf{...},
          or {\protect\sffamily\space ...},
          or the oldfontcommands option}}
\fi

\if@memoldfont
  \DeclareOldFontCommand{\tt}{%
    \ClassWarning{memoir}{The \protect\tt\space font command is deprecated.
    \MessageBreak Try to use \protect\ttfamily\space
                          or \protect\texttt\space instead}%
    \normalfont\ttfamily}{\mathtt}
\else
  \def\tt{\ClassError{memoir}{Font command \protect\tt\space is not supported}{%
          Use \protect\texttt{...},
          or {\protect\ttfamily\space ...},
          or the oldfontcommands option}}
\fi

\if@memoldfont
  \DeclareOldFontCommand{\bf}{%
    \ClassWarning{memoir}{The \protect\bf\space font command is deprecated.
    \MessageBreak Try to use \protect\bfseries\space
                          or \protect\textbf\space instead}%
    \normalfont\bfseries}{\mathbf}
\else
  \def\bf{\ClassError{memoir}{Font command \protect\bf\space is not supported}{%
          Use \protect\textbf{...},
          or {\protect\bfseries\space ...},
          or the oldfontcommands option}}
\fi

\if@memoldfont
  \DeclareOldFontCommand{\it}{%
    \ClassWarning{memoir}{The \protect\it\space font command is deprecated.
    \MessageBreak Try to use \protect\itshape\space
                          or \protect\textit\space instead}%
    \normalfont\itshape}{\mathit}
\else
  \def\it{\ClassError{memoir}{Font command \protect\it\space is not supported}{%
          Use \protect\textit{...},
          or {\protect\itshape\space ...},
          or the oldfontcommands option}}
\fi

\if@memoldfont
  \DeclareOldFontCommand{\sl}{%
    \ClassWarning{memoir}{The \protect\sl\space font command is deprecated.
    \MessageBreak Try to use \protect\slshape\space
                          or \protect\textsl\space instead}%
    \normalfont\slshape}{\@nomath\sl}
\else
  \def\sl{\ClassError{memoir}{Font command \protect\sl\space is not supported}{%
          Use \protect\textsl{...},
          or {\protect\slshape\space ...},
          or the oldfontcommands option}}
\fi

\if@memoldfont
  \DeclareOldFontCommand{\sc}{%
    \ClassWarning{memoir}{The \protect\sc\space font command is deprecated.
    \MessageBreak Try to use \protect\scshape\space
                          or \protect\textsc\space instead}%
    \normalfont\scshape}{\@nomath\sc}
\else
  \def\sc{\ClassError{memoir}{Font command \protect\sc\space is not supported}{%
          Use \protect\textsc{...},
          or {\protect\scshape\space ...},
          or the oldfontcommands option}}
\fi

\if@memoldfont
  \DeclareRobustCommand*\cal{%
    \ClassWarning{memoir}{The \protect\cal\space font command is deprecated.
    \MessageBreak Try to use \protect\mathcal\space instead}%
    \@fontswitch\relax\mathcal}
\else
  \def\cal{\ClassError{memoir}{Font command \protect\cal\space is not supported}{%
          Use \protect\mathcal,
          or the oldfontcommands option}}
\fi

\if@memoldfont
  \DeclareRobustCommand*\mit{%
    \ClassWarning{memoir}{The \protect\mit\space font command is deprecated.
    \MessageBreak Try to use \protect\mathnormal\space instead}%
    \@fontswitch\relax\mathnormal}
\else
  \def\mit{\ClassError{memoir}{Font command \protect\mit\space is not supported}{%
          Use \protect\mathnormal,
          or the oldfontcommands option}}
\fi

\let\@memoldem\em
\DeclareTextFontCommand{\emph}{\@memoldem}
\renewcommand{\em}{\@memoldem%
  \ClassWarning{memoir}{Font command \protect\em\space is deprecated.\MessageBreak
        Use \protect\emph{...} instead}}

\newcommand{\fref}[1]{\figurerefname~\ref{#1}}
\newcommand{\tref}[1]{\tablerefname~\ref{#1}}
\newcommand{\pref}[1]{\pagerefname~\pageref{#1}}
\newcommand{\@pnumwidth}{1.55em}
\newcommand{\@tocrmarg} {2.55em}
\newcommand{\@dotsep}{4.5}
\newlength{\tocentryskip} \setlength{\tocentryskip}{1em}
\newlength{\tocbaseline} \setlength{\tocbaseline}{20pt}
\newcommand{\tocskip}[1]{%
    \addtocontents{toc}{\protect\vspace{#1}}}
\newcommand*{\ensureonecol}{%
  \if@twocolumn
    \@restonecoltrue\onecolumn
  \else
    \@restonecolfalse
  \fi}
\newcommand*{\restorefromonecol}{\if@restonecol\twocolumn\fi}

\newlength{\cftparskip}
\setlength{\cftparskip}{0pt}

\newcommand{\newlistof}[3]{%
  \@namedef{ext@#2}{#2}
  \@ifundefined{c@#2depth}{\newcounter{#2depth}}{}
  \setcounter{#2depth}{1}

  \@namedef{#2mark}{\markboth{#3}{#3}}

  \@namedef{#1}{\@ifstar{\@nameuse{@star#2}}{\@nameuse{@plain#2}}}
  \@namedef{@star#2}{%
    \ensureonecol
    \par
    \begingroup
%%%      \parindent\z@ \parskip\cftparskip
      \@nameuse{@#2maketitle}
      \@starttoc{#2}%
    \endgroup
    \restorefromonecol}

  \@namedef{@plain#2}{%
    \ensureonecol
    \par
    \begingroup
%%%      \parindent\z@ \parskip\cftparskip
      \@nameuse{@#2maketitle}
      \phantomsection
      \addcontentsline{toc}{chapter}{#3}
      \@starttoc{#2}%
    \endgroup
    \restorefromonecol}

  \@namedef{@#2maketitle}{%
    \@nameuse{#2headstart}
   {\parindent\z@ \parskip\cftparskip
    \interlinepenalty\@M
    \@nameuse{print#2title}{#3}%
    \@nameuse{#2mark}%
    \thispagestyle{chapter}%
    \@nameuse{after#2title}
   }
    \@afterheading}

  \@namedef{#2headstart}{\chapterheadstart}
  \@namedef {after#2title}{\afterchaptertitle}
    \@namedef{print#2title}##1{\printchaptertitle{##1}}
} % end \newlistof

\newlistof{tableofcontents}{toc}{\contentsname}
\newcommand{\setpnumwidth}[1]{\renewcommand{\@pnumwidth}{#1}}
\newcommand{\setrmarg}[1]{\renewcommand{\@tocrmarg}{#1}}
\providecommand{\cftdot}{.}
\providecommand{\cftdotfill}[1]{%
  \leaders\hbox{$\m@th\mkern #1 mu\hbox{\cftdot}\mkern #1 mu$}\hfill}
\providecommand{\cftdotsep}{4.5}
\newcommand{\cftnodots}{10000}
\newcommand{\cftparfillskip}{\parfillskip=0pt plus1fil}
\renewcommand{\numberline}[1]{%
  \hb@xt@\@tempdima{\@cftbsnum #1\@cftasnum\hfil}\@cftasnumb}
\newcommand{\@cftbsnum}{}
\newcommand{\@cftasnum}{}
\newcommand{\@cftasnumb}{}
\newcommand{\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
       \@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
       \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#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{toclevel@#2}{#4}
  \@namedef{cft#2fillnum}##1{%
    {\@nameuse{cft#2leader}}\nobreak
    \hb@xt@\@pnumwidth{%
      \hfil\@nameuse{cft#2pagefont}##1}\@nameuse{cft#2afterpnum}\par}
} % end \newlistentry

\newcommand{\cftsetindents}[3]{%
  \setlength{\@nameuse{cft#1indent}}{#2}
  \setlength{\@nameuse{cft#1numwidth}}{#3}
}

\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
       \@tempdima \cftpartnumwidth\relax
       \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}

  \newlength{\cftbeforepartskip}
    \setlength{\cftbeforepartskip}{2.25em \@plus\p@}
  \newlength{\cftpartnumwidth}
    \setlength{\cftpartnumwidth}{0em}
  \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}{}
  \newlength{\cftpartindent}
    \setlength{\cftpartindent}{0em}
  \newcommand{\cftpartfillnum}[1]{%
    {\cftpartleader}%
    {\hb@xt@\@pnumwidth{\hss {\cftpartpagefont #1}}}\cftpartafterpnum\par}

\newcommand*{\l@chapter}[2]{%
  \ifnum \c@tocdepth >\m@ne
    \addpenalty{-\@highpenalty}%
    \vskip \cftbeforechapterskip
    {\leftskip \cftchapterindent\relax
     \rightskip \@tocrmarg
     \parfillskip -\rightskip
     \parindent \cftchapterindent\relax\@afterindenttrue
     \interlinepenalty\@M
     \leavevmode
     \@tempdima \cftchapternumwidth\relax
     \let\@cftbsnum \cftchapterpresnum
     \let\@cftasnum \cftchapteraftersnum
     \let\@cftasnumb \cftchapteraftersnumb
     \advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
     {\cftchapterfont #1}\nobreak
     \cftchapterfillnum{#2}}
  \fi}
  \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}

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

\newcommand*{\l@paragraph}[2]{%
  \ifnum \c@tocdepth >3\relax
    \vskip \cftbeforesubsubsectionskip
    {\leftskip \cftparagraphindent\relax
     \rightskip \@tocrmarg
     \parfillskip -\rightskip
     \parindent \cftparagraphindent\relax\@afterindenttrue
     \interlinepenalty\@M
     \leavevmode
     \@tempdima \cftparagraphnumwidth\relax
     \let\@cftbsnum \cftsubsubsectionpresnum
     \let\@cftasnum \cftsubsubsectionaftersnum
     \let\@cftasnumb \cftsubsubsectionaftersnumb
     \advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
     {\cftsubsubsectionfont #1}\nobreak
     \cftsubsubsectionfillnum{#2}}
  \fi}
\newlength{\cftparagraphindent}
\newlength{\cftparagraphnumwidth}
  \cftsetindents{paragraph}{10.0em}{5.0em}

\newcommand*{\l@subparagraph}[2]{%
  \ifnum \c@tocdepth >4\relax
    \vskip \cftbeforesubsubsectionskip
    {\leftskip \cftsubparagraphindent\relax
     \rightskip \@tocrmarg
     \parfillskip -\rightskip
     \parindent \cftsubparagraphindent\relax\@afterindenttrue
     \interlinepenalty\@M
     \leavevmode
     \@tempdima \cftsubparagraphnumwidth\relax
     \let\@cftbsnum \cftsubsubsectionpresnum
     \let\@cftasnum \cftsubsubsectionaftersnum
     \let\@cftasnumb \cftsubsubsectionaftersnumb
     \advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
     {\cftsubsubsectionfont #1}\nobreak
     \cftsubsubsectionfillnum{#2}}
  \fi}
\newlength{\cftsubparagraphindent}
\newlength{\cftsubparagraphnumwidth}
  \cftsetindents{subparagraph}{12.0em}{6.0em}

\newcommand{\@cftl@subfigtab}{
\newlistentry[figure]{subfigure}{lof}{1}
  \cftsetindents{subfigure}{2.3em}{2.5em}
\newlistentry[table]{subtable}{lot}{1}
  \cftsetindents{subtable}{2.3em}{2.5em}
}

\AtBeginDocument{\@ifpackageloaded{subfigure}{\@cftl@subfigtab}{}}

\DeclareRobustCommand{\cftpagenumbersoff}[1]{%
  \@namedef{cft#1fillnum}##1{%
    \cftparfillskip\@nameuse{cft#1afterpnum}\par}}

\DeclareRobustCommand{\cftpagenumberson}[1]{%
  \@namedef{cft#1fillnum}##1{%
    {\@nameuse{cft#1leader}}\nobreak
    \hb@xt@\@pnumwidth{%
      \hfil\@nameuse{cft#1pagefont}##1}\@nameuse{cft#1afterpnum}\par}}

\newcommand{\chapterprecis}[1]{%
  \chapterprecishere{#1}
  \chapterprecistoc{#1}}
\newcommand{\chapterprecishere}[1]{%
  \vspace*{-2\baselineskip}
  \begin{quote}\textit{#1}\end{quote}}
\newcommand{\chapterprecistoc}[1]{\addtocontents{toc}{%
  {\leftskip \cftchapterindent\relax
   \advance\leftskip \cftchapternumwidth\relax
   \rightskip \@tocrmarg\relax
   \textit{#1}\protect\par}}}
\newcommand{\cftlocalchange}[3]{%
  \addtocontents{#1}{\protect\setpnumwidth{#2} \protect\setrmarg{#3}}}
\newcommand{\cftaddtitleline}[4]{%
  \addtocontents{#1}{\protect\contentsline{#2}{#3}{#4}}}
\newcommand{\cftaddnumtitleline}[5]{%
  \addtocontents{#1}%
    {\protect\contentsline{#2}{\protect\numberline{#3}{\protect\ignorespaces #4}}{#5}}}
\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
    \ClassError{memoir}{%
      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
}
\newcommand{\settocdepth}[1]{%
  \@tempswafalse
  \nametest{#1}{none}
  \ifsamename
    \addtocontents{toc}{\protect\setcounter{tocdepth}{-10}}
    \@tempswatrue
  \fi
  \nametest{#1}{part}
  \ifsamename
    \addtocontents{toc}{\protect\setcounter{tocdepth}{-1}}
    \@tempswatrue
  \fi
  \nametest{#1}{chapter}
  \ifsamename
    \addtocontents{toc}{\protect\setcounter{tocdepth}{0}}
    \@tempswatrue
  \fi
  \nametest{#1}{section}
  \ifsamename
    \addtocontents{toc}{\protect\setcounter{tocdepth}{1}}
    \@tempswatrue
  \fi
  \nametest{#1}{subsection}
  \ifsamename
    \addtocontents{toc}{\protect\setcounter{tocdepth}{2}}
    \@tempswatrue
  \fi
  \nametest{#1}{subsubsection}
  \ifsamename
    \addtocontents{toc}{\protect\setcounter{tocdepth}{3}}
    \@tempswatrue
  \fi
  \nametest{#1}{paragraph}
  \ifsamename
    \addtocontents{toc}{\protect\setcounter{tocdepth}{4}}
    \@tempswatrue
  \fi
  \nametest{#1}{subparagraph}
  \ifsamename
    \addtocontents{toc}{\protect\setcounter{tocdepth}{5}}
    \@tempswatrue
  \fi
  \nametest{#1}{all}
  \ifsamename
    \addtocontents{toc}{\protect\setcounter{tocdepth}{50}}
    \@tempswatrue
  \fi
    \if@tempswa\else
    \ClassError{memoir}{%
      Unknown document division name (#1)
    }{%
      I'll ignore it. Type \space <return> and I'll continue.}
  \fi
}
\newcommand{\maxtocdepth}[1]{%
  \@setclcnt{#1}{tocdepth}}
\newcommand{\setsecnumdepth}[1]{%\leavevmode%
  \@setclcnt{#1}{secnumdepth}}
\newcounter{maxsecnumdepth}
\newcommand{\maxsecnumdepth}[1]{%
  \@setclcnt{#1}{secnumdepth}\@setclcnt{#1}{maxsecnumdepth}}

\newdimen\bibindent
\setlength\bibindent{1.5em}

\newenvironment{bibitemlist}[1]{%
  \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}}%
  \sloppy
  \clubpenalty4000
  \@clubpenalty \clubpenalty
  \widowpenalty4000%
  \sfcode`\.\@m}
  {\def\@noitemerr
    {\@latex@warning{Empty `thebibliography' environment}}%
    \endlist}

\newcommand{\newblock}{\hskip .11em\@plus.33em\@minus.07em}
\let\@openbib@code\@empty
\newcommand*{\setbiblabel}[1]{%
  \renewcommand*{\@biblabel}[1]{#1}}
\setbiblabel{[#1]\hfill}
\newenvironment{thebibliography}[1]{%
  \chapter*{\bibname}
  \bibmark
  \ifnobibintoc\else
    \phantomsection
    \addcontentsline{toc}{chapter}{\bibname}
  \fi
  \prebibhook
  \begin{bibitemlist}{#1}}{\end{bibitemlist}\postbibhook}
\newif\ifnobibintoc
\newcommand*{\bibintoc}{\nobibintocfalse}
\newcommand*{\nobibintoc}{\nobibintoctrue}
\bibintoc

\newcommand{\prebibhook}{}
\newcommand{\postbibhook}{}

\newenvironment{theindex}{%
  \if@twocolumn
    \@restonecolfalse
  \else
    \@restonecoltrue
  \fi
  \twocolumn[\@makeschapterhead{\indexname}
             \preindexhook]%
  \indexmark
  \setlength{\columnseprule}{\indexrule}
  \setlength{\columnsep}{\indexcolsep}
  \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\clearpage\fi}
\newif\ifnoindexintoc
\newcommand*{\indexintoc}{\noindexintocfalse}
\newcommand*{\noindexintoc}{\noindexintoctrue}
\indexintoc

\newlength{\indexcolsep} \setlength{\indexcolsep}{35pt}
\newlength{\indexrule}   \setlength{\indexrule}{0pt}

\newcommand{\preindexhook}{}
\newcommand{\l@index}{\@dottedtocline{1}{0em}{0pt}}
\newcommand{\@idxitem}  {\par\hangindent 40\p@}
\newcommand{\subitem}   {\par\hangindent 40\p@ \hspace*{20\p@}}
\newcommand{\subsubitem}{\par\hangindent 40\p@ \hspace*{30\p@}}
\newcommand{\indexspace}{\par \vskip 10\p@ \@plus5\p@ \@minus3\p@\relax}
\renewcommand{\footnoterule}{%
  \kern-3\p@
  \hrule width .4\columnwidth
  \kern 2.6\p@}
\@addtoreset{footnote}{chapter}
\newcommand{\@makefntext}[1]{%
    \parindent 1em%
    \noindent
    \hb@xt@1.8em{\hss\@makefnmark}#1}
\newcommand{\footref}[1]{%
  \begingroup
    \unrestored@protected@xdef\@thefnmark{\ref{#1}}%
  \endgroup
  \@footnotemark}

\newif\ifchangemarks\changemarksfalse
\newcommand{\v@rid}[2]{%
    \ifchangemarks
       \ifdraftdoc
           \marginpar[#1]{#2}
    \fi\fi}
\newcommand{\added}[1]{%
    \ifchangemarks
        \v@rid{\small$\oplus$ #1}%
              {\small$\oplus$ #1}
    \fi}
\newcommand{\deleted}[1]{%
    \ifchangemarks
        \v@rid{\small$\neq$ #1}%
              {\small$\neq$ #1}
    \fi}
\newcommand{\changed}[1]{%
    \ifchangemarks
        \v@rid{\small$\Leftrightarrow$ #1}%
              {\small$\Leftrightarrow$ #1}
    \fi}

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

\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{%
        \hb@xt@\paperwidth{\trimmark\hfil\trimmark}%
        \vfil
        \hb@xt@\paperwidth{\trimmark\hfil\trimmark}}%
    \hss}%
  \vss}}

\let\mem@oldshipout\shipout
\newcommand*{\mem@shipi}{%
  \ifvoid\@cclv\expandafter\aftergroup\fi\mem@shipii}
\newcommand*\mem@shipii{\mem@oldshipout\vbox{\trimmarks\unvbox\@cclv}}
\ifshowtrims
  \renewcommand*{\shipout}{\afterassignment\mem@shipi\setbox\@cclv=}
\fi

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

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

\newcommand{\maketitlehooka}{}
\newcommand{\maketitlehookb}{}
\newcommand{\maketitlehookc}{}
\newcommand{\maketitlehookd}{}

\newcommand{\thanksmarkseries}[1]{%
  \def\@bsmarkseries{\renewcommand{\thefootnote}{\@nameuse{#1}{footnote}}}}
\newcommand{\symbolthanksmark}{\thanksmarkseries{\fnsymbol}}
\newcommand{\@bscontmark}{\setcounter{footnote}{0}}
\newcommand{\continuousmarks}{\def\@bscontmark{}}
\newcommand{\thanksheadextra}[2]{%
  \def\@bsthanksheadpre{#1}%
  \def\@bsthanksheadpost{#2}}
\newcommand{\thanksfootextra}[2]{%
  \def\thanksfootpre{#1}%
  \def\thanksfootpost{#2}}

\DeclareRobustCommand{\thanksmark}[1]{\footnotemark[#1]}
\newcommand{\thanksgap}[1]{\hspace{#1}}
\newcommand{\tamark}{\@thefnmark}

\newlength{\thanksmarkwidth}
\newlength{\thanksmargin}
\newcommand{\thanksscript}[1]{\textsuperscript{#1}}
\newcommand{\makethanksmarkhook}{}

\newcommand{\thanksfootmark}{%
  \hb@xt@\thanksmarkwidth{\hfil\normalfont\thanksscript{%
    \thanksfootpre \tamark \thanksfootpost}}}

\newcommand{\makethanksmark}{%
  \leavevmode%
  \makethanksmarkhook\relax
  \parindent 1em\noindent
  \leftskip\thanksmargin\relax
  \advance\leftskip \thanksmarkwidth \null\nobreak\hskip -\leftskip
  \thanksfootmark
}

\newcommand{\usethanksrule}{\let\footnoterule\thanksrule}
\newcommand{\cancelthanksrule}{\let\footnoterule\@bsfootnoterule}

\thanksmarkseries{fnsymbol}  % symbols
\thanksheadextra{}{}
\thanksfootextra{}{}
\setlength{\thanksmarkwidth}{1.8em}
\setlength{\thanksmargin}{-\thanksmarkwidth}

\AtBeginDocument{%
  \let\thanksrule\footnoterule
  \let\@bsfootnoterule\footnoterule
}

\newlength{\droptitle}
\setlength{\droptitle}{0pt}

\newcommand{\maketitle}{\par
  \begingroup
    \@bsmarkseries
    \def\@makefnmark{\rlap{\@textsuperscript{%
       \normalfont\@bsthanksheadpre \tamark \@bsthanksheadpost}}}%
    \long\def\@makefntext##1{\makethanksmark ##1}
    \if@twocolumn
      \ifnum \col@number=\@ne
        \@maketitle
      \else
        \twocolumn[\@maketitle]%
      \fi
    \else
      \newpage
      \global\@topnum\z@
      \@maketitle
    \fi
    \thispagestyle{title}\@thanks
  \endgroup
  \@bscontmark  %  \setcounter{footnote}{0}%
  }
\aliaspagestyle{title}{plain}

\newcommand{\@maketitle}{%
  \newpage
  \null
  \vskip 2em%
        \vspace*{\droptitle}
  \maketitlehooka
  {\@bspretitle \@title \@bsposttitle}
  \maketitlehookb
  {\@bspreauthor \@author \@bspostauthor}
  \maketitlehookc
  {\@bspredate \@date \@bspostdate}
  \maketitlehookd
  \par
  \vskip 1.5em}

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

\newcommand{\emptythanks}{\global\let\@thanks\@empty}

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

\newcommand{\keepthetitle}{%
  \@bsmtitlempty
  \global\let\thanks\relax
  \global\let\and\relax
  \global\let\@thanks\@empty
  \global\let\@title\@empty
  \global\let\@author\@empty
  \global\let\@date\@empty
}

\newcommand{\killtitle}{%
  \keepthetitle
  \global\let\thetitle\relax
  \global\let\theauthor\relax
  \global\let\thedate\relax
}

\addtoiargdef{\title}{}{%
  \begingroup
    \renewcommand{\thanks}[1]{}
    \renewcommand{\thanksmark}[1]{}
    \renewcommand{\thanksgap}[1]{}
    \protected@xdef\thetitle{#1}
  \endgroup}
\addtoiargdef{\author}{}{%
  \begingroup
    \renewcommand{\thanks}[1]{}
    \renewcommand{\and}{\unskip, }
    \renewcommand{\thanksmark}[1]{}
    \renewcommand{\thanksgap}[1]{}
    \protected@xdef\theauthor{#1}
  \endgroup}
\addtoiargdef{\date}{}{%
  \begingroup
    \renewcommand{\thanks}[1]{}
    \renewcommand{\thanksmark}[1]{}
    \renewcommand{\thanksgap}[1]{}
    \protected@xdef\thedate{#1}
  \endgroup}

\newif\if@bsonecol
  \@bsonecoltrue
\newif\ifadd@bstotoc
  \add@bstotocfalse
\newif\ifnumber@bs
  \number@bsfalse
\newif\if@bsrunin
  \@bsruninfalse

\newcommand{\abstractcol}{\@bsonecolfalse}
\newcommand{\abstractintoc}{\add@bstotoctrue}
\newcommand{\abstractnum}{\number@bstrue\@bsruninfalse}
\newcommand{\abstractrunin}{\@bsrunintrue\number@bsfalse}

\newcommand{\abstractnamefont}{\normalfont\small\bfseries}
\newcommand{\abstracttextfont}{\normalfont\small}

\newcommand{\absnamepos}{center}
\newlength{\abstitleskip} \setlength{\abstitleskip}{-0.5em}
\newlength{\absleftindent}
\newlength{\absrightindent}
\newlength{\absparindent}
\newlength{\absparsep}

\newcommand{\abslabeldelim}[1]{\def\@bslabeldelim{#1}}
\abslabeldelim{}
\newcommand{\@bsrunintitle}{%
  \hspace*{\abstitleskip}{\abstractnamefont\abstractname\@bslabeldelim}}

\newcommand{\setup@bstract}{%
  \if@twocolumn
    \if@bsonecol
      \setlength{\absleftindent}{\leftmargin}
    \else
      \setlength{\absleftindent}{\z@}
      \renewcommand{\abstractnamefont}{\normalfont\Large\bfseries}
      \renewcommand{\abstracttextfont}{\normalfont}
      \renewcommand{\absnamepos}{flushleft}
      \setlength{\abstitleskip}{-2ex}
    \fi
  \else
    \setlength{\absleftindent}{\leftmargin}
  \fi
  \setlength{\absrightindent}{\absleftindent}
}

\AtBeginDocument{\setlength{\absparindent}{\parindent}
                 \setlength{\absparsep}{\parskip}}

\newenvironment{@bstr@ctlist}{%
  \list{}{%
          %%\topsep        \z@
          \partopsep     \z@
          \listparindent \absparindent
          \itemindent    \listparindent
          \leftmargin    \absleftindent
          \rightmargin   \absrightindent
          \parsep        \absparsep}%
  \item\relax}
  {\endlist}

\newcommand{\put@bsintoc}{%
  \ifadd@bstotoc
    \ifnumber@bs\else
      \phantomsection
      \addcontentsline{toc}{chapter}{\abstractname}
    \fi
  \fi}

\newcommand{\num@bs}{\chapter{\abstractname}}

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

\newenvironment{onecolabstract}{%
  \begin{@twocolumnfalse}\begin{abstract}}{%
  \end{abstract}\end{@twocolumnfalse}}

\addtoiargdef{\thanks}{}{%
  \protected@xdef\@bs@thanks{\@bs@thanks
    \protect\footnotetext[\the\c@footnote]{#1}}%
}
\let\@bs@thanks\@empty

\newcommand{\saythanks}{\begingroup
  \renewcommand{\thefootnote}{\fnsymbol{footnote}}\@bs@thanks
  \endgroup\global\let\@bs@thanks\@empty}

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

\newcommand{\@ifbothcntrs}[3]{%
  \@ifundefined{c@#1}{% counter undefined
    \ClassError{memoir}{#1 is not a counter}{\@eha}}%
  {% else counter is defined
    \@ifundefined{c@#2}{% within undefined
      \ClassError{memoir}{#2 is not a counter}{\@eha}}%
    {% else both counter and within  are defined
     #3}}}

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

\newcommand{\counterwithout}{\@ifstar{\@csoutstar}{\@csout}}
\newcommand{\@csoutstar}[2]{%
  \@ifbothcntrs{chngcntr}{#1}{#2}{\@removefromreset{#1}{#2}}}
\newcommand{\@csout}[2]{%
  \@ifbothcntrs{#1}{#2}{\@removefromreset{#1}{#2}%
                        \@namedef{the#1}{\arabic{#1}}}}

\newif\ifoddpage
\newif\ifstrictpagecheck
  \strictpagecheckfalse
\newcounter{cp@cntr}
\newcommand{\cplabel}{^_}
\newcommand{\cpset@ref}[3]{%
  \ifx#1\relax
    0% in case no label in the *.aux file (page number made = 0)
  \else
    \expandafter#2#1%
  \fi}
\newcommand{\cpp@geref}[1]{%
  \expandafter\cpset@ref\csname r@#1\endcsname\@secondoftwo{#1}}
\AtBeginDocument{%  % special code if hyperrref package is used
  \@ifpackageloaded{hyperref}{%
    \renewcommand{\cpp@geref}[1]{%
      \expandafter\ifx\csname r@#1\endcsname\relax
        0%    % zero
      \else
        \expandafter\expandafter\expandafter\expandafter
        \expandafter\expandafter\expandafter\@car
        \expandafter\expandafter\expandafter\@gobble
        \csname r@#1\endcsname\@nil
      \fi}}{}}

\DeclareRobustCommand{\checkoddpage}{%
  \oddpagefalse%
  \ifstrictpagecheck
    \stepcounter{cp@cntr}\label{\cplabel\thecp@cntr}%
    \@memcnta=\cpp@geref{\cplabel\thecp@cntr} % must have a space here
    \ifodd\@memcnta\oddpagetrue\fi
  \else
    \ifodd\c@page\oddpagetrue\fi
  \fi
}

\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

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

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

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

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

\newcommand{\calccentering}[1]{
  #1 = \paperwidth
  \advance #1 by -\textwidth
  \divide #1 by \tw@
  \advance #1 by -\spinemargin
}

\newcommand{\cleartoevenpage}[1][\@empty]{%
  \clearpage%
  \ifodd\c@page\hbox{}#1\clearpage\fi}

\newcommand{\movetoevenpage}[1][\@empty]{%
  \newpage%
  \ifodd\c@page\hbox{}#1\newpage\fi}

\newcommand{\cleartooddpage}[1][\@empty]{%
  \clearpage%
  \ifodd\c@page\else\hbox{}#1\clearpage\fi}

\newcommand{\movetooddpage}[1][\@empty]{%
  \newpage%
  \ifodd\c@page\else\hbox{}#1\newpage\fi}

\newcommand{\needspace}[1]{%
  \vskip \z@ \@plus #1\pagebreak[3]\vskip \z@ \@plus -#1\relax}

\newif\iflowernumtoname
  \lowernumtonamefalse

\newcommand{\numtoname}[1]{%
  \lowernumtonametrue%
  \numtoName{#1}%
  \lowernumtonamefalse}

\newcommand{\numtoName}[1]{%
  \ifnum #1<\@ne
  \else
    \ifnum #1<20\relax
        \ifcase #1\relax
         \or\nNamei\or\nNameii\or\nNameiii\or\nNameiv\or\nNamev\or
            \nNamevi\or\nNamevii\or\nNameviii\or\nNameix\or\nNamex\or
            \nNamexi\or\nNamexii\or\nNamexiii\or\nNamexiv\or\nNamexv\or
            \nNamexvi\or\nNamexvii\or\nNamexviii\or\nNamexix\fi
    \else
      \tenstoName{#1}%    % must have the first comment char
    \fi
  \fi}

\newcommand{\tenstoName}[1]{%
  \@tempcnta=#1\relax \@tempcntb=\m@ne
  \divide\@tempcnta by 10
  \ifcase\@tempcnta\or\or
    \nNamexx%
     \@tempcntb=#1\relax\advance\@tempcntb -20\relax\or
    \nNamexxx%
     \@tempcntb=#1\relax\advance\@tempcntb -30\relax\or
    \nNamexl%
     \@tempcntb=#1\relax\advance\@tempcntb -40\relax\or
    \nNamel%
     \@tempcntb=#1\relax\advance\@tempcntb -50\relax\or
    \nNamelx%
     \@tempcntb=#1\relax\advance\@tempcntb -60\relax\or
    \nNamelxx%
     \@tempcntb=#1\relax\advance\@tempcntb -70\relax\or
    \nNamelxxx%
     \@tempcntb=#1\relax\advance\@tempcntb -80\relax\or
    \nNamexc%
     \@tempcntb=#1\relax\advance\@tempcntb -90\relax\else
    \nNamec%
  \fi%
  \ifnum\@tempcntb>\z@
    \space
    \numtoName{\@tempcntb}%
  \fi}

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

\newinsert\sideins
  \skip\sideins=0pt
  \count\sideins=0

\newcommand{\setsidebarheight}[1]{\dimen\sideins= #1}
  \setsidebarheight{20\onelineskip}

\newlength{\sidebarhsep} \setlength{\sidebarhsep}{\marginparsep}
\newlength{\sidebarvsep} \setlength{\sidebarvsep}{2\onelineskip}
\newlength{\sidebarwidth} \setlength{\sidebarwidth}{\marginparwidth}

\newcommand{\sidebarfont}{\normalfont}

\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
       \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
     \fi
     \vtop to0pt{%
     \vskip\onelineskip
     \unvbox\sideins \vss}%
    }
}

\newcommand{\sidebar}[1]{%%%%\textbf{[sidebar]}%
  \insert\sideins{%
    \splittopskip\sidebarvsep\relax
    \hsize \sidebarwidth \rightskip=0pt plus 20pt \sidebarfont
    \noindent \vbox to \sidebarvsep{}\ignorespaces #1%
    \ifhmode\unskip\strut\fi\par
   }%
}

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

\let\memold@doclearpage\@doclearpage
\newcommand{\mem@doclearpage}{%
%%%%  \typeout{Redefining @doclearpage}
  \ifvoid\footins
    \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
}

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

\gdef\@makecol{\mem@makecol}

\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{\figurename}{Figure}
\newcommand{\tablename}{Table}
\newcommand{\figurerefname}{Figure}
\newcommand{\tablerefname}{Table}
\newcommand{\pagename}{page}
\newcommand{\pagerefname}{page}
\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}
\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}
\newcommand*\nNamexi{\iflowernumtoname e\else E\fi leven}
\newcommand*\nNamexii{\iflowernumtoname t\else T\fi welve}
\newcommand*\nNamexiii{\iflowernumtoname t\else T\fi hirteen}
\newcommand*\nNamexiv{\iflowernumtoname f\else F\fi ourteen}
\newcommand*\nNamexv{\iflowernumtoname f\else F\fi ifteen}
\newcommand*\nNamexvi{\iflowernumtoname s\else S\fi ixteen}
\newcommand*\nNamexvii{\iflowernumtoname s\else S\fi eventeen}
\newcommand*\nNamexviii{\iflowernumtoname e\else E\fi ighteen}
\newcommand*\nNamexix{\iflowernumtoname n\else N\fi ineteen}
\newcommand*\nNamexx{\iflowernumtoname t\else T\fi wenty}
\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}
\newcommand*\nNamelxxx{\iflowernumtoname e\else E\fi ighty}
\newcommand*\nNamexc{\iflowernumtoname n\else N\fi inety}
\newcommand*\nNamec{\iflowernumtoname h\else H\fi undred or more}

\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}
\setlength\columnsep{10\p@}
\setlength\columnseprule{0\p@}
\pagestyle{headings}
\pagenumbering{arabic}

\setcounter{part}{0}
\setcounter{chapter}{0}
\setcounter{tocdepth}{1}
\setcounter{secnumdepth}{2}

\if@twoside
\else
  \raggedbottom
\fi
\if@twocolumn
  \twocolumn
  \sloppy
  \flushbottom
\else
  \onecolumn
\fi

\newfloat[chapter]{figure}{lof}{\figurename}
\renewcommand{\thefigure}{\thechapter.\@arabic\c@figure}

\newlistof{listoffigures}{lof}{\listfigurename}
\newlistentry[chapter]{figure}{lof}{0}
  \cftsetindents{figure}{0em}{2.3em}
  \kill@lastcounter{lofdepth}

\newfloat[chapter]{table}{lot}{\tablename}
\renewcommand{\thetable}{\thechapter.\@arabic\c@table}

\newlistof{listoftables}{lot}{\listtablename}
\newlistentry[chapter]{table}{lot}{0}
  \cftsetindents{table}{0em}{2.3em}
  \kill@lastcounter{lotdepth}

\AtBeginDocument{%
  \@ifundefined{c@lofdepth}{\newcounter{lofdepth}\setcounter{lofdepth}{1}}{}
  \@ifundefined{c@lotdepth}{\newcounter{lotdepth}\setcounter{lotdepth}{1}}{}
}

\ifartopt
  \chapterstyle{article}
  \counterwithout{figure}{chapter}
  \counterwithout{table}{chapter}
  \counterwithout{footnote}{chapter}
  \renewcommand{\chaptername}{}
\fi

\newcommand{\msdoublespacing}{}
\newcommand{\mssinglespacing}{}
\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

\AtBeginDocument{%
  \@ifpackageloaded{natbib}{% natbib is loaded
    \renewcommand{\bibsection}{%
      \chapter*{\bibname}
      \bibmark
      \ifnobibintoc\else
        \phantomsection
        \addcontentsline{toc}{chapter}{\bibname}
      \fi
      \prebibhook}
    \addtodef{\endthebibliography}{}{\vskip-\lastskip\postbibhook}
}{}}

\endinput
%%
%% End of file `memoir.cls'.
