Few now remember that originally man pages were typeset for printing and not only terminal viewing. It's still possible by outputting postscript (man -Tps
) or better dvi intermediate:
man -Tdvi ... > /tmp/man.dvi && dvipdfmx /tmp/man.dvi -o /tmp/man.pdf && xdg-open /tmp/man.pdf
and it's a somewhat good test for "semantic correctness" of formatting that otherwise looks same on terminal.
evil otto's answer is a good start but keeps the regular variable-width font:

You probably want a monospaced Courier font:

.RS
.ft CR
.nf
.eo
foreach my $n (1..10) {
print "$n\n";
}
.ec
.fi
.ft R
.RE
which can be shortened with .EX
...EE
macro for typesetting examples (plus, it saves and restores previous font family which is cleaner than hardcoding .ft R
):
.RS
.EX
.eo
foreach my $n (1..10) {
print "$n\n";
}
.ec
.EE
.RE
.EX
is an "extension", no idea what that means for its portability
groff documentation claims .EX
takes an optional indent parameter (which could replace the .RS
....RE
) but it didn't work for me (and I don't see any such logic in the implementation).