.so ../mk/k.tmac .so ../mk/kx.tmac .so toc.tmac .mediasize letter . .t .z 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 register 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 ." (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 ." (toc) , printed on standard error, are written to the file .i $g with the ." (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 ." (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 $