0

Context:

Let's say I am trying to write a macro in groff in order to draw a rectangle given some arguments (like width and height for example). For this I need to perform some simple arithmetic, that I would like to store in registers for readability.

How to reproduce

In any normal groff document I can just use:

.nr a 1  \"creates a number register with the name a and the value 1
\n[a]      \"prints the content of register a

which prints 1 on the document However when I place it in a macro MACRO and call it like so:

.de MACRO  \"defines a new macro with name MACRO
.nr a 1    \"creates a number register with the name a and the value 1
\n[a]        \"prints the content of register a
..         \"end of MACRO definition
.MACRO     \"calls MACRO

It prints 0, which is the default value for new registers, according to the documentation (man groff, man 7 groff, and info groff).

How it is compiled

I compile it using this script:

groff -kep "$file" -Tpdf > "$file.pdf"

What am I doing wrong? Is it even possible to use registers inside of a macro? How should I do it?

0
1

Yes, it is possible to use registers inside a macro, but you have to tell troff to wait evaluating the value to be evaluated at the time the macro is called, instead of defined.

You therefore have to escape the backslash in front of the line asking for the register value in the macro definition, ending up with

.de MACRO  \"defines a new macro with name MACRO
.nr a 1    \"creates a number register with the name a and the value 1
\\n[a]       \"prints the content of register a
..         \"end of MACRO definition
.MACRO     \"calls MACRO

This ways, the value given within the macro is printed, so 1 in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.