10.  Number Registers and Arithmetic

      troff has a facility for doing arithmetic, and for defining and using variables with numeric values, called number registers. Number registers, like strings and macros, can be useful in setting up a document so it is easy to change later. And of course they serve for any sort of arithmetic computation.

      Like strings, number registers have one or two character names. They are set by the .nrcommand, and are referenced anywhere by \nx(one character name) or \n(xy(two character name).

      There are quite a few pre-defined number registers maintained by troff, among them %for the current page number; nlfor the current vertical position on the page; dymoand yrfor the current day, month and year; and .sand .ffor the current size and font. (The font is a number from 1 to 4.) Any of these can be used in computations like any other register, but some, like .sand .fcannot be changed with .nr

      As an example of the use of number registers, in the -msmacro package [4], most significant parameters are defined in terms of the values of a handful of number registers. These include the point size for text, the vertical spacing, and the line and title lengths. To set the point size and vertical spacing for the following paragraphs, for example, a user may say ^nr PS 9
^nr VS 11
The paragraph macro
.PPis defined (roughly) as follows:
^de PP
^ps \\n(PS \" reset size
^vs \\n(VSp \" spacing
^ft R \" font
^sp 0.5v \" half a line
^ti +3m
^^
This sets the font to Roman and the point size and line spacing
to whatever values are stored in the number registers
PSand
VS

      Why are there two backslashes? This is the eternal problem of how to quote a quote. When troff originally reads the macro definition, it peels off one backslash to see what's coming next. To ensure that another is left in the definition when the macro is used, we have to put in two backslashes in the definition. If only one backslash is used, point size and vertical spacing will be frozen at the time the macro is defined, not when it is used.

      Protecting by an extra layer of backslashes is only needed for \n\*\$(which we haven't come to yet), and \itself. Things like \s\f\h\vand so on do not need an extra backslash, since they are converted by troff to an internal code immediately upon being seen.

      Arithmetic expressions can appear anywhere that a number is expected. As a trivial example, ^nr PS \\n(PS-2
decrements PS by 2.
Expressions can use the arithmetic operators +, -, *, /, % (mod),
the relational operators >, >=, <, <=, =, and != (not equal),
and parentheses.

      Although the arithmetic we have done so far has been straightforward, more complicated things are somewhat tricky. First, number registers hold only integers. troff arithmetic uses truncating integer division, just like Fortran. Second, in the absence of parentheses, evaluation is done left-to-right without any operator precedence (including relational operators). Thus 7*-4+3/13
becomes `-1'.
Number registers can occur anywhere in an expression,
and so can scale indicators like
pimand so on (but no spaces).
Although integer division causes truncation,
each number and its scale indicator is converted
to machine units (1/432 inch) before any arithmetic is done,
so
1i/2u
evaluates to
0.5i
correctly.

      The scale indicator uoften has to appear when you wouldn't expect it _ in particular, when arithmetic is being done in a context that implies horizontal or vertical dimensions. For example, ^ll 7/2i
would seem obvious enough _
3½ inches.
Sorry.
Remember that the default units for horizontal parameters like
.llare ems.
That's really `7 ems / 2 inches',
and when translated into machine units, it becomes zero.
How about
^ll 7i/2
Sorry, still no good _
the `2' is `2 ems', so `7i/2' is small,
although not zero.
You
must
use
^ll 7i/2u
So again, a safe rule is to
attach a scale indicator to every number,
even constants.

      For arithmetic done within a .nrcommand, there is no implication of horizontal or vertical dimension, so the default units are `units', and 7i/2 and 7i/2u mean the same thing. Thus ^nr ll 7i/2
^ll \\n(llu
does just what you want,
so long as you
don't forget the
uon the
.llcommand.