⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unx27.htm

📁 Linux Unix揭密.高质量电子书籍.对学习Linux有大帮助,欢迎下载学习.
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<BR></P>

<UL>

<LI>set (or define or initialize) it

<BR>

<BR></LI>

<LI>interpolate it (that is, examine the contents and, optionally, compare the contents to a specified number or even to the contents of a different number register)

<BR>

<BR></LI>

<LI>remove it

<BR>

<BR></LI></UL>

<P>Number registers are used very frequently in macro definitions. They contain such information as line length, page offset, current font number, previous font number, current indent, current list item number, and so on.

<BR></P>

<P>For example, if you're formatting an automatic list (with mm), you would find the following information in number registers: current indent; current nesting level (That is, is the list a list-within-a-list?); item number; format of item number (that is, 

Arabic, uppercase roman numeral, lowercase roman numeral, uppercase alphabetic, or lowercase alphabetic).

<BR></P>

<P>Every time troff processes a .LI, the number registers that control these characteristics are interpolated. Some of them (the list item number, for example) are also incremented.

<BR></P>

<P>This information can be useful if you are formatting what I call a &quot;discontinuous list&quot; (a list that has ordinary text between two of the list items).

<BR></P>

<P>Before you insert the ordinary text, you must end the current list. When you want to continue the list, another .AL and .LI will start the list at 1. However, you want it to start at 5. If you know which number register stores this information, you can 

reset it.

<BR></P>

<P>To set a number register:

<BR></P>

<PRE>.nr a 0

.nr aa 0

.nr AA 1i

.nr b +1i</PRE>

<P>The units are optional&#151;and very tricky. If you specify a unit of measurement (called a scaling factor) with a number register, the value is stored by troff in troff units (u), no matter what unit you specify. Thus, (for a 300dpi device) 1i is 
stored as 300u. When you add 1 to the register, you are adding 1 troff unit&#151;unless you specify units. Note the following:

<BR></P>

<PRE>.nr x 21    \&quot;has a value of 600u

.nr x +1    \&quot;now has a value of 601u

.nr x 2i   \&quot;has a value of 600u

.nr x +1   \&quot;now has a value of 900u</PRE>

<P>You also have the option of specifying the increment/decrement to the register when you define it:

<BR></P>

<PRE>.nr b 10 1

.nr bb 0 2</PRE>

<P>Note that you do not specify whether 1 (in the first instance) or 2 (in the second instance) is to be an increment or a decrement. That is done when you interpolate the register.

<BR></P>

<P>To interpolate the contents of a number register:

<BR></P>

<PRE>\\na       \&quot;one-character name

\\n(aa     \&quot;two-character name

\\n+a      \&quot;increments register b

\\n-(bb    \&quot;decrements register bb</PRE>

<P>Number registers contain numbers. They are often used in arithmetic expressions:

<BR></P>

<PRE>.if \\na&lt;1

.if \\na=\\nb

.if \\na+\\nb&lt;\\nc</PRE>

<P>There is another arithmetic expression, common in troff, that looks unfinished:

<BR></P>

<PRE>.if \\na         \&quot;if a is greater than 0

.if \\na-\\nb    \&quot;if a minus b is greater than 0

.if !\\na        \&quot;if a is not greater than 0</PRE>

<P>To increment or decrement a number register, use

<BR></P>

<PRE>.nr a \\na+1

.nr a \\na-1</PRE>

<P>(Note that you don't use an equal sign when you set a register.)

<BR></P>

<P>You can define a number register in terms of another number register (or two):

<BR></P>

<PRE>.nr z (\\nx+\\ny)</PRE>

<P>Toward the end of this chapter there are two tables of number registers predefined by troff. You will not want to use those names for your own number registers. If you are working with a macro package like ms or mm, however, you must also check the 
number registers used by your macro package because you don't want to overwrite the contents of the number register that numbers lists or stores indents. 

<BR></P>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="more.gif" WIDTH = 35 HEIGHT = 35><B>Using Number Registers for Automatic Numbering</B>

<BR>

<BR>Every now and then you work on a document that cries out for automatic numbering. The examples that come to mind (because they're documents I've worked on) are storyboards for training materials. Each &quot;board&quot; represents a screen (in 
computer-based tutorials) or a viewgraph (in ordinary courses). Each board consists of graphics, text, and possibly animation or sound instructions.

<BR>

<BR>I've found that you need to number the boards, both for your own convenience and to make things simple for your reviewers. I've also found that the order of the boards changes with depressing frequency.

<BR>

<BR>If you explicitly number the boards, you have to explicitly change the numbers every time you switch 7 and 8 or 30 and 54. This is not fun and not an efficient way to spend your time.

<BR>

<BR>You can use number registers to provide automatic numbers for the boards. (You can also write an awk program, but, if you don't know awk, this isn't an efficient solution either.)

<BR>

<BR>To use number registers for automatic numbering, do the following:

<OL>

<LI>Select a number register that is not being used by troff or by your macro package. (For this example, I'm using vv.)

<BR>

<BR></LI>

<LI>Initialize the register at the top of your file: .nr vv 0.

<BR>

<BR></LI>

<LI>Whenever you want a number, interpolate vv : \n(vv+1. (Remember, you're doing this in your text file. You don't have to hide the register from troff.)

<BR>

<BR></LI></OL>

You can do this even more elegantly by defining an autoincrementing/decrementing number register:

<BR>

<BR>.nr vv 0 1

<BR>

<BR>The initial value in vv is 0; the autoincrement/decrement is 1. At this point, troff doesn't know whether you want the register contents to be incremented or decremented. You specify that when you interpolate the register.

<BR>

<BR>\n+(vv

<BR>

<BR>(The plus sign tells troff to increment the register.)

<BR>

<BR>You can refine this to include a unit number, giving you compound folios, but this is practical only if you're using a macro package with chapter numbers (or some similar device like section or unit numbers) and you're using these numbers in your 
files.

<BR>

<BR>Assuming you're using chapter numbers and the register for chapter numbers is cn, you can specify your board numbers like this:

<BR>

<BR>.\n(cn-\n+(vv

<BR>

<BR>If your chapter numbers are stored in a string called cn, do this:

<BR>

<BR>\*(cn-\n+(vv

<BR>

<BR>There is one disadvantage to using automatic numbering in this way. It's the same disadvantage you may have experienced with mm's automatic lists. When you look at your file, you have no idea what your current step (or board) is. And, if you have to 
refer to a previous step or board, you probably end up writing &quot;Repeat Steps 1 through ???,&quot; printing your file, and inserting the correct numbers later.

<BR></NOTE>

<HR ALIGN=CENTER>

<P>Sometimes you need to remove registers. This is especially necessary if your macros use a large number of registers. It's a good idea to get into the habit of removing temporary registers as soon as you're done with them. To remove a register, use the 
.rr primitive:

<BR></P>

<PRE>.rr a     \&quot;remove register a</PRE>

<H4 ALIGN="CENTER">

<CENTER><A ID="I8" NAME="I8">

<FONT SIZE=3><B>Defined Strings</B>

<BR></FONT></A></CENTER></H4>

<P>A defined string is a set of characters to which you assign a name. The string is always treated as a literal, and you cannot perform any arithmetic operation on it. You can, however, compare it to another string, or even compare the string 
&quot;2&quot; to the contents of a number register.

<BR></P>

<P>A string definition looks a lot like a macro definition:

<BR></P>

<PRE>.ds name value

.ds name &quot;value that has a lot of separate words in it

.dsU U\s-1NIX\s0

.dsUU &quot;UNIX Unleashed</PRE>

<P>String names consist of one or two characters. The names come from the same pool as macro names, so be careful to choose a unique name for your string. In the examples above, note that the .ds can (but does not have to be) followed by a space. Note also 

that you use only the opening quotation marks when your string consists of multiple words. (If you forget and include a closing quotation mark, it will be printed as part of the string).

<BR></P>

<P>To invoke the string:

<BR></P>

<PRE>\\*a      \&quot;one-character name

\\*(aa    \&quot;two-character name</PRE>

<P>Sometimes a string is a better choice than a number register. If you're dealing with alphabetic characters, a string may be your only choice.

<BR></P>

<P>Consider the following: You want to define something to hold the number of your current chapter. If you use a number register, you can increment these numbers very easily. You'll only have to set the value once, at the beginning of the book. Unless you 

have appendixes. If you have appendixes, you'll have to reset to 1 when you reach Appendix A, and then you'll have to translate that number into a letter.

<BR></P>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> To use a number register for chapter numbers, use .af (alter format) to produce uppercase letters for your appendixes. .af recognizes the following formats:

<BR>

<BR>1  Arabic numerals

<BR>

<BR>i  lowercase roman numerals

<BR>

<BR>I  uppercase roman numerals

<BR>

<BR>a  lowercase alphabetic characters

<BR>

<BR>A  uppercase alphabetic characters

<BR>

<BR>To use the letter A in a compound page number (where the number register storing chapter numbers is cn), specify the following:  .af cn A.

<BR></NOTE>

<HR ALIGN=CENTER>

<P>Perhaps a string would be simpler. You'll have to redefine the string at the beginning of each chapter, but you won't have to do any diddling.

<BR></P>

<P>Strings can be used as general purpose abbreviations, although this is not their primary purpose, nor even the best use of strings. A better use is to define a string containing the preliminary name of the product you are documenting. Then, when the 
marketing people finally decide to call their new brainchild &quot;XYZZY Universal Widget,&quot; you don't have to do any searching or grepping to replace the temporary name. You can just redefine the string.

<BR></P>

<P>Define a string near the top of your file as the preliminary name of the product:

<BR></P>

<PRE>.ds Pn &quot;Buzzy     \&quot;code name for product</PRE>

<P>Remember that strings cannot share names with macros.

<BR></P>

<P>When the ugly duckling &quot;Buzzy&quot; becomes the swan &quot;XYZZY Universal Widget,&quot; just change the definition:

<BR></P>

<PRE>.ds Pn &quot;XYZZY Universal Widget     \&quot;official name for product</PRE>

<P>Like macros, strings can have information appended. To add to a string, use the troff primitive .as. Although it's hard to imagine a use for this primitive, consider the following:

<BR></P>

<P>You are documenting three versions of the XYZZY Universal Widget in three separate documents. For the first document, you could add &quot;Version 1.0&quot; to the string:

<BR></P>

<PRE>.as Pn &quot;(Version 1.0)</PRE>

<P>The other versions can be similarly identified in their documents as &quot;Version 2.0&quot; and &quot;Version 3.0.&quot;

<BR></P>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="more.gif" WIDTH = 35 HEIGHT = 35><B>Listing Names of Existing Macros, Strings, and Number </B><B>Registers</B>

<BR>

<BR>If you are using mm or ms and adding macros to either of these packages, you need to know what names (for macros, strings, and number registers) are available and what names have already been used.

<BR>

<BR>To create a file called sortmac containing the macro names used in mm (assuming mm to be where it ought&#151;namely in /usr/lib/tmac/tmac.m):

<BR>

<BR>grep &quot;^\.de&quot; /usr/lib/tmac/tmac.m | sort | uniq &gt; sortmac

<BR>

<BR>(That code also assumes that you are executing grep from the directory in which you want sortmac to end up.)

<BR>

<BR>Strings are listed pretty much the same way:

<BR>

<BR>grep &quot;^\.ds&quot; /usr/lib/tmac/tmac.m | sort | uniq &gt; sortstr

<BR>

<BR>To list number registers defined in the mm macro package, execute the following sed script in the directory with the macros (/usr/lib/tmac):

<BR>

<BR>sed -n -e 's/.*.nr *\(..\).*/\1/p' tmac.m | sort |uniq &gt; $HOME/sortnum

<BR>

<BR>The standard macro packages should all be in /usr/lib/tmac. The macro filenames are as follows:

<BR>

<BR>tmac.m  mm macros

<BR>

<BR>tmac.s  ms macros

<BR>

<BR>tmac.e  me macros (don't hold your breath looking for this one)

<BR>

<BR>tmac.an  man macros

<BR>

<BR>Remember that troff and nroff&#151;and each macro package&#151;use predefined number registers, and these may not be set within the package.

<BR></NOTE>

<HR ALIGN=CENTER>

<H3 ALIGN="CENTER">

<CENTER><A ID="I9" NAME="I9">

<FONT SIZE=4><B>Getting Started</B>

<BR></FONT></A></CENTER></H3>

<P>To define a macro, you use the .de primitive and end the definition with two periods. A macro to indent the first line of a paragraph could be defined like this:

<BR></P>

<PRE>.dePX     \&quot;macro to create indented paragraphs, no space between

.ti 3P

..</PRE>

<P>This is a very simple example. A &quot;real&quot; paragraph macro would check to make sure there was room for two or three lines and, if not, go to the next page. Nevertheless, this simple definition illustrates some important points. The macro name can 

consist of one or two characters. If you use a name that's already assigned to a macro, the definition in your text file overrides the definition in a macro package. The macro name follows the .de. It can be separated from the .de by a space, but a space 
is not necessary.

<BR></P>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="imp.gif" WIDTH = 68 HEIGHT = 35><B>TIP:</B> Although the space following the .de doesn't matter, consistency does. Some-day, you'll want to list and sort your macro definitions, and you can't sort them as easily unless you can rely on a space (or 

no space) between the .de and the macro name.

<BR></NOTE>

<HR ALIGN=CENTER>

<P>A macro definition can include troff primitives and other macros. A brief description of the macro is included on the definition line. This is crucial. You can forget more about macros in two weeks that you can learn in two years. Comment lavishly. And 

make sure you include a comment on the definition line to identify the macro. This helps when you grep for macro definitions and then sort them. 

<BR></P>

<P>There is one more constraint on macro names: A macro cannot have the same name as a defined string. (Macros are perfectly happy sharing names with number registers, however.)

<BR></P>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> By the way, there's no law against giving a macro the same names as a primitive. In fact, it sounds like an excellent April Fool. If you should be foolish enough to try this, bear in mind that the 
primitive will, for all intents and purposes, cease to exist. All that will remain will be your macro. So make it a good one.

<BR></NOTE>

<HR ALIGN=CENTER>

<P>If, instead of defining a new macro, you want to redefine an existing one, then you use the existing macro's name:

<BR></P>

<PRE>.deP

.ti 3P

..</PRE>

<P>If you redefine the .P macro, the old definition is no longer used (although it's still sitting there in the mm macro package). To return to the old definition, you must get rid of your new definition (delete it from the top of your file or delete the 
file containing the definition).

<BR></P>

<P>The benefit to writing a new macro with a new name is that the old definition is still usable. The drawback is that you're used to typing .P, so you'll probably forget to type .PX when you want to use your new macro.

<BR></P>

<P>In addition to defining a new macro and redefining an existing macro, you can remove a macro, and you can add to an existing macro.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I10" NAME="I10">

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -