%\filename{tagsectionmacros.tex}
%\edited{last on Wed Dec 14 23:25:04 1988 by stolfi}

% SECTION AND SUBSECTIONS

% With automatic numbering

\catcode`@=11\endlinechar=-1

  % Section and subsection counters:

    \newlocaltag\Sec
    \newtag\Th
    \newtag\Eq
    \newtag\Ref
    \newtag\Fig

    \newcount\SecCounter      % Count of sections in chapter
    \newcount\SubsecCounter   % Count of subsections in section
    \newcount\FootnoteCounter % Count of footnotes

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

    \newdimen\secindent \secindent=0pt

  % Begin of new section, subsection, dimsection

  % Note: sections and subsections are automatically numbered starting
  % from 0 in each chapter and section, respectively.    

    % Internal procedure for writing out table of contents entries:
    
    \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
      }
    }
    
    % Usage: 
    %   \chapter{tag}{Title}
    %   \appendix{tag}{Title}
    %   \junkpiece{tag}{Title}
    %   \generalchapter{tag}{title}{type}
    % where /prefix/ is a string to be prefixed to section numbers,
    % /tag/ is the internal name of the chapter, /title/ is the bare
    % title, and /type/ is "Chapter", "Part", "Appendix", etc.
    
    \def\Ch#1{\tagget\Ch{#1}}
    
    \def\generalchapter#1#2#3{
      \gdef\chaptag{#1}
      \def\cr@ck{\let\b@h=}
      \expandafter\cr@ck\csname Ch.#1\endcsname
      \def\b@@h{}
      \ifx\b@h\b@@h
        \def\titl@{}
      \else
        \def\titl@{Part \b@h:\ }
      \fi
      \xdef\pagetitle{\titl@ #2}
      \chapterheader{\titl@ #2}
      \global\SecCounter=0
      \global\SubsecCounter=0
      \global\FootnoteCounter=0
      \resetlocaltags
    }

    \outer\def\chapter#1#2{
      \generalchapter{#1}{#2}{Chapter}
      \contput\CHAPTER{#1}{#2}
    }
    \outer\def\part#1#2{
      \generalchapter{#1}{#2}{Chapter}
      \contput\PART{#1}{#2}
    }
    \outer\def\appendix#1#2{
      \generalchapter{#1}{#2}{Appendix}
      \contput\APPENDIX{#1}{#2}
    }
    \outer\def\junkpiece#1#2{
      \generalchapter{#1}{#2}{Junk Chapter}
      \contput\JUNK{#1}{#2}
    }

    % Usage:
    %   \section{tag}{title}
    %   \subsection{tag}{title}
    %   \dimsection{tag}{title}
    
    \outer\def\section#1#2{
      \global\advance\SecCounter by 1
      \tagput\Sec{#1}{\number\SecCounter}
      \sectionheader{\Sec{#1}}{#2}
      \contput\SECTION{\Ch{\chaptag}\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{\Ch{\chaptag}\number\SecCounter.\number\SubsecCounter}{#2}
      \subsectionheader{\Sec{#1}}{#2}
    }

    \outer\def\dimsection#1#2{
      % Right now it is an error to refer to a \dimsection
      \dimsectionheader{}{#2}
    }

  % Junk sections

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

  % default header styles:

    \def\chapterstyle{\chapterfont}
    \def\secstyle{\secfont}
    \def\subsecstyle{\subsecfont}
    \def\dimsecstyle{\dimsecfont}

  % Header formats for chapter, section, etc.

    \def\chapterheader#1{
      \ifhmode\endgraf\fi
      \bigvskip
      \vskip5\baselinesep\penalty-50\vskip-5\baselinesep
      \hbox{\chapterstyle #1}
      \message{ #1 ...}
      \penalty 10000\smallvskip\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
      \noindent\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

