Is it possible to perform arithmetic using groff?
Say I want to calculate what page is the current one, and I currently am on page 2.
I can get the next page's number by using:
.PP
Next page is number \n[.pn]
Which will compile as Next page is number 3
.
How can I perform arithmetic to render instead:
.PP
Current page is ((\n[.pn]-1))
into Current page is 2
?
Best Answer
The current page number is
\n%
.To interpolate the result of an arithmetic expression, I think you'll need to put it in a number register first.
Here's a macro (not exhaustively tested) that takes an expression as its argument, assigns the result to a (presumably unused) register named
__
, then interpolates the contents of the register.After running through nroff:
This seems like such a common thing to do that there may be a macro like it already in the standard macro packages, but I don't know.