aboutsummaryrefslogtreecommitdiff
path: root/toc/README.t
blob: 10e0ee57533684ca504bc4c77582c185eaafd49c (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
.so ../g.tmac
.
.t
.x Toc ,
a multipass framework for troff
.d
John Ankarstr\(:om
.d e
.
.h
Introduction
.p
.i Toc
is a very simple solution
\(en in 50 or so lines \(en
to the problem of generating tables of contents
and other forms of forward references
in troff documents.
It consists of a shell script called
.i toc
and a macro package called
.i toc.tmac .
.p
The
.i toc
script is a wrapper around troff,
passing the document through the typesetter three times.
In every pass, it sets the values of two registers named
.i te
and
.i to .
.p
The
.i toc.tmac
package defines two macros, also named
.c te
and
.c to .
If the
.i te
register is non-zero, the
.c te
macro hands its argument to the
.c tm
request, which prints it on standard error.
If the
.i to
register is non-zero, the
.c to
macro invokes the
.c rd
request, reading and inserting lines from standard input
into the troff document.
.
.h
Operation
.p
In the first pass, the
.i toc
script sets
.i te
= 1 and
.i to
= 0.
This enables the
.c te
macro, printing its arguments on standard error.
The lines printed on standard error
are piped to the second invocation of troff.
.p
In the second pass,
.i toc
sets
.i te
= 1 and
.i to
= 1.
This enables the
.c to
macro as well, inserting the lines given on standard error
into the troff source.
The
.c te
macro is still activated and accordingly
prints its arguments on standard error yet another time.
Like before, the output is piped to the third invocation of troff.
.p
In the third and final pass,
.i toc
sets
.i te
= 0 and
.i to
= 1.
This disables
.c te ,
which means that nothing is printed on standard error,
but
.c to
is still enabled,
once again inserting the input on standard error into the document.
.p
Three is the number of passes necessary \(en and sufficient \(en
for generating forward references,
such as tables of contents.
Two passes are not enough,
as the generated references may push
a referent to the next page,
rendering the generated references incorrect.
To account for the addition of the generated references,
a third pass is needed.
.
.h
Usage
.s
Macro package
.p
The
.c te
and
.c to
macros do not apply any formatting to
or perform any processing of their input.
The
.c te
macro prints its arguments on standard error verbatim.
For example, the request
.l
\&.te .nr &a \\n%
.p
will print
.l
\&.nr &a \\n%
.p
on standard error.
Let us assume the page number is 3.
When
.c to
is invoked, it will literally insert
.l
\&.nr &a 3
.p
into the troff source.
.p
As such,
.i toc
provides the tools needed to create forward references,
including tables of contents,
but the exact formatting must be programmed by the user himself.
.
.s
Script
.p
The
.i toc
script is designed to be called instead of troff.
It is not a troff preprocessor, but a wrapper around it.
Any arguments given to
.i toc
are passed to troff
(or whichever program specified in the
.i TROFF
environment variable).
.p
Pre- and post-processors can be added by setting
.i TROFF
to a custom shell script:
.l
.ne 3
$ cat xroff
#!/bin/sh
\&... | groff "$@" | ...
$ TROFF=./xroff toc document.t
.
.h
Examples
.
.s
Table of contents
.p
.l
\&.so toc.tmac
\&.eo
\&.de he
\&. ft B
\&. sp 1v
\\$*
\&. br
\&. ft
\&. te .the \\n% \\$*
\&..
\&.de the
\&. nr _ \\$1
\&. shift
\&. ta 0 \\n(.luR
\&. tc .
\\$*     \\n_
\&. tc
\&. br
\&..
\&.ec
\&.sp |1i
\&.to
\&.\\" ...
\&.he First heading
\&.\\" ...
\&.he Second heading