Search code examples
groff

Groff "Page n of m" with mom


We're using groff with the mom macros for generating reports, and we'd like to change the page numbering from the "- n -" format to "Page n of m". Looking through the mom documentation, I see several ways of formatting the page number, but nothing about including the total number of pages. Is it possible to override the mom page numbering and include the total page count?


Solution

  • I think you would need to do this as a multi-pass operation: first count the number of pages, then add the page count into the source.

    You could get the page count from the \n[%] register after the whole document has been formatted (presuming you never adjusted the page count manually), so something like this (in a shell script) will get you the page count:

    PAGE_COUNT=$(echo ".tm \n[%]" | groff -i -z -mom my_doc.mom 2>&1)
    

    Then on the next pass, set that count in a register, i.e.,

    groff -rpages=$PAGE_COUNT -mom my_doc.mom
    

    and use mom's macros for setting the title. I'm not familiar with mom specifically, but it should be similar to the generic

    .tl ''Page % of \n[pages]''