📄 unx21.htm
字号:
<BR></P>
<P>Two examples are illustrated in Listing 21.15 and Figure 21.21. The first macro defined, PP, is used to signal a new paragraph. You first request a space, then you temporarily indent the first line by a quarter-inch. You also insure that the font is
Times Roman. The second macro defined is for a header, HD. Give it two arguments: the first is the point size desired for the header and the second is the text of the header. First request a space, then change the point size to the requested size. Next,
request that the text be centered and made bold. Then issue the text, reset the point size and font, and request an additional space.
<BR></P>
<P>
<BR><B><A HREF="21unx21.gif">Figure 21.21. </B><B><I>troff</I></B><B> output with a defined </B><B>macro.</A></B>
<BR></P>
<UL>
<LH><B>Listing 21.15. </B><B>troff</B><B> source defining macros.</B></LH></UL>
<PRE>.de PP
.sp
.ti +0.25i
.ft R
..
.de HD
.sp
.ps \\$1
.ce
.ft B
\\$2
.ps
.ft P
.sp
..
.HD 14 "A sample header"
.PP
We begin the text of the first paragraph here. This is indented
and formatted. We continue with the text of the first paragraph
until we want the second paragraph.
.PP
We re-issue the macro, and get the space and indent.</PRE>
<P>Macros can be changed after creation using the .am request. This appends troff requests to the already existing macro, given in the first argument. In the preceding example, assume you wanted the second and subsequent paragraphs to have a point size of
ten for the text. You could do this by including the following commands after the first call to PP:
<BR></P>
<PRE>.am PP
.ss 10
..</PRE>
<P>You could have redefined the macro with .de, but the .am request is quicker. You can also rename macros with .rn. This can be used even with standard troff requests and strings. The original name is the first argument, and the new name is the second
argument. The old name is not retained.
<BR></P>
<P>Lastly, macros, strings, and other requests can be removed with .rm. Any subsequent commands to the macro will be ignored.
<BR></P>
<H3 ALIGN="CENTER">
<CENTER><A ID="I14" NAME="I14">
<FONT SIZE=4><B>Number Registers</B>
<BR></FONT></A></CENTER></H3>
<P>troff provides number registers for the tracking of parameters for troff. These can be accessed with the escape sequence \n. For single character names, like x, use \nx. For multiple character names, like xx, use \n(xx. Number registers are used for
items such as page numbers and line length. The predefined registers include % for page number, dw for the day of the week, dy for the day, mo for the month, and yr for the year. nl also shows the position of the last printed line. Listing 21.16 shows how
some of these registers can be used. Figure 21.22 is the output.
<BR></P>
<P>
<BR><B><A HREF="21unx22.gif">Figure 21.22. </B><B><I>troff</I></B><B> number registers in output.</A></B>
<BR></P>
<UL>
<LH><B>Listing 21.16. Using number registers.</B></LH></UL>
<PRE>.nf
.ll 5.0i
.in 2.5i
101 Main Street
Morristown, NJ 07960
\n(mo/\n(dy/\n(yr
.in 0
.sp
Dear Sir,</PRE>
<P>Many read-only registers contain configuration parameters. Some of these are listed in Table 21.5.
<BR></P>
<UL>
<LH><B>Table 21.5. Common number registers in </B><B>troff</B><B>.</B>
<BR></LH></UL>
<TABLE BORDER>
<TR>
<TD>
<PRE><I>Register</I>
<BR></PRE>
<TD>
<PRE><I>Description</I>
<BR></PRE>
<TR>
<TD>
<P>.$</P>
<TD>
<P>Number of arguments to a macro</P>
<TR>
<TD>
<P>.A</P>
<TD>
<P>Set to 1 if -a is used on troff command line; always 1 for nroff</P>
<TR>
<TD>
<P>.T</P>
<TD>
<P>Set to 1 if -T is used on nroff command line; always 1 for troff</P>
<TR>
<TD>
<P>.a</P>
<TD>
<P>Value of extra space most recently used</P>
<TR>
<TD>
<P>.c</P>
<TD>
<P>Number of lines read from current input file</P>
<TR>
<TD>
<P>.f</P>
<TD>
<P>Current font</P>
<TR>
<TD>
<P>.h</P>
<TD>
<P>Text high water mark for current page</P>
<TR>
<TD>
<P>.i</P>
<TD>
<P>Current indent</P>
<TR>
<TD>
<P>.l</P>
<TD>
<P>Current line length</P>
<TR>
<TD>
<P>.n</P>
<TD>
<P>Length of text on previous output line</P>
<TR>
<TD>
<P>.o</P>
<TD>
<P>Current page offset</P>
<TR>
<TD>
<P>.p</P>
<TD>
<P>Current page length</P>
<TR>
<TD>
<P>.s</P>
<TD>
<P>Current point size</P>
<TR>
<TD>
<P>.u</P>
<TD>
<P>Fill more flag (1 for on, 0 for off)</P>
<TR>
<TD>
<P>.v</P>
<TD>
<P>Current vertical line spacing</P></TABLE>
<P>A short script to list default values is shown in Listing 21.17, with Figure 21.23 showing the output.
<BR></P>
<P>
<BR><B><A HREF="21unx23.gif">Figure 21.23. </B><B><I>troff</I></B><B> output with register </B><B>values.</A></B>
<BR></P>
<UL>
<LH><B>Listing 21.17. </B><B>troff</B><B> source to identify register values.</B></LH></UL>
<PRE>.nf
The current font is \n(.f
The current point size is \n(.s
The line length is \n(.l
The page length is \n(.p
The page offset is \n(.o</PRE>
<P>Of course, these registers are useful; however, the real benefit of registers comes from the user's ability to define their own registers. These can be used to track headers, paragraph numbers, and section numbers. The .nr request initializes and
modifies user-specified registers. It takes two or three arguments: the first is the register name, and the second is the register modification. When first created, a register is assigned the value of zero. A positive number is added to the value; a
negative number is subtracted. An optional third argument sets a default increment and decrement value. The automatic increment can be used in escape sequences: \n+(xx adds the increment to register xx, and \n-(xx subtracts the decrement.
<BR></P>
<P>The appearance of the number is set with the .af request. The first argument is the register, the second is one of six formats. 1 is for an Arabic number sequence, 001 is for a zero-filled Arabic number sequence. i and I are for Times Roman numbers,
upper- and lowercase, and a and A are for alphabetic sequences.
<BR></P>
<P>Lastly, the .rr request removes a number register. There are a limited number of registers available, identified by the read-only register .R. The document writer may need to remove registers if space becomes a problem.
<BR></P>
<P>Listing 21.18 illustrates the source of a macro that numbers sections of the Constitution. The output is in Figure 21.24. The section header macros are for Articles, sections, and paragraphs of the constitution. First define the aR number register to
count the articles, and set its display format to Times Roman numerals. You first define the AR macro. It centers a 16-point bold text, with the word "Article" and the number register. Note that you increment the number register every time you
print the value. You also set the sE number register to zero, as an Arabic number. You then reset the point size and font. The SE macro is similar, printing the section and number, and setting pP to zero. The PP macro increments pP.
<BR></P>
<P>
<BR><B><A HREF="21unx24.gif">Figure 21.24. </B><B><I>troff</I></B><B> output with number </B><B>registers.</A></B>
<BR></P>
<UL>
<LH><B>Listing 21.18. </B><B>troff</B><B> source using number registers.</B></LH></UL>
<PRE>.ce
Preamble
.sp
We, the people of the United States, in order
to form a more perfect Union, establish justice, insure
domestic tranquility, provide for the common defense, promote
the general welfare,
and secure the blessing of liberty to ourselves and our
posterity do ordain and establish this Constitution for the
United States of America.
.sp
.nr aR 0 1
.af aR I
.de AR
.ce
.ps 16
.ft B
Article \\n+(aR
.nr sE 0 1
.af sE 1
.ps 12
.ft P
..
.de SE
.sp
.ft B
\\s-2SECTION \\n+(sE:\\s+2
.ft P
.nr pP 0 1
.af pP 1
..
.de PP
.sp
.ft I
\\s-3Paragraph \\n+(pP:\\s+3
.ft P
..
.AR
.SE
Legislative powers; in whom vested:
.PP
All legislative powers herein granted shall be vested in a
Congress of the United States, which shall consist of a Senate
and a House of Representatives.
.SE
House of Representatives, how and by whom chosen, Qualifications
of a Representative. Representatives and direct taxes, how
apportioned. Enumeration. Vacancies to be filled. Power of
choosing officers and of impeachment.
.PP
The House of Representatives shall be composed of members chosen
every second year by the people of the several states, and the
electors in each State shall have the qualifications requisite
for electors of the most numerous branch of the State Legislature.
.PP
No person shall be a Representative who shall not have attained
to the age of twenty-five years, and been seven years a citizen
of the United States, and who shall not, when elected, be an
inhabitant of that State in which he shall be chosen.
.PP
Representatives and direct taxes shall be apportioned among the
several States which maybe included within this Union, according
to their respective numbers, which shall be determined by adding
to the whole number of free persons, including those bound for
service for a term of years, and excluding Indians not taxed,
three-fifths of all other persons. The actual enumeration shall
be made within three years after the first meeting of the
Congress of the United States, and within every subsequent term
of ten years, in such manner as they shall by law direct. The
number of Representatives shall not exceed one for every thirty
thousand, but each State shall have at least one Representative;
and until such enumeration shall be made, the State of New
Hampshire shall be entitled to choose three, Massachusetts eight,
Rhode Island and Providence Plantations one, Connecticut
five, New York six, New Jersey four, Pennsylvania eight,
Delaware one, Maryland six, Virginia ten, North Carolina five,
South Carolina five, and Georgia three.
.PP
When vacancies happen in the representation from any State, the
Executive Authority thereof shall issue writs of election to fill
such vacancies.
.PP
The House of Representatives shall choose their Speaker and other
officers; and shall have the sole power of impeachment.</PRE>
<H3 ALIGN="CENTER">
<CENTER><A ID="I15" NAME="I15">
<FONT SIZE=4><B>Traps and Diversions</B>
<BR></FONT></A></CENTER></H3>
<P>So far, I have examined the results of invoking troff requests where you place them in your document. These are not the only examples of executing troff requests. You can specify macros to be executed at any given physical position on a document. These
are called traps, and they can be triggered by page position, diversions, and input line count. A common use for page traps is to place headers or footers on pages. Diversion traps can be used to create footnotes in text, or to create a reference list for
the end of a chapter. Input line traps count the number of lines since the request. These are useful for when macros are meant to be a single line.
<BR></P>
<P>Diversions are used to direct output to a macro, instead of to the page. The diversion requests are usually in macros, and traps must be set to produce the diversion output. Diversions are created with the .di request, the call followed by the name of
the diversion macro. If no argument is present, the diversion ends, and output resumes on the page. Text can be added to the diversion with the .da request. Diversions can also be requested by page position with .dt, followed by the position and diversion
macro name.
<BR></P>
<P>Traps are set with the .wh request. This is followed by a position and a macro name. If a macro had previously been set at that position, then that trap is removed. If no macro is passed as an argument, then any traps at that position are removed. The
position of a macro trap can be changed with the .ch request, followed by the macro name and position. If a position is missing, the macro is removed. Input traps are set with the .it request, followed by a text position and macro name. Finally, a trailing
macro can be set with .em. This sets a macro to be run at the end of input.
<BR></P>
<P>The use of diversions and traps is illustrated in Chapter 26, "Writing Your Own Macros."
<BR></P>
<H3 ALIGN="CENTER">
<CENTER><A ID="I16" NAME="I16">
<FONT SIZE=4><B>Tabs, Character Conversions, and Controls</B>
<BR></FONT></A></CENTER></H3>
<P>Tabs and tab spacing can be set by the document writer in troff. This gives the writer the ability to create simple tables by lining up columns and using tabs. The .ta request sets the tab stops, and if a stop value is preceded by a +, then the stop is
relative to the previous tab stop. The tab repetition character (the character that fills the space between text and tab stops) can be specified with the .tc request. The nature of the tab stop can also be specified right after the tab stop distance. The
text left-adjusts within the tab space by default. To center the text, use a
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -