Search code examples
groff

How is it possible to assign a macro parameter to a number register in groff?


Given 2 number registers, embedding this in a document works fine:

.nr weeks 12
.tm start is \n[weeks] weeks away.

The expected value is printed to the console.

Moving this code to a function does not appear to work:

.de set_weeks
.  nr weeks \\$1
.  tm start is \n[weeks] weeks away.
..

And

.br
.set_weeks 6
.br

Prints 'start is 0 weeks away.', which is unexpected.

The groff / troff manual does not say you can't do this, but in the past I have only ever passed parameters that needed to get interpreted as strings.


Solution

  • That is, because the \n[weeks] is evaluated at definition time of the macro. To have it evaluated at execution time, add another \.

    As demo:

    .nr weeks 3
    .de set_weeks
    .  nr weeks \\$1
    .  tm start is \n[weeks] weeks away.
    ..
    .de set_weeks2
    .  nr weeks \\$1
    .  tm start is \\n[weeks] weeks away.
    ..
    .br
    .set_weeks 6
    .set_weeks2 6
    .br
    

    gives as output:

    start is 3 weeks away.
    start is 6 weeks away.