📄 331-335.html
字号:
<HTML>
<HEAD>
<TITLE>Linux Unleashed, Third Edition:geqn and gtbl</TITLE>
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!--ISBN=0672313723//-->
<!--TITLE=Linux Unleashed, Third Edition//-->
<!--AUTHOR=Tim Parker//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Sams//-->
<!--CHAPTER=18//-->
<!--PAGES=331-335//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="../ch17/326-330.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="335-338.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H2><A NAME="Heading1"></A><FONT COLOR="#000077">Chapter 18<BR>geqn and gtbl
</FONT></H2>
<P><I>by Tim Parker</I></P>
<DL>
<DT><B>In This Chapter</B>
<DT>• geqn
<DT>• gtbl
</DL>
<P>Now that you are comfortable with <TT>groff</TT>, you can look at two useful add-ons for <TT>groff</TT>: <TT>geqn</TT> and <TT>gtbl</TT>. In this chapter, you learn the following:</P>
<DL>
<DD><B>•</B> What are <TT>geqn</TT> and <TT>gtbl</TT>?
<DD><B>•</B> How to create complex equations easily.
<DD><B>•</B> How to format tables for <TT>groff</TT> documents.
</DL>
<P>In the last chapter, you saw how <TT>groff</TT> is used to produce formatted documents for both screen and printer. Unfortunately, <TT>groff</TT> 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.</P>
<P>The utilities <TT>gtbl</TT> and <TT>geqn</TT> are preprocessors which means that you write the source code as usual, but then the <TT>gtbl</TT> and <TT>geqn</TT> programs scan through and replace their specific commands with <TT>groff</TT> commands. Except for these specific commands, no other changes to the text or <TT>groff</TT> commands are performed.</P>
<H3><A NAME="Heading2"></A><FONT COLOR="#000077">geqn</FONT></H3>
<P>The <TT>geqn</TT> preprocessor is designed for formatting complex equations and printing special symbols. You need only use <TT>geqn</TT> if you are using <TT>groff</TT> to create a document with these kinds of characters embedded within them.</P>
<P>Although <TT>groff</TT> 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, <TT>geqn</TT> is quite easy to work with. Most aspects of <TT>geqn</TT> are designed to look like equivalent English commands or words.</P>
<P>You can quickly move through a set of the important parts of <TT>geqn</TT>. As you will see, it is remarkably easy to work with.</P>
<H4 ALIGN="LEFT"><A NAME="Heading3"></A><FONT COLOR="#000077">Executing geqn</FONT></H4>
<P>The <TT>geqn</TT> preprocessor is invoked before the <TT>groff</TT> formatter. Usually, this is accomplished with a simple pipe command:</P>
<!-- CODE SNIP //-->
<PRE>
geqn <I>filename</I> | groff
</PRE>
<!-- END CODE SNIP //-->
<P>This processes <I>filename</I> through <TT>geqn</TT>, which converts <TT>geqn</TT> commands to equivalent <TT>groff</TT> commands and then sends the result to <TT>groff</TT> for processing.</P>
<P>The command</P>
<!-- CODE SNIP //-->
<PRE>
geqn <I>file1 file2 file3</I> | groff
</PRE>
<!-- END CODE SNIP //-->
<P>processes three files and sends them all to <TT>groff</TT>.</P>
<P>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.</P>
<H4 ALIGN="LEFT"><A NAME="Heading4"></A><FONT COLOR="#000077">Equations</FONT></H4>
<P>You must tell <TT>geqn</TT> where equations begin and end by using the commands <TT>.EQ</TT> (equation start) and <TT>.EN</TT> (equation end). Within the two commands, anything that is typed is treated as an equation. For example, the command</P>
<!-- CODE SNIP //-->
<PRE>
.EQ
b=c*(d+x)
.EN
</PRE>
<!-- END CODE SNIP //-->
<P>is formatted to the equation
</P>
<!-- CODE SNIP //-->
<PRE>
b=c*(d+x)
</PRE>
<!-- END CODE SNIP //-->
<P>If you try that line without the equation indicators, feeding it straight to <TT>groff</TT>, you don’t receive the same output because <TT>groff</TT> can’t interpret the characters properly.</P>
<P>You can number equations, as is often required in technical documents, by placing a number after the <TT>.EQ</TT> command. For example, the command</P>
<!-- CODE SNIP //-->
<PRE>
.EQ 15
b=c*(d+x)
.EN
</PRE>
<!-- END CODE SNIP //-->
<P>places the number <TT>15</TT> in the left margin next to the equation.</P>
<H4 ALIGN="LEFT"><A NAME="Heading5"></A><FONT COLOR="#000077">Subscripts and Superscripts</FONT></H4>
<P>To place superscripts and subscripts in an equation, use the commands <TT>sup</TT> and <TT>sub</TT>. The words <TT>sup</TT> and <TT>sub</TT> must be surrounded by spaces. For example, the command</P>
<!-- CODE SNIP //-->
<PRE>
E=mc sup 2
</PRE>
<!-- END CODE SNIP //-->
<P>produces Einstein’s most famous equation.
</P>
<P>To indicate the end of a subscript or superscript and continue with normal characters, use a space or a tilde (<TT>~</TT>) character. For example, the command</P>
<!-- CODE SNIP //-->
<PRE>
x=(z sup 2)+1
</PRE>
<!-- END CODE SNIP //-->
<P>gives you the finished output
</P>
<!-- CODE SNIP //-->
<PRE>
x=(z<SUP>2)+1</SUP>
</PRE>
<!-- END CODE SNIP //-->
<P>which is probably not what you want. Instead, use one of the following commands:
</P>
<!-- CODE SNIP //-->
<PRE>
x=(z sup 2 )+1
x=(z sup 2~)+1
</PRE>
<!-- END CODE SNIP //-->
<P>In these commands, the space or the tilde indicates the end of the superscript. This gives you the following output:
</P>
<!-- CODE SNIP //-->
<PRE>
x=(z<SUP>2</SUP>)+1
</PRE>
<!-- END CODE SNIP //-->
<P>You can subscript subscripts and superscript superscripts by simply combining the formats:
</P>
<!-- CODE SNIP //-->
<PRE>
y sub x sub 3
</PRE>
<!-- END CODE SNIP //-->
<P>You can also produce both subscript and superscript on the same character using the two commands together:
</P>
<!-- CODE SNIP //-->
<PRE>
x sub y sup 3
</PRE>
<!-- END CODE SNIP //-->
<P>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:
</P>
<!-- CODE SNIP //-->
<PRE>
w sup {x alpha y}
</PRE>
<!-- END CODE SNIP //-->
<P>This shows that the Greek letters are also available, as they are within <TT>groff</TT>. You can have braces within braces, as well:</P>
<!-- CODE SNIP //-->
<PRE>
omega sub { 2 pi r sup { 2 + rho }}
</PRE>
<!-- END CODE SNIP //-->
<P>Try these commands for yourself and experiment to see the output.
</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>Note: </B><BR>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.<HR></FONT>
</BLOCKQUOTE>
<H4 ALIGN="LEFT"><A NAME="Heading6"></A><FONT COLOR="#000077">Fractions</FONT></H4>
<P>To create a proper-looking fraction, use the keyword <TT>over</TT>. The <TT>geqn</TT> preprocessor automatically adjusts the length of the line separating the parts. For example, the command</P>
<!-- CODE SNIP //-->
<PRE>
a = 2b over {3c alpha}
</PRE>
<!-- END CODE SNIP //-->
<P>produces an equation with a horizontal line separating the two components, just as if you were writing the equation out on paper.
</P>
<P>You can, of course, combine all the other elements of <TT>geqn</TT> to create more complex-looking equations:</P>
<!-- CODE SNIP //-->
<PRE>
{alpha + beta * gamma sup 3} over {3 sub {4 + alpha}}
</PRE>
<!-- END CODE SNIP //-->
<P>When you are combining <TT>sup</TT> and <TT>sub</TT> with <TT>over</TT>, <TT>geqn</TT> processes <TT>sup</TT> and <TT>sub</TT> first, and then it does <TT>over</TT>, much as if it were actually writing the equation.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="../ch17/326-330.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="335-338.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -