%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% LaTeX book setup for                                                 %%
%%   ``Introduction to Algorithms'' by Cormen, Leiserson and Rivest,    %%
%%   implementing design by Rebecca Daw, MIT Press                      %%
%% Adapted from clr.sty (Amy Hendrickson, TeXnology Inc., July 1988)    %%
%% by Michael Downes [June 1998]                                        %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% $RCSfile: clr.cls,v $ $Revision: 11.0 $ $Date: 2008-04-15 14:10:39 $
\ProvidesClass{clr}[1998/07/16 v1.10
  Setup for `Introduction to Algorithms']

\errorcontextlines=99 % easier debugging

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

% From CLRS 1/e, 2/e, 3/e.

%% %\textwidth=28pc					%% 1/e \textwidth
%% \textwidth=30pc						%% 2/e \textwidth
%% \textheight=49.5pc  				%% 1/e \textheight
%% \addtolength{\textheight}{-0.75in}	%% 2/e \textheight
%% \addtolength{\textheight}{-0.1in}   %% 3/e experimental \textheight
%% \oddsidemargin 95pt \evensidemargin 87pt
%% \topmargin 0pt
%% \headheight 0pt
%% \headsep 24pt

%% %\newdimen\textwidthextra \textwidthextra=7.5pc		%% 1/e \textwidthextra
%% \newdimen\textwidthextra \textwidthextra=5.5pc		%% 2/e \textwidthextra
%% \newdimen\typeblockwidth
%% \typeblockwidth=\textwidth \advance\typeblockwidth\textwidthextra

%% Dimensions for the Essential Knowledge series.
% \textwidth=22pc
% \textheight=32pc
% \oddsidemargin 4pc
% \evensidemargin 4pc
% \topmargin 3pc
% \headheight 0pt
% \headsep 24pt

%% Dimensions for a 6"x9" trim size.
\textwidth=26pc
\textheight=43pc
\oddsidemargin 5pc
\evensidemargin 5pc
\topmargin 3pc
\headheight 0pt
\headsep 24pt

%\newdimen\textwidthextra \textwidthextra=7.5pc		%% 1/e \textwidthextra
\newdimen\textwidthextra \textwidthextra=0pc		%% 2/e \textwidthextra
\newdimen\typeblockwidth
\typeblockwidth=\textwidth \advance\typeblockwidth\textwidthextra

% For the Adobe Times text fonts, scale up slightly from 10pt to better
% match the appearance of the Autologic Times fonts used in the
% previous edition.
\def\@xpt{10.95}\def\@xipt{12}
\def\@xviiipt{18}
\DeclareMathSizes{\@xpt}{\@xpt}{8}{6}
\DeclareMathSizes{\@xipt}{\@xipt}{9}{7}
\DeclareMathSizes{\@xviiipt}{\@xviiipt}{\@xivpt}{\@xpt}

\DeclareOption{cmfonts}{%
%    Using 17.28 here instead of 18 is the easiest way to avoid font
%    warnings about substitution of nearest accessible size.
  \def\@xviiipt{17.28}%
  \def\@xpt{10}\def\@xipt{10.95}%
  \let\font@setup\relax
}

% I won't be needing these.

%% \DeclareOption{cmmath}{%
%%   \def\font@setup{\RequirePackage[cmmath]{clrtimes}\relax}%
%% }

%% \DeclareOption{cmbold}{%
%%   \def\font@setup{\RequirePackage[cmbold]{clrtimes}\relax}%
%% }

\def\font@setup{\RequirePackage{clrtimes}\relax}

%\newdimen\hruleheight \hruleheight=.1pt
\newdimen\hruleheight \hruleheight=.5pt	% Yasuyo says at least 0.5 point

\voffset=-2pc
\raggedbottom

\@twosidetrue \@mparswitchtrue

\DeclareOption{draft}{\overfullrule 5pt}
\DeclareOption{final}{\overfullrule 0pt}

\lineskip 1pt \normallineskip 1pt \relax

\def\normalsize{\@setsize\normalsize{13pt}\xpt\@xpt
  \abovedisplayskip 10pt plus2pt minus5pt
  \belowdisplayskip \abovedisplayskip
  \abovedisplayshortskip \z@ plus3pt
  \belowdisplayshortskip 6pt plus3pt minus3pt\relax
}

\def\footnotesize{\@setsize\footnotesize{10pt}\viiipt\@viiipt
  \topsep=2pt plus 1pt minus1pt
  \abovedisplayskip 1pt plus 1pt minus 1pt
  \belowdisplayskip \abovedisplayskip
  \abovedisplayshortskip \z@ plus 1pt
  \belowdisplayshortskip \abovedisplayshortskip
  \def\@listi{\topsep 3pt plus 1pt minus 1pt\parsep 2pt plus 1pt minus 1pt
    \itemsep \parsep}%
}

\def\small{\@setsize\small{11pt}\ixpt\@ixpt
  \abovedisplayskip \z@ plus 3pt minus 3pt
  \belowdisplayskip \abovedisplayskip
  \abovedisplayshortskip \z@ plus2pt
  \belowdisplayshortskip\abovedisplayshortskip
  \def\@listi{\topsep 4pt plus 2pt minus 2pt\parsep 2pt plus 1pt minus 1pt
    \itemsep1pt}%
}

\def\scriptsize{\@setsize\scriptsize{8pt}\viipt\@viipt}
\def\tiny{\@setsize\tiny{6pt}\vpt\@vpt}

\def\semilarge{\@setsize\semilarge{13pt}\xipt\@xipt}
\def\large{\@setsize\large{14pt}\xiipt\@xiipt}
\def\Large{\@setsize\Large{18pt}\xivpt\@xivpt}
\def\LARGE{\@setsize\LARGE{22pt}\xviiipt\@xviiipt}
\def\huge{\@setsize\huge{25pt}\xxpt\@xxpt}
\def\Huge{\@setsize\Huge{30pt}\xxvpt\@xxvpt}

\ExecuteOptions{final}
\ProcessOptions\relax

\font@setup %

%    Some more font setup that is independent of the Times/CM switching.

\newcommand{\bit}{\fontseries\bfdefault\itshape}

\def\eighteenbf{\LARGE\bfseries}%
\def\eighteenrm{\LARGE\fontfamily\rmdefault \fontseries\mddefault\upshape}%
\def\elevenbf{\semilarge\bfseries}%
\def\elevenbit{\elevenbf\itshape}
\def\tenbit{\normalsize\fontseries\bfdefault\itshape}
\def\ninepoint{\small}
\def\eightpoint{\footnotesize}

\def\verysmstarfont{\usefont{OML}{cmm}{m}{it}}
\let\smstarfont=\verysmstarfont
\let\bigstarfont=\verysmstarfont

\RequirePackage{latexsym}[1995/01/01]

% Define the latex2.09 font commands like book.cls.
% Letting them produce an error message instead, if used inadvertently,
% seems a little too drastic.

\DeclareOldFontCommand{\rm}{\normalfont\rmfamily}{\mathrm}
\DeclareOldFontCommand{\sf}{\normalfont\sffamily}{\mathsf}
\DeclareOldFontCommand{\tt}{\normalfont\ttfamily}{\mathtt}
\DeclareOldFontCommand{\bf}{\normalfont\bfseries}{\mathbf}
\DeclareOldFontCommand{\it}{\normalfont\itshape}{\mathit}
\DeclareOldFontCommand{\sl}{\normalfont\slshape}{\@nomath\sl}
\DeclareOldFontCommand{\sc}{\normalfont\scshape}{\@nomath\sc}
\DeclareRobustCommand*\cal{\@fontswitch\relax\mathcal}
\DeclareRobustCommand*\mit{\@fontswitch\relax\mathnormal}

% Some fonts that were defined in the old clr.sty for LaTeX 2.09. Define
% them to give warnings rather than error messages, until various
% internal invocations of them can be tracked down and substituted.

\def\subfont#1{%
  \gdef#1{\typeout{Unhandled font `\protect#1' (line \the\inputlineno)}%
    \global\let#1\relax}%
}

\subfont\eighteenbit \subfont\twelverm \subfont\tenrm \subfont\tenit
\subfont\tenbf \subfont\tentt \subfont\tenpoint
\subfont\ninerm \subfont\nineit \subfont\ninebit
\subfont\ninebf \subfont\ninett
\subfont\eightrm \subfont\eightit \subfont\eightbf \subfont\eighttt

\normalsize

\marginparwidth .75in \marginparsep 7pt

\footskip.35in \relax

\columnsep 10pt \columnseprule 0pt

\footnotesep 13pt%
\skip\footins 24pt%
\floatsep 12pt plus 2pt minus 2pt \textfloatsep 18pt %was 20
plus 2pt minus 4pt
\intextsep 12pt plus 2pt minus 2pt \dblfloatsep 12pt plus 2pt
minus 2pt \dbltextfloatsep 20pt plus 2pt minus 4pt
\@fptop 0pt plus 1fil \@fpsep 8pt plus 2fil \@fpbot 0pt plus 1fil
\@dblfptop 0pt plus 1fil \@dblfpsep 8pt plus 2fil \@dblfpbot 0pt plus 1fil
\marginparpush 5pt

\parskip 0pt
\parindent 1em \topsep 8pt plus 2pt minus 4pt
\partopsep 2pt plus 1pt minus 1pt \itemsep1pt% 4pt plus 2pt minus 1pt
\@lowpenalty 51 \@medpenalty 151 \@highpenalty 301
\@beginparpenalty -\@lowpenalty \@endparpenalty -\@lowpenalty \@itempenalty
-\@lowpenalty

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

\newcommand{\partname}{Part}

\def\part{%
  \clearpage\thispagestyle{empty}%
  \ifodd\value{page}\thispagestyle{empty}\mbox{}\newpage\fi
  \secdef\@part\@spart
}

\def\@part[#1]#2{%
  \refstepcounter{part}%
  \thispagestyle{empty}%
  \addcontentsline{toc}{part}{{\thepart}{\boldmath#1}}%
  \markboth{}{}
  \outerline{\vbox to14.5pc{\hsize=\typeblockwidth\parindent=0pt\parskip=0pt
    \hrule height 6pt
    \vskip12pc
    \hrule height\hruleheight
    \vfill
    {\LARGE\bfseries\itshape\thepart\hskip1em#2}}}%
  \partintro{#2}%
}

\newcommand\partmark[1]{\markboth{#1}{#1}}

\newcommand{\partintro}[1]{\newpage
  \partmark{\partname~\thepart\quad#1}%
  \setcounter{equation}{0}%
  \addcontentsline{toc}{fmatter}{{}{Introduction}}%
  \protected@edef\@currentlabel{\thepart}%
  \thispagestyle{empty}%
  \@afterindentfalse
  \vspace*{12.5pc}%
  \section*{Introduction}\@ignoretrue
}

% \def\@makechapterhead#1{\vfill\newpage
%   \outerline{\vbox to12pc{\hsize=\typeblockwidth\hrule height 6pt
%   \vbox to 24pt{\hsize=\typeblockwidth\vfill\parindent=0pt\parskip=0pt
%   \ \unskip\hbox to\textwidthextra{\eighteenbf\thechapter\hfill}\eighteenbf#1}
%   \vfill}}}

\newlength{\chapnumwidth}
\setlength{\chapnumwidth}{2pc}
\newlength{\chapnamewidth}
\setlength{\chapnamewidth}{\textwidth}
\addtolength{\chapnamewidth}{-\chapnumwidth}

\def\@makechapterhead#1{\vfill\newpage
%  \outerline{\vbox to12pc{\hsize=\typeblockwidth\hrule height 4pt
  \outerline{\vbox to6pc{\hsize=\typeblockwidth\hrule height 4pt
  \vbox to 24pt{\hsize=\typeblockwidth\vfill\parindent=0pt\parskip=0pt
	\parbox[t]{\chapnumwidth}{\eighteenbf\boldmath\thechapter}\hyphenpenalty=10000\parbox[t]{\chapnamewidth}{\raggedright\eighteenbf#1}\hyphenpenalty=50}
	\vfill}}}

\def\@makechapterheadnonumber#1{\vfill\newpage
%  \outerline{\vbox to12pc{\hsize=\typeblockwidth\hrule height 4pt
  \outerline{\vbox to6pc{\hsize=\typeblockwidth\hrule height 4pt
  \vbox to 24pt{\hsize=\typeblockwidth\vfill\parindent=0pt\parskip=0pt
	\hyphenpenalty=10000\parbox[t]{\chapnamewidth}{\raggedright\eighteenbf#1}\hyphenpenalty=50}
	\vfill}}}

\def\@makeschapterhead#1{\matter{#1}}

\def\chapter{\newpage\thispagestyle{empty}%% was plain
  \let\thefigure\savethefigure \gdef\thesection {\thechapter.\arabic{section}}%
  \def\thechapter{\arabic{chapter}}\setcounter{equation}{0}%
  %\ifodd\count0\else\vfill\newpage\thispagestyle{empty}\fi
   \global\@topnum\z@\@afterindentfalse \secdef\@chapter\@schapter}

\newcommand{\appendix}{\setcounter{chapter}{0}}
\newif\ifinappendix
\inappendixfalse

\def\appchapter{\newpage\thispagestyle{empty}%% was plain
  \let\thefigure\savethefigure \gdef\thesection {\thechapter.\arabic{section}}%
  \def\thechapter{\Alph{chapter}}\setcounter{equation}{0}%
  %\ifodd\count0\else\vfill\newpage\thispagestyle{empty}\fi
   \global\@topnum\z@\@afterindentfalse \secdef\@appchapter\@schapter}

\def\@chapter[#1]#2{\refstepcounter{chapter}%
  \typeout{\@chapapp\space\thechapter.}%
  \addcontentsline{toc}{chapter}{{\thechapter}{\boldmath#1}}%
  \chaptermark{#2}\inappendixfalse%
  \if@twocolumn \@topnewpage[\@makechapterhead{#2}]%
  \else \@makechapterheadnonumber{#2}\@afterheading
  \fi
}

\def\@appchapter[#1]#2{\refstepcounter{chapter}%
  \typeout{\@chapapp\space\thechapter.}%
  \addcontentsline{toc}{chapter}{{\thechapter}{\boldmath#1}}%
  \appchaptermark{#2}\inappendixtrue%
  \if@twocolumn \@topnewpage[\@makechapterhead{#2}]%
  \else \@makechapterhead{#2}\@afterheading
  \fi
}

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

\def\add@tocsection#1#2#3{%
  \addcontentsline{toc}{#2}{%
    {\ifnum #1>\c@secnumdepth \else \csname the#2\endcsname \fi}{#3}}%
}

%    Redefine \@sect in order to replace wimpy \numberline mechanism
%    with \add@tocsection. [mjd,1998/07/07]

\def\@sect#1#2#3#4#5#6[#7]#8{%
  \ifnum #2>\c@secnumdepth
    \let\@svsec\@empty
  \else
    \refstepcounter{#1}%
    \protected@edef\@svsec{\@seccntformat{#1}\relax}%
  \fi
  \@tempskipa #5\relax
  \ifdim \@tempskipa>\z@
    \begingroup
      #6{%
        \@hangfrom{\hskip #3\relax\@svsec}%
          \interlinepenalty \@M #8\@@par}%
    \endgroup
    \csname #1mark\endcsname{#7}%
    \add@tocsection{#2}{#1}{#7}%
  \else
    \def\@svsechd{%
      #6{\hskip #3\relax
      \@svsec #8}%
      \csname #1mark\endcsname{#7}%
      \add@tocsection{#2}{#1}{#7}%
    }%
  \fi
  \@xsect{#5}}

% \@startsection {NAME}{LEVEL}{INDENT}{BEFORESKIP}{AFTERSKIP}{STYLE}
%
% NOTE: Negative beforeskip means use positive beforeskip value but don't
% indent first line after section heading.

\def\section{\global\@topnum\z@ \relax
  \@startsection {section}{1}{-\textwidthextra}{-16pt}{16pt}
  {\outerline{\vrule width\typeblockwidth height\hruleheight depth\z@}%
   \nobreak\semilarge\bfseries\boldmath}}

%% THC 8/6/99 commented out defns of \bigstar, \smstar, \verysmstar
%% that use \bigstarfont, \smstarfont, and \verysmstarfont because they
%% eventually use cmmi10 font.  Instead, by just using $\star$, we get
%% a MathTime font instead.

\chardef\starchar=63\relax
%\def\bigstar{{\protect\bigstarfont\starchar}}
%\def\smstar{{\protect\smstarfont\starchar}}
%\def\verysmstar{{\protect\verysmstarfont\starchar}}
\def\bigstar{{\Large$\star$}}
\def\smstar{$\star$}
\def\verysmstar{$\star$}

% \def\subsectionstar{%
%   \@startsection{subsection}{2}{\z@}
%     {-20pt}{14pt}{\smstar\hspace{3pt}\bfseries}}

\def\sectionstar#1{%
  \addpenalty\@highpenalty \addvspace{26pt}%
  \refstepcounter{section}%
  \markright{\thesection\protect\quad #1}%
  \outerline{%
    \vbox{\hsize\typeblockwidth
      \hrule height\hruleheight
      \vskip 8pt
      \noindent
      \bigstar\quad{\semilarge\bfseries\boldmath\thesection\quad #1}}%
  }%
  \vskip16pt\relax
  \@afterindentfalse \@afterheading%
  \addcontentsline{toc}{sectionstar}{{\thesection}{#1}}
}

\def\sectiondblstar#1{\refstepcounter{section}%
  \addpenalty\@highpenalty \addvspace{26pt}%
  \markright{\thesection\hskip1em #1}%
  \outerline{%
  \vbox{\hsize\typeblockwidth
  \hrule height\hruleheight
  \vskip 8pt
  \noindent
  \bigstar\bigstar\hskip1em{\semilarge\bfseries\boldmath\thesection\quad #1}}}
  \vskip16pt
  \@afterindentfalse\@afterheading%
  \addcontentsline{toc}{sectiondblstar}{{\thesection}{#1}}
}

%% THC changed AFTERSKIP param for subsection and subsubsection in
%% accordance with Julie's suggestion that the space below a subheading
%% should be noticeably less than the space above.
%\def\subsection{\@startsection{subsection}{2}{\z@}{-16pt}{14pt}{\bf}}
\def\subsection{\@startsection{subsection}{2}{\z@}{-16pt}{8pt}{\bf}}
\def\subsectionstar{\@startsection{subsection}{2}{\z@}{-16pt}{8pt}{\hspace*{-3em}\makebox[2em][l]{\bigstar}\bf}}

\def\subsubsection{\@startsection{subsubsection}{3}{\z@}{-16pt}%
%  {14pt}{\normalsize\bfseries}}
  {8pt}{\normalsize\bfseries\boldmath}}

\def\paragraph{\@startsection
  {paragraph}{4}{\z@}{-8pt}{1sp}{\normalsize\bit\boldmath}}
% \def\paragraph{\@startsection
%   {paragraph}{4}{\z@}{-16pt}{1sp}{\normalsize\bit\boldmath}}

\def\subparagraph{\@startsection
  {subparagraph}{4}{0pt}{16pt}{-1em}{\normalsize\bit}}

\setcounter{secnumdepth}{2}

%\leftmargini 1pc \leftmarginii 1pc
\leftmargini 1.25pc \leftmarginii 1pc
\leftmarginiii 18.7pt \leftmarginiv 17pt \leftmarginv 10pt
\leftmarginvi 10pt
\leftmargin\leftmargini
\labelsep 0.25pc
\labelwidth=\leftmargini\advance\labelwidth-\labelsep
%\labelsep 0pt \parsep 4pt plus 2pt minus 1pt
\parsep 4pt plus 2pt minus 1pt
\def\@listi{\leftmargin\leftmargini\itemsep1pt}
\def\@listii{\leftmargin\leftmarginii
  \labelsep=0pt
  \labelwidth\leftmarginii\advance\labelwidth-\labelsep
  \topsep 4pt plus 2pt minus 1pt
  \parsep 2pt plus 1pt minus 1pt
  \itemsep1pt\relax
}
\def\@listiii{\leftmargin\leftmarginiii
  \labelwidth\leftmarginiii\advance\labelwidth-\labelsep
  \topsep 2pt plus 1pt minus 1pt
  \parsep \z@ \partopsep 1pt plus 0pt minus 1pt
  \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}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Listing commands and changes

\def\descriptionlabel#1{\hspace\labelsep \textbf{#1}\enskip}
\def\description{\leftmarginii=1pc
\list{}{\labelsep=0pt\labelwidth\z@ \itemindent-1.25pc%\leftmargin
\let\makelabel\descriptionlabel}}
\let\enddescription\endlist

\def\descriptiontextlabel#1{\hspace\labelsep \textbf{#1}\ }
\def\descriptiontext{\leftmarginii=1pc
\list{}{\labelsep=0pt\labelwidth\z@ \itemindent-1.25pc%\leftmargin
\let\makelabel\descriptiontextlabel}}
\let\enddescriptiontext\endlist

% \newenvironment{letters}{%
%   \begin{enumerate}%
%   \letterstrue
%   \expandafter\edef\csname the\@enumctr\endcsname{%
%     \protect\alph{\@enumctr}}%
% }{%
%   \end{enumerate}%
% }

\RequirePackage{enumerate}

\newenvironment{letters}{\begin{enumerate}[a. ]}{\end{enumerate}}

\def\theenumi{\arabic{enumi}}
\let\arabicenumi=\theenumi

% \newif\ifprimed
% \primedfalse

% \newenvironment{enumerateprimed}{%
%   \begin{enumerate}\primedtrue%
% }{%
%   \end{enumerate}%
% }

\newcommand{\setenumi}[1]{\setcounter{enumi}{\numref{#1}}%
\addtocounter{enumi}{-1}}
\newcommand{\setenumiplus}[2]{\setcounter{enumi}{\numref{#1}}%
\addtocounter{enumi}{#2}\addtocounter{enumi}{-1}}

\def\labelenumi{\hbox to 1pc{%
  \ifletters\rlap{\hbox to1pc{\alph{enumi}.\hfill}}\global\def\@currentlabel{\alph{enumi}}\else
  \ifx\theenumi\arabicenumi
%    \hbox to.5em{\hss\theenumi}%
%     \rlap{\hbox to1pc{\arabic{enumi}\ifprimed$'$\fi.\hfill}}
     \rlap{\hbox to1pc{\arabic{enumi}.\hfill}}
    \else  %% Shouldn't happen
    \theenumi.\hfill\fi
%  \fi
%  .\hfill
  \fi
}}

\newif\ifletters
\def\labelenumii{%
  \ifletters\llap{\hbox to1pc{\alph{enumii}.\hfill}}\global\def\@currentlabel{\alph{enumii}}%
  \else
    \llap{\settowidth\labelwidth{0.}%
      \hbox to\labelwidth{\arabic{enumii}.\hfill}}%
  \fi
}

\def\theenumii{\alph{enumii}}
\def\p@enumii{\theenumi}
\def\labelenumiii{\roman{enumiii}.}
\def\theenumiii{\roman{enumiii}}
%\def\p@enumiii{\theenumi(\theenumii)}
\def\p@enumiii{\theenumi\theenumii}
%\def\labelenumiv{\Alph{enumiv}.}
%\def\theenumiv{\Alph{enumiv}}
%\def\p@enumiv{\p@enumiii\theenumiii}

\def\labelitemi{\raise.5ex\hbox to1pc{$\scriptscriptstyle\bullet$\hfill}}
\def\labelitemii{\raise.5ex
\llap{\hbox to 1pc{$\scriptscriptstyle\bullet$\hss}}}
\def\labelitemiii{$\ast$}
%\def\labelitemiv{$\cdot$}

\def\verse{\let\\=\@centercr
 \list{}{\itemsep\z@ \itemindent -1.5em\listparindent \itemindent
 \rightmargin\leftmargin\advance\leftmargin 1.5em}\item[]}
\let\endverse\endlist

\def\quotation{\list{}{\listparindent 1.5em
 \itemindent\listparindent
 \rightmargin\leftmargin\parsep 0pt plus 1pt}\item[]}
\let\endquotation=\endlist
\def\quote{\list{}{\rightmargin\leftmargin}\item[]}
\let\endquote=\endlist

\RequirePackage[gray]{xcolor}

\definecolor{procbackground}{gray}{0.85}
\newlength{\procwidth}
\setlength{\procwidth}{\textwidth}
\addtolength{\procwidth}{-6pt}
\newenvironment{procbox}{%
   \vspace{2ex}\noindent\begin{lrbox}{\@tempboxa}\begin{minipage}{\procwidth}\raggedright}{\end{minipage}\end{lrbox}\vspace{2ex}%
   \colorbox{procbackground}{\usebox{\@tempboxa}}}
\newenvironment{procboxcont}{\begin{procbox}\vspace*{1.25ex}}{\end{procbox}}
\newcommand{\shade}[1]{\hspace*{-1em}\colorbox{procbackground}{#1}\hspace*{-1em}}
\newcommand{\cshade}[1]{\colorbox{procbackground}{\hspace*{-0.3em}\vspace*{-0.2ex}#1\vspace*{-0.2ex}\hspace*{-0.3em}}}

\newlength{\IORspace}
\setlength{\IORspace}{0.75ex}
\newcommand{\Proc}[2]{\indexproc{#1}\index{#1}\textit{Procedure\/} $\proc{#1}{#2}$}
\newcommand{\Input}{\vspace{\IORspace}\par \textit{Input:}\ }
\newcommand{\Inputs}{\vspace{\IORspace}\par \textit{Inputs:}\ }
\newcommand{\Output}{\vspace{\IORspace}\par \textit{Output:}\ }
\newcommand{\InputOutput}{\vspace{\IORspace}\par \textit{Input and Output:}\ }
\newcommand{\InputsOutput}{\vspace{\IORspace}\par \textit{Inputs and Output:}\ }
\newcommand{\Result}{\vspace{\IORspace}\par \textit{Result:}\ }
\newcommand{\InputResult}{\vspace{\IORspace}\par \textit{Input and Result:}\ }
\newcommand{\InputsResult}{\vspace{\IORspace}\par \textit{Inputs and Result:}\ }

\newenvironment{procitemize}{\begin{itemize}\setlength{\itemsep}{-0.8ex}\vspace{-1.5ex}}{\end{itemize}\vspace{-1.5ex}}
\newcommand{\itemx}[1]{\item[\textnormal{#1:}] \mbox{}}

\newenvironment{proci}{\begin{enumerate}[1. ]\setlength{\itemsep}{-0.6ex}\vspace{-1ex}}{\end{enumerate}\vspace{-1.5ex}}
\newenvironment{procii}{\begin{enumerate}[A. ]\setlength{\itemsep}{-0.4ex}\vspace{-1.5ex}}{\end{enumerate}\vspace{-0.8ex}}
\newenvironment{prociii}{\begin{enumerate}[i. ]\setlength{\itemsep}{0.2ex}\vspace{-0.8ex}}{\end{enumerate}\vspace{-0.4ex}}
\newenvironment{prociv}{\begin{enumerate}[a. ]\setlength{\itemsep}{0.2ex}\vspace{-0.6ex}}{\end{enumerate}\vspace{-0.4ex}}

\newif\ifsteparg
\stepargtrue

\def\step{
	\@ifnextchar [\@step{\stepargfalse \@step[]}}

\def\@step[#1]{%
	\item %
	\ifsteparg \label{st:#1} \fi
	\stepargtrue
}

%% Environments for loop invariants.
\newenvironment{loopinv}{\begin{quote}}{\end{quote}}
\newenvironment{loopinvproof}{\begin{description}}{\end{description}}
\newcommand{\loopinvinit}{\item[Initialization:] }
\newcommand{\loopinvmaint}{\item[Maintenance:] }
\newcommand{\loopinvterm}{\item[Termination:] }

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

\arraycolsep 5pt \tabcolsep 6pt \arrayrulewidth .4pt \doublerulesep 2pt
\tabbingsep=5pt% \labelsep
\skip\@mpfootins = \skip\footins
\fboxsep = 3pt \fboxrule = .4pt

\newcounter {part}
\newcounter {chapter}
\newcounter {section}[chapter]
\newcounter {subsection}[section]
%\newcounter {subsubsection}[subsection]
%\newcounter {paragraph}[subsubsection]
%\newcounter {subparagraph}[paragraph]

\def\thepart {\Roman{part}}
\def\thechapter {\arabic{chapter}}
\def\thesection {\thechapter.\arabic{section}}
\def\thesubsection {\thesection.\arabic{subsection}}
%\def\thesubsubsection {\thesubsection .\arabic{subsubsection}}
%\def\theparagraph {\thesubsubsection.\arabic{paragraph}}
%\def\thesubparagraph {\theparagraph.\arabic{subparagraph}}
\def\@chapapp{Chapter}


\def\@pnumwidth{1.55em}
%\def\@tocrmarg {2.55em}
%\def\@dotsep{4.5}
\setcounter{tocdepth}{2}

\def\outerline#1{%
  \hbox to\textwidth{%
    \hbox to0pt{\hss\hbox to \textwidthextra{#1\hss}}%
    \hfill
  }%
}

\def\matter#1{%
  \thispagestyle{empty}%
  \vbox to 12pc{\outerline{\hbox to \typeblockwidth{\eighteenrm\boldmath #1\hfill}}\vfill}
  \noindent\ignorespaces}

\def\preface{\vfill\newpage
  \def\currentfile{preface}
  \ifodd\value{page}\else\ \vfill\thispagestyle{empty}\newpage\fi
% THC's attempt to get preface into TOC
  \addcontentsline{toc}{fmatter}{{}{Preface}}%
  \@mkboth{Preface}{Preface}%
  \thispagestyle{empty}%
%  \vbox to 12pc{\outerline{\hbox to \typeblockwidth{\eighteenrm Preface\hfill}}%
%  \vfill}%
	\@makechapterheadnonumber{Preface}
  \ignorespaces
}

% Need this so that - doesn't turn into -_ in a url
\disablesubscriptcorrection

%% Now we can use the URL package in 3/e!
\RequirePackage{url}
\urlstyle{rm}

% But I want subscripcorrection everywhere else
\enablesubscriptcorrection

\def\thebibliography#1{%
  \addtocontents{toc}{\protect\tocadjust{\protect\partrule}}%
  \addcontentsline{toc}{fmatter}{{}{Bibliography}}%
% \@mkboth{Bibliography}{Bibliography}\matter{Bibliography}\list
 \@mkboth{Bibliography}{Bibliography}\thispagestyle{empty}\@makechapterheadnonumber{Bibliography}\list
 {\hbox to1pc{\hss[\arabic{enumi}]\ }}{\settowidth\labelwidth{[#1\ ]}\leftmargin\labelwidth
 \advance\leftmargin by 5pt
 \usecounter{enumi}}
% Use 9pt type to reduce the bad line breaks.
 \ninepoint
 \def\newblock{\hskip .11em plus .33em minus -.07em}
% CEL says "Let's run the bibliography without \sloppy ..."
% \sloppy
 \sfcode`\.=1000\relax}
\let\endthebibliography=\endlist
%\def\endthebibliography{\endlist\egroup}


\newif\if@restonecol
\def\theindex{\vfill\eject%
\ifodd\count0\else\thispagestyle{empty}\ \vfil\newpage\fi
\@restonecoltrue\if@twocolumn\@restonecolfalse\fi
\columnseprule \z@
% Change made by THC to use a little bit more of the available space
%	in the index.
%\columnsep 35pt\twocolumn[\matter{Index}
%\columnsep 18pt\twocolumn[\matter{Index}
\columnsep 18pt\twocolumn[\thispagestyle{empty}\@makechapterheadnonumber{Index}

% This index uses the following conventions.  Numbers are alphabetized
% as if spelled out; for example, ``2-3-4 tree'' is indexed as if it
% were ``two-three-four tree.''  When an entry refers to a place other
% than the main text, the page number is followed by a tag: ex.\ for
% exercise, pr.\ for problem, fig.\ for figure, and n.\ for footnote.  A
% tagged page number often indicates the first page of an exercise or
% problem, which is not necessarily the page on which the reference
% actually appears.
\vskip12pt %change this skip if you'd like
]
 \addcontentsline{toc}{fmatter}{{}{Index}}%
% Change by THC to get no page number in first page of Index.
% \@mkboth{Index}{Index}\thispagestyle{plain}\parindent\z@
 \@mkboth{Index}{Index}\thispagestyle{empty}\parindent\z@
 \parskip\z@ plus .3pt\relax\let\item\@idxitem%
% Change by THC to use \ninepoint.
\bgroup\ninepoint%
% Attempt to get hyphenation just the way we want it...
\finalhyphendemerits=0
% Change by THC to prevent overfull hboxes.
\sloppy\bgroup\raggedright}
% Changes by THC to turn 20pt into 9pt (about an em-space).
%\def\@idxitem{\par\hangindent 40pt}
%\def\subitem{\par\hangindent 40pt \hspace*{20pt}}
%\def\subsubitem{\par\hangindent 40pt \hspace*{30pt}}
%\def\indexspace{\par \vskip 10pt plus 5pt minus 3pt\relax}
\def\@idxitem{\par\hangindent 18pt}
\def\subitem{\par\hangindent 18pt \hspace*{9pt}}
\def\subsubitem{\par\hangindent 18pt \hspace*{13.5pt}}
\def\endtheindex{\if@restonecol\onecolumn\else\clearpage\fi\egroup\egroup\thispagestyle{empty}\fussy}
\def\indexspace{\par \vskip 9pt plus 5pt minus 3pt\relax}





\setcounter{topnumber}{3}%
%\def\topfraction{.7}%
%\def\topfraction{.75}%
\def\topfraction{0.85}
\setcounter{bottomnumber}{1}
\def\bottomfraction{.45}
\setcounter{totalnumber}{5}%
\def\textfraction{.1}% was .2

%Redefined by THC 1/7/89 as an experiment.
%\def\floatpagefraction{.5}
%\def\floatpagefraction{.7}% was .7
\def\floatpagefraction{.8}% was .7
\setcounter{dbltopnumber}{2}
\def\dbltopfraction{.7}
\def\dblfloatpagefraction{.5}


\newcounter{figure}[chapter]
\def\thefigure{\thechapter.\@arabic\c@figure}
\def\theprobfigure{\thepart.\@arabic\c@figure}
\def\fps@figure{tbp}
\def\ftype@figure{1}
\def\ext@figure{lof}
\def\fnum@figure{Figure \thefigure}
\def\figure{\@float{figure}}
\let\endfigure\end@float
\@namedef{figure*}{\@dblfloat{figure}}
\@namedef{endfigure*}{\end@dblfloat}
\newcounter{table}[chapter]
\def\thetable{\thechapter.\@arabic\c@table}
\def\fps@table{tbp}
\def\ftype@table{2}
\def\ext@table{lot}
\def\fnum@table{Table \thetable}

\def\tableline{\noalign{\vskip6pt\hrule\vskip6pt}}
\def\savecaption{}


\long\def\@makecaption#1#2{\iftableon
\gdef\savecaption{\noindent\ninepoint\bf#1\hskip1em\rm#2}\else
\vskip23pt\noindent\ninepoint\bf#1\hskip1em\rm#2\fi}

\long\def\@caption#1[#2]#3{%\addcontentsline{\csname
%  ext@#1\endcsname}{#1}{\protect\numberline{\csname
%  the#1\endcsname}{\ignorespaces #2}}\par
%% above, dont send things to aux file, save memory
  \begingroup
    \@parboxrestore
    \normalsize
    \@makecaption{\csname fnum@#1\endcsname}{\ignorespaces #3}\par
  \endgroup}

\newif\ifinmath
\newif\ifreallyinmath
\def\@tabular{\leavevmode \hbox \bgroup \ifinmath\reallyinmathtrue\fi$
\inmathfalse\ifreallyinmath\inmathtrue\fi\let\@acol\@tabacol
   \let\@classz\@tabclassz
   \let\@classiv\@tabclassiv \let\\\@tabularcr\@tabarray}


\newif\ifvertline
\def\verticallines{\global\vertlinetrue}

\def\@array[#1]#2{\setbox\@arstrutbox=\hbox{\vrule
     height\arraystretch \ht\strutbox
     depth\arraystretch \dp\strutbox
     width\z@}\@mkpream{#2}\edef\@preamble{\halign \noexpand\@halignto
\bgroup\ifinmath\hskip-\arraycolsep\fi%<<<<===========oct 6 tab
\tabskip\z@\@arstrut\@preamble\ifinmath\hskip-\arraycolsep\else%
\ifvertline\else\hskip-\tabcolsep\fi%% Feb. 20, 1990
\fi\tabskip\z@\cr}%
\let\@startpbox\@@startpbox \let\@endpbox\@@endpbox%
  \if #1t\vtop \else \if#1b\vbox \else \vcenter \fi\fi%
  \bgroup\let\par\relax%
  \let\@sharp##\let\protect\relax \lineskip\z@\baselineskip\z@\@preamble}

\def\@tabclassz{\ifcase \@lastchclass\@acolampacol%
\or \@ampacol \or
   \or \or \@addamp \or \@acolampacol\or \@firstampfalse \new@tabacol \fi%
\edef\@preamble{\@preamble%
  \ifcase \@chnum%
     \hfil\ignorespaces\@sharp\unskip\hfil%
     \or \ignorespaces\@sharp\unskip\hfil%
     \or \hfil\hskip\z@ \ignorespaces\@sharp\unskip\fi}}

\def\@tabclassiv{\@addtopreamble\@nextchar}

\newif\ifbigtablefonts
\def\normaltype{\global\bigtablefontstrue}

\def\tabular{\vskip6pt\def\@halignto{}%
\setbox0=\hbox\bgroup
\ifbigtablefonts\global\bigtablefontsfalse\else\ninepoint\fi\@tabular}

\def\endtabular{\crcr\egroup\egroup$\egroup\egroup%
\hbox to\hsize{\hss\unhbox0\hfill}
\iftableon\else\vskip6pt\fi\global\vertlinefalse\global\reallyinmathfalse\inmathfalse\relax}

\def\new@tabacol{\edef\@preamble{\@preamble \hskip0pt% \tabcolsep
}}

\newif\iftableon
\newbox\ahtablebox
\newdimen\widesttablebox


\def\@mkpream#1{\@firstamptrue\@lastchclass6
\def\@preamble{}\def\protect{\noexpand\protect\noexpand}\let\@sharp\relax
\let\@startpbox\relax\let\@endpbox\relax
\@expast{#1}\expandafter\@tfor \expandafter
  \@nextchar \expandafter:\expandafter=\reserved@a\do{\@testpach\@nextchar
  \ifcase \@chclass \@classz \or \@classi \or \@classii \or \@classiii
    \or \@classiv \or\@classv \fi\@lastchclass\@chclass}%
\ifcase \@lastchclass \@acol
    \or \or \@preamerr \@ne\or \@preamerr \tw@\or \or \@acol \fi}


\def\table{%
\global\tableontrue\@float{table}\setbox\ahtablebox\vbox\bgroup\everymath{}}

\def\endtable{\egroup\hbox to\textwidth{\ifdim\widesttablebox>\textwidth\hss\fi
\vbox{\ifx\savecaption\empty\else%
\ifdim\widesttablebox>\textwidth\hbox to\typeblockwidth{%
\hfill\vbox{\hsize=\textwidth\noindent\savecaption}}
\else\noindent\savecaption\fi\fi
\ifvoid\ahtablebox\else\vskip6pt
\ifdim\widesttablebox>\textwidth
\hbox to\typeblockwidth{\vbox{\hrule\hsize=\typeblockwidth%
\hskip\tabcolsep\unvbox\ahtablebox\vskip3pt
\hrule}\hss}\else
\vbox{\hrule width \widesttablebox \vskip-3pt
\unvbox\ahtablebox
\vskip3pt
\hrule width \widesttablebox
}\fi\fi}\hfill}\end@float\global\widesttablebox=0pt
\global\tableonfalse\gdef\savecaption{}\global\setbox\ahtablebox=\hbox{}}


\@namedef{table*}{\@dblfloat{table}}
\@namedef{endtable*}{\end@dblfloat}


\mark{{}{}}

\newif\ifturnoffsectnum
\newif\iffirstchapprob
\newif\iftocon

\newif\ifbottomrunninghead

\def\ps@headings{}% Just run the definitions directly

\let\@mkboth\markboth
\ifbottomrunninghead
  \def\@oddfoot{\vbox to0pt{\hbox to \textwidth{{\ninepoint\itshape
    \rightmark\hfill\thepage}}\vss}}%
  \def\@evenfoot{\vbox to 0pt{\hbox to \textwidth{%
    \llap{\hbox to\textwidthextra{\ninepoint\itshape
    \thepage\hfill}}%
    {\ninepoint\itshape
    \leftmark}%
    \hfill}\vss}}%
  \def\@evenhead{}\def\@oddhead{}%
\else
  \def\@oddhead{\vbox to0pt{\vss\hbox to \textwidth{{\ninepoint\itshape
    \rightmark\hfill\thepage}}}}%
%   \def\@evenhead{\vbox to 0pt{\vss\hbox to \textwidth{%
%     \llap{\hbox to\textwidthextra{\ninepoint\itshape
%     \thepage\hfill}}%
%     {\ninepoint\itshape \leftmark}%
%     \hfill}}}%
  \def\@evenhead{\vbox to 0pt{\vss\hbox to \textwidth{%
    {\ninepoint\itshape\thepage\hspace{1.5em}\leftmark}%
    \hfill}}}%
  \def\@oddfoot{}\def\@evenfoot{}%
\fi

\def\chaptermark#1{%
  \markboth{Chapter \thechapter\protect: #1}%
           {Chapter \thechapter\protect: #1}}%

\def\appchaptermark#1{%
  \markboth{Appendix \thechapter\protect: #1}%
           {Appendix \thechapter\protect: #1}}%

\def\sectionmark#1{\markright{\thesection\protect\quad#1}}%

\onecolumn

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% More new macros

\def\booktitle{\newpage\thispagestyle{empty}\vbox to \textheight{%
  \vbox to0pt{\hbox to\textwidth{\hbox to0pt{\hss\hbox to \textwidthextra{\hbox
  to \typeblockwidth{\vtop{\hsize=\typeblockwidth \parindent=0pt
  \hrule height 6pt
  \vskip1sp
  \baselineskip16pt
  \parskip0pt\relax
  \large
  \vrule width 0pt height 16pt \\%  Thomas H. Cormen\\
%% %	\textit{Dartmouth College}\medskip
%% %
%%   Charles E. Leiserson\\
%% %	\textit{Massachusetts Institute of Technology}\medskip
%% %
%%   Ronald L. Rivest\\
%% %	\textit{Massachusetts Institute of Technology}\medskip
%% %
%%   Clifford Stein\\
%% %	\textit{Dartmouth College}
	}}\hss}}\hfill}\vss}

  \vskip10pc
%  \hrule height\hruleheight
%   \vbox to 24pt{\parindent=0pt\vfill
%   {\LARGE\textbf{Introduction to Algorithms}}}
  \vbox to 45pt{\parindent=0pt\vfill
  {\LARGE\textbf{Algorithms Unlocked}}\\[4pc]
  {\Large\textit{\bfseries Thomas H. Cormen}}} %\\[0.4pc] Dartmouth College}}}
  \vfill
  {\obeylines\parskip=0pt
  \parindent=0pt
  \normalsize The MIT Press

  Cambridge, Massachusetts\hspace*{1em}London, England

%   \vrule width 0pt height 20.5pt McGraw-Hill Book Company

% 	Boston \hfill
% 	Burr Ridge,~IL \hfill
% 	Dubuque,~IA \hfill
% 	Madison,~WI \\%\hfill
% 	New~York \hfill
% 	San~Francisco \hfill
% 	St.~Louis \hfill
% % 	Bangkok \hfill
% % 	Bogot\'{a} \hfill
% % 	Caracas \hfill
% % 	Kuala~Lampur \hfill
% % 	Lisbon \\
% % 	London \hfill
% % 	Madrid \hfill
% % 	Mexico~City \hfill
% % 	Milan \hfill
% 	Montr\'{e}al \hfill
% % 	New~Delhi \\
% % 	Santiago \hfill
% % 	Seoul \hfill
% % 	Singapore \hfill
% % 	Sydney \hfill
% % 	Taipei \hfill
% 	Toronto
% %  New York\hspace*{1em}St.\ Louis\hspace*{1em}San Francisco\hspace*{1em}Montreal\hspace*{1em}Toronto%
}}\newpage}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Macros for table of contents

% To make a page in the table of contents run longer than normal,
% use
%
%    \addtocontents{toc}{\protect\tocadjust{\protect\enlargethispage*{...}}}
%
% where ... is the desired amount of extra length

\setcounter{tocdepth}{1}% ignore toc entries below \section level.
  
\newcommand{\newtocpage}{%
  \addtocontents{toc}{\protect\tocadjust{\protect\pagebreak}}}

\newcommand{\tocadjust}[1]{#1}

% Redefine \contentsline to remove unwanted extra braces around arg #2.
\renewcommand{\contentsline}[3]{\csname l@#1\endcsname#2{#3}}

\newcommand{\tocendpart}{\par}
\newcommand{\tocendchapter}{\par}

\newdimen\tocindent
\AtBeginDocument{%
  % Maximum width section number is two-digit chapter + one-digit
  % section number
%  \settowidth{\tocindent}{\hskip2pc \semilarge 00.0\enskip}%
  \settowidth{\tocindent}{\hskip2pc}%
}

\newcommand{\partrule}{\par
  \noindent\rule{\textwidth}{\hruleheight}\par\nobreak
}

\newcommand{\l@part}[3]{% #1 number #2 title #3 page
  \tocendchapter
  \tocendpart
  {\par}\penalty-9999
  \vskip 3ex plus2ex\relax
  \moveleft\textwidthextra\vbox{\hsize\typeblockwidth
    \semilarge\bfseries\itshape
    \noindent\rule{\hsize}{2pt}\par
    \noindent#1\quad#2\par
  }%
  \nobreak
  \vskip 2ex\relax
  \hrule height\hruleheight
  \nobreak
  \gdef\chapterbreak{\nobreak}%
}

\newcommand{\l@chapter}[3]{%
  \tocendchapter
  \addvspace{4pt}%
  \begingroup \@setpar{{\@@par}}%
  \interlinepenalty\@M
  \def\tocendchapter{\par \chapterbreak \endgroup }%
  \hangindent\tocindent
  \noindent
%  {\bfseries\hbox to\leftmargini{#1\hss}#2\quad{\itshape#3}}%
  {\bfseries\hbox to2pc{#1\hss}#2\quad{\itshape#3}}%
  % Don't allow a break before a chapter heading until a numbered
  % chapter comes along.
  \def\@tempa{#1}%
  \ifx\@empty\@tempa \else\gdef\chapterbreak{\penalty -99 }\fi
}

\newcommand{\l@section}[3]{%
%  \newline\rlap{\hbox to\leftmarginii{#1\hss}#2\quad{\itshape #3}}%
%  \newline\hangindent\tocindent\hbox to\leftmarginii{#1\hss}#2\quad{\itshape #3}
%   \newline\llap{\hbox to\leftmarginii{#1\hss}}#2\quad{\itshape #3}
   \newline\llap{\hbox to\leftmarginii{#1\hss}}\quad#2\quad{\itshape #3}
}

\newcommand{\l@sectionstar}[3]{%
%  \newline\llap{\hbox to\leftmargini{\bigstar\hss}}%
%    \rlap{\hbox to\leftmarginii{#1\hss}#2\quad{\itshape #3}}%
%   \newline\llap{\hbox to\leftmargini{\bigstar\hss}}\hangindent\tocindent\hbox to\leftmarginii{#1\hss}#2\quad{\itshape #3}
   \newline\hspace*{-\leftmarginii}\llap{\hbox to\leftmargini{\bigstar\hss}}\hangindent\tocindent\hbox to\leftmarginii{#1\hss}#2\quad{\itshape #3}
}

% This is the same as \l@chapter but for some extra space preceding the
% page number.
\newcommand{\l@fmatter}[3]{\l@chapter{#1}{#2}{\quad#3}}

\newcommand*\l@subsection[3]{\ignorespaces}
\newcommand*\l@subsubsection[3]{\ignorespaces}
\newcommand*\l@paragraph[3]{\ignorespaces}
\newcommand*\l@subparagraph[3]{\ignorespaces}

% Redefine \@starttoc to add \opentocfile functionality, and to close
% chapter/part before the \endgroup.

\def\@starttoc#1{%
  \begingroup
    \raggedright
    \makeatletter
    \@input{\jobname.#1}%
    \@nobreakfalse
    \tocendchapter \tocendpart
  \endgroup
  \opentocfile
}

\newcommand{\opentocfile}{%
  \if@filesw
    \expandafter\newwrite\csname tf@toc\endcsname
    \immediate\openout \csname tf@toc\endcsname \jobname.toc\relax
  \fi
  \global\let\opentocfile\relax
}

\def\tableofcontents{\cleardoublepage
  \thispagestyle{empty}%
  \@mkboth{Contents}{Contents}%
%  \outerline{\vbox to 11pc{\hsize =\typeblockwidth\noindent
%   \outerline{\vbox to 6pc{\hsize =\typeblockwidth\noindent
%   {\LARGE Contents}%
%   \vfill
%   }}%
  \@makechapterheadnonumber{Contents}
  \begingroup
  \parskip\z@skip \baselineskip=13pt\relax % 10/13
  \leftmargini=2pc
  \leftmarginii=\tocindent \advance\leftmarginii-\leftmargini
  \gdef\chapterbreak{\nobreak}%
  \@starttoc{toc}%
  \tocendchapter
  \tocendpart
  \endgroup
  \clearpage
}

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

%% Julie has threatened me with bodily harm if I don't make the footnote
%% type larger than footnote size.  So I've taken the \footnotetext
%% definition from latex.ltx and replaced '\footnotesize' on the second
%% line by '\small'.  May the LaTeX gods forgive me...THC.
\long\def\@footnotetext#1{\insert\footins{%
    \reset@font\small
    \interlinepenalty\interfootnotelinepenalty
    \splittopskip\footnotesep
    \splitmaxdepth \dp\strutbox \floatingpenalty \@MM
    \hsize\columnwidth \@parboxrestore
    \protected@edef\@currentlabel{%
       \csname p@footnote\endcsname\@thefnmark
    }%
    \color@begingroup
      \@makefntext{%
        \rule\z@\footnotesep\ignorespaces#1\@finalstrut\strutbox}%
    \color@endgroup}}%

\def\footnoterule{\kern-3\p@
 \hrule width \textwidth height\hruleheight
 \kern 2.6\p@}
\@addtoreset{footnote}{chapter}
\@addtoreset{footnote}{part}
\long\def\@makefntext#1{%\parindent 1em
\noindent%\hbox to 1.8em{\hss
$^{\@thefnmark}$%}
#1}

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

\widowpenalty=10000
\clubpenalty=10000

\newif\if@mainmatter
\providecommand{\frontmatter}{\pagenumbering{roman}\@mainmatterfalse}
\providecommand{\mainmatter}{%
\ifodd\value{page}\else\clearpage\thispagestyle{empty}\mbox{}\newpage\fi
\pagenumbering{arabic}%
%% \input{pagei} % From CLRS
%  \addtocontents{toc}{\protect\tocadjust{\protect\partrule}}%
}
\providecommand{\backmatter}{\cleardoublepage\@mainmatterfalse
%  \addtocontents{toc}{\protect\tocadjust{\protect\partrule}}%
}

\renewcommand{\cleardoublepage}{\clearpage
  \ifodd\c@page \else \thispagestyle{empty}\hbox{}\newpage\fi
}


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

\def\proofsketch{%
  \vskip-\lastskip\vskip-4pt\vskip1sp
  \subparagraph{Sketch of proof}%
}

\newif\ifqed
\newif\ifmathqed

\newenvironment{proof}{%
  \par\vskip-\lastskip\vskip-4pt\vskip1sp\subparagraph{Proof}%
  \global\qedtrue \global\mathqedfalse
}{%
  \ifqed \qed \fi
  \global\qedtrue \global\mathqedfalse
}

\newcommand{\qedsymbol}{\vrule height 5pt width 5pt\relax}

\newcommand{\qed}{\relax
  \ifmmode
    \global\mathqedtrue \global\qedtrue
  \else\ifineqnarrayreason
    \global\mathqedtrue \global\qedtrue
  \else
    \global\qedfalse
    \begingroup
      \ifhmode\unskip\parfillskip=\z@skip\nobreak\quad\fi
      \hfill\llap{\qedsymbol}\par
      \vskip11pt\relax
      \relax
    \endgroup
  \fi\fi
}

\let\inmathqed=\qed
\let\thqed=\qed

% \renewcommand this for horizontal/vertical struts within tables.
\newcommand{\tbox}{\makebox[0em]{\rule[0ex]{0em}{0ex}}}

\def\@makethmnumber#1#2{#1 #2}
\def\@begintheorem#1#2{\let\qed\thqed
  \vskip-\lastskip\vskip-4pt\vskip1sp\relax
  \paragraph{#1\ #2}}

\def\@endtheorem{\vskip11pt\relax}

\def\@opargbegintheorem#1#2#3{%
  \vskip-\lastskip\vskip-4pt\vskip1sp\relax
  \paragraph{#1\ #2\ (#3)}}

% NEEDS WORK
\everymath{\inmathtrue}

\@addtoreset {equation}{chapter}
\renewcommand\theequation
  {\ifnum \c@chapter>\z@ \thechapter.\fi \@arabic\c@equation}

\newcommand{\eqnref}[1]{(\protect\ref{#1})}

\def\@@eqncr{\let\reserved@a\relax
    \ifcase\@eqcnt \def\reserved@a{& & &}\or \def\reserved@a{& &}%
     \or \def\reserved@a{&}\else
       \let\reserved@a\@empty
       \@latex@error{Too many columns in eqnarray environment}\@ehc\fi
     \reserved@a \if@eqnsw\@eqnnum\stepcounter{equation}\fi
		\ifmathqed\qedsymbol\fi
     \global\@eqnswtrue\global\@eqcnt\z@\cr}

\def\@eqnnum{\textnormal{(\theequation)%
  \ifmathqed \global\qedfalse \global\mathqedfalse
    \llap{\lower14pt\hbox{\qedsymbol}}%
  \fi}}

\def\[{\relax\global\inmathtrue%
  \ifmmode\@badmath\else\begin{trivlist}\item[]\leavevmode
 \hbox to\linewidth\bgroup$ \displaystyle
 \hskip\mathindent\bgroup\fi}

\def\]{\relax
  \ifmmode
    \egroup $\hfil
    \ifmathqed \global\qedfalse \global\mathqedfalse
      \displaywidth\linewidth
      \hfill\hbox{\qedsymbol}%
    \fi
    \egroup \end{trivlist}%
  \else \@badmath
  \fi
  \global\inmathfalse
}

\def\equation{\global\inmathtrue
  \refstepcounter{equation}\let\@currentlabel=\theequation
  \trivlist \item[]\leavevmode
  \hbox to\linewidth\bgroup $\displaystyle
    \hskip\mathindent
}

\def\endequation{$\hfil
  \displaywidth\linewidth \@eqnnum\egroup \endtrivlist
  \global\inmathfalse
}

\def\eqnarray{\global\inmathtrue
  \stepcounter{equation}\let\@currentlabel=\theequation
  \global\@eqnswtrue
  \global\@eqcnt\z@\tabskip\mathindent\let\\=\@eqncr
  \abovedisplayskip\topsep\ifvmode\advance\abovedisplayskip\partopsep\fi
  \belowdisplayskip\abovedisplayskip
  \belowdisplayshortskip\abovedisplayskip
  \abovedisplayshortskip\abovedisplayskip
  $$\halign to \linewidth\bgroup
    \@eqnsel\hskip\@centering$\displaystyle\tabskip\z@
  {##}$&\global\@eqcnt\@ne \hskip 2\arraycolsep \hfil${##}$\hfil
  &\global\@eqcnt\tw@ \hskip 2\arraycolsep $\displaystyle{##}$\hfil
   \tabskip\@centering&\llap{##}\tabskip\z@\cr}

\def\endeqnarray{\@@eqncr\egroup
      \global\advance\c@equation\m@ne$$% $$ MATCHING
%   \ifmathqed \global\qedfalse \global\mathqedfalse
% %    \if@eqnsw\vspace*{-16pt}\fi\vbox to0pt{\vss
%     \vbox to0pt{\vss
%       \hbox to\textwidth{\hfill
% %        \if@eqnsw\raise24pt\fi
%         \if@eqnsw\raise21pt\fi
%         \hbox{\qedsymbol}}}%
% 		\vspace{-12pt}%
%   \fi
	\ifmathqed \global\qedfalse \global\mathqedfalse\fi
  \global\inmathfalse\global\@ignoretrue
}

\def\eqnarrayl{\global\inmathtrue
  \stepcounter{equation}\let\@currentlabel=\theequation
  \global\@eqnswtrue
  \global\@eqcnt\z@\tabskip\mathindent\let\\=\@eqncr
  \abovedisplayskip\topsep\ifvmode\advance\abovedisplayskip\partopsep\fi
  \belowdisplayskip\abovedisplayskip
  \belowdisplayshortskip\abovedisplayskip
  \abovedisplayshortskip\abovedisplayskip
  $$\halign to \linewidth\bgroup
    \@eqnsel\hskip\@centering$\displaystyle\tabskip\z@
  {##}$\hfil&\global\@eqcnt\@ne \hskip 2\arraycolsep \hfil${##}$\hfil
  &\global\@eqcnt\tw@ \hskip 2\arraycolsep $\displaystyle{##}$\hfil
   \tabskip\@centering&\llap{##}\tabskip\z@\cr}

\def\endeqnarrayl{\@@eqncr\egroup
      \global\advance\c@equation\m@ne$$% $$ MATCHING
%   \ifmathqed \global\qedfalse \global\mathqedfalse
% %    \if@eqnsw\vspace*{-16pt}\fi\vbox to0pt{\vss
%     \vbox to0pt{\vss
%       \hbox to\textwidth{\hfill
% %        \if@eqnsw\raise24pt\fi
%         \if@eqnsw\raise21pt\fi
%         \hbox{\qedsymbol}}}%
% 		\vspace{-12pt}%
%   \fi
	\ifmathqed \global\qedfalse \global\mathqedfalse\fi
  \global\inmathfalse\global\@ignoretrue
}

\@namedef{eqnarrayl*}{\def\@eqncr{\nonumber\@seqncr}\eqnarrayl}
\@namedef{endeqnarrayl*}{\nonumber\endeqnarrayl}

\def\eqnarrayr{\global\inmathtrue
  \stepcounter{equation}\let\@currentlabel=\theequation
  \global\@eqnswtrue
  \global\@eqcnt\z@\tabskip\mathindent\let\\=\@eqncr
  \abovedisplayskip\topsep\ifvmode\advance\abovedisplayskip\partopsep\fi
  \belowdisplayskip\abovedisplayskip
  \belowdisplayshortskip\abovedisplayskip
  \abovedisplayshortskip\abovedisplayskip
  $$\halign to \linewidth\bgroup
    \@eqnsel\hskip\@centering$\displaystyle\tabskip\z@
  {##}$&\global\@eqcnt\@ne \hskip 2\arraycolsep \hfil${##}$\hfil
  &\global\@eqcnt\tw@ \hskip 2\arraycolsep \hfil$\displaystyle{##}$
   \tabskip\@centering&\llap{##}\tabskip\z@\cr}

\def\endeqnarrayr{\@@eqncr\egroup
      \global\advance\c@equation\m@ne$$% $$ MATCHING
%   \ifmathqed \global\qedfalse \global\mathqedfalse
%     \vbox to0pt{\vss
%       \hbox to\textwidth{\hfill
% %        \if@eqnsw\raise24pt\fi
%         \if@eqnsw\raise21pt\fi
%         \hbox{\qedsymbol}}}%
% 		\vspace{-12pt}%
%   \fi
	\ifmathqed \global\qedfalse \global\mathqedfalse\fi
  \global\inmathfalse\global\@ignoretrue
}

\@namedef{eqnarrayr*}{\def\@eqncr{\nonumber\@seqncr}\eqnarrayr}
\@namedef{endeqnarrayr*}{\nonumber\endeqnarrayr}

\newif\ifineqnarrayreason
\global\ineqnarrayreasonfalse

\def\eqnarrayreason{\global\inmathtrue
  \global\ineqnarrayreasontrue
  \stepcounter{equation}\let\@currentlabel=\theequation
  \global\@eqnswtrue
  \global\@eqcnt\z@\tabskip\mathindent\let\\=\@eqncr
  \abovedisplayskip\topsep\ifvmode\advance\abovedisplayskip\partopsep\fi
  \belowdisplayskip\abovedisplayskip
  \belowdisplayshortskip\abovedisplayskip
  \abovedisplayshortskip\abovedisplayskip
  $$\halign to \linewidth\bgroup
    \@eqnsel\hskip\@centering$\displaystyle\tabskip\z@
  {##}$&\global\@eqcnt\@ne \hskip 2\arraycolsep \hfil${##}$\hfil
  &\global\@eqcnt\tw@ \hskip 2\arraycolsep $\displaystyle{##}$\hfil
  &\global\@eqcnt\tw@ \hskip 2\arraycolsep {##}\hfil
   \tabskip\@centering&\llap{##}\tabskip\z@\cr}

\def\endeqnarrayreason{\@@eqncr\egroup
      \global\ineqnarrayreasonfalse
      \global\advance\c@equation\m@ne$$% $$ MATCHING
%   \ifmathqed \global\qedfalse \global\mathqedfalse
%     \vbox to0pt{\vss
%       \hbox to\textwidth{\hfill
% %        \if@eqnsw\raise24pt\fi
%         \if@eqnsw\raise21pt\fi
%         \hbox{\qedsymbol}}}%
% 		\vspace{-12pt}%
%   \fi
	\ifmathqed \global\qedfalse \global\mathqedfalse\fi
  \global\inmathfalse\global\@ignoretrue
}

\@namedef{eqnarrayreason*}{\def\@eqncr{\nonumber\@seqncr}\eqnarrayreason}
\@namedef{endeqnarrayreason*}{\nonumber\endeqnarrayreason}

\def\eqnarraylp{\global\inmathtrue
  \stepcounter{equation}\let\@currentlabel=\theequation
  \global\@eqnswtrue
  \global\@eqcnt\z@\tabskip\mathindent\let\\=\@eqncr
  \abovedisplayskip\topsep\ifvmode\advance\abovedisplayskip\partopsep\fi
  \belowdisplayskip\abovedisplayskip
  \belowdisplayshortskip\abovedisplayskip
  \abovedisplayshortskip\abovedisplayskip
  $$\halign to \linewidth\bgroup
    \@eqnsel\hskip\@centering
  \tabskip\z@{##}\hfil&\hskip 2\arraycolsep
  \hskip\@centering$\displaystyle\tabskip\z@
  {##}$&\global\@eqcnt\@ne \hskip 2\arraycolsep \hfil${##}$\hfil
  &\global\@eqcnt\tw@ \hskip 2\arraycolsep $\displaystyle{##}$\hfil
  &\global\@eqcnt\tw@ \hskip 2\arraycolsep $\displaystyle{##}$\hfil
   \tabskip\@centering&\llap{##}\tabskip\z@\cr}

\def\endeqnarraylp{\@@eqncr\egroup
      \global\advance\c@equation\m@ne$$% $$ MATCHING
%   \ifmathqed \global\qedfalse \global\mathqedfalse
% %    \if@eqnsw\vspace*{-16pt}\fi\vbox to0pt{\vss
%     \vbox to0pt{\vss
%       \hbox to\textwidth{\hfill
% %        \if@eqnsw\raise24pt\fi
%         \if@eqnsw\raise21pt\fi
%         \hbox{\qedsymbol}}}%
% 		\vspace{-12pt}%
%   \fi
	\ifmathqed \global\qedfalse \global\mathqedfalse\fi
  \global\inmathfalse\global\@ignoretrue
}

\@namedef{eqnarraylp*}{\def\@eqncr{\nonumber\@seqncr}\eqnarraylp}
\@namedef{endeqnarraylp*}{\nonumber\endeqnarraylp}

\def\eqnarrayll{\global\inmathtrue
  \stepcounter{equation}\let\@currentlabel=\theequation
  \global\@eqnswtrue
  \global\@eqcnt\z@\tabskip\mathindent\let\\=\@eqncr
  \abovedisplayskip\topsep\ifvmode\advance\abovedisplayskip\partopsep\fi
  \belowdisplayskip\abovedisplayskip
  \belowdisplayshortskip\abovedisplayskip
  \abovedisplayshortskip\abovedisplayskip
  $$\halign to \linewidth\bgroup
    \@eqnsel\hskip\@centering$\displaystyle\tabskip\z@
  {##}$&\global\@eqcnt\@ne \hskip 2\arraycolsep \hfil${##}$\hfil
  &\global\@eqcnt\tw@ \hskip 2\arraycolsep $\displaystyle{##}$\hfil
  &\global\@eqcnt\tw@ $\displaystyle{##}$\hfil
   \tabskip\@centering&\llap{##}\tabskip\z@\cr}

\def\endeqnarrayll{\@@eqncr\egroup
      \global\advance\c@equation\m@ne$$% $$ MATCHING
%   \ifmathqed \global\qedfalse \global\mathqedfalse
%     \vbox to0pt{\vss
%       \hbox to\textwidth{\hfill
% %        \if@eqnsw\raise24pt\fi 
%         \if@eqnsw\raise21pt\fi
%        \hbox{\qedsymbol}}}%
% 		\vspace{-12pt}%
%   \fi
	\ifmathqed \global\qedfalse \global\mathqedfalse\fi
  \global\inmathfalse\global\@ignoretrue
}

\@namedef{eqnarrayll*}{\def\@eqncr{\nonumber\@seqncr}\eqnarrayll}
\@namedef{endeqnarrayll*}{\nonumber\endeqnarrayll}

\def\eqnarraylll{\global\inmathtrue
  \stepcounter{equation}\let\@currentlabel=\theequation
  \global\@eqnswtrue
  \global\@eqcnt\z@\tabskip\mathindent\let\\=\@eqncr
  \abovedisplayskip\topsep\ifvmode\advance\abovedisplayskip\partopsep\fi
  \belowdisplayskip\abovedisplayskip
  \belowdisplayshortskip\abovedisplayskip
  \abovedisplayshortskip\abovedisplayskip
  $$\halign to \linewidth\bgroup
    \@eqnsel\hskip\@centering$\displaystyle\tabskip\z@
  {##}$\hfil&\global\@eqcnt\@ne \hskip 2\arraycolsep \hfil${##}$\hfil
  &\global\@eqcnt\tw@ \hskip 2\arraycolsep $\displaystyle{##}$\hfil
  &\global\@eqcnt\tw@ $\displaystyle{##}$\hfil
   \tabskip\@centering&\llap{##}\tabskip\z@\cr}

\def\endeqnarraylll{\@@eqncr\egroup
      \global\advance\c@equation\m@ne$$% $$ MATCHING
%   \ifmathqed \global\qedfalse \global\mathqedfalse
%     \vbox to0pt{\vss
%       \hbox to\textwidth{\hfill
% %        \if@eqnsw\raise24pt\fi 
%         \if@eqnsw\raise21pt\fi
%        \hbox{\qedsymbol}}}%
% 		\vspace{-12pt}%
%   \fi
	\ifmathqed \global\qedfalse \global\mathqedfalse\fi
  \global\inmathfalse\global\@ignoretrue
}

\@namedef{eqnarraylll*}{\def\@eqncr{\nonumber\@seqncr}\eqnarraylll}
\@namedef{endeqnarraylll*}{\nonumber\endeqnarraylll}

\def\eqnarraylrl{\global\inmathtrue
  \stepcounter{equation}\let\@currentlabel=\theequation
  \global\@eqnswtrue
  \global\@eqcnt\z@\tabskip\mathindent\let\\=\@eqncr
  \abovedisplayskip\topsep\ifvmode\advance\abovedisplayskip\partopsep\fi
  \belowdisplayskip\abovedisplayskip
  \belowdisplayshortskip\abovedisplayskip
  \abovedisplayshortskip\abovedisplayskip
  $$\halign to \linewidth\bgroup
    \@eqnsel\hskip\@centering$\displaystyle\tabskip\z@
  {##}$\hfil&\global\@eqcnt\@ne \hskip 2\arraycolsep \hfil${##}$\hfil
  &\global\@eqcnt\tw@ \hskip 2\arraycolsep \hfil$\displaystyle{##}$
  &\global\@eqcnt\tw@ $\displaystyle{##}$\hfil
   \tabskip\@centering&\llap{##}\tabskip\z@\cr}

\def\endeqnarraylrl{\@@eqncr\egroup
      \global\advance\c@equation\m@ne$$% $$ MATCHING
%   \ifmathqed \global\qedfalse \global\mathqedfalse
%     \vbox to0pt{\vss
%       \hbox to\textwidth{\hfill
% %        \if@eqnsw\raise24pt\fi 
%         \if@eqnsw\raise21pt\fi
%        \hbox{\qedsymbol}}}%
% 		\vspace{-12pt}%
%   \fi
	\ifmathqed \global\qedfalse \global\mathqedfalse\fi
  \global\inmathfalse\global\@ignoretrue
}

\@namedef{eqnarraylrl*}{\def\@eqncr{\nonumber\@seqncr}\eqnarraylrl}
\@namedef{endeqnarraylrl*}{\nonumber\endeqnarraylrl}

\def\eqnarrayllll{\global\inmathtrue
  \stepcounter{equation}\let\@currentlabel=\theequation
  \global\@eqnswtrue
  \global\@eqcnt\z@\tabskip\mathindent\let\\=\@eqncr
  \abovedisplayskip\topsep\ifvmode\advance\abovedisplayskip\partopsep\fi
  \belowdisplayskip\abovedisplayskip
  \belowdisplayshortskip\abovedisplayskip
  \abovedisplayshortskip\abovedisplayskip
  $$\halign to \linewidth\bgroup
    \@eqnsel\hskip\@centering$\displaystyle\tabskip\z@
  {##}$\hfil&\global\@eqcnt\@ne \hskip 2\arraycolsep \hfil${##}$\hfil
  &\global\@eqcnt\tw@ \hskip 2\arraycolsep $\displaystyle{##}$\hfil
  &\global\@eqcnt\tw@ \hskip 2\arraycolsep $\displaystyle{##}$\hfil
  &\global\@eqcnt\tw@ \hskip 2\arraycolsep $\displaystyle{##}$\hfil
   \tabskip\@centering&\llap{##}\tabskip\z@\cr}

\def\endeqnarrayllll{\@@eqncr\egroup
      \global\advance\c@equation\m@ne$$% $$ MATCHING
%   \ifmathqed \global\qedfalse \global\mathqedfalse
%     \vbox to0pt{\vss
%       \hbox to\textwidth{\hfill
% %        \if@eqnsw\raise24pt\fi 
%         \if@eqnsw\raise21pt\fi
%        \hbox{\qedsymbol}}}%
% 		\vspace{-12pt}%
%   \fi
	\ifmathqed \global\qedfalse \global\mathqedfalse\fi
  \global\inmathfalse\global\@ignoretrue
}

\@namedef{eqnarrayllll*}{\def\@eqncr{\nonumber\@seqncr}\eqnarrayllll}
\@namedef{endeqnarrayllll*}{\nonumber\endeqnarrayllll}

\newdimen\mathindent \mathindent = 0pt

% Hack to fix TeX putting in delimiters that are too large.
% Default \delimiterfactor=901.
% \delimiterfactor=601
\delimiterfactor=801
% \delimiterfactor=937	%% Smallest value s.t. \floor{\frac{n}{2^i}} in displaymode looks right

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

%    \EndChapter and \EndPartIntro call \printpartialindex, which uses
%    the @partsw switch to include a printout of the index terms from
%    the current file iff \includeonly was invoked.

\edef\currentfile{\jobname} % init

\newcommand{\printpartialindex}{%
  \if@partsw \InputIfFileExists{\currentfile.index}{}{}\fi
}

%%\providecommand{\StartChapter}[1]{%
%%  \chapter{#1\label{ch:\currentchapter}}%
%%}

\newcommand{\StartChapter}[2][\jobname]{%
  \def\currentfile{#1}%
  \chapter{#2\label{ch:#1}}%
}

\providecommand{\EndChapter}{%
  \clearpage \printpartialindex
}

\newcommand{\StartAppendix}[2][\jobname]{%
  \def\currentfile{#1}%
  \appchapter{#2\label{app:#1}}%
}

\providecommand{\EndAppendix}{%
  \EndChapter
}

\newcommand{\StartPartIntro}[2][\jobname]{%
  \def\currentfile{#1}%
  \part{#2\label{part:#1}}%
}

\newcommand{\EndPartIntro}{\clearpage \printpartialindex }

%    This message goes into the .log file to make it easier for a shell
%    script to reliably decide when the new .aux file is OK to replace
%    the previous one.
\AtEndDocument{\typeout{<:Successful LaTeX run:>}}

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

%% Some macros added by THC for To Do Lists...

\newif\iffileexists
\newcommand{\checkfile}[1]{\openin2=#1 \ifeof2 \fileexistsfalse\else\fileexiststrue\fi\closein2}

\newif\iftodoonly
\todoonlyfalse

\newcommand{\clrsinclude}[1]{%
  \iftodoonly
    \checkfile{#1.todo.tex}
    \iffileexists\def\todoname{#1.tex}\include{#1.todo}\fi
  \else
    \include{#1}
    \iftodo
      \checkfile{#1.todo.tex}
      \iffileexists\def\todoname{#1.tex}\include{#1.todo}\fi
    \fi
  \fi
}

\newif\iftodo
%\todotrue
\todofalse

\makeatletter

\newenvironment{todolist}{\subsection*{To Do List for \todoname:}\begin{enumerate}}{\if@newlist\item Nothing to do.\fi\end{enumerate}}

\newenvironment{donelist}{\outerline{\vrule width\typeblockwidth height\hruleheight}\subsection*{List of Major Changes for \todoname:}\begin{enumerate}}{\if@newlist\item No Major Changes.\fi\end{enumerate}}

\newenvironment{thirdedition}{\outerline{\vrule width\typeblockwidth height\hruleheight}\subsection*{Changes Left for Third
Edition for \todoname:}\begin{enumerate}}{\if@newlist\item Nothing Planned.\fi\end{enumerate}}
\makeatother



\newcommand{\bugtodo}[1]{Bug \##1: }

%% for lp  -- three, four and five, replaced by max and slack below
\makeatletter
% \def\eqnarraylpthree{\global\inmathtrue
%   \stepcounter{equation}\let\@currentlabel=\theequation
%   \global\@eqnswtrue
%   \global\@eqcnt\z@\tabskip\mathindent\let\\=\@eqncr
%   \abovedisplayskip\topsep\ifvmode\advance\abovedisplayskip\partopsep\fi
%   \belowdisplayskip\abovedisplayskip
%   \belowdisplayshortskip\abovedisplayskip
%   \abovedisplayshortskip\abovedisplayskip
%   $$\halign to \linewidth\bgroup
%     \@eqnsel\hskip\@centering
%   \tabskip\z@{##}&\hskip 2\arraycolsep
%   \hskip\@centering$\displaystyle\tabskip\z@
%   {##}$&\global\@eqcnt\@ne \hskip 2\arraycolsep \hfil${##}$\hfil
%   &\global\@eqcnt\tw@ \hskip 2\arraycolsep $\displaystyle{##}$\hfil
%   &\global\@eqcnt\tw@ \hskip 2\arraycolsep $\displaystyle{##}$\hfil
%   &\global\@eqcnt\tw@ \hskip 2\arraycolsep $\displaystyle{##}$\hfil
%   &\global\@eqcnt\tw@ \hskip 2\arraycolsep $\displaystyle{##}$\hfil
%   &\global\@eqcnt\tw@ \hskip 2\arraycolsep $\displaystyle{##}$\hfil
%    \tabskip\@centering&\llap{##}\tabskip\z@\cr}

% \def\endeqnarraylpthree{\@@eqncr\egroup
%       \global\advance\c@equation\m@ne$$% $$ MATCHING
% %   \ifmathqed \global\qedfalse \global\mathqedfalse
% % %    \if@eqnsw\vspace*{-16pt}\fi\vbox to0pt{\vss
% %     \vbox to0pt{\vss
% %       \hbox to\textwidth{\hfill
% % %        \if@eqnsw\raise24pt\fi
% %         \if@eqnsw\raise21pt\fi
% %         \hbox{\qedsymbol}}}%
% % 		\vspace{-12pt}%
% %   \fi
% 	\ifmathqed \global\qedfalse \global\mathqedfalse\fi
%   \global\inmathfalse\global\@ignoretrue
% }

% \@namedef{eqnarraylpthree*}{\def\@eqncr{\nonumber\@seqncr}\eqnarraylpthree}
% \@namedef{endeqnarraylpthree*}{\nonumber\endeqnarraylpthree}

% \def\eqnarraylpfour{\global\inmathtrue
%   \stepcounter{equation}\let\@currentlabel=\theequation
%   \global\@eqnswtrue
%   \global\@eqcnt\z@\tabskip\mathindent\let\\=\@eqncr
%   \abovedisplayskip\topsep\ifvmode\advance\abovedisplayskip\partopsep\fi
%   \belowdisplayskip\abovedisplayskip
%   \belowdisplayshortskip\abovedisplayskip
%   \abovedisplayshortskip\abovedisplayskip
%   $$\halign to \linewidth\bgroup
%     \@eqnsel\hskip\@centering
%   \tabskip\z@{##}&\hskip 2\arraycolsep
%   \hskip\@centering$\displaystyle\tabskip\z@
%   {##}$&\global\@eqcnt\@ne \hskip 2\arraycolsep \hfil${##}$\hfil
%   &\global\@eqcnt\tw@ \hskip 2\arraycolsep $\displaystyle{##}$\hfil
%   &\global\@eqcnt\tw@ \hskip 2\arraycolsep $\displaystyle{##}$\hfil
%   &\global\@eqcnt\tw@ \hskip 2\arraycolsep $\displaystyle{##}$\hfil
%   &\global\@eqcnt\tw@ \hskip 2\arraycolsep $\displaystyle{##}$\hfil
%   &\global\@eqcnt\tw@ \hskip 2\arraycolsep $\displaystyle{##}$\hfil
%   &\global\@eqcnt\tw@ \hskip 2\arraycolsep $\displaystyle{##}$\hfil
%   &\global\@eqcnt\tw@ \hskip 2\arraycolsep $\displaystyle{##}$\hfil
%    \tabskip\@centering&\llap{##}\tabskip\z@\cr}

% \def\endeqnarraylpfour{\@@eqncr\egroup
%       \global\advance\c@equation\m@ne$$% $$ MATCHING
% %   \ifmathqed \global\qedfalse \global\mathqedfalse
% % %    \if@eqnsw\vspace*{-16pt}\fi\vbox to0pt{\vss
% %     \vbox to0pt{\vss
% %       \hbox to\textwidth{\hfill
% % %        \if@eqnsw\raise24pt\fi
% %         \if@eqnsw\raise21pt\fi
% %         \hbox{\qedsymbol}}}%
% % 		\vspace{-12pt}%
% %   \fi
% 	\ifmathqed \global\qedfalse \global\mathqedfalse\fi
%   \global\inmathfalse\global\@ignoretrue
% }

% \@namedef{eqnarraylpfour*}{\def\@eqncr{\nonumber\@seqncr}\eqnarraylpfour}
% \@namedef{endeqnarraylpfour*}{\nonumber\endeqnarraylpfour}

% \def\eqnarraylpfive{\global\inmathtrue
%   \stepcounter{equation}\let\@currentlabel=\theequation
%   \global\@eqnswtrue
%   \global\@eqcnt\z@\tabskip\mathindent\let\\=\@eqncr
%   \abovedisplayskip\topsep\ifvmode\advance\abovedisplayskip\partopsep\fi
%   \belowdisplayskip\abovedisplayskip
%   \belowdisplayshortskip\abovedisplayskip
%   \abovedisplayshortskip\abovedisplayskip
%   $$\halign to \linewidth\bgroup
%     \@eqnsel\hskip\@centering
%   \tabskip\z@{##}&\hskip 2\arraycolsep
%   \hskip\@centering$\displaystyle\tabskip\z@
%   {##}$&\global\@eqcnt\@ne \hskip 2\arraycolsep \hfil${##}$\hfil
%   &\global\@eqcnt\tw@ \hskip 2\arraycolsep $\displaystyle{##}$\hfil
%   &\global\@eqcnt\tw@ \hskip 2\arraycolsep $\displaystyle{##}$\hfil
%   &\global\@eqcnt\tw@ \hskip 2\arraycolsep $\displaystyle{##}$\hfil
%   &\global\@eqcnt\tw@ \hskip 2\arraycolsep $\displaystyle{##}$\hfil
%   &\global\@eqcnt\tw@ \hskip 2\arraycolsep $\displaystyle{##}$\hfil
%   &\global\@eqcnt\tw@ \hskip 2\arraycolsep $\displaystyle{##}$\hfil
%   &\global\@eqcnt\tw@ \hskip 2\arraycolsep $\displaystyle{##}$\hfil
%   &\global\@eqcnt\tw@ \hskip 2\arraycolsep $\displaystyle{##}$\hfil
%   &\global\@eqcnt\tw@ \hskip 2\arraycolsep $\displaystyle{##}$\hfil
%    \tabskip\@centering&\llap{##}\tabskip\z@\cr}

% \def\endeqnarraylpfive{\@@eqncr\egroup
%       \global\advance\c@equation\m@ne$$% $$ MATCHING
% %   \ifmathqed \global\qedfalse \global\mathqedfalse
% % %    \if@eqnsw\vspace*{-16pt}\fi\vbox to0pt{\vss
% %     \vbox to0pt{\vss
% %       \hbox to\textwidth{\hfill
% % %        \if@eqnsw\raise24pt\fi
% %         \if@eqnsw\raise21pt\fi
% %         \hbox{\qedsymbol}}}%
% % 		\vspace{-12pt}%
% %   \fi
% 	\ifmathqed \global\qedfalse \global\mathqedfalse\fi
%   \global\inmathfalse\global\@ignoretrue
% }

% \@namedef{eqnarraylpfive*}{\def\@eqncr{\nonumber\@seqncr}\eqnarraylpfive}
% \@namedef{endeqnarraylpfive*}{\nonumber\endeqnarraylpfive}



\def\eqnarraylpmax{\global\inmathtrue
  \stepcounter{equation}\let\@currentlabel=\theequation
  \global\@eqnswtrue
  \global\@eqcnt\z@\tabskip\mathindent\let\\=\@eqncr
  \abovedisplayskip\topsep\ifvmode\advance\abovedisplayskip\partopsep\fi
  \belowdisplayskip\abovedisplayskip
  \belowdisplayshortskip\abovedisplayskip
  \abovedisplayshortskip\abovedisplayskip
  $$\halign to \linewidth\bgroup
    \@eqnsel\hskip\@centering
  \tabskip\z@{##}\hfil &\hskip 2\arraycolsep
  \hskip\@centering$\displaystyle\tabskip\z@
  {##}$&\global\@eqcnt\@ne \hskip 2\arraycolsep \hfil${##}$\hfil
  &\global\@eqcnt\tw@ \hskip 2\arraycolsep \hfil$\displaystyle{##}$
  &\global\@eqcnt\tw@ \hskip 2\arraycolsep \hfil$\displaystyle{##}$\hfil
  &\global\@eqcnt\tw@ \hskip 2\arraycolsep \hfil$\displaystyle{##}$
  &\global\@eqcnt\tw@ \hskip 2\arraycolsep \hfil$\displaystyle{##}$\hfil
  &\global\@eqcnt\tw@ \hskip 2\arraycolsep \hfil$\displaystyle{##}$
  &\global\@eqcnt\tw@ \hskip 2\arraycolsep \hfil$\displaystyle{##}$\hfil
  &\global\@eqcnt\tw@ \hskip 2\arraycolsep \hfil$\displaystyle{##}$
  &\global\@eqcnt\tw@ \hskip 2\arraycolsep \hfil$\displaystyle{##}$\hfil
  &\global\@eqcnt\tw@ \hskip 2\arraycolsep \hfil$\displaystyle{##}$
   \tabskip\@centering&\llap{##}\tabskip\z@\cr}

\def\endeqnarraylpmax{\@@eqncr\egroup
      \global\advance\c@equation\m@ne$$% $$ MATCHING
%   \ifmathqed \global\qedfalse \global\mathqedfalse
% %    \if@eqnsw\vspace*{-16pt}\fi\vbox to0pt{\vss
%     \vbox to0pt{\vss
%       \hbox to\textwidth{\hfill
% %        \if@eqnsw\raise24pt\fi
%         \if@eqnsw\raise21pt\fi
%         \hbox{\qedsymbol}}}%
% 		\vspace{-12pt}%
%   \fi
	\ifmathqed \global\qedfalse \global\mathqedfalse\fi
  \global\inmathfalse\global\@ignoretrue
}

\@namedef{eqnarraylpmax*}{\def\@eqncr{\nonumber\@seqncr}\eqnarraylpmax}
\@namedef{endeqnarraylpmax*}{\nonumber\endeqnarraylpmax}


\def\eqnarraylpslack{\global\inmathtrue
  \stepcounter{equation}\let\@currentlabel=\theequation
  \global\@eqnswtrue
  \global\@eqcnt\z@\tabskip\mathindent\let\\=\@eqncr
  \abovedisplayskip\topsep\ifvmode\advance\abovedisplayskip\partopsep\fi
  \belowdisplayskip\abovedisplayskip
  \belowdisplayshortskip\abovedisplayskip
  \abovedisplayshortskip\abovedisplayskip
  $$\halign to \linewidth\bgroup
    \@eqnsel\hskip\@centering
  \tabskip\z@${##}$&\hskip 2\arraycolsep
  \hskip\@centering$\displaystyle\tabskip\z@
  {##}$&\global\@eqcnt\@ne \hskip 2\arraycolsep \hfil${##}$
  &\global\@eqcnt\tw@ \hskip 2\arraycolsep \hfil$\displaystyle{##}$\hfil
  &\global\@eqcnt\tw@ \hskip 2\arraycolsep \hfil$\displaystyle{##}$
  &\global\@eqcnt\tw@ \hskip 2\arraycolsep \hfil$\displaystyle{##}$\hfil
  &\global\@eqcnt\tw@ \hskip 2\arraycolsep \hfil$\displaystyle{##}$
  &\global\@eqcnt\tw@ \hskip 2\arraycolsep \hfil$\displaystyle{##}$\hfil
  &\global\@eqcnt\tw@ \hskip 2\arraycolsep \hfil$\displaystyle{##}$
  &\global\@eqcnt\tw@ \hskip 2\arraycolsep \hfil$\displaystyle{##}$\hfil
  &\global\@eqcnt\tw@ \hskip 2\arraycolsep \hfil$\displaystyle{##}$
   \tabskip\@centering&\llap{##}\tabskip\z@\cr}

\def\endeqnarraylpslack{\@@eqncr\egroup
      \global\advance\c@equation\m@ne$$% $$ MATCHING
%   \ifmathqed \global\qedfalse \global\mathqedfalse
% %    \if@eqnsw\vspace*{-16pt}\fi\vbox to0pt{\vss
%     \vbox to0pt{\vss
%       \hbox to\textwidth{\hfill
% %        \if@eqnsw\raise24pt\fi
%         \if@eqnsw\raise21pt\fi
%         \hbox{\qedsymbol}}}%
% 		\vspace{-12pt}%
%   \fi
	\ifmathqed \global\qedfalse \global\mathqedfalse\fi
  \global\inmathfalse\global\@ignoretrue
}

\@namedef{eqnarraylpslack*}{\def\@eqncr{\nonumber\@seqncr}\eqnarraylpslack}
\@namedef{endeqnarraylpslack*}{\nonumber\endeqnarraylpslack}


%%% for referring to linear programs -- firstline, lastline
\newcommand{\lpref}[2]{\eqnref{#1}\ndash \eqnref{#2}}

%% this value needs to be adjusted
%\newcommand{\lppunc}[1]{\hspace*{-.25in}{#1}}
\newcommand{\lppunc}[1]{\hfilneg\hspace*{-1em}{#1}\hfill}

\endinput %
