-->
Previous Table of Contents Next


Chapter 18
geqn and gtbl

by Tim Parker

In This Chapter
•   geqn
•   gtbl

Now that you are comfortable with groff, you can look at two useful add-ons for groff: geqn and gtbl. In this chapter, you learn the following:

  What are geqn and gtbl?
  How to create complex equations easily.
  How to format tables for groff documents.

In the last chapter, you saw how groff is used to produce formatted documents for both screen and printer. Unfortunately, groff is not the easiest package to work with for complex problems such as tables and equations, so a set of macros for these tasks was de-veloped.

The utilities gtbl and geqn are preprocessors which means that you write the source code as usual, but then the gtbl and geqn programs scan through and replace their specific commands with groff commands. Except for these specific commands, no other changes to the text or groff commands are performed.

geqn

The geqn preprocessor is designed for formatting complex equations and printing special symbols. You need only use geqn if you are using groff to create a document with these kinds of characters embedded within them.

Although groff has enough power to provide simple equations, it is not particularly friendly or powerful enough for more than single-line material. On the other hand, geqn is quite easy to work with. Most aspects of geqn are designed to look like equivalent English commands or words.

You can quickly move through a set of the important parts of geqn. As you will see, it is remarkably easy to work with.

Executing geqn

The geqn preprocessor is invoked before the groff formatter. Usually, this is accomplished with a simple pipe command:


geqn filename | groff

This processes filename through geqn, which converts geqn commands to equivalent groff commands and then sends the result to groff for processing.

The command


geqn file1 file2 file3 | groff

processes three files and sends them all to groff.

Remember that many consoles can’t display equations properly because they are not bitmapped and don’t have the character set available. You may have to output the results to a printer to see any exercises you try.

Equations

You must tell geqn where equations begin and end by using the commands .EQ (equation start) and .EN (equation end). Within the two commands, anything that is typed is treated as an equation. For example, the command


.EQ

b=c*(d+x)

.EN

is formatted to the equation


b=c*(d+x)

If you try that line without the equation indicators, feeding it straight to groff, you don’t receive the same output because groff can’t interpret the characters properly.

You can number equations, as is often required in technical documents, by placing a number after the .EQ command. For example, the command


.EQ 15

b=c*(d+x)

.EN

places the number 15 in the left margin next to the equation.

Subscripts and Superscripts

To place superscripts and subscripts in an equation, use the commands sup and sub. The words sup and sub must be surrounded by spaces. For example, the command


E=mc sup 2

produces Einstein’s most famous equation.

To indicate the end of a subscript or superscript and continue with normal characters, use a space or a tilde (~) character. For example, the command


x=(z sup 2)+1

gives you the finished output


x=(z2)+1

which is probably not what you want. Instead, use one of the following commands:


x=(z sup 2 )+1

x=(z sup 2~)+1

In these commands, the space or the tilde indicates the end of the superscript. This gives you the following output:


x=(z2)+1

You can subscript subscripts and superscript superscripts by simply combining the formats:


y sub x sub 3

You can also produce both subscript and superscript on the same character using the two commands together:


x sub y sup 3

Because a space is used to indicate the end of a subscript or superscript, this can cause a problem when you want spaces either as part of the equation or to separate words to be converted. To get around this problem, use braces to enclose the subscript or superscript:


w sup {x alpha y}

This shows that the Greek letters are also available, as they are within groff. You can have braces within braces, as well:


omega sub { 2 pi r sup { 2 + rho }}

Try these commands for yourself and experiment to see the output.


Note:  
While it might seem at first that you’d want Greek letters only for complex equations, the letters are handy for many simple mathematical formulas, too. It’s well worth knowing how to generate each of the common letters and what they look like on paper.

Fractions

To create a proper-looking fraction, use the keyword over. The geqn preprocessor automatically adjusts the length of the line separating the parts. For example, the command


a = 2b over {3c alpha}

produces an equation with a horizontal line separating the two components, just as if you were writing the equation out on paper.

You can, of course, combine all the other elements of geqn to create more complex-looking equations:


{alpha + beta * gamma sup 3} over {3 sub {4 + alpha}}

When you are combining sup and sub with over, geqn processes sup and sub first, and then it does over, much as if it were actually writing the equation.


Previous Table of Contents Next