aboutsummaryrefslogtreecommitdiff
path: root/toc/README.t
blob: b5614867baafe3dc493381cea3e4416341dbaf30 (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
.so ../g.tmac
.so ../gx.tmac
.so toc.tmac
.
.t
.x Toc ,
a multipass framework for troff
.d
John Ankarstr\(:om
.d e
.
.s
.ce
.i "TABLE OF CONTENTS"
.@e 0
.to
.sp 0.5i
.he 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 a registers and a string named
.i te
and
.i tf ,
respectively.
.p
The
.i toc.tmac
package defines two macros, 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,
prefixed by
.q (toc) .
If the
.i tf
string is non-empty, the
.c to
macro invokes the
.c so
request, reading and inserting lines from the file
specified by
.i tf
into the troff document.
.
.he Operation
.p
In the first pass, the
.i toc
script sets
.i te
= 1 and
.i tf
= empty.
This enables the
.c te
macro, printing its arguments on standard error.
The lines prefixed with
.q (toc) ,
printed on standard error,
are written to the file
.i $g
with the
.q (toc)
prefix removed.
.p
In the second pass,
.i toc
sets
.i te
= 1 and
.i tf
=
.i $g .
This enables the
.c to
macro as well, inserting the contents of
.i $g
into the troff source.
The
.c te
macro is still activated and accordingly
prints its arguments on standard error yet another time.
The output is processed like earlier and written to the file
.i $h .
.p
In the third and final pass,
.i toc
sets
.i te
= 0 and
.i tf
=
.i $h .
This disables
.c te ,
which means that nothing is printed on standard error,
but
.c to
is still enabled,
inserting the contents of
.i $h
into the document.
.p
Theoretically, three is the number of passes that are 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.
.p
In practice, however,
.i toc
has the ability to detect how many passes are needed
and will never do more work than what is necessary.
.
.he 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 &ref \\n%
.p
will print
.l
\&(toc).nr &ref \\n%
.p
on standard error.
The
.q (toc)
prefix is removed before the line is written to the temporary file.
.p
When
.c to
is invoked, it will literally insert
.l
\&.nr &ref 3
.p
into the troff source, assuming
.c te
was invoked on the third page.
.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 a wrapper around troff
and any potential troff pre- or post-processors.
On standard input, it expects the troff source to be processed.
As arguments, it takes a shell command line to be evaluated on every pass.
For example, the invocation
.l
$ <example.t toc refer -prefs \\| groff -C
.p
passes the contents of
.i example.t
through the
.i refer
preprocessor and the
.i groff
implementation of troff.
Note the escaped pipe character;
because
.i toc
passes its arguments directly to
.i /bin/sh 's
.i eval ,
arbitrary shell syntax is supported,
as long as it is escaped.\c
.(
This also means that whitespace in arguments is not properly preserved.
If you need to include whitespace in the arguments to troff or
a troff preprocessor, create a separate shell script
and invoke
.i toc
on that.
.)
.p
Note that input must be given on standard in;
it cannot be provided as a filename to, for example,
.i refer
or
.i groff .
.
.he 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