% tex2page.tex
% Dorai Sitaram

% TeX files using these macros
% can be converted by the program
% tex2page into HTML

\ifx\shipout\UnDeFiNeD\endinput\fi

\message{version 2005-09-11} % last change 

\let\texonly\relax
\let\endtexonly\relax

%\def\verbwritefile#1 {\relax}
%\let\verbwrite\gobbleencl

\def\verbwritefile{%
  \ifx\verbwritefileQport\UNDEFINED
    \expandafter\csname newwrite\endcsname\verbwritefileQport 
  \else\immediate\closeout\verbwritefileQport
  \fi
  \futurelet\verbwritefileQnext\verbwritefileQcheckchar}

\def\verbwritefileQcheckchar{%
  \ifx\verbwritefileQnext\bgroup
    \let\verbwritefileQnext\verbwritefileQbracedfile
  \else
    \let\verbwritefileQnext\verbwritefileQspacedfile
  \fi\verbwritefileQnext}

\def\verbwritefileQspacedfile#1 {%
  \immediate\openout\verbwritefileQport #1
}

\def\verbwritefileQbracedfile#1{%
  \verbwritefileQspacedfile #1
}

\def\verbwrite{%
  \ifx\verbwritefileQport\UNDEFINED
    \verbwritefile \jobname.txt \fi
  \begingroup
    \def\do##1{\catcode`##1=12 }\dospecials
    \catcode`\{=1 \catcode`\}=2 
    \catcode`\^^M=12 \newlinechar=`\^^M%
    \futurelet\verbwriteQopeningchar\verbwriteQii}

\def\verbwriteQii{\ifx\verbwriteQopeningchar\bgroup
  \let\verbwriteQiii\verbwriteQbrace\else
  \let\verbwriteQiii\verbwriteQnonbrace\fi
  \verbwriteQiii}

\def\verbwriteQbrace#1{\immediate
  \write\verbwritefileQport{#1}\endgroup}

\def\verbwriteQnonbrace#1{%
  \catcode`\{12 \catcode`\}12
  \def\verbwriteQnonbraceQii##1#1{%
    \immediate\write\verbwritefileQport{##1}\endgroup}%
  \verbwriteQnonbraceQii} 

\ifx\slatexignorecurrentfile\UNDEFINED\relax\fi

\edef\atcatcodebeforetiip{\the\catcode`\@ }
\catcode`\@11

% margins

\def\sidemargin{\afterassignment\sidemarginQadjustoffset
  \hoffset}

\def\sidemarginQadjustoffset{%
  \advance\hoffset -1true in
  \advance\hsize -2\hoffset}

\def\vertmargin{\afterassignment\vertmarginQadjustoffset
  \voffset}

\def\vertmarginQadjustoffset{%
  \advance\voffset -1true in
  \advance\vsize -2\voffset}

%

\def\defcsactive#1{\defnumactive{`#1}}

\def\defnumactive#1{\catcode#1\active
  \begingroup\lccode`\~#1%
    \lowercase{\endgroup\def~}}

% gobblegobblegobble

\def\gobblegroup{\bgroup
  \def\do##1{\catcode`##1=9 }\dospecials
  \catcode`\{1 \catcode`\}2 \catcode`\^^M=9
  \gobblegroupI}

\def\gobblegroupI#1{\egroup}

\def\gobbleencl{\bgroup
  \def\do##1{\catcode`##1=12 }\dospecials
  \catcode`\{1 \catcode`\}2 \catcode`\^^M=9
  \futurelet\gobbleenclnext\gobbleenclI}

\def\gobbleenclI{\ifx\gobbleenclnext\bgroup
    \let\gobbleenclnext\gobblegroupI
  \else\let\gobbleenclnext\gobbleenclII\fi
  \gobbleenclnext}

\def\gobbleenclII#1{%
  \def\gobbleenclIII##1#1{\egroup}%
  \gobbleenclIII}

% \verb
% Usage: \verb{...lines...} or \verb|...lines...|
% In the former case, | can be used as escape char within
% the verbatim text

\let\verbhook\relax

\def\verbfont{\tt}
%\hyphenchar\tentt-1

\def\verbsetup{\frenchspacing
  \def\do##1{\catcode`##1=12 }\dospecials
  \catcode`\|=12 % needed?
  \verbfont
  \edef\verbQoldhyphenchar{\the\hyphenchar\font}%
  \hyphenchar\font-1
  \def\verbQendgroup{\hyphenchar\font\verbQoldhyphenchar\endgroup}%
}

% The current font is cmtt iff fontdimen3 = 0 _and_
% fontdimen7 != 0

\def\checkifusingcmtt{\let\usingcmtt n%
  \ifdim\the\fontdimen3\the\font=0.0pt
    \ifdim\the\fontdimen7\the\font=0.0pt
    \else\let\usingcmtt y\fi\fi}

% In a nonmonospaced font, - followed by a letter
% is a regular hyphen.  Followed by anything else, it is a
% typewriter hyphen.

\def\variablelengthhyphen{\futurelet\variablelengthhyphenI
  \variablelengthhyphenII}

\def\variablelengthhyphenII{\ifcat\noexpand\variablelengthhyphenI
 a-\else{\tt\char`\-}\fi}

\def\verbavoidligs{% avoid ligatures
  \defcsactive\`{\relax\lq}%
  \defcsactive\ {\leavevmode\ }%
  \defcsactive\^^I{\leavevmode\ \ \ \ \ \ \ \ }%
  \defcsactive\^^M{\leavevmode\endgraf}%
  \checkifusingcmtt
  \ifx\usingcmtt n%
  \defcsactive\<{\relax\char`\<}%
  \defcsactive\>{\relax\char`\>}%
  \defcsactive\-{\variablelengthhyphen}%
  \fi}

\def\verbinsertskip{%
  \let\firstpar y%
  \defcsactive\^^M{\ifx\firstpar y%
    \let\firstpar n%
    \verbdisplayskip
    \parskip 0pt
    \aftergroup\verbdisplayskip
    \else\leavevmode\fi\endgraf}%
  \verbhook}

\ifx\verb\UnDeFiNeD\else
% Save LaTeX's \verb away, because
% we'll be defining our own \verb
\let\LaTeXverb\verb
\fi

%\def\verb{\begingroup
%  \verbsetup\verbI}

\def\verb{\begingroup
  \verbsetup\verbavoidligs\verbQcheckstar}

\def\verbQcheckstar{%
  \futurelet\verbQcheckstarQnext\verbQcheckstarQii}

\def\verbQcheckstarQii{%
  \if\verbQcheckstarQnext*%
    \let\verbQcheckstarQnext\verbQcheckstarQiii
  \else
    \let\verbQcheckstarQnext\verbI
  \fi
  \verbQcheckstarQnext}

\def\verbQcheckstarQiii#1{%
  \defcsactive\ {\relax\char`\ }%
  \verbI}

\def\verbc{\begingroup
  \verbsetup\afterassignment\verbcI
  \let\verbcII=}

\def\verbcI{{\verbfont\verbcII}\endgroup}

\let\E\verbc

\newcount\verbbracebalancecount

\def\verblbrace{\char`\{}
\def\verbrbrace{\char`\}}

\def\verbescapechar#1{%
  \def\escapifyverbescapechar{\catcode`#1=0 }}

\verbescapechar\|

{\catcode`\[1 \catcode`\]2
\catcode`\{12 \catcode`\}12
\gdef\verbI#1[%\verbavoidligs
  \verbinsertskip\verbhook
  %\edef\verbQoldhyphenchar{\the\hyphenchar\tentt}%
  %\hyphenchar\tentt=-1
  %\def\verbQendgroup{\hyphenchar\tentt\verbQoldhyphenchar\endgroup}%
  %\let\verbQendgroup\endgroup%
  \if#1{\escapifyverbescapechar
    \def\{[\char`\{]%
    \def\}[\char`\}]%
    \def\|[\char`\|]%
    \verbbracebalancecount0
    \defcsactive\{[\advance\verbbracebalancecount by 1
      \verblbrace]%
    \defcsactive\}[\ifnum\verbbracebalancecount=0
      \let\verbrbracenext\verbQendgroup\else
      \advance\verbbracebalancecount by -1
      \let\verbrbracenext\verbrbrace\fi
      \verbrbracenext]\else
  \defcsactive#1[\verbQendgroup]\fi
  \verbII
]]

\def\verbII{\futurelet\verbIInext\verbIII}

{\catcode`\^^M\active%
\gdef\verbIII{\ifx\verbIInext^^M\else%
  \defcsactive\^^M{\leavevmode\ }\fi}}

\let\verbdisplayskip\medbreak

% \verbatiminput FILENAME
% displays contents of file FILENAME verbatim.

%\def\verbatiminput#1 {{\verbsetup\verbavoidligs\verbhook
%  \input #1 }}

% ^ original \verbatiminput

\ifx\verbatiminput\UNDEFINED
% LaTeX's (optional) verbatim package defines a \verbatiminput --
% don't clobber it
\def\verbatiminput{%
  \futurelet\verbatiminputQnext\verbatiminputQcheckchar}%
\fi

\def\verbatiminputQcheckchar{%
  \ifx\verbatiminputQnext\bgroup
    \let\verbatiminputQnext\verbatiminputQbracedfile
  \else
    \let\verbatiminputQnext\verbatiminputQspacedfile
  \fi\verbatiminputQnext}

\def\verbatiminputQbracedfile#1{\verbatiminputQdoit{#1}}

\def\verbatiminputQspacedfile#1 {\verbatiminputQdoit{#1}}

\def\verbatiminputQdoit#1{{\verbsetup
  \verbavoidligs\verbhook
  \input #1 }}


% \url{URL} becomes
% <a href="URL">URL</a> in HTML, and
% URL in DVI.

% A-VERY-VERY-LONG-URL in a .bib file
% could be split by BibTeX
% across a linebreak, with % before the newline.
% To accommodate this, %-followed-by-newline will
% be ignored in the URL argument of \url and related
% macros.

\ifx\url\UnDeFiNeD
\def\url{\bgroup\urlsetup\let\dummy=}%
\fi

\def\urlsetup{\verbsetup\urlfont\verbavoidligs
  \catcode`\{1 \catcode`\}2
  \defcsactive\%{\urlpacifybibtex}%
  \defcsactive\ {\relax}%
  \defcsactive\^^M{\relax}%
  \defcsactive\.{\discretionary{}{\char`\.}{\char`\.}}%
  \defcsactive\/{\discretionary{\char`\/}{}{\char`\/}}%
  \defcsactive\`{\relax\lq}}

\let\urlfont\relax

\def\urlpacifybibtex{\futurelet\urlpacifybibtexnext\urlpacifybibtexI}

\def\urlpacifybibtexI{\ifx\urlpacifybibtexnext^^M%
  \else\%\fi}

% \mailto{ADDRESS} becomes
% <a href="mailto:ADDRESS">ADDRESS</a> in HTML, and
% ADDRESS in DVI.

\let\mailto\url

% \urlh{URL}{TEXT} becomes
% <a href="URL">TEXT</a> in HTML, and
% TEXT in DVI.

% If TEXT contains \\, the part after \\ appears in
% the DVI only.  If, further, this part contains \1,
% the latter is replaced by a fixed-width representation
% of URL.

\def\urlh{\bgroup\urlsetup
  \afterassignment\urlhI
  \gdef\urlhQurlarg}

\def\urlhI{\egroup
  \bgroup
    \let\\\relax
    \def\1{{\urlsetup\urlhQurlarg}}%
    \let\dummy=}

\def\urlp#1{{#1} \bgroup\urlsetup
  \afterassignment\urlpQwrapparens
  \gdef\urlpQurlarg}

\def\urlpQwrapparens{\egroup
  {\rm(}{\urlsetup\urlpQurlarg}{\rm)}}

% \urlhd{URL}{HTML-TEXT}{DVI-TEXT} becomes
% <a href="URL">HTML-TEXT</a> in HTML, and
% DVI-TEXT in DVI

\def\urlhd{\bgroup
  \def\do##1{\catcode`##1=12 }\dospecials
  \catcode`\{1 \catcode`\}2
  \urlhdQeaturlhtmlargs}

\def\urlhdQeaturlhtmlargs#1#2{\egroup}

\ifx\hyperref\UnDeFiNeD
\let\href\urlh
\let\hypertarget\gobblegroup
\let\hyperlink\gobblegroup
\def\hyperref#1#2#3#4{#2\ref{#4}#3}
\fi

%

\let\ignorenextinputtimestamp\relax

% don't let caps disable end-of-sentence spacing

\def\nocapdot{%
\count255=`\A
\loop
\sfcode\the\count255=1000
\ifnum\count255<`\Z
\advance\count255 by 1
\repeat
}

%

%\ifx\newenvironment\UnDeFiNeD
\let\htmlonly\iffalse
\let\endhtmlonly\fi
%\else
%\usepackage{comment}
%\excludecomment{htmlonly}
%\fi

\def\rawhtml{\errmessage{Can't occur outside
  \string\htmlonly}}
\def\endrawhtml{\errmessage{Can't occur outside
  \string\htmlonly}}

\let\htmlheadonly\iffalse
\let\endhtmlheadonly\fi

\let\cssblock\iffalse
\let\endcssblock\fi

\def\inputcss#1 {\relax}
\let\htmlstylesheet\inputcss
\let\htmladdimg\gobblegroup

\def\htmlref{\bgroup\aftergroup\gobblegroup\let\dummy=}

%

\let\htmlcolophon\gobblegroup
\let\htmldoctype\gobblegroup
\let\htmlmathstyle\gobblegroup
\let\htmladvancedentities\relax

\let\manpagesection\gobblegroup
\let\manpagedescription\gobblegroup

% Scheme

\let\scm\verb
\let\scminput\verbatiminput

\let\scmwritefile\verbwritefile
\let\scmwrite\verbwrite
\let\scmdribble\scm

\ifx\slatexversion\UNDEFINED
% SLaTeX compat
\let\scmkeyword\gobblegroup
\let\scmbuiltin\gobblegroup
\let\scmconstant\scmbuiltin
\let\scmvariable\scmbuiltin
\let\setbuiltin\scmbuiltin
\let\setconstant\scmbuiltin
\let\setkeyword\scmkeyword 
\let\setvariable\scmvariable
\def\schemedisplay{\begingroup
  \verbsetup\verbavoidligs
  \verbinsertskip
  \schemedisplayI}%
\def\schemeresponse{\begingroup
  \verbsetup\verbavoidligs
  \verbinsertskip
  \schemeresponseI}%
{\catcode`\|0 |catcode`|\12
  |long|gdef|schemedisplayI#1\endschemedisplay{%
    #1|endgroup}%
  |long|gdef|schemeresponseI#1\endschemeresponse{%
    #1|endgroup}}%
\fi

\let\slatexlikecomments\relax
\let\noslatexlikecomments\relax

% definitions (useful in reference manuals)

\ifx\@@line\UnDeFiNeD
\let\@@line\line
\fi

\def\defun#1{\def\defuntype{#1}%
\medbreak
\@@line\bgroup
  \hbox\bgroup
    \aftergroup\enddefun
    \vrule width .5ex \thinspace
    \vrule \enspace
    \vbox\bgroup\setbox0=\hbox{\defuntype}%
      \advance\hsize-\wd0
      \advance\hsize-1em
      \obeylines
      \parindent=0pt
      \aftergroup\egroup
      \strut
      \let\dummy=}

\def\enddefun{\hfil\defuntype\egroup\smallskip}


% Images

\let\imgdef\def

%\def\imgpreamble{\let\magnificationoutsideimgpreamble\magnification
%  \def\magnification{\count255=}}
%
%\def\endimgpreamble{\let\magnification\magnificationoutsideimgpreamble}


\let\imgpreamble\iffalse
\let\endimgpreamble\fi

\let\makehtmlimage\relax


% Tally control sequences are cheap count
% registers: they doesn't use up TeX's limited number of
% real count registers.

% A tally is a macro that expands to the
% number kept track of.  Thus \edef\kount{0} defines a
% tally \kount that currently contains 0.

% \advancetally\kount n increments \kount by n.
% \globaladvancetally increments the global \kount.
% If \kount is not defined, the \[global]advancetally
% macros define it to be 0 before proceeding with the
% incrementation.

\def\newtally#1{\edef#1{0}}

\def\advancetallyhelper#1#2#3{%
  \ifx#2\UNDEFINED
    #1\edef#2{0}\fi
  \edef\setcountCCLV{\count255=#2 }%
  \setcountCCLV
  \advance\count255 by #3
  #1\edef#2{\the\count255 }}

\def\advancetally{\advancetallyhelper\relax}
\def\globaladvancetally{\advancetallyhelper\global}

% plain's \beginsection splits pages too easily

%\def\beginsection#1\par{\sectionhelp{1}{}{#1}}

\def\beginsection{\vskip-\lastskip
  \bigbreak\noindent
  \bgroup\bf
    \let\par\sectionafterskip}

\def\beginsectionstar*{\beginsection}

% plain's \{left,center,right}line can't handle catcode change
% within their argument

\def\leftline{\@@line\bgroup\bgroup
  \aftergroup\leftlinefinish
  \let\dummy=}

\def\leftlinefinish{\hss\egroup}

\def\centerline{\@@line\bgroup\bgroup
  \aftergroup\leftlinefinish
  \hss\let\dummy=}

\def\rightline{\@@line\bgroup\hss\let\dummy=}

%

\let\strike\fiverm % can be much better!

%

\let\htmlpagebreak\relax

\let\htmlpagelabel\gobblegroup

\def\htmlpageref{\errmessage{Can't occur except inside
  \string\htmlonly}}

% Miscellaneous stuff

%\def\hr{$$\hbox{---}$$}
\def\hr{\medbreak\centerline{---}\medbreak}
%\def\hr{\par\centerline{$*$}\par}
%\def\hr{\smallskip\@@line{\leaders\hbox{~.~}\hfill}\smallskip}

%Commonplace math that doesn't require image files.  (Avoiding $
%here because $ triggers image-file generation.)

\let\nohtmlmathimg\relax
\let\nohtmlmathintextimg\relax
\let\nohtmlmathdisplayimg\relax

\let\htmlimageformat\gobblegroup
\let\htmlimgmagnification\gobblegroup

\let\externaltitle\gobblegroup

\def\mathg{$\bgroup\aftergroup\closemathg\let\dummy=}
\def\closemathg{$}

\let\mathp\mathg

\def\mathdg{$$\bgroup\aftergroup\closemathdg\let\dummy=}
\def\closemathdg{$$}

%


\let\evalh\gobblegroup
\let\evalq\gobblegroup

% Backward compatible stuff

\let\endgifpreamble\endimgpreamble
\let\endhtmlgif\relax
\let\endhtmlimg\relax
\let\gifdef\imgdef
\let\gifpreamble\imgpreamble
%\let\href\urlh
\let\htmlgif\relax
\let\htmlimg\relax
\let\htmlimgformat\htmlimageformat
\let\n\noindent
\let\p\verb
\let\q\scm
\let\scmfile\scmdribble
\let\scmfileonly\scmwrite
\let\scmp\scm
%\let\scmverbatim\scm
\let\scmverbatimfile\scminput
\let\setverbatimescapechar\verbescapechar
%\let\verbatim\verb
\let\verbatimfile\verbatiminput
\let\verbinput\verbatiminput
\let\verbfilename\verbwritefile
\let\scmfilename\scmwritefile

% uppercase version of \romannumeral

\def\Romannumeral{\afterassignment\RomannumeralI\count255=}

\def\RomannumeralI{\uppercase\expandafter{\romannumeral\the\count255 }}

\def\f{\footnote}

\ifx\label\UnDeFiNeD
\else 
\def\tag#1#2{\@bsphack
  \protected@write\@auxout{}%
    {\string\newlabel{#1}{{#2}{\thepage}}}%
\@esphack}%
\let\tagref\ref
\fi

\def\inputexternallabels#1 {\relax}
\def\includeexternallabels#1 {\relax}

\ifx\IfFileExists\UnDeFiNeD
\def\IfFileExists#1#2#3{%
  \openin0 #1 %
  \ifeof0 %
    #3%
  \else 
    #2\fi
  \closein0 }%
\fi

\ifx\eval\UnDeFiNeD
\IfFileExists{eval4tex.tex}{\input eval4tex }{}\fi

\let\schemeeval\eval

\def\inputepsf{%
\ifx\pdfoutput\UNDEFINED
  \input epsf
\else
  \input supp-pdf
  \def\epsfbox##1{\convertMPtoPDF{##1}{1}{1}}%
\fi
}

% The rest of the file isn't needed for LaTeX

\ifx\section\UnDeFiNeD
\let\maybeloadfollowing\relax
\else 
\catcode`\@\atcatcodebeforetiip
\let\maybeloadfollowing\endinput
\fi\maybeloadfollowing

% LaTeX stops loading here!

% Title

\newwrite\sectionscratchfileport

\def\subject{%
  \immediate\openout\sectionscratchfileport Z-sec-temp
  \begingroup
    \def\do##1{\catcode`##1=11 }\dospecials
    \catcode`\{=1 \catcode`\}=2
    \subjectI}

\def\subjectI#1{\endgroup
  \immediate\write\sectionscratchfileport {#1}%
  \immediate\closeout\sectionscratchfileport
  $$\vbox{\bf \def\\{\cr}%
      \halign{\hfil##\hfil\cr
        \input Z-sec-temp
        \cr}}$$%
  \medskip}

\let\title\subject

% Sections

\def\tracksectionchangeatlevel#1{%
  \expandafter\let\expandafter\thiscount\csname
    sectionnumber#1\endcsname
  \ifx\thiscount\relax
    \expandafter\edef\csname sectionnumber#1\endcsname{0}%
  \fi
  \expandafter\advancetally
    \csname sectionnumber#1\endcsname 1%
  \ifx\doingappendix0%
    \edef\recentlabel{\csname sectionnumber1\endcsname}%
  \else
    %\count255=\expandafter\csname sectionnumber1\endcsname
    \edef\recentlabel{\char\csname sectionnumber1\endcsname}%
  \fi
  \count255=0
  \loop
    \advance\count255 by 1
    \ifnum\count255=1
    \else\edef\recentlabel{\recentlabel.\csname
      sectionnumber\the\count255\endcsname}\fi
  \ifnum\count255<#1%
  \repeat
  \loop
    \advance\count255 by 1
    \expandafter\let\expandafter\nextcount\csname
      sectionnumber\the\count255\endcsname
    \ifx\nextcount\relax
      \let\continue0%
    \else
      \expandafter\edef\csname
        sectionnumber\the\count255\endcsname{0}%
      \let\continue1\fi
  \ifx\continue1%
  \repeat}

% Vanilla section-header look -- change this macro for new look

\newcount\secnumdepth

\secnumdepth=10

\def\sectiond#1{\count255=#1%
  \ifx\usingchapters1\advance\count255 by 1 \fi
  \edef\sectiondlvl{\the\count255 }%
  \futurelet\sectionnextchar\sectiondispatch}

\def\sectiondispatch{\ifx\sectionnextchar*%
  \def\sectioncontinue{\sectionstar{\sectiondlvl}}\else
  \ifnum\sectiondlvl>\secnumdepth
  \def\sectioncontinue{\sectionhelp{\sectiondlvl}{}}\else
  \tracksectionchangeatlevel{\sectiondlvl}
  \def\sectioncontinue{\sectionhelp{\sectiondlvl}%
    {\recentlabel\enspace}}\fi\fi
  \sectioncontinue}

\def\sectionstar#1*{\sectionhelp{#1}{}}


\def\sectionhelp#1#2{%
  \edef\sectiondepth{#1}%
  \def\sectionnr{#2}%
  \immediate\openout\sectionscratchfileport Z-sec-temp
  \begingroup
  \def\do##1{\catcode`##1=11 }\dospecials
  \catcode`\{=1 \catcode`\}= 2
  \sectionheader}

\def\sectionheader#1{\endgroup
  \immediate\write\sectionscratchfileport {#1}%
  \immediate\closeout\sectionscratchfileport 
  \vskip -\lastskip
  \ifnum\sectiondepth>\tocdepth\else
  \tocactivate
  {\let\folio0%
   \edef\temp{\write\tocout
     {\string\tocentry{\sectiondepth}{\sectionnr}{#1}{\folio}}}%
   \temp}\fi
  \vskip1.5\bigskipamount
\goodbreak %???
  \noindent
  \hbox{\vtop{\pretolerance 10000
    \raggedright
    \noindent\bf\sectionnr
    \input Z-sec-temp }}%
  \bgroup\let\par\sectionafterskip}

% \edef\temp{\write\tocout{\string\hskip#1\space em\string\relax\space #2%
%    \string\vtop{\string\hsize=.7\string\hsize
%    \string\noindent\string\raggedright\space #3}\string\par}}\temp

\def\sectionafterskip{\egroup\nobreak\par\noindent}

\def\section{\sectiond1}
\def\subsection{\sectiond2}
\def\subsubsection{\sectiond3}
\def\paragraph{\sectiond4}
\def\subparagraph{\sectiond5}

\let\usingchapters0

\def\chapter{\global\let\usingchapters1%
\futurelet\chapternextchar\chapterdispatch}

\def\chapterdispatch{\ifx\chapternextchar*%
  \let\chaptercontinue\chapterstar\else
  \tracksectionchangeatlevel{1}%
  \def\chaptercontinue{\chapterhelp{\recentlabel\enspace}}\fi
  \chaptercontinue}

\def\chapterstar*{\chapterhelp{}}

\def\chapterhelp#1{%
  % #1=number #2=heading-text
  \def\chapternr{#1}%
  \immediate\openout\sectionscratchfileport Z-sec-temp
  \begingroup
  \def\do##1{\catcode`##1=11 }\dospecials
  \catcode`\{=1 \catcode`\}=2
  \chapterheader}

\def\chapterheader#1{\endgroup
  \immediate\write\sectionscratchfileport {#1}%
  \immediate\closeout\sectionscratchfileport
  \tocactivate
  {\let\folio0%
   \edef\temp{\write\tocout{\string\tocentry{1}{\chapternr}{#1}{\folio}}}%
   \temp}%
  \vfill\eject
  \null\vskip3em
  \noindent
  \ifx\chapternr\empty\hbox{~}\else
  \ifx\doingappendix0%
    \hbox{\bf Chapter \chapternr}\else
  \hbox{\bf Appendix \chapternr}\fi\fi
  \vskip 1em
  \noindent
  \hbox{\bf\vtop{%\hsize=.7\hsize
    \pretolerance 10000
    \noindent\raggedright\input Z-sec-temp }}%
  \bgroup\let\par\chapterafterskip}

\def\chapterafterskip{\egroup\nobreak\vskip3em \noindent}

\let\doingappendix=0

\def\appendix{\let\doingappendix=1%
  \count255=`\A%
  \advance\count255 by -1
  \expandafter\edef\csname
    sectionnumber1\endcsname{\the\count255 }}


% toc

\let\tocactive0

\newcount\tocdepth

\tocdepth=10

\def\tocoutensure{\ifx\tocout\UNDEFINED
  \csname newwrite\endcsname\tocout\fi}

\def\tocactivate{\ifx\tocactive0%
  \tocoutensure
  \tocsave
  \openout\tocout \jobname.toc
  \global\let\tocactive1\fi}

\def\tocspecials{\def\do##1{\catcode`##1=12 }\dospecials}

\def\tocsave{\openin0=\jobname.toc
  \ifeof0 \closein0 \else
    \openout\tocout Z-T-\jobname.tex
    \let\tocsaved 0%
    \loop
      \ifeof0 \closeout\tocout
        \let\tocsaved1%
      \else{\tocspecials
         \read0 to \tocsaveline
         \edef\temp{\write\tocout{\tocsaveline}}\temp}%
      \fi
    \ifx\tocsaved0%
    \repeat
  \fi
  \closein0 }

\def\tocentry#1#2{%
  %#1=depth #2=secnum
  \ifnum#1=1
    \ifnum\tocdepth>2
    \medbreak\begingroup\bf
    \else\begingroup\fi
  \else\begingroup\fi
  \vtop\bgroup\raggedright
  \noindent\hskip #1 em
  #2%
  \bgroup
  \aftergroup\tocentryI
  %read section title
  \let\dummy=}

\def\tocentryI#1{%
  %#1=page nr
  , #1\strut\egroup
  \endgroup\par
}

\def\tableofcontents{%
  \ifx\tocactive0%
    \openin0 \jobname.toc
    \ifeof0 \closein0 \else
      \closein0 \input \jobname.toc
    \fi
    \tocoutensure
    \openout\tocout \jobname.toc
    \global\let\tocactive1%
  \else
    \input Z-T-\jobname.tex
  \fi}

% allow {thebibliography} to be used directly
% in (plain-TeX) source document without 
% generating it via BibTeX

\ifx\thebibliography\UnDeFiNeD
\def\thebibliography#1{\vskip-\lastskip
  \begingroup
  \def\endthebibliography{\endgroup\endgroup}%
  \def\input##1 ##2{\relax}%
  \setbox0=\hbox{\biblabelcontents{#1}}%
  \biblabelwidth=\wd0
  \@readbblfile}%
\fi

% Cross-references

% \openxrefout loads all the TAG-VALUE associations in
% \jobname.xrf and then opens \jobname.xrf as an
% output channel that \tag can use

\def\openxrefout{%
  \openin0=\jobname.xrf
  \ifeof0 \closein0 
  \else \closein0 {\catcode`\\0 \input \jobname.xrf }%
  \fi
  \expandafter\csname newwrite\endcsname\xrefout
  \openout\xrefout=\jobname.xrf
}

% I'd like to call \openxrefout lazily, but
% unfortunately it produces a bug in MiKTeX.
% So let's call it up front.

\openxrefout

% \tag{TAG}{VALUE} associates TAG with VALUE.
% Hereafter, \ref{TAG} will output VALUE.
% \tag stores its associations in \xrefout.
% \tag calls \openxrefout if \jobname.xrf hasn't
% already been opened

\def\tag#1#2{\ifx\xrefout\UNDEFINED\openxrefout\fi
  {\let\folio0%
    \edef\temp{%
     \write\xrefout{\string\expandafter\string\gdef
        \string\csname\space XREF#1\string\endcsname
        {#2}\string\relax}}%
    \temp}}

% \tagref{TAG} outputs VALUE, assuming \tag put such
% an association into \xrefout.  \tagref calls
% \openxrefout if \jobname.xrf hasn't already
% been opened

% Later, we will \let \ref = \tagref after making
% sure we aren't in eplain, which uses the ctlseq
% \ref differently

\def\tagref#1{\ifx\xrefout\UNDEFINED\openxrefout\fi
  \expandafter\ifx\csname XREF#1\endcsname\relax
  %\message or \write16 ?
  \message{\the\inputlineno: Unresolved label `#1'.}?\else
  \csname XREF#1\endcsname\fi}

% \label, as in LaTeX

\let\recentlabel\relax

% The sectioning commands
% define \recentlabel so a subsequent call to \label will pick up the
% right label.

\def\label#1{\tag{#1}{\recentlabel}%
  \tag{PAGE#1}{\folio}}

% \pageref, as in LaTeX

\def\pageref#1{\ref{PAGE#1}}

% eplain users see the \ref they are used to.  Others
% have \ref = \tagref

\ifx\eplain\UnDeFiNeD
\let\ref\tagref
\fi

%


\ifx\InputIfFileExists\UnDeFiNeD
\def\InputIfFileExists#1#2#3{%
  \IfFileExists{#1}{#2\input #1 }{#3}}%
\fi

\let\iffileexists\IfFileExists

%

% dummy def to let load dvipsnam.def 

\ifx\ProvidesFile\UnDeFiNeD
\def\ProvidesFile#1[#2]{}%
\fi

%

% Index generation
%
% Your TeX source contains \index{NAME} to
% signal that NAME should be included in the index.
% Check the makeindex documentation to see the various
% ways NAME can be specified, eg, for subitems, for
% explicitly specifying the alphabetization for a name
% involving TeX control sequences, etc.
%
% The first run of TeX will create \jobname.idx.
% makeindex on \jobname[.idx] will create the sorted
% index \jobname.ind.
%
% Use \inputindex (without arguments) to include this
% sorted index, typically somewhere to the end of your
% document.  This will produce the items and subitems.
% It won't produce a section heading however -- you
% will have to typeset one yourself.
%
% Use \printindex instead of \inputindex if you want
% the section heading ``Index'' automatically generated.

\def\sanitizeidxletters{\def\do##1{\catcode`##1=11 }%
  \do\\\do\$\do\&\do\#\do\^\do\_\do\%\do\~%
  \do\@\do\"\do\!\do\|\do\-\do\ \do\'}

\def\index{%\unskip
  \ifx\indexout\UNDEFINED
    \csname newwrite\endcsname\indexout
    \openout\indexout \jobname.idx\fi
  \begingroup
    \sanitizeidxletters
    \indexI}

\def\indexI#1{\endgroup
  \write\indexout{\string\indexentry{#1}{\folio}}%
  \ignorespaces}

% The following index style indents subitems on a
% separate lines

\def\theindex{\begingroup
  \parskip0pt \parindent0pt
  \def\indexitem##1{\par\hangindent30pt \hangafter1
    \hskip ##1 }%
  \def\item{\indexitem{0em}}%
  \def\subitem{\indexitem{2em}}%
  \def\subsubitem{\indexitem{4em}}%
  \def\see{{\it see} \bgroup\aftergroup\gobblegroup\let\dummy=}%
  \let\indexspace\medskip}

\def\endtheindex{\endgroup}

% \packindex declares that subitems be bundled into one
% semicolon-separated paragraph

\def\packindex{%
  \def\theindex{\begingroup
    \parskip0pt \parindent0pt
    \def\item{\par\hangindent20pt \hangafter1 }%
    \def\subitem{\unskip; }%
    \def\subsubitem{\unskip; }%
    \def\see{\bgroup\it see \aftergroup\gobblegroup\let\dummy=}%
    \let\indexspace\medskip}}

\def\inputindex{%
  \openin0 \jobname.ind
  \ifeof0 \closein0
    \message{\jobname.ind missing.}%
  \else\closein0
    \begingroup
      \def\begin##1{\csname##1\endcsname}%
      \def\end##1{\csname end##1\endcsname}%
      \input\jobname.ind
    \endgroup\fi}

\def\printindex{\csname beginsection\endcsname Index\par
  \inputindex}

%

\def\italiccorrection{\futurelet\italiccorrectionI
  \italiccorrectionII}

\def\italiccorrectionII{%
  \if\noexpand\italiccorrectionI,\else
  \if\noexpand\italiccorrectionI.\else
  \/\fi\fi}

\def\em{\it\ifmmode\else\aftergroup\italiccorrection\fi}

%\def\emph{\bgroup\it
%  \ifmmode\else\aftergroup\italiccorrection\fi
%  \let\dummy=}

\def\quote{\bgroup\narrower\smallbreak}
\def\endquote{\smallbreak\egroup}

\def\begin#1{\begingroup
  \def\end##1{\csname end#1\endcsname\endgroup}%
  \csname #1\endcsname}

\def\raggedleft{%
  \leftskip 0pt plus 1fil
  \parfillskip 0pt
}

\def\r#1{{\accent23 #1}}


\ifx\strip@pt\UNDEFINED
\begingroup
  \catcode`P 12 \catcode`T 12
  \lowercase{\endgroup
    \gdef\strip@pt#1PT{#1}}%
\fi

% color

\ifx\color\UnDeFiNeD 
%
\ifx\pdfoutput\UnDeFiNeD % PostScript
%
\def\colorCurrentColor{color cmyk 0 0 0 1}%
%
\def\colorRestoreCurrentColor{%
\special{\colorCurrentColor}\egroup\egroup}%
%
\def\colorWithModelrgb#1{\bgroup
\def\colorCurrentColor{color rgb #1}%
\special{\colorCurrentColor}%
\aftergroup\colorRestoreCurrentColor}%
%
\def\colorWithModelRGBaux#1 #2 #3\end{\bgroup
\dimen0=#1pt \divide\dimen0 by 255
\edef\red{\expandafter\strip@pt\the\dimen0 }%
\dimen0=#2pt \divide\dimen0 by 255
\edef\green{\expandafter\strip@pt\the\dimen0 }%
\dimen0=#3pt \divide\dimen0 by 255
\edef\blue{\expandafter\strip@pt\the\dimen0 }%
\def\colorCurrentColor{color rgb \red\space \green\space \blue}%
\special{\colorCurrentColor}%
\aftergroup\colorRestoreCurrentColor
\ignorespaces}
%
\def\colorWithModelcmyk#1{\bgroup
\def\colorCurrentColor{color cmyk #1}%
\special{\colorCurrentColor}%
\aftergroup\colorRestoreCurrentColor}%
%
\def\colorWithModelgray#1{\bgroup
\def\colorCurrentColor{color gray #1}%
\special{\colorCurrentColor}%
\aftergroup\colorRestoreCurrentColor}%
%
\def\colorWithModelnamed#1{\bgroup
\edef\colorCurrentColor{\csname
ColorNamed#1\endcsname}%
\special{\colorCurrentColor}%
\aftergroup\colorRestoreCurrentColor}%
%
\def\definecolorWithModelrgb#1#2{\egroup
\expandafter\def\csname ColorNamed#1\endcsname
{color rgb #2}}%
%
\def\definecolorWithModelcmyk#1#2{\egroup
\expandafter\def\csname ColorNamed#1\endcsname
{color cmyk #2}}%
%
\def\definecolorWithModelgray#1#2{\egroup
\expandafter\def\csname ColorNamed#1\endcsname
{color gray #2}}%
%
\else % PDF
%
\def\colorCurrentColor{0 0 0 1 k}%
%
\def\colorRestoreCurrentColor{%
\pdfliteral{\colorCurrentColor}\egroup\egroup}%
%
\def\colorWithModelrgb#1{\bgroup
\def\colorCurrentColor{#1 rg}%
\pdfliteral{\colorCurrentColor}%
\aftergroup\colorRestoreCurrentColor
\ignorespaces}%
%
\def\colorWithModelRGBaux#1 #2 #3\end{\bgroup
\dimen0=#1pt \divide\dimen0 by 255
\edef\red{\expandafter\strip@pt\the\dimen0 }%
\dimen0=#2pt \divide\dimen0 by 255
\edef\green{\expandafter\strip@pt\the\dimen0 }%
\dimen0=#3pt \divide\dimen0 by 255
\edef\blue{\expandafter\strip@pt\the\dimen0 }%
\def\colorCurrentColor{\red\space \green\space \blue\space rg}%
\pdfliteral{\colorCurrentColor}%
\aftergroup\colorRestoreCurrentColor
\ignorespaces}
%
\def\colorWithModelcmyk#1{\bgroup
\def\colorCurrentColor{#1 k}%
\pdfliteral{\colorCurrentColor}%
\aftergroup\colorRestoreCurrentColor
\ignorespaces}%
%
\def\colorWithModelgray#1{\bgroup
\def\colorCurrentColor{#1 g}%
\pdfliteral{\colorCurrentColor}%
\aftergroup\colorRestoreCurrentColor
\ignorespaces}%
%
\def\colorWithModelnamed#1{\bgroup
\edef\colorCurrentColor{\csname
ColorNamed#1\endcsname}%
\pdfliteral{\colorCurrentColor}%
\aftergroup\colorRestoreCurrentColor
\ignorespaces}%
%
\def\definecolorWithModelrgb#1#2{\egroup
\expandafter\def\csname ColorNamed#1\endcsname
{#2 rg}}%
%
\def\definecolorWithModelcmyk#1#2{\egroup
\expandafter\def\csname ColorNamed#1\endcsname
{#2 k}}%
%
\def\definecolorWithModelgray#1#2{\egroup
\expandafter\def\csname ColorNamed#1\endcsname
{#2 g}}%
%
\fi
%
\def\color{\futurelet\colorQpeekchar\colorQpossiblynamed}%
%
\def\colorQpossiblynamed{\bgroup
\defcsactive\,{ }%
\if\colorQpeekchar[%
\let\colorQproceed\colorQexplicitmodel\else
\let\colorQproceed\colorWithModelnamed\fi
\colorQproceed}%
%
\def\colorQexplicitmodel[#1]{%
\csname colorWithModel#1\endcsname}%
%
\def\colorWithModelRGB#1{%
\colorWithModelRGBaux#1\end}%
%
\def\definecolor#1#2{\bgroup
\defcsactive\,{ }%
\csname definecolorWithModel#2\endcsname{#1}}%
%
% foll lets load texmf/tex/latex/graphics/dvipsnam.def
\def\DefineNamedColor#1{\definecolor}%
%
% these colors are standard in latex
\definecolor{red}{rgb}{1 0 0}%
\definecolor{green}{rgb}{0 1 0}%
\definecolor{blue}{rgb}{0 1 1}%
\definecolor{cyan}{cmyk}{1 0 0 0}%
\definecolor{magenta}{cmyk}{0 1 0 0}%
\definecolor{yellow}{cmyk}{0 0 1 0}%
\definecolor{black}{cmyk}{0 0 0 1}%
\definecolor{white}{rgb}{1 1 1}%
%
\fi

%the rest of the file isn't needed for eplain?

\def\itemize{\par\begingroup
  \advance\leftskip\parindent
  \smallbreak
  \def\item{\smallbreak\noindent
  \llap{$\bullet$\enspace}\ignorespaces}}

\def\enditemize{\smallbreak\smallbreak\endgroup\par}

\newtally\enumeratelevel

\def\enumerate{\par\begingroup
  \advancetally\enumeratelevel1%
  \newtally\enumeratenumber
  \advance\leftskip\parindent
  \smallbreak
  \def\item{\smallbreak\noindent
    \advancetally\enumeratenumber1%
    \ifnum\enumeratelevel=1 
      \edef\enumeratemark{\enumeratenumber}\else
    \ifnum\enumeratelevel=2 
      \count255=\enumeratenumber 
      \advance\count255 by -1 \advance\count255 by `a
      \edef\enumeratemark{\noexpand\char\the\count255 }\else
    \ifnum\enumeratelevel=3 
      \edef\enumeratemark{\romannumeral\enumeratenumber}\else
    \ifnum\enumeratelevel=4
      \count255=\enumeratenumber 
      \advance\count255 by -1 \advance\count255 by `A
      \edef\enumeratemark{\noexpand\char\the\count255 }\else
    \edef\enumeratemark{\enumeratenumber}\fi\fi\fi\fi
    \edef\recentlabel{\enumeratemark}% needed?
    \llap{\enumeratemark.\enspace}\ignorespaces}}

\def\endenumerate{\smallbreak\smallbreak\endgroup\par}

% Numbered footnotes

\ifx\plainfootnote\UNDEFINED
  \let\plainfootnote\footnote
\fi

\newtally\footnotenumber

\def\numfootnote{\globaladvancetally\footnotenumber 1%
  \bgroup\csname footnotehook\endcsname
    \plainfootnote{$^{\footnotenumber}$}\bgroup
      \edef\recentlabel{\footnotenumber}%
      \aftergroup\egroup
      \let\dummy=}

\let\f\numfootnote

\ifx\frac\UnDeFiNeD
\def\frac#1/#2{{#1\over#2}}%
\fi

% \path is like \verb except that its argument
% can break across lines at `.' and `/'.

\ifx\path\UnDeFiNeD
\def\path{\begingroup\verbsetup
  \pathfont
  \defcsactive\.{\discretionary{\char`\.}{}{\char`\.}}%
  \defcsactive\/{\discretionary{\char`\/}{}{\char`\/}}%
  \verbI}%
\fi

\let\pathfont\relax

\catcode`\@\atcatcodebeforetiip

% end of file
