Next: , Previous: Measurements, Up: gtroff Reference


5.3 Expressions

gtroff has most arithmetic operators common to other languages:

Parentheses may be used as in any other language. However, in gtroff they are necessary to ensure order of evaluation. gtroff has no operator precedence; expressions are evaluated left to right. This means that gtroff evaluates ‘3+5*4’ as if it were parenthesized like ‘(3+5)*4’, not as ‘3+(5*4)’, as might be expected.

For many requests which cause a motion on the page, the unary operators ‘+’ and ‘-’ work differently if leading an expression. They then indicate a motion relative to the current position (down or up, respectively).

Similarly, a leading ‘|’ operator indicates an absolute position. For vertical movements, it specifies the distance from the top of the page; for horizontal movements, it gives the distance from the beginning of the input line.

+’ and ‘-’ are also treated differently by the following requests and escapes: bp, in, ll, lt, nm, nr, pl, pn, po, ps, pvs, rt, ti, \H, \R, and \s. Here, leading plus and minus signs indicate increments and decrements.

See Setting Registers, for some examples.

— Escape: \B'anything'

Return 1 if anything is a valid numeric expression; or 0 if anything is empty or not a valid numeric expression.

Due to the way arguments are parsed, spaces are not allowed in expressions, unless the entire expression is surrounded by parentheses.

See Request and Macro Arguments, and Conditionals and Loops.


Footnotes

[1] Note that, for example, ‘!(-1)’ evaluates to `true' because gtroff treats both negative numbers and zero as `false'.