summaryrefslogtreecommitdiff
path: root/macros/plain/contrib/misc/automagic.tex
blob: 75d047cb535200ff41611b2788438db72749a72a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
% 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=amssbx10 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
}