%\filename{sectionmacros.tex}
%\edited{last by stolfi on Thu May 26 15:41:55 1988}

% Chapter and section macros for thesis, with auto-numbering

\catcode`@=11\endlinechar=-1

  % Section and subsection counters:

    \newlocaltag\Sec

    \newcount\SubsecCounter % count of subsections in section
    
  % Footnote counter:
  
    \newcount\FootnoteCounter

  % Indentation unit for section titles
  % Useful for printing hacked table of contents

    \newdimen\secindent \secindent=0pt

  % Begin of new book, part, chapter, section, subsection, dimsection

  % Note: sections and subsections are automatically numbered starting
  % from 0 in each chapter.  Parts, chapters, appendices, junkpieces
  % are NOT numbered automatically; the tags \Ch{tag} should be defined
  % explicitly.  

    % Beginning of book. Usage: \book{Title}

    \outer\def\book#1{\shorttitle{#1} \bookheader{#1}{}}

    % Usage: 
    %   \chapter{tag}{Title}
    %   \appendix{tag}{Title}
    %   \junkpiece{tag}{Title}
    %   \generalchapter{tag}{Chapter \Ch{tag}\endgraf title}
    
    \def\Ch#1{\tagget\Ch{#1}}
    
    \def\contput#1#2#3{
      % #1 is a single control sequence, that will be written unexpanded
      % #2, #3 are token strings that will be expanded immediately.
      {
        \def\wrc@nt##1##2\endwr{
          \write\contents{\noexpand#1{##1}{##2}{\number\pageno}}
        }
        \edef\next{{#2}{#3}}
        \expandafter\wrc@nt\next\endwr
      }
    }
    
    \def\generalchapter#1#2{
      \gdef\chaptag{#1}
      \chapterheader{#2}
      \def\b@h{\global\pageno=}
      \expandafter\b@h\expandafter\csname\chaptag page\endcsname
      \ifodd\pageno \else \global\advance\pageno by 1\fi
      \global\SecCounter=0
      \global\SubsecCounter=0
      \global\FootnoteCounter=0
      \resetlocaltags
    }

    \outer\def\chapter#1#2{
      \gdef\pagetitle{\Ch{#1}. #2}
      \generalchapter{#1}{Chapter \Ch{#1}\endgraf#2}
      \contput\CHAPTER{\chaptag}{#2}
    }
    \outer\def\appendix#1#2{
      \gdef\pagetitle{#2}
      \generalchapter{#1}{#2}
      \contput\APPENDIX{\chaptag}{#2}
    }
    \outer\def\junkpiece#1#2{
      \gdef\pagetitle{Junk \Ch{#1}. #2}
      \generalchapter{#1}{Junk Chapter \Ch{#1}\endgraf#2}
      \contput\JUNK{\chaptag}{#2}
    }

    \outer\def\section#1#2{
      \global\advance\SecCounter by 1
      \tagput\Sec{#1}{\number\SecCounter}
      \sectionheader{\Sec{#1}}{#2}
      \contput\SECTION{\number\SecCounter}{#2}
      \global\SubsecCounter=0
      }

    \outer\def\subsection#1#2{
      \ifnum\SecCounter=0 \relax \SecCounter=1 \fi
      \global\advance\SubsecCounter by 1
      \tagput\Sec{#1}{\number\SecCounter.\number\SubsecCounter}
      \contput\SUBSECTION{\number\SecCounter.\number\SubsecCounter}{#2}
      \subsectionheader{\Sec{#1}}{#2}
      }

    \outer\def\dimsection#1#2{
      \tagput\Sec{#1}{\number\SecCounter.\number\SubsecCounter}
      \dimsectionheader{}{#2}
    }

  % Junk sections

    \def\junked#1{\subsectionheader{}{Junked #1}}

  % default header styles:

    \def\chapstyle{\chapfont \baselinesep=2.5ex\linesep 0.2ex \parsep=0.5ex} 
    \let\titlestyle=\chapstyle
    \let\partstyle=\chapstyle
    \def\secstyle{\secfont}
    \def\subsecstyle{\subsecfont}
    \def\dimsecstyle{\dimsecfont}

  % Header formats for book, part, chapter, section, etc.

    \def\bookheader#1{
      \vbox to 0.6\vsize{\titlestyle\centered \vfil #1\par \vfil} 
      \vfill\eject
      }

    \def\chapterheader#1{
      \hrule height0pt
      \vskip 10pt plus 20pt minus 10pt
      \vbox{\chapstyle\raggedleft \advance\rightglue by 30pt #1\par}
      \penalty 10000\vskip 20pt plus 20pt minus 5pt\penalty10000
      }

    \def\sectionheader#1#2{
      \ifhmode\endgraf\fi
      \bigvskip
      \vskip5\baselinesep\penalty-50\vskip-5\baselinesep
      \hbox{\secstyle #1. #2}
      \message{ #2 ...}
      \penalty 10000\smallvskip\penalty10000
      }

    \def\subsectionheader#1#2{
      \ifhmode\endgraf\fi
      \medvskip
      \vskip4\baselinesep\penalty-50\vskip-4\baselinesep
      \hbox{\subsecstyle #1. #2}
      \message{ #2 ...}
      \penalty 10000\smallvskip\penalty10000
      }

    \def\dimsectionheader#1#2{
      \ifhmode\endgraf\fi
      \smallvbreak
      \hbox{\dimsecfont #1 #2}
    }

  % Dummy (untitled) sections and subsections:

    \def\dummyheader#1{#1}
    
    \outer\def\sectiondummy{\dummyheader{\bigvskip}}

    \outer\def\subsectiondummy{\dummyheader{\medvskip}}

    \outer\def\dimsectiondummy{\dummyheader{\smallvskip\noindent}}

  % Begin junk section of chapter:

    \newif\ifomitjunk \omitjunkfalse
    \outer\def\junkyard{
      \par
      \vbreak\forbidden\vskip 30pt minus 1000pt
      \vbreak\forbidden\hrule\vfill
      \ifomitjunk 
        \def\next{\supereject\end}
      \else
        \def\next{\eject \smallsize\rm}
      \fi
      \next
    } 

% Footnotes. Different from papermacros.tex of Thu Mar 28 12:42:45 1985:
%    automatic numbering by chapter.

  \long\def\foot#1{\s@vesf\global\advance\FootnoteCounter by 1\relax
    $^{\number\FootnoteCounter}$\@sf
    \botinsert{\hsize=\textwidth\f@@ttext{\number\FootnoteCounter}{#1}}}

\catcode`@=12\endlinechar=`\^^M

% END

