% mldoc-book.cls
%
% COPYRIGHT (c) 2000 Bell Labs, Lucent Technologies.
% COPYRIGHT (c) 2004 John Reppy (http://www.cs.uchicago.edu/~jhr).
% All rights reserved.
%
% A modification of the standard book class for formatting the
% output of the ML-Doc latex-gen tool.  The actual layout is based
% on the Cambridge University Press style as used in Concurrent
% Programming in ML.
%
% CHANGE LOG
%
%	[2003-07-23]	Make math-mode TT font be Courier.
%	[2000-02-01]	Created class file.
%
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{mldoc-book}[2000/02/01]
%
% We use the times package because it looks nice and supports styled code
% displays.  We need the code package for typesetting code, but we define
% our own names.
\RequirePackage{ifthen}
\RequirePackage{times}
\RequirePackage{amsmath}
%-------------------------
% the following magic makes the tt font in math mode be the same as the
% normal tt font (i.e., Courier)
%
\SetMathAlphabet{\mathtt}{normal}{OT1}{pcr}{n}{n}
\SetMathAlphabet{\mathtt}{bold}{OT1}{pcr}{bx}{n}
%-------------------------
%
% Options
%
%\newboolean{@draftmode}
\newboolean{@cropmode}
\DeclareOption{draft}{\setboolean{@draftmode}{true}}
\DeclareOption{cropmarks}{\setboolean{@cropmode}{true}}
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{book}}
%
\ProcessOptions
%
\LoadClass[11pt,twoside,openright]{book}
%
% Option flags
\newboolean{@draftmode}
%
% process draft option
%
\ifthenelse{\boolean{@draftmode}}{
  \InputIfFileExists{draftcopy.sty}{%
    \usepackage[dvips,light]{draftcopy}
    \ifthenelse{\boolean{@cropmode}}{
      \typeout{draft option disables cropmarks option}
      \setboolean{@cropmode}{false}
    }{}
  }{%
    \typeout{draftcopy style not found; draft option ignored}
  }
}{}
%
% process cropmark option
%
\ifthenelse{\boolean{@cropmode}}{
  \usepackage[dvips]{cropframe}
}{}
%%
%% The "large" CUP style defines the following
%% textsizes (size/baseline in pt):
%%
%% \tiny		5/6
%% \scriptsize		7/8
%% \footnotesize	8/9
%% \enotesize		9/10	(CUP extension)
%% \small		10/11
%% \smallish		10/12	(CUP extension)
%% \normalsmall		10/12	(CUP extension)
%% \listsize		10/13	(CUP extension)
%% \normalsize		11/14
%% \large		11/14
%% \Large		12/17
%% \LARGE		17/19
%% \huge		20/24
%% \Huge		17/24	(not used)
%%
%% The CUP style sets the skips as follows (given a baselineskip B):
%%
%%   \abovedisplayskip      0.50*B \@plus 0.25*B \@minus1\p@
%%   \belowdisplayskip      \abovedisplayskip
%%   \abovedisplayshortskip 0.25*B \@plus 0.25*B
%%   \belowdisplayshortskip \abovedisplayskip
%%
%% and the first-level list parameters are set as:
%%
%%   \def\@listi{%
%%      \leftmargin\leftmargini
%%      \topsep  0.50*B \@plus2\p@ \@minus1\p@%
%%      \parsep  \z@
%%      \itemsep \z@
%%   }
%%
%%
%% \footnotesize 8/9
%%
\renewcommand\footnotesize{%
  \@setfontsize\footnotesize\@viiipt\@ixpt%
  \abovedisplayskip      4.5\p@ \@plus 2.25\p@ \@minus1\p@
  \belowdisplayskip      \abovedisplayskip
  \abovedisplayshortskip 2.25\p@ \@plus 2.25\p@
  \belowdisplayshortskip \abovedisplayskip
  \def\@listi{%
     \leftmargin\leftmargini
     \topsep  4.5\p@ \@plus2\p@ \@minus1\p@%
     \parsep  \z@
     \itemsep \z@
  }
}
%%
%% \enotesize 9/10
%%
\newcommand\enotesize{%
  \@setfontsize\enotesize{\@ixpt}{\@xpt}%
  \abovedisplayskip      4.5\p@ \@plus 2.25\p@ \@minus1\p@
  \belowdisplayskip      \abovedisplayskip
  \abovedisplayshortskip 2.25\p@ \@plus 2.25\p@
  \belowdisplayshortskip \abovedisplayskip
  \def\@listi{%
     \leftmargin\leftmargini
     \topsep  4.5\p@ \@plus2\p@ \@minus1\p@%
     \parsep  \z@
     \itemsep \z@
  }
}
%
\renewcommand\small{%
  \@setfontsize\small{\@xpt}{11\p@}%
  \abovedisplayskip      5.5\p@ \@plus2.75\p@ \@minus1\p@
  \belowdisplayskip      \abovedisplayskip
  \abovedisplayshortskip 2.75\p@ \@plus2.75\p@
  \belowdisplayshortskip \abovedisplayskip
  \def\@listi{%
     \leftmargin\leftmargini
     \topsep  5.5\p@ \@plus2\p@ \@minus1\p@%
     \parsep  \z@
     \itemsep \z@
  }
}
%%
%% \smallish 10/12
%%
\newcommand\smallish{%
  \@setfontsize\smallish{\@xpt}{\@xiipt}%
  \abovedisplayskip      6\p@ \@plus3\p@ \@minus1\p@
  \belowdisplayskip      \abovedisplayskip
  \abovedisplayshortskip 3\p@ \@plus3\p@
  \belowdisplayshortskip \abovedisplayskip
  \def\@listi{%
     \leftmargin\leftmargini
     \topsep  6\p@ \@plus2\p@ \@minus1\p@%
     \parsep  \z@
     \itemsep \z@
  }
}
%
\newcommand\normalsmall{%
  \@setfontsize\normalsmall{\@xpt}{\@xiipt}%
  \abovedisplayskip      6\p@ \@plus3\p@ \@minus1\p@
  \belowdisplayskip      \abovedisplayskip
  \abovedisplayshortskip 3\p@ \@plus3\p@
  \belowdisplayshortskip \abovedisplayskip
  \def\@listi{%
     \leftmargin\leftmargini
     \topsep  6\p@ \@plus2\p@ \@minus1\p@%
     \parsep  \z@
     \itemsep \z@
  }
}
%%
%% \listsize 10/13
%%
\newcommand\listsize{%
  \@setfontsize\listsize{\@xpt}{13}%
  \abovedisplayskip      6.5\p@ \@plus3.25\p@ \@minus1\p@
  \belowdisplayskip      \abovedisplayskip
  \abovedisplayshortskip 3.25\p@ \@plus3.25\p@
  \belowdisplayshortskip \abovedisplayskip
  \let\@listi\@listI
}
%%
%% \normalsize 10/14
%%
\renewcommand\normalsize{%
  \@setfontsize\normalsize{11\p@}{14}%
  \abovedisplayskip      7.5\p@ \@plus 3.75\p@ \@minus1\p@
  \belowdisplayskip      \abovedisplayskip
  \abovedisplayshortskip 3.75\p@ \@plus 3.75\p@
  \belowdisplayshortskip \abovedisplayskip
  \let\@listi\@listI
}
%
% The other sizes are trivial:
%
\renewcommand{\tiny}{\@setfontsize\tiny\@vpt\@vipt}
\renewcommand{\scriptsize}{\@setfontsize\scriptsize\@viipt\@viiipt}
\renewcommand{\large}{\@setfontsize\large{11\p@}{14}}
\renewcommand{\Large}{\@setfontsize\Large{12\p@}{17}}
\renewcommand{\LARGE}{\@setfontsize\LARGE{17\p@}{19}}
\renewcommand{\huge}{\@setfontsize\huge{20\p@}{24}}
\renewcommand{\Huge}{\@setfontsize\Huge{20\p@}{24}}
%
\normalsize
%
%% PAGE LAYOUT (from cupbook.sty; large format)
%%
%% [97-01-13; JHR] changes to increase width by 2pc
%%   \oddsidemargin	4pc --> 3pc
%%   \evensidemargin	4pc --> 3pc
%%   \marginparwidth	4pc --> 3pc
%%   \textwidth		30pc --> 32pc
%%
\setlength{\oddsidemargin}{3pc}
\setlength{\evensidemargin}{3pc}
\setlength{\marginparwidth}{3pc}
\setlength{\marginparsep}{0.5pc}
\setlength{\topmargin}{34pt}
\setlength{\headheight}{14pt}
%% the book style doesn't define footheight, so we make footskip be the
%% sum of footheight and footskip.
%% \setlength{\footheight}{14pt}
%% \setlength{\footskip}{28pt}
\setlength{\footskip}{42pt}
\setlength{\topskip}{11pt}
\setlength{\textheight}{39\baselineskip}
\addtolength{\textheight}{\topskip}
\setlength{\textwidth}{32pc}

\clubpenalty=0         % 'Club line'  at bottom of page.
\widowpenalty=10000    % 'Widow line' at top of page.
%%
%% FOOTNOTES
%%
\setlength{\footnotesep}{6.5pt}
\addtolength{\skip\footins}{9pt plus 4pt minus 2pt}
%%
%% FLOATS
%%
%% floats on a text page
%%
\setlength\floatsep{13\p@ \@plus 2\p@ \@minus 2\p@}
\setlength\textfloatsep{20\p@ \@plus 2\p@ \@minus 4\p@}
\setlength\intextsep{13\p@ \@plus 2\p@ \@minus 2\p@}
%%
%% floats on a separate float page
%%
\setlength\@fptop{\z@ \@plus 0fil}
\setlength\@fpsep{13\p@ \@plus 0fil}
\setlength\@fpbot{\z@ \@plus 3fil}
%%
%% PARAGRAPHING
%%
\setlength\parskip{0pt plus .25pt}
\setlength\parindent{1em}
\setlength\listparindent{\parindent}
\setlength\partopsep{0pt plus 1pt}
%%
%% Section numbering
\renewcommand{\thechapter}{\arabic{chapter}}
\renewcommand{\thesection}{\thechapter{}.\arabic{section}}
\renewcommand{\thesubsection}{\thesection{}.\arabic{subsection}}

%% The following flag controls the adding of entries to the TOC
%%
\newboolean{SFB@addtoc}

%% The following flag is set when we start the appendix
%%
\newboolean{SFB@appendix}

%%
%% We don't want headers or page numbers on blank pages
\newcommand{\clearemptydoublepage}{\newpage{\pagestyle{empty}\cleardoublepage}}

%%
%% APPENDIX
%%
\renewcommand\appendix{%
  \clearemptydoublepage
  \setboolean{SFB@appendix}{true}
  \setcounter{chapter}{0}
  \setcounter{section}{0}
  \addtocontents{toc}{\protect\setcounter{tocdepth}{0}}
  \renewcommand\@chapapp{Appendix}
  \renewcommand\thechapter{\@Alph\c@chapter}
  \renewcommand{\sectionmark}[1]{\markright{\normalfont\itshape{}##1}}
}
%%
%% INDEX
%%
%% NOTE that we use the index style, which supports multiple
%% indexes, so the redefinition of theindex environment needs
%% to be compatible with it.
%%
\RequirePackage{index}
\renewenvironment{theindex}{%
  \clearemptydoublepage
  \if@twocolumn\@restonecolfalse \else \@restonecoltrue \fi%
  \columnseprule \z@ %
  \columnsep 35\p@ %
  \edef\@indexname{%
      \expandafter\the\@nameuse{idxtitle@\@indextype}%
  }%
  \addcontentsline{toc}{chapter}{\normalfont\itshape{}\@indexname}
  \twocolumn[\@makeschapterhead{\@indexname}]%
  \@mkboth{\it\@indexname}%
          {\it\@indexname}%
  \thispagestyle{empty}%
  \parindent\z@%
  \parskip\z@ \@plus .3\p@\relax
  \let\item\@idxitem%
  \footnotesize%
}{%
  \if@restonecol\onecolumn\else\clearpage\fi
}
%%
%% Change these as necessary
%%
%\newcommand\@idxitem{\par\hangindent 40\p@}
\renewcommand\@idxitem{\par\hangindent 2em}
%\newcommand\subitem{\@idxitem \hspace*{20\p@}}
\renewcommand\subitem{\par\hangindent 3em \hspace*{1em}}
%\newcommand\subsubitem{\@idxitem \hspace*{30\p@}}
\renewcommand\subsubitem{\par\hangindent 4em \hspace*{2em}}
%\newcommand\indexspace{\par \vskip 10\p@ \@plus5\p@ \@minus3\p@\relax}
\renewcommand\indexspace{\par \vskip 9\p@ \@plus5\p@ \@minus3\p@\relax}
%%
%% CHAPTER AND SECTION HEADINGS
%%
%%   * Chapter headings are a centered \huge chapter number, over a centered
%%     \LARGE chapter title.
%%   * Section headings are centered and bold
%%   * Subsection headings are left justified and bold
%%   * Subsubsection headings are left justified and italic

\def\@makechapterhead#1{{%
  \normalsize
  \parindent 0pt
  \vspace*{-0.5\baselineskip}
  \raisebox{0.5\baselineskip}[0pt][0pt]{%
    \mbox{\hspace*{\z@}}%
    \begin{minipage}[t]{\textwidth}
      \centering
      \ifthenelse{\boolean{SFB@appendix}}{%
        \huge Appendix \thechapter
      }{%
        \huge\bfseries \thechapter
      }
      \par
      \vskip 9pt
      \LARGE\rm #1
      \nobreak
      \par
    \end{minipage}}
  \par \nobreak
  \vskip 10.5\baselineskip% 2.5 less than the first line of text
}}
\def\@makeschapterhead#1{{%
  \normalsize
  \parindent 0pt
  \vspace*{-0.5\baselineskip}
  \raisebox{0.5\baselineskip}[0pt][0pt]{%
    \mbox{\hspace*{\z@}}%
    \begin{minipage}[t]{\textwidth}
      \centering
      {\huge\bfseries \mbox{}}
      \par
      \vskip 9pt
      \LARGE\rm #1
      \nobreak
      \par
    \end{minipage}}
  \par \nobreak
  \vskip 10.5\baselineskip % 2.5 less than the first line of text
}}
\renewcommand{\chapter}{%
  \clearemptydoublepage
  \thispagestyle{empty}%
  \global\@topnum\z@
  \@afterindentfalse
  \secdef\@chapter\@schapter
}
\def\@chapter[#1]#2{%
  \refstepcounter{chapter}
  \typeout{\@chapapp\space\thechapter.}
  \ifthenelse{\boolean{SFB@appendix}}{%
    \addcontentsline{toc}{chapter}{\protect\SFB@numberline{\thechapter}#1}
    \schaptermark{#1}
  }{%
    \addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}
    \chaptermark{#1}
  }
  \@makechapterhead{#2}
  \@afterheading
}
\def\@schapter#1{%
%  \ifthenelse{\boolean{SFB@addtoc}}{%
%    \addcontentsline{toc}{schapter}{#1}
%  }{}
  \schaptermark{#1}
  \@makeschapterhead{#1}
  \@afterheading
  \normalsize
}
%
%
\renewcommand{\section}{%
  \@startsection{section}{1}{0pt}{-23pt plus-6.5pt minus-2pt}{3pt plus 1pt}{%
    \centering\normalfont\rmfamily\bfseries}}
\renewcommand{\subsection}{%
  \@startsection{subsection}{2}{0pt}{-23pt plus-6.5pt minus-2pt}{3pt plus 1pt}{%
    \normalfont\rmfamily\bfseries}}
\renewcommand{\subsubsection}{%
  \@startsection{subsubsection}{3}{0pt}{-10pt plus-6.5pt minus-2pt}{3pt plus 1pt}{%
    \normalfont\rmfamily\itshape}}

%% PAGE HEADER & FOOT
%%
%%   * The beginning of a chapter has no header, and the page # centered
%%     in the foot. (plain style)
%%   * An odd page has the section # and title in italics in the center, and
%%     the page number on the right (fancy style)
%%   * An even page has the chapter # and title in italics in the center, and
%%     the page number on the left (fancy style)
%\usepackage{fancyheadings}
\usepackage{fancyhdr}
\pagestyle{fancyplain}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
%\plainheadrulewidth 0pt
%\plainfootrulewidth 0pt
\renewcommand{\chaptermark}[1]{%
  \markboth{\normalfont\itshape\thechapter\hspace{0.5em}#1}{%
  \normalfont\itshape\thechapter\hspace{0.5em}#1}}
\newcommand{\schaptermark}[1]{%
  \markboth{\normalfont\itshape{}#1}{\normalfont\itshape{}#1}}
\renewcommand{\sectionmark}[1]{%
  \markright{\normalfont\itshape\thesection\hspace{0.5em}#1}}
\lhead[\fancyplain{}{\rm\thepage}]{\fancyplain{}{}}
\chead[\fancyplain{}{\leftmark}]{\fancyplain{}{\rightmark}}
\rhead[\fancyplain{}{}]{\fancyplain{}{\rm\thepage}}
\lfoot{\fancyplain{}{}}
\cfoot{\fancyplain{\rm\thepage}{}}
\rfoot{\fancyplain{}{}}
%%
%% TABLEOFCONTENTS (this follows David Tranah's patch)
%%
\renewcommand\tableofcontents{%
  \setboolean{SFB@addtoc}{false}
  \chapter*{Contents}
  \setboolean{SFB@addtoc}{true}
  \@starttoc{toc}%
}
%%
%% the command for formatting TOC entries
%%
\newdimen\SFB@toclmarg
\newdimen\SFB@tocindent
\setlength{\SFB@toclmarg}{28\p@}
\setlength{\SFB@tocindent}{65\p@}
%
% Numberline command for Appendix
\def\SFB@numberline#1{%
  \hbox to \SFB@tocindent {{\normalfont\itshape Appendix #1\hfil}}}
%
\newif\ifSFB@firstTOCentry \SFB@firstTOCentrytrue
\def\l@part#1#2{%
  \addpenalty{-\@highpenalty}%
  \begingroup
    \parindent \z@ \rightskip \@pnumwidth
    \parfillskip -\@pnumwidth
    \leavevmode
    \hspace*{\SFB@toclmarg}
    \ifSFB@firstTOCentry
      {\bfseries #1} \hfil \hbox to\@pnumwidth{\hss {\it page\/} #2}\par
      \global\SFB@firstTOCentryfalse
    \else
      {\bfseries #1} \hfil \hbox to\@pnumwidth{\hss #2}\par
    \fi
    \nobreak
  \endgroup
}
\def\l@chapter#1#2{%
  \pagebreak[3]
  \vspace{3pt plus 2pt}%   %% from patch.sty %%
  \setlength{\@tempdima}{\SFB@toclmarg}
  \begingroup
    \parindent \z@  \rightskip \@pnumwidth  \parfillskip -\@pnumwidth
    \leavevmode
    \advance \leftskip \@tempdima  \hskip -\leftskip
    \ifSFB@firstTOCentry
      {\bfseries #1}\nobreak\hfil \nobreak\hbox to\@pnumwidth{\hss {\it page\/} #2}\par
      \global\SFB@firstTOCentryfalse
    \else
      {\bfseries #1}\nobreak\hfil \nobreak\hbox to\@pnumwidth{\hss #2}\par
    \fi
  \endgroup
}
\def\l@schapter#1#2{
  \pagebreak[3]
  \setlength{\@tempdima}{\SFB@toclmarg}
  \vspace{3pt plus 2pt}%   %% following \l@chapter %%
  \begingroup
    \parindent \z@  \rightskip \@pnumwidth  \parfillskip -\@pnumwidth
    \leavevmode
    \advance \leftskip \@tempdima  \hskip -\leftskip
    \ifSFB@firstTOCentry
      {\normalfont\itshape #1}\nobreak\hfil \nobreak\hbox to\@pnumwidth{\hss {\it page\/} #2}\par
      \global\SFB@firstTOCentryfalse
    \else
      {\normalfont\itshape #1}\nobreak\hfil \nobreak\hbox to\@pnumwidth{\hss #2}\par
    \fi
  \endgroup
}
\def\l@section      {\@dottedtocline{1}{\z@}{\SFB@toclmarg}}
\def\l@subsection   {\@dottedtocline{2}{\z@}{\SFB@toclmarg}}
\def\l@subsubsection{\@dottedtocline{3}{\z@}{\SFB@toclmarg}}
\def\l@paragraph    {\@dottedtocline{4}{\z@}{\SFB@toclmarg}}
\def\l@subparagraph {\@dottedtocline{5}{\z@}{\SFB@toclmarg}}
%%
%% LIST OF FIGURES
%%
%% CUP uses "List of Illustrations"  We should probably redefine \listoffigures
%% so that we get a TOC entry.
%%
\renewcommand{\listoffigures}{%
  \bgroup
    \chapter*{List of Illustrations}
    \normalsmall
    \setcounter{tocdepth}{1}
    \@starttoc{lof}
  \egroup
}
\def\l@figure{\@dottedtocline{1}{\z@}{\SFB@toclmarg}}
%%
%% \@dottedtocline{LEVEL}{INDENT}{NUMWIDTH}{TITLE}{PAGE} without the dots
%% (from the CUP style file)
%%
\def\@dottedtocline#1#2#3#4#5{%
  \ifnum #1>\c@tocdepth
  \else
    \vskip \z@ plus .2pt
    { \leftskip #2\relax \rightskip \@tocrmarg plus2em % for ragged right entries
      \parfillskip -\rightskip
      \parindent #2\relax
      \@afterindenttrue
      \interlinepenalty\@M
      \leavevmode
      \@tempdima #3\relax \advance\leftskip \@tempdima \hbox{}\hskip -\leftskip
      #4\nobreak
      \hfill \nobreak
      \ifSFB@firstTOCentry
	\hbox to\@pnumwidth{\hfil\rm {\it page\/} #5}\par
	\global\SFB@firstTOCentryfalse
      \else
        \hbox to\@pnumwidth{\hfil\rm #5}\par
      \fi
    }
  \fi
}
%    ****************************************
%    *             BIBLIOGRAPHY             *
%    ****************************************
%
\renewcommand{\thebibliography}[1]{%
  \chapter*{Bibliography}
  \addcontentsline{toc}{chapter}{\normalfont\itshape{}Bibliography}
  \typeout{Bibliography.}
  \bibliographylist
}
\let\endthebibliography=\endlist
\def\bibliographylist{%
  \smallish
  \list{}{\labelwidth\z@
    \leftmargin 1pc% \advance\leftmargin\labelsep%%ajw change
    \itemsep \z@ plus .1pt
    \itemindent-\leftmargin
  }
  \parindent\z@
  \parskip\z@ plus .1pt\relax
  \def\newblock{\hskip .11em plus .33em minus .07em}
  \sloppy\clubpenalty4000\widowpenalty4000
  \sfcode`\.=1000\relax
}
\let\endthebibliographylist=\endlist
%
% Load ML-Doc specific macros
\RequirePackage{mldoc-code}
\usepackage{mldoc}
%
\makeatother
