Generating a Formatted Index

The process of generating an index usually involves three steps:

  • Code the index entries in the document.

  • Format the document, producing index entries with page numbers.

  • Process the index entries to sort them, combining entries that differ only in page number, and then preparing the formatted index.

This process remains pretty much the same whether using troff, other coded batch formatters, or a WYSIWYG formatter such as FrameMaker, although the steps are not as clearly separated with the latter. However, I will be describing how we use troff to generate an index such as the one for this book. We code the index using the following macros:

MacroDescription
.XXProduces general index entries.
.XNCreates “see” or “see also” cross references.
.XBCreates bold page entry indicating primary reference.
.XSBegins range of pages for entry.
.XEEnds range of pages for entry.

These macros take a single quoted argument, which can have one of several forms, indicating primary, secondary, or tertiary keys:

"primary [ : secondary [ ; tertiary ]]"

A colon is used as the separator between the primary and secondary keys. To support an earlier coding convention, the first comma is interpreted as the separator if no colon is used. A semicolon indicates the presence of a tertiary key. The page number is always associated with the last key.

Here is an entry with only a primary key:

.XX "XView"

The next two entries specify a secondary key:

.XX "XView: reserved names"
.XX "XView, packages"

The ...

Get sed & awk, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.