Number Registers and Arithmetic
has a facility for doing arithmetic,
and for defining and using variables with numeric values,
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.
and are referenced anywhere by
There are quite a few pre-defined number registers maintained by
for the current page number;
for the current vertical position on the page;
for the current day, month and year; and
for 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,
As an example of the use of number registers,
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
is defined (roughly) as follows:
^ps \e\en(PS \e" reset size
^vs \e\en(VSp \e" spacing
This sets the font to Roman and the point size and line spacing
to whatever values are stored in the number registers
Why are there two backslashes?
This is the eternal problem of how to quote a quote.
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
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
(which we haven't come to yet),
and so on do not need an extra backslash,
since they are converted by
to an internal code immediately upon being seen.
Arithmetic expressions can appear anywhere that
Expressions can use the arithmetic operators +, \-, *, /, % (mod),
the relational operators >, >=, <, <=, =, and != (not equal),
Although the arithmetic we have done so far
has been straightforward,
more complicated things are somewhat tricky.
number registers hold only integers.
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).
Number registers can occur anywhere in an expression,
and so can scale indicators like
and 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,
when you wouldn't expect it _
in particular, when arithmetic is being done
in a context that implies horizontal or vertical dimensions.
would seem obvious enough _
Remember that the default units for horizontal parameters like
That's really `7 ems / 2 inches',
and when translated into machine units, it becomes zero.
the `2' is `2 ems', so `7i/2' is small,
So again, a safe rule is to
attach a scale indicator to every number,
For arithmetic done within a
there is no implication of horizontal or vertical dimension,
so the default units are `units',