[comp.text] How to forward reference chapters in troff

npn@cbnewsl.att.com (nils-peter.nelson) (11/30/90)

As usual, I've forgotten who posted the question.
There is a way to forward reference chapters by number
in troff, but it requires two passes.  I always start
chapters with a macro, which guarantees a consistent
type font, size and position. The magic is to include
in that macro a ".tm" to divert information to stderr.
Then run troff, diverting stderr via "2>toc" (throw
away the actual troff output via >dev/null).
The information diverted is a mnemonic for the
chapter; references to the chapter will be via the mnemonic.
Here's an example:
.so toc2
.de CH
.sp2
.ce
\\$2
.sp
.tm \\$1
..
.CH NW "Networking"
blah blah
.CH SA "System Administration"
blah blah.
For further information, see Chaper \n(NW.
==
After running this as indicated, file toc will be:
NW
SA

Now run the one line awk progam:
awk <toc ' { print ".nr " $1 " " NR }' >toc2

File toc2 will now look like:
.nr NW 1
.nr SA 2

Now, re-run troff and all your forward references
will be to the correct chapter number.
(First time through, make sure toc2 exists,
e.g. "echo >toc2"; all the references will be
to chapter 0, but you're throwing away the output
anyway.)