%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%   Here come chapter, section, subsection & appendix macros.
%
\newcount\chapternumber      \chapternumber=0
\newcount\sectionnumber      \sectionnumber=0
\newcount\equanumber         \equanumber=0
\let\chapterlabel=\relax
\let\sectionlabel=\relax
\newtoks\chapterstyle        \chapterstyle={\Number}
\newtoks\sectionstyle        \sectionstyle={\chapterlabel\Number}
\newskip\chapterskip         \chapterskip=\bigskipamount
\newskip\sectionskip         \sectionskip=\medskipamount
\newskip\headskip            \headskip=8pt plus 3pt minus 3pt
\newdimen\chapterminspace    \chapterminspace=15pc
\newdimen\sectionminspace    \sectionminspace=10pc
\newdimen\referenceminspace  \referenceminspace=25pc
%
\def\chapterreset{\global\advance\chapternumber by 1
   \ifnum\equanumber<0 \else\global\equanumber=0\fi
   \sectionnumber=0 \makechapterlabel}
\def\makechapterlabel{\let\sectionlabel=\relax
   \xdef\chapterlabel{\the\chapterstyle{\the\chapternumber}.}}
%
\def\alphabetic#1{\count255='140 \advance\count255 by #1\char\count255}
\def\Alphabetic#1{\count255='100 \advance\count255 by #1\char\count255}
\def\Roman#1{\uppercase\expandafter{\romannumeral #1}}
\def\roman#1{\romannumeral #1}
\def\Number#1{\number #1}
\def\BLANC#1{}
%
\def\titlestyle#1{\par\begingroup \interlinepenalty=9999
     \leftskip=0.02\hsize plus 0.23\hsize minus 0.02\hsize
     \rightskip=\leftskip \parfillskip=0pt
     \hyphenpenalty=9000 \exhyphenpenalty=9000
     \tolerance=9999 \pretolerance=9000
     \spaceskip=0.333em \xspaceskip=0.5em
     \iftwelv@\fourteenpoint\else\twelvepoint\fi
   \noindent #1\par\endgroup }
%
\def\spacecheck#1{\dimen@=\pagegoal\advance\dimen@ by -\pagetotal
   \ifdim\dimen@<#1 \ifdim\dimen@>0pt \vfil\break \fi\fi}
\def\TableOfContentEntry#1#2#3{\relax}
%
\def\chapter#1{\par \penalty-300 \vskip\chapterskip
   \spacecheck\chapterminspace
   \chapterreset \titlestyle{\chapterlabel\ #1}
   \TableOfContentEntry c\chapterlabel{#1}
   \nobreak\vskip\headskip \penalty 30000
   \wlog{\string\chapter\space \chapterlabel} }
\let\chap=\chapter
%
\def\section#1{\par \ifnum\the\lastpenalty=30000\else
   \penalty-200\vskip\sectionskip \spacecheck\sectionminspace\fi
   \global\advance\sectionnumber by 1
   \xdef\sectionlabel{\the\sectionstyle\the\sectionnumber}
   \wlog{\string\section\space \sectionlabel}
   \TableOfContentEntry s\sectionlabel{#1}
   \noindent {\caps\enspace\sectionlabel\quad #1}\par
   \nobreak\vskip\headskip \penalty 30000 }
%
\def\subsection#1{\par
   \ifnum\the\lastpenalty=30000\else \penalty-100\smallskip \fi
   \noindent\undertext{#1}\enspace \vadjust{\penalty5000}}
\let\subsec=\subsection
%
\def\undertext#1{\vtop{\hbox{#1}\kern 1pt \hrule}}
%
\def\ack{\par\penalty-100\medskip \spacecheck\sectionminspace
   \line{\fourteenrm\hfil ACKNOWLEDGEMENTS\hfil}\nobreak\vskip\headskip }
%
\def\APPENDIX#1#2{\par\penalty-300\vskip\chapterskip
   \spacecheck\chapterminspace \chapterreset \xdef\chapterlabel{#1}
   \titlestyle{APPENDIX #2} \nobreak\vskip\headskip \penalty 30000
   \TableOfContentEntry a{#1}{#2}
   \wlog{\string\Appendix\ \chapterlabel} }
\def\Appendix#1{\APPENDIX{#1}{#1}}
\def\appendix{\APPENDIX{A}{}}
%
\def\unnumberedchapters{\let\makechapterlabel=\relax \let\chapterlabel=\relax
   \sectionstyle={\BLANC}\let\sectionlabel=\relax \sequentialequations }
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%   Here come macros for equation numbering.
%
\def\eqname#1{\relax \ifnum\equanumber<0
     \xdef#1{{\noexpand\rm(\number-\equanumber)}}%
       \global\advance\equanumber by -1
    \else \global\advance\equanumber by 1
      \xdef#1{{\noexpand\rm(\chapterlabel\number\equanumber)}} \fi #1}
%
\def\eq{\eqname\?}
\def\eqn{\eqno\eqname}
\def\leqn{\leqno\eqname}
%
\def\eqinsert#1{\noalign{\dimen@=\prevdepth \nointerlineskip
   \setbox0=\hbox to\displaywidth{\hfil #1}
   \vbox to 0pt{\kern 0.5\baselineskip\hbox{$\!\box0\!$}\vss}
   \prevdepth=\dimen@}}
%
\def\leqinsert#1{\eqinsert{#1\hfill}}
\def\mideqn#1{\eqinsert{\eqname{#1}}}
\def\midleqn#1{\leqinsert{\eqname{#1}}}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
