% for automagic equation numbering

\newcount\LastEqNum
\LastEqNum=0
\def\BuildEqBox#1{\hbox{{\rm\the\count1--\the#1}}}
\def\nexteqnum{{\count9=\LastEqNum\advance\count9 by1
    \BuildEqBox{\count9}}}
\def\lasteqnum{\BuildEqBox{\LastEqNum}}
\def\neweqnum{\global\advance\LastEqNum by1 \lasteqnum}
\def\namelasteq#1{\xdef#1{\BuildEqBox{\LastEqNum}}}

% for automagic section numbering

\newcount\sectiondepth
\newbox\sectnum
\newif\iftoc
\let\Z=\relax
\def\SP{ }

\def\sectA{\LastEqNum=0\goodbreak}
\let\sectB=\goodbreak
\let\sectC=\goodbreak
\let\sectD=\goodbreak
\let\sectE=\goodbreak
\let\sectF=\goodbreak
\let\sectG=\goodbreak
\let\sectH=\goodbreak
\let\sectI=\goodbreak

% Start a new section.  First argument is depth (1 to 9 please!), second
% is caption.  \esection is a section that has nothing above it and
% shouldn't get a \goodbreak\bigskip.

\outer\def\section{\bigskip\SECTION}
\outer\def\esection{\nobreak\smallskip\SECTION}
\def\SECTBAD{\errmessage{Section depths should be between 1 and 9}}
\def\SECTION#1#2{%
    \sectiondepth=#1
    \ifcase\sectiondepth\SectBad\or\sectA\or\sectB\or\sectC\or
	\sectD\or\sectE\or\sectF\or\sectG\or\sectH\or\sectI\else
	\SECTBAD\fi
    \advance\count\sectiondepth by1
    \loop\advance\sectiondepth by1	% zero all lower levels
	\ifnum\sectiondepth<10\count\sectiondepth=0\repeat
    \sectiondepth=1
    \setbox\sectnum=\hbox{\number\count1}%
    \global\def\Sectnum{\number\count1}%
    \loop\ifnum\sectiondepth<#1		% Glue together each level
	\advance\sectiondepth by1
	\ifnum0=\count\sectiondepth	% force to 1
	    \set\count\sectiondepth=1
	\fi
	\setbox\sectnum=\hbox
	    {\unhbox\sectnum.\number\count\sectiondepth}%
	\global\edef\Sectnum{\Sectnum.\number\count\sectiondepth}%
    \repeat
    \message{\Sectnum.  #2}%
    \setbox0=\hbox{\bf\copy\sectnum.\hskip1em#2}%
    \def\text{\Sectnum.\SP!#2}%
    \tocentry{#1}{\text}{\box0\hfil}
    \ignorespaces
}

% unnumbered section
\def\unsection#1{%
    \message{#1}%
    \setbox0=\hbox{\bf#1}%
    \def\text{\SP!#1}%
    \tocentry{0}{\text}{\box0\hfil}%
    \ignorespaces
}

% print arg 3 as a line, put arg 2 into the table of contents with
% a "level" of arg 1
% takes care of putting box0 out as a line, and putting it into the
% table of contents if required
\def\tocentry#1#2#3{%
    \line{#3}% stuff to appear in the text
    \nobreak\smallskip\nobreak % now in vert mode
    \iftoc{\let\the=0 % make it unexpandable
	\xdef\ixout{\write\inx{\Z#1\SP!#2!\SP\the\pageno.}}\ixout
    }\fi
}

\ifx\titlefont\undefinecontrolsequence
    \font\titlefont=cmssbx10 scaled\magstep2
\fi
\newcount\Siz
\newwrite\inx
\def\DoContents{\toctrue\immediate\openout\inx=\jobname.toc}
\def\PrintTOC{\vfill\supereject\immediate\closeout\inx
    \def\LeadFill{\leaders\hbox to .75em{\hss.\hss}\hfill}
    \def\Z##1!##2!##3!##4.{
	\Siz=##1\count1=\Siz
	\advance\Siz by 1 \multiply\Siz by \count1
	\divide\Siz by 2  \advance\Siz by -1
	\dimen0=1em\multiply\dimen0 by \Siz
	\Siz=##1\advance\Siz by 1
	\dimen1=1em\multiply\dimen1 by \Siz
	\line{\hskip\dimen0\hbox to\dimen1{##2\hfil}{##3}\LeadFill\hbox{##4}}
    }
    \centerline{\titlefont Table of Contents}\vskip .75in
    \global\count0=-1
    \input\jobname.toc
}

.
