|
|
1.1 root 1: .\" @(#)tt10 6.1 (Berkeley) 5/23/86
2: .\"
3: .NH
4: Number Registers and Arithmetic
5: .PP
6: .UL troff
7: has a facility for doing arithmetic,
8: and for defining and using variables with numeric values,
9: called
10: .ul
11: number registers.
12: Number registers, like strings and macros, can be useful in setting up a document
13: so it is easy to change later.
14: And of course they serve for any sort of arithmetic computation.
15: .PP
16: Like strings, number registers have one or two character names.
17: They are set by the
18: .BD .nr
19: command,
20: and are referenced anywhere by
21: .BD \enx
22: (one character name) or
23: .BD \en(xy
24: (two character name).
25: .PP
26: There are quite a few pre-defined number registers maintained by
27: .UL troff ,
28: among them
29: .BD %
30: for the current page number;
31: .BD nl
32: for the current vertical position on the page;
33: .BD dy ,
34: .BD mo
35: and
36: .BD yr
37: for the current day, month and year; and
38: .BD .s
39: and
40: .BD .f
41: for the current size and font.
42: (The font is a number from 1 to 4.)
43: Any of these can be used in computations like any other register,
44: but some, like
45: .BD .s
46: and
47: .BD .f ,
48: cannot be changed with
49: .BD .nr .
50: .PP
51: As an example of the use of number registers,
52: in the
53: .BD \-ms
54: macro package [4],
55: most significant parameters are defined in terms of the values
56: of a handful of number registers.
57: These include the point size for text, the vertical spacing,
58: and the line and title lengths.
59: To set the point size and vertical spacing for the following paragraphs, for example, a user may say
60: .P1
61: ^nr PS 9
62: ^nr VS 11
63: .P2
64: The paragraph macro
65: .BD .PP
66: is defined (roughly) as follows:
67: .P1
68: .ta 1i
69: ^de PP
70: ^ps \e\en(PS \e" reset size
71: ^vs \e\en(VSp \e" spacing
72: ^ft R \e" font
73: ^sp 0.5v \e" half a line
74: ^ti +3m
75: ^^
76: .P2
77: This sets the font to Roman and the point size and line spacing
78: to whatever values are stored in the number registers
79: .BD PS
80: and
81: .BD VS .
82: .PP
83: Why are there two backslashes?
84: This is the eternal problem of how to quote a quote.
85: When
86: .UL troff
87: originally reads the macro definition,
88: it peels off one backslash
89: to see what's coming next.
90: To ensure that another is left in the definition when the
91: macro is
92: .ul
93: used,
94: we have to put in two backslashes in the definition.
95: If only one backslash is used,
96: point size and vertical spacing will be frozen at the time the macro
97: is defined, not when it is used.
98: .PP
99: Protecting by an extra layer of backslashes
100: is only needed for
101: .BD \en ,
102: .BD \e* ,
103: .BD \e$
104: (which we haven't come to yet),
105: and
106: .BD \e
107: itself.
108: Things like
109: .BD \es ,
110: .BD \ef ,
111: .BD \eh ,
112: .BD \ev ,
113: and so on do not need an extra backslash,
114: since they are converted by
115: .UL troff
116: to an internal code immediately upon being seen.
117: .WS
118: .PP
119: Arithmetic expressions can appear anywhere that
120: a number is expected.
121: As a trivial example,
122: .P1
123: ^nr PS \e\en(PS\-2
124: .P2
125: decrements PS by 2.
126: Expressions can use the arithmetic operators +, \-, *, /, % (mod),
127: the relational operators >, >=, <, <=, =, and != (not equal),
128: and parentheses.
129: .PP
130: Although the arithmetic we have done so far
131: has been straightforward,
132: more complicated things are somewhat tricky.
133: First,
134: number registers hold only integers.
135: .UL troff
136: arithmetic uses truncating integer division, just like Fortran.
137: Second, in the absence of parentheses,
138: evaluation is done left-to-right
139: without any operator precedence
140: (including relational operators).
141: Thus
142: .P1
143: 7*\-4+3/13
144: .P2
145: becomes `\-1'.
146: Number registers can occur anywhere in an expression,
147: and so can scale indicators like
148: .BD p ,
149: .BD i ,
150: .BD m ,
151: and so on (but no spaces).
152: Although integer division causes truncation,
153: each number and its scale indicator is converted
154: to machine units (1/432 inch) before any arithmetic is done,
155: so
156: 1i/2u
157: evaluates to
158: 0.5i
159: correctly.
160: .PP
161: The scale indicator
162: .BD u
163: often has to appear
164: when you wouldn't expect it _
165: in particular, when arithmetic is being done
166: in a context that implies horizontal or vertical dimensions.
167: For example,
168: .P1
169: ^ll 7/2i
170: .P2
171: would seem obvious enough _
172: 3\(12 inches.
173: Sorry.
174: Remember that the default units for horizontal parameters like
175: .BD .ll
176: are ems.
177: That's really `7 ems / 2 inches',
178: and when translated into machine units, it becomes zero.
179: How about
180: .P1
181: ^ll 7i/2
182: .P2
183: Sorry, still no good _
184: the `2' is `2 ems', so `7i/2' is small,
185: although not zero.
186: You
187: .ul
188: must
189: use
190: .P1
191: ^ll 7i/2u
192: .P2
193: So again, a safe rule is to
194: attach a scale indicator to every number,
195: even constants.
196: .PP
197: For arithmetic done within a
198: .BD .nr
199: command,
200: there is no implication of horizontal or vertical dimension,
201: so the default units are `units',
202: and 7i/2 and 7i/2u
203: mean the same thing.
204: Thus
205: .P1
206: ^nr ll 7i/2
207: ^ll \e\en(llu
208: .P2
209: does just what you want,
210: so long as you
211: don't forget the
212: .BD u
213: on the
214: .BD .ll
215: command.
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.