gtroff Command in Linux



While gtroff is a powerful tool for creating formatted documents, it requires a certain level of familiarity with its syntax and commands. Modern text editors and word processors often provide more user-friendly interfaces for creating documents. However, gtroff remains a valuable tool for tasks that require precise control over the formatting and layout of text.

Here's a comprehensive guide to using the gtroff command with examples.

Table of Contents

Here is a comprehensive guide to the options available with the gtroff command in linux −

Understanding gtroff Command in Linux

The gtroff command in Linux is a powerful typesetting system that is used to format text and produce readable documents. It stands for GNU troff, where 'troff' means 'typesetter roff', which is a document processing system.

The groff system is versatile, allowing for the creation of a variety of document types, from simple letters to complex books.

How to use gtroff Command in Linux?

gtroff is a text formatted in the Unix family of operating systems, specifically designed to produce high-quality typesetting output. It's a powerful tool that can be used to create everything from simple documents to complex manuals and books.

Syntax

The basic syntax for using gtroff is −

gtroff [options] input_file

Where −

  • options − Various options can be used to customize the output and control the formatting of the output.
  • input_file − The plain text file to be formatted. The input file containing the text to be formatted.

gtroff Command Options

Here are some of the most commonly used flags and options in gtroff, along with their descriptions −

Options Descriptions
-me Sets the output format to TeX.
-md Sets the output format to dvi.
-p fontname Specifies the point size for the font.
-f fontname Specifies the font to be used.
-ms Specifies the macro package to use (e.g., -ms for the standard macro package)
-v Prints the version information
-Tdevice Sets the output device (e.g., -Tpostscript for PostScript output)
-o output_file Specifies the output file name
-p Prevents automatic page ejection
-ms Sets the output format to PostScript.
-mb Sets the output format to troff.
-ms Sets the output format to PostScript.
-l lines Sets the number of lines per page.
-b Turns on boldface.
-e Turns on subscript.
-f Sets the footer text.
-h header Sets the header text.
-o offset Sets the offset from the left margin.
-w width Sets the page width.
-i Turns on italics.
-u Turns on underline.
-s Turns on superscript.
-T device Specifies the output device.
-o file Specifies the output file.
-v Prints the version information.
-h Prints the help message.

Examples of gtroff Command in Linux

Lets discuss a few examples of gtroff commands in Linux systems. This will help you in learning how to get started with the command.

  • Basic Usage
  • Simple Formatting
  • Using Macros
  • Format a Plain Text File into a PostScript
  • Formatting a Document
  • Advanced Formatting
  • Using Preprocessors
  • Output Formats

Basic Usage

To create a PostScript file from an input file named document.roff, using the Times Roman font at 12 points, with 60 lines per page and a page width of 72 characters, you would use the following command −

gtroff -ms -f T -p 12 -l 60 -w 72 document.roff

Simple Formatting

Create a file named document.txt with the following content −

This is a sample document.

To format the document using the standard macro package and output it as a PostScript file, run −

gtroff -ms -Tpostscript document.txt -o output.ps

Using Macros

Create a file named macros.tmac with the following macro definition −

.de Center
.sp 1
.center

This macro defines a Center command that adds a space and centers the following text.

In your document, you can use this macro like this −

.Center This is centered text.

To format the document using the custom macro file, run −

gtroff -ms -Tpostscript -m macros.tmac document.txt -o output.ps

Format a Plain Text File into a PostScript

At its simplest, groff can be used to format a plain text file into a PostScript or PDF document. For example −

groff -Tps -man filename.man > filename.ps

This command will take a 'man' formatted file and convert it to a PostScript file. The -Tps flag specifies the output format, in this case, PostScript.

Formatting a Document

groff uses a markup language to define the structure and formatting of a document. Here's a basic example of a .roff file −

.TH "MY DOCUMENT" "1" "September 2024" "Version 1.0" "Manual"
.SH Introduction
This is an introduction.
.PP
This is a new paragraph.
.SH "Another Section"

This section contains more information.

To format this document, you would use −

groff -Tps -man filename.roff > filename.ps

Advanced Formatting

groff also supports more advanced formatting options, such as defining fonts, styles, and page layouts. For example, to specify a font size, you could use −

.fp 1 R
.fp 2 I
.fp 3 B

This sets up three font positions: Roman, Italic, and Bold.

Using Preprocessors

groff can be extended with preprocessors for handling tables, equations, and pictures. For example, the tbl preprocessor is used for tables, eqn for equations, and pic for pictures. To use these preprocessors, you would pipe the output through them before sending it to groff. Here's an example using tbl −

tbl filename.tbl | groff -Tps -man > filename.ps

Output Formats

groff can produce output in various formats, such as HTML, PDF, and plain text. The -T option is used to specify the output device. For example, to create a PDF, you would use −

groff -Tpdf -man filename.man > filename.pdf

Notegtroff is a complex tool with many more options available. The above list is just a small sample of the most commonly used ones. For a complete list of options and their descriptions, refer to the gtroff manual page.

Conclusion

The gtroff command is a testament to the flexibility and power of Linux systems for document processing. With its extensive capabilities and integration with other tools, groff remains an essential utility for anyone looking to produce professional-quality documents on Linux platforms.

Advertisements