2

I'm trying to write a man page for a software, and would like to include some code snippets. I'm currently using the .RS and .RE macros as part of a custom-made .SAMPLE macro, but for some reason that doesn't work. Here is the man page:

.TH MYMANPAGE 1 

.de SAMPLE
.br
.RS
.nf
.nh
..
.de ESAMPLE
.hy
.fi
.RE
..

.SH TEST SECTION HEADING
This is a test section heading.
.TP
.B Test Paragraph Label
This is some test paragraph text. This is some test paragraph text. This
is some test paragraph text. This is some indented test code:
.SAMPLE
int main(void) {
   return 42;
}
.ESAMPLE
This is more text after the test code. This is more text after the test
code.

What ends up happening is that the text after .ESAMPLE is not indented as much as the paragraph text. Instead, it's lined up with the paragraph label. What would be the proper .[E]SAMPLE macro definitions to get them to play nice with .TP?

1 Answer 1

4

The .RE restores the default indentation level, not the current .TP indentation level. All you need to do is save and restore the actual indent in play when .RS is called. The fix below assumes you will not nest SAMPLEs inside SAMPLEs:

.de SAMPLE
.br
.nr saveIN \\n(.i   \" double the backslash when defining a macro
.RS
.nf
.nh
..
.de ESAMPLE
.hy
.fi
.RE
.in \\n[saveIN]u    \" 'u' means 'units': do not scale this number 
..

$ man ./i
[...]
Test Paragraph Label
  This  is  some  test paragraph text. This is some test paragraph
  text. This is some test paragraph text. This  is  some  indented
  test code:
  int main(void) {
     return 42;
  }
  This  is  more text after the test code. This is more text after
  the test code.
3
  • Thanks for the answer. I have two questions: 1. Why do you need \\n and not \n? In groff manuals I see e.g. \n(xx used instead. 2. What's the 'u' for in '\\n[saveIN]u'? I couldn't find any documentation on 'retrieving' register values that explained the 'u'. Thanks
    – Meta
    Commented Oct 17, 2013 at 15:28
  • Ahh OK, got my answer to 1. from here: lists.gnu.org/archive/html/groff/2010-05/msg00043.html Still looking for number 2.
    – Meta
    Commented Oct 17, 2013 at 15:32
  • 2
    Using .in 5 means "indent 5 ems" because .in defaults to scaling all numbers as ems. (See "man 7 groff".) The \n(.i register returns the indent in "units", not "ems", and that's the value we saved. So when we need to restore its value as an argument to .in we must append u to the number or else the value will be scaled up by .in to ems (and be huge). Commented Oct 18, 2013 at 20:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .