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?

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.
  • 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 Oct 17 '13 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 Oct 17 '13 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). – Ian D. Allen Oct 18 '13 at 20:54

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.