Полезная информация

Previous Page TOC Next Page Home


Formatting with Macro Packages

This chapter is about macros and macro packages. Starting with a sample macro, you'll see how and why it works, and you'll see it evolve from simple to complex.

Macro packages are made of macros. By way of analogy, a macro package is to a macro as a macro is to a troff primitive. In the chapter, we will examine how to use the man macro package.

What Is a Macro?

With embedded troff primitives, you can format a page just about any way you want. The trouble is that you have to reinvent the wheel every time you write a new document. For example, every time you format a first-level heading, you have to remember the sequence of primitives you used to produce a centered 14-point Helvetica bold heading. Then you have to type three or four troff requests, the heading itself, and another three or four requests to return to the normal body style. (This is practical only if you're being paid by the line.) It's a laborious process and one that makes it difficult—perhaps impossible—to maintain consistency over a set of files.

Good news: You can use macros to simplify formatting and ensure consistency. Macros take advantage of one of the UNIX system's distinguishing characteristics: the ability to build complex processes from basic—primitive—units. A macro is nothing more than a series of troff requests, specified and named, that perform a special formatting task.

The man Macro Package

The man macro package produces a specialized format: the format used for UNIX system documentation manual pages—manpages, for short. In addition, information entered with the man macros is used to create the formidable permuted indexes so dear to the hearts of UNIX users.

There are only a few macros in this package. If you're familiar with ms, you already know most of them.

NOTE: If you use mm, man's paragraph style macros and the way they're used to produce lists will dismay you.

The man macros produce an 8.5- by -11 inches page with a text area of 6.5- by-10 inches. There is a troff—but not an nroff—option for producing a smaller page—6-by-9 inches with a text area of 4.75- by-8.375 inches. If you choose this option, point size and leading are reduced from 10/12 to 9/10.

Page Layout

The .IN macro sets the indent relative to subheads. The default setting is 7.2 ens in troff and 5 ens in nroff.

The .LL macro sets the line length, which includes the value of IN.

The footer produced by the man macros is an example of making the best of a bad deal. The date is hard coded into the macro package. (This is usually a sign that you're not supposed to change it.) It's not the current date. It's whatever date your local macromancer deems appropriate. The reason for this eccentricity has been lost over time. Perhaps people used to be smarter and used to like playing with number registers. Perhaps this was a way of controlling updates to reference manuals. I don't know. I do know how to change the date, though.

In the definition of the .TH macro (table heading), there is a string definition for a string called [5. That's the date. All you have to do is redefine [5 at the top of your file. For example,

.ds [5 "January 1, 2001

TIP: When you define strings, use an opening quotation mark, but no closing mark. If you forget and put that closing quotation mark, the closing quotation mark will be printed.

Now, what about that "Page 1"? Manpages are not numbered like ordinary document pages. The reason is that reference manuals are lengthy and are updated frequently. Furthermore, Bell Laboratories decided many years ago never to number replacement pages with letters, such as 101a, 101b, and so on. Because it was impractical to reprint a 2000-page manual just because you had inserted two pages at the beginning, Bell Labs came up with another solution: Number the pages consecutively only for each entry; then start again with "Page 1."

You can change this, but you'll face the same dilemma that Bell Labs faced: What do you do about updates? Assuming this isn't a problem, how do you number reference manual pages consecutively?

You can achieve consecutive page numbering by using the register (-r) option when you print your file

troff -rc1 filename


The man macros fall into two basic categories: headings and paragraph styles. Using these macros correctly is an art, whereas once it was a science. Fonts are no longer as rigidly defined. For example, earlier UNIX reference manuals did not use a monospace—or constant width—font. Today, monospace is routinely used for file and directory names and for "computer voice"—that is, anything you see on the screen. Sometimes a distinction is made between monospace (\f(CW) and bold monospace (\f(CB). Bold monospace is used to indicate what the user types; it appears in the syntax section of a manpage.

The example in Figure 22.1 represents one way of using the man macros. Type styles are a matter of individual or company preference.

man recognizes three types of headings:

.TH and .SH are mandatory. A manpage must have a .TH and at least one .SH.

.TH takes up to four arguments. These are positional arguments. Therefore, if you don't use the third (and least common) argument but you want the fourth, you must insert a null argument ("") before the fourth argument. The syntax for .TH is

.TH <title> <section number> <commentary> <manual name>

title specifies the title of the manpage. This appears in the page header on the left and the right. It can be more than one word, so enclose it in quotation marks. The title of the manpage shown in Figure 22.1 is namehim.

CAUTION: Failure to enclose arguments to the .TH macro in quotation marks produces random unsightly dots on the printed page.

section number is a number from 1 through 5 that indicates the section of the reference manual to which the entry belongs. (Refer to Chapter 5, "Popular Tools," for information about UNIX reference manuals.) This number appears in the header in parentheses after the title. Don't include parentheses; they are supplied automatically. The manpage shown in Figure 22.1 has 0 as the section number. (Note: 0 is not really a permissible section number.)

commentary is an extra comment, such as Local. The argument appears in the header. It must be enclosed in quotation marks if there are embedded blanks. The manpage shown in Figure 22.1 doesn't have any commentary.

NOTE: Local means that the command described by the manual page is not a standard SVR4 command. It might be a brand new command created for your particular UNIX system, or it might be a standard SVR4 command that has been modified for your system.

manual name is the name of the manual—for example, UNIX System V or Documenter's Workbench. The name of the manual shown in Figure 22.1 is Novelist's Workbench.

.TH is a shared macro name. The tbl preprocessor, identified by its starting and ending macros—.TS and .TE—relies on a .TH macro to specify column headings on a multipage table. This presents a potential problem. (tbl is discussed fully in Chapter 23, "Formatting Tables with tbl.")

The .TH table heading macro can appear only within a .TS and .TE pair. Supposedly, this insulates the macro and alerts the macromancer to rename the .TH man title macro whenever a .TS is encountered. Don't bet on it.

NOTE: The troff primitive .rn renames macros.

CAUTION: Don't use the .TH table heading macro on a manpage. The results are unpredictable and depend on your individual system. If you have a multipage table, you can always create the column headings manually. It isn't an elegant solution, but it doesn't break anything.

The .SH macro is a crucial one. With .TH it is mandatory for manpages. It is customarily followed by a keyword, although you can specify any word or words you want. The most common .SH keywords are


The .SH macros are used like this:


namehim - brief description of entry

Text following .SH is indented, as shown in Figure 22.1.

.SH keywords are always printed in all caps, and you don't need to put quotation marks around a two-word keyword. If you do use quotation marks, they won't be printed.

The most crucial .SH is .SH NAME. .SH NAME is mandatory. It is used to produce the permuted index, and its arguments must be entered on a single line—no matter how long that line is. No period is used at the end of the line. Naturally, it's a good idea to be as terse as possible.

The manpage shown in Figure 22.1 uses .SH OPTIONS after .SH SYNTAX. An alternate style sometimes seen in the reference manuals is the where form, which puts the word where on a line by itself and lists the options and arguments shown in the syntax section.

If a manpage needs headings under the .SHs, use .SS. Text following .SS is indented further.

Paragraph Styles

Almost all the man paragraph styles will be familiar to ms users. There are four ordinary paragraph macros:


Begins a paragraph with an indented first line


Synonym for .PP. The only thing it does is call .PP


Begins left-blocked paragraphs (no indent)


Specifies interparagraph spacing

To set the indentation for .PP (and .P), use number register PI. The default unit is ens, but you can use any unit you want as long as you specify it. Unlike ms, man provides a macro to change the spacing between paragraphs: .PD.

NOTE: This section ("The man Macro Package") shows you how to use number registers that are useful with man macros. If you want to find out what other registers are available in troff, refer to the "Number Registers" section in Chapter 21, "Basic Formatting with troff and nroff."

The .PD macro is nothing more than ms's PD number register turned into a macro. Because the format of manpages is so exacting, writers need more control over spacing. The argument to .PD specifies interparagraph spacing. (Note that for nroff this argument is interpreted as whole lines; for troff you can specify .3v or something similar.) .PD is most often used to suppress spacing between list items, which are paragraphs in man. This is done very simply: .PD 0. The default spacing for .PD is .4r in troff, one line in nroff.

man has two hanging paragraph styles that will be new to ms users. They are .HP and .TP. .HP is a simple hanging paragraph. The first line is flush with the margin. All subsequent lines in the paragraph are indented by the amount specified in the argument to .HP. .TP is more complex. It is described below, following the discussion of .IP.

In addition to these more or less straightforward paragraph styles, man has the same indented paragraph as ms, also initiated by the .IP macro. The .IP macro is useful for formatting lists.

.IP can take two arguments. The first argument is a label, or tag. It can be a word, a number, or even the troff code for a bullet. The second argument specifies how far in from the left margin to indent the rest of the first line and all the rest of the paragraph. For a detailed description of this technique, refer to "Paragraph Styles" in the "Using the ms Macro Package" section earlier in this chapter.

The .RS and .RE pair is used to create relative indents. .RS (relative start) starts a 5-en indent from whatever the current indent is. .RE returns to the indent whatever it was before .RS was called. For every .RS in your file, you need a .RE to undo it. You can use this pair of macros to build nested lists. This technique is described under "Paragraph Styles" in the "Using the ms Macro Package" section earlier in this chapter.

.TP is similar to .IP. In fact, .TP produces virtually the same output. However, you specify it a little differently. Whereas .IP takes two arguments, .TP takes only one—the indentation. The line following the .TP macro call is called the tag. If the tag is wider than the specified indentation, the text following the tag starts on the next line. Therefore, although you can use .IP without a tag—or, more accurately, with a null tag—.TP requires a tag.

These codes that produces uses .IP, .RS., and .RE. Here are some lines of that code:

.TH namehim 0 "Novelist's Work Bench"


\f3namehim \fP - supplies one or more names (first, last, or both) for fictional character


\f5namehim\f1[ \f5F | L\f1 ]  [ \f5-a\f2age\f1 ]  [ \f5-y\f2year\f1 ] ...


.IP "\f5-F | -L\fP" 3m

specifies first or last nam; if neither \fF\fP nor \f5L\fP

is specified, both are produced.

.IP \f5-t\fP 3m

Specifies type of name:

select from the following (may be combined):


.IP \f5a\fP 3m


.IP \f5f\fP 3m


.IP \f5h\fP 3m


.IP \f5l\fP 3m


Fonts and Point Size

man recognizes the .R (roman), .I (italics), and .B (bold) macros, all of which operate exactly as they do in ms and mm. Because man was originally designed to produce output using only those fonts—no monospace—it also has some macros that specify alternating fonts. At first glance, these seem superfluous. Take another look, though, at the code. Formatted without monospace, the first part of the syntax line would have alternated bold and oman:


namehim [ F | L ]  [ -a ...

man permits all six permutations of alternating roman, italic, and bold fonts:


You may never have occasion to use these macros, but it's nice to know that they're available.

In addition to the font change macros, there is one macro for changing point size: .SM. (Users of ms might wonder what became of .LG and .NL.) man needs .SM more than the other macro packages because manual pages contain terms with long names that must be written in capital letters. To make these terms more readable and to conserve space, man includes a macro that produces a smaller point size—two points smaller.

.SM has another special use: printing the word UNIX in capital or small cap letters. Because UNIX is a registered trademark, it should be printed in a way that distinguishes it from ordinary text. Sometimes it appears in all capital letters. Another acceptable way is with a capital U and small capital N, I, and X, as in Unix.

Preprocessor Macros

The only preprocessor macros recognized by man are the .TS and .TE table macros. Remember not to use the table macro .TH.

Predefined Strings

The man package has three predefined strings. They are


Produces the registered trademark symbol


Produces the trademark symbol


Returns to the default point size and vertical spacing

Miscellaneous Macros

.TH resets tab stops whenever it is called. The default settings are every 7.2 ens in troff and every 5 ens in nroff. However, experimenting with various customized indents might affect tab settings. If you want to restore the tab settings and you can't wait for the next .TH, use the .DT macro.

The .PM (proprietary marking) macro is interesting for its history, but unless you change its text, it isn't really useful. It takes two arguments. The first argument identifies the type of marking, such as Proprietary or Restricted. The second argument is the year. If you omit the year, the default is the current year.

Using man Macros with troff and nroff

You can invoke the man macros with the troff or nroff command. Printing man files is covered in detail in the "Printing Files Formatted with man Macros" section in Chapter 29, "Processing and Printing Formatted Files."

man Macro Summary

Table 22.1 lists the man macros and describes their functions.






With text, sets text in bold. On a line by itself, changes to bold font.


Bold italic

Alternates bold and italic fonts.


Bold roman

Alternates bold and roman fonts.


Defines tabs and sets and tab stops

The default is 7.2 ens in troff 5 ens in nroff.


Hanging paragraph



With text, sets text in italics. On a line by itself, changes to italic font.


Italic bold

Alternates italic and bold fonts.


Indented paragraph


Italic roman

Alternates italic and roman fonts.


Block-style paragraph



Synonym for .PP. .P actually calls .PP.


Sets the distance between paragraphs 1v

The default is .4v in troff and in nroff.


Proprietary marking

This is an AT&T macro for placing different types of Proprietary notices at the bottom of each page.





With text, sets text in roman type. On a line by itself, changes to roman type.


Roman bold

Alternates roman and bold fonts.


Ends a relative indent begun by .RS


Roman italic

Alternates roman and italic fonts.


Begins a relative indent




Reduces point size by 2 points

Stands for small.0




Table end


Title head

.SN NAME is the crucial macro for producing the permuted index


Indented paragraph with hanging tag.


Table start

Supposedly, the H argument with the .TH macro for continuing table column heads works with the man macros. It's safer, though, to avoid the issue.


Macro packages take the guesswork out of formatting. Your document has a defined "look and feel"—no surprises. And it's easier than you think to use a macro package. As you work with a package, you learn more about it, but you can start with only the basics and produce good-looking documents.

Previous Page TOC Next Page Home