HMC Homepage      CS Home

Printing out formatted text with troff/nroff

Troff and Nroff are text formatting programs, much like LaTeX, that use a markup language, similar to HTML or LaTeX. Troff is designed for typesetting output, while nroff is designed to output to line printers. Basically, nroff will output ASCII text, while troff will output something that can be converted to postscript. There are three different base macro packages that can be used, this quickref will focus on the use of the use of ms macros. To use another macro package, simply replace "-ms" with "-mm" or "-me".There is also a -man macro package for Nroff, that can format documents like UNIX man pages.

Macro packages provide simple commands to do basic formatting tasks in nroff and troff. To use a built in package, either ms, mm, or me, simply use the package name as a command line argument when invoking troff. ie:
%troff -ms foo
This will tell troff to use the ms macro package to compile foo. In the input file, all troff commands and macros are invoked by having a period "." at the beginning of a line followed by the tag and arguments.

  • Some ms macro commands

    • To make a new paragraph with an indent:
      .PP

    • To make an unindented paragraph, use:
      .LP
      It simply offsets the current text from the previous paragraph using vertical space, but it does not indent the first line.

    • To make a font italic in troff and to underline it in nroff use
      .I

    • To make a font bold in both troff and nroff, use
      .B

    • To return to a normal font in both, use
      .R

    • The ms macro package can make a cover page, table of contents, and do all sorts of formatting, see links below or the chapter six of "UNIX in a Nutshell" which is found in the terminal room for more info. Also, look at the ms man page by typing from a shell:
      %man ms

  • Some mm macro commands

    • To start a new paragraph with indenting, use:
      .P 1

    • To start a new paragraph without indenting use:
      .P 0
      (Note that just ".P" will also work).

    • The macros for bold and italics work the same as for ms macros, with ".B" making the text bold, ".I" making the text italic or underlined, and ".R" returning the text to normal.

    • To find more information, check the man page for mm, or "UNIX in a Nutshell" found in the terminal room.

  • Some me macro commands

    • To start a new indented paragraph, use:
      .pp
      (note that it is in lowercase, like most me commands)

    • To start a new paragraph without indenting, use:
      .lp

    • To start a new numbered paragraph, use:
      .np

    • To make a font bold in troff, and underline it in nroff, use:
      .b

    • In troff, to italicize and in nroff to underline, use:
      .i

    • To revert text back to normal:
      .r

  • How to run Troff (Using lot's of pipes)

    1. Create a file, using your favorite text editor (emacs, vi, etc.).

    2. To create a troffed file from filename, use the command:
      %troff -ms filename > file

    3. That will create a file which you can use the dpost command to turn into a postscript file,file.ps using the command:
      %/usr/lib/lp/postscript/dpost inputfilename > file.ps

    4. This postscript file can be printed using:
      %lpr filename
      or viewed with ghostscript using:
      %gs filename

    5. Troff and print a file called filename in one step simply use this series of pipes:
      %troff -ms filename | /usr/lib/lp/postscipt/dpost | lpr

    6. You can add /usr/lib/lp/postscript/ to your search path in the initiation file for your shell, ".cshrc" if you use csh. Then all you would need is to type dpost, rather than the whole path.

  • How to run Nroff (Using a bit less pipes)

    1. To Nroff a file named inputfile and output to a file named outputfile simply use this command:
      %nroff -ms inputfile > outputfile

    2. This file can be printed using
      %lpr outputfile
      or it can be viewed using:
      %less outputfile

    3. To do this in one step, use this string:
      %nroff -ms inputfile | lpr

  • Further Information about Troff and Nroff

    • Unix Unleashed Troff/Nroff Pages (Chapters 8, 9, 10, 11) provide a very good source of information regarding troff and nroff, as well as macro packages.

    • Text Formatting Using ms Macros from the Berkeley computing Services provides very good information on how to use ms macros.

    • Also check the Unix in a Nutshell books found in the terminal room, They have a few chapters on Troff/Nroff and the various macro packages for them.

    • For info the -man macros for Nroff, check man(5), accessed by the command:
      %man -s 5 man

    Copyright (c) HMC Computer Science Department. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with the no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled ``GNU Free Documentation License.''

    HMC Computer Science Department
    Contact Information
Last Modified Monday, 27-Jan-2003 16:34:07 PST