% TOC DOCUMENT STYLE -- Released 18 Jul 90
%    for LaTeX version 2.09
% Copyright (C) 1990 by Nico Poppelier

\typeout{Document-style option `toc' <18 Jul 90>.}


%     ****************************************
%     *       SECTIONAL-UNIT LEVELS          *
%     ****************************************
%
% \new@level{LEVEL}[WITHIN]
%   - if X = \n@level, then define
%     \dl@LEVEL == X and \dln@X == LEVEL
%   - allocate an extra counter \xc@LEVEL, to be used to save
%     the original counter value while we are generating the TOC,
%     during which \c@LEVEL holds the counter value that is read
%     from the .toc file
%   - \newcounter{LEVEL} is the last part of the macro, so it picks up
%     the optional argument, if this is present 

\newcount\n@level \n@level=\m@ne

\def\new@level#1{\advance\n@level\@ne
  \expandafter\xdef\csname dl@#1\endcsname{\the\n@level}
  \expandafter\gdef\csname dln@\the\n@level\endcsname{#1}
  \expandafter\newcount\csname xc@#1\endcsname 
  \expandafter\gdef\csname #1mark\endcsname##1{}
  \newcounter{#1}}


%     ****************************************
%     *              TABLE FILES             *
%     ****************************************
%
% In the document style, the definitions
%   \def\ext@figure{lof}
%   \def\ext@table{lot}
% are replaced by
%   \@extension{figure}{lof}
%   \@extension{table}{lot}
% The calls to \@extension should be put in the \tableofcontents,
% \listoffigures, ... commands
%
% \@extension{TYPE}{EXT}: 
% - globally defines \ext@TYPE == EXT
% - globally sets \@extlist := \@extlist * \@op {EXT}
% for TYPE = type of float object, EXT = file extension.
% The .toc file is always on the list.
\newtoks\@extlist \@extlist={\@op{toc}}

\def\@extension#1#2{\expandafter\gdef\csname ext@#1\endcsname{#2}
  \begingroup \let\@op\relax 
  \xdef\@tempa{\noexpand\@extlist={\the\@extlist\@op{#2}}}
  \endgroup \@tempa}


%    ****************************************
%    *           TABLE OF CONTENTS          *
%    *     (PROCESSING THE INFORMATION)     *
%    ****************************************
%
% The command \contentsline{NAME} executes \l@NAME.
% So, to specify the table of contents, we must define \l@chapter, 
% \l@section, \l@subsection, ... ; to specify the list of figures, we must 
% define \l@figure; and so on.
%
% If a full table (of contents, of figures, ...) must be produced, we 
% read the entire table file, e.g. JOB.toc or JOB.lof. If a partial table 
% (of contents, of figures, ...) must be produced, we set up a restriction 
% and then read the entire table file, but only execute those entries that 
% satisfy the restriction.

\newcount\cur@level \cur@level=\m@ne
\newcount\cur@count \cur@count=\m@ne

% \currentsection{TYPE}{NUMBER}
\def\currentsection#1#2{\cur@level=\csname dl@#1\endcsname
  \cur@count=#2 \ifnum\cur@count<0 \else \csname c@#1\endcsname=#2\fi}

% \contentsline{TYPE}{NUMBER}{TITLE}{PAGE}
%
% Making a fulll TOC means that only those entries are processed 
% for which \cur@level <= \max@level.
%
% Making a partial TOC at level D for counter values (c_0, ..., c_D) 
% means that only those entries are processed for which
%   \min@level <= \cur@level AND
%   \cur@level <= \max@level AND
%   (c_0, ..., c_D) = (r_0, ..., r_D) 
% \min@level and D are dependent, since we have: \min@level= D+1.
% r_k is the value of the k-th counter as read from the table file.
%
% \check@result := true;
% FOR k := 0 TO d DO
%   \check@result := \check@result AND (c_k = r_k) 
% OD
% \check@result := \check@result AND
%   (\min@level <= \cur@level) AND (\cur@level <= \max@level)
%
% We translate each of these D+3 conditions to true = 1, false = 0.
% If the result is 1 we process the entry, otherwise we don't.
%
% One \@whilenum loop is used to build up a token list. The token list  
% is used to simulate a loop in the decision whether to process 
% a certain entry.
%
% \if@partialtab indicates whether the table currently being constructed 
% is a partial one (\@partialtabtrue) or a full one (\@partialtabfalse) 
\newif\if@partialtab \@partialtabfalse

\newtoks\@levlist
\newcount\min@level \newcount\max@level

\def\@addlevel#1{\begingroup \let\@op\relax
  \xdef\@tempa{\noexpand\@levlist={\the\@levlist\@op{#1}}}
  \endgroup \@tempa}

\def\settocdepth#1{\c@tocdepth=\csname dl@#1\endcsname}
\def\setsecnumdepth#1{\c@secnumdepth=\csname dl@#1\endcsname}

% \@starttoc{EXTENSION} ==
% - set \max@level; \min@level should be set by calling macro
% - build list of all counters that need to be compared
% - save the global values of the counters \c@TYPE in \xc@TYPE
% - define condition on counter values
% - read table file
% - open new table file
%
\def\@starttoc#1{\begingroup  
  \max@level=\c@tocdepth
  \if@partialtab 
    \@tempcnta\z@ \@levlist={} 
    \@whilenum \@tempcnta<\min@level\do%
      {\edef\@tempa{\noexpand\@addlevel{\csname dln@\the\@tempcnta\endcsname}}
       \@tempa \advance\@tempcnta\@ne}\fi 
  \def\@op##1{\csname xc@##1\endcsname=\csname c@##1\endcsname}\the\@levlist
  \def\@op##1{\ifnum\csname c@##1\endcsname=\csname xc@##1\endcsname
              \else \check@result\z@ \fi}\makeatletter
  \@input{\jobname.#1}\global\@nobreakfalse \endgroup}

% opening of the new .toc, .lof, .lot, ... files is postponed until 
% the end of the document, just before the .aux file is processed
%
\def\enddocument{\@checkend{document}\clearpage\begingroup  
\if@filesw \immediate\closeout\@mainaux 
\def\@op##1{\expandafter\newwrite\csname tf@##1\endcsname
            \expandafter\immediate\expandafter\openout
            \csname tf@##1\endcsname=\jobname.##1\relax}\the\@extlist
\def\global\@namedef##1##2{}\def\newlabel{\@testdef r}%
\def\bibcite{\@testdef b}\@tempswafalse \makeatletter\input \jobname.aux
\if@tempswa \@warning{Label(s) may have changed.  Rerun to get
cross-references right}\fi\fi\endgroup\deadcycles\z@\@@end}

\newif\if@numbered
\newcount\check@result

\def\@checkentry{\ifnum\cur@level<\min@level \check@result\z@ \fi
  \ifnum\cur@level>\max@level \check@result\z@ \fi
  \the\@levlist}

% \contentsline{LEVEL}{NUMBER}{TITLE}{PAGE}
\def\contentsline#1#2#3#4{\cur@level=\csname dl@#1\endcsname
  \cur@count=#2 \ifnum\cur@count<0 \@numberedfalse \else 
    \@numberedtrue \csname c@#1\endcsname=#2\fi
  \check@result\@ne \if@partialtab \@checkentry \else
    \ifnum\cur@level>\max@level \check@result\z@ \fi \fi
  \ifnum\check@result=\@ne \csname l@#1\endcsname{#3}{#4}\fi}

% \def\contentsextra#1{\if@partialtab\@checkentry\else\check@result\@ne\fi
%   \ifnum\check@result=\@ne #1\fi}

\def\contentsextra#1{#1}

% \@starttable{FULL}{PARTIAL} ==
% BEGIN
%   IF optional argument present THEN
%     absorb optional argument LEVEL
%     \@partialtab := true
%     current level := \dl@LEVEL
%     PARTIAL
%   ELSE
%     \@partialtab := false; 
%     FULL
%   FI
% END

\def\@starttable#1#2{\@ifnextchar [{\@xstarttable{#2}}{\@partialtabfalse #1}}

\def\@xstarttable#1[#2]{\@partialtabtrue 
  \expandafter\min@level\csname dl@#2\endcsname 
  \edef\@tempa{\the\min@level}\advance\min@level\@ne #1{\@tempa}}

%    ****************************************
%    *           TABLE OF CONTENTS          *
%    *     (FORMATTING THE INFORMATION)     *
%    ****************************************
%
% \@dottedtocline{INDENT}{NUMWIDTH}{STYLE}{NUMBER}{TITLE}{PAGE} :
%                 #1      #2        #3     #4      #5     #6
%   Macro to produce a table of contents line with the following
%   parameters:
%     INDENT   : Total indentation from the left margin.
%     NUMWIDTH : Width of box for number.
%                This is also the amount of extra indentation added 
%                to second and later lines of a multiple line entry.   
%     STYLE    : Style declarations that determine layout of entry.
%     NUMBER   : Formatted sectional-unit number
%     TITLE    : Contents of entry.
%     PAGE     : Formatted page number.
%     STYLE    : Style declarations.
%
%  Uses the following parameters, which must be set by the document style.
%  They should be defined with \def's.
%    \@pnumwidth : Width of box in which page number is set.
%    \@tocrmarg  : Right margin indentation for all but last line of
%                  multiple-line entries.
%    \@dotsep    : Separation between dots, in mu units.  Should be \def'd to
%                  a number like 2 or 1.7
%     
% \@dottedtocline{INDENT}{NUMWIDTH}{STYLE}{NUMBER}{TITLE}{PAGE} :
%                 #1      #2        #3     #4      #5     #6
\def\@dottedtocline#1#2#3#4#5#6{\vskip \z@ plus 0.2pt
  \begingroup #3
    \@tempdima #1\relax \@tempdimb #2\relax
    \leftskip\@tempdima \advance\leftskip by \@tempdimb
    \rightskip\@tocrmarg plus 1fil \interlinepenalty\@M 
    \parfillskip-\rightskip \parindent\z@
    \leavevmode \hbox{}\hskip-\@tempdimb
    \if@numbered \hbox to \@tempdimb{#4\hfill}\fi #5\nobreak
    \leaders\hbox{$\m@th \mkern \@dotsep mu.\mkern \@dotsep
      mu $}\hfill \nobreak \hbox to\@pnumwidth{\hfil #6}\par
  \endgroup}

%%% Note: \nobreak's added 7 Jan 86 to prevent bad line break that
%%% left the page number dangling by itself at left edge of a new line.
%%%
%%% Changed 25 Jan 88 to use \leftskip instead of \hangindent so
%%% leaders of multiple-line contents entries would line up properly.


%    ****************************************
%    *           TABLE OF CONTENTS          *
%    *     (GENERATING THE INFORMATION)     *
%    ****************************************
%
% Entries in the table of contents, or another table file, are
% generated by a call to 
%   \addcontentsline{EXT}{TYPE}{ENTRY}
%
% For every sectional-unit command \TYPE the following instructions must
% be written to the .aux file:
%   \currentsection{TYPE}{NUMBER}
%   \contentsline{TYPE}{NUMBER}{TITLE}{PAGE}
%
% For every sectional-unit command \TYPE the following instruction must
% also be written to all table files, e.g. the .lof and .lot files:
%   \currentsection{TYPE}{NUMBER}
% 
% For every caption of a float object TYPE the following 
% instruction must be written to the corresponding table file:
%   \contentsline{TYPE}{NUMBER}{TITLE}{PAGE}
%
% Extra instructions, added via \addtocontents{EXT}{TEXT},
% must result in the following line, without the in this case
% irrelevant page number, on the .EXT file
%   \contentsextra{TEXT}
%
% Legend:
%   NUMBER  = the value of the counter corresponding to LEVEL
%   TITLE   = heading of sectional unit 
%   PAGE    = page number (formatted)
% 
\def\addcontentsline#1#2#3{\if@filesw \begingroup
  \let\label\@gobble \let\index\@gobble \let\glossary\@gobble
  \def\protect##1{\string\string\string##1\string\space
    \space}\@temptokena{\thepage}%
  \edef\@tempa{\write \@auxout{\string\@writefile{#1}{\protect
    \contentsline{#2}{\the\cur@count}{#3}{\the\@temptokena}}}}\@tempa
  \if@nobreak \ifvmode\nobreak\fi\fi\endgroup\fi}

\def\addcurrentsection#1{\if@filesw \begingroup
  \def\protect##1{\string\string\string##1\string\space
    \space}\edef\@tempa
     {\write\@auxout{\string\currentsection{#1}{\the\cur@count}}}\@tempa
  \def\@op##1{\edef\@tempa{\write \@auxout{\string\@writefile{##1}{\protect
    \currentsection{#1}{\the\cur@count}}}}\@tempa} \the\@extlist
  \if@nobreak \ifvmode\nobreak\fi\fi\endgroup\fi}

\long\def\addtocontents#1#2{\if@filesw \begingroup
  \let\label\@gobble \let\index\@gobble \let\glossary\@gobble
  \def\protect##1{\string\string\string##1\string\space\space}%
  \edef\@tempa{\write \@auxout{\string\@writefile{#1}{\protect
    \contentsextra{#2}}}}\@tempa
  \if@nobreak \ifvmode\nobreak\fi\fi\endgroup\fi}


%     ****************************************
%     *               FLOATS                 *
%     ****************************************
%
% \long\def\@caption#1[#2]#3{\par\addcontentsline{\csname
%   ext@#1\endcsname}{#1}{\protect\numberline{\csname 
%   the#1\endcsname}{\ignorespaces #2}}\begingroup
%     \@parboxrestore
%     \normalsize
%     \@makecaption{\csname fnum@#1\endcsname}{\ignorespaces #3}\par
%   \endgroup}

\long\def\@caption#1[#2]#3{\cur@count=\csname c@#1\endcsname \par 
  \addcontentsline{\csname ext@#1\endcsname}{#1}{{\ignorespaces #2}}
  \begingroup 
    \@parboxrestore \normalsize
    \@makecaption{\csname fnum@#1\endcsname}{\ignorespaces #3}\par
  \endgroup}
