📄 unx23.htm
字号:
<HTML>
<HEAD>
<TITLE>UNIX Unleashed unx23.htm</TITLE>
<LINK REL="ToC" HREF="index.htm">
<LINK REL="Next" HREF="unx24.htm">
<LINK REL="Previous" HREF="unx22.htm"></HEAD>
<BODY TEXT="#000000" LINK="#0000FF" VLINK="#800080" bgcolor=white>
<P><A HREF="unx22.htm"><IMG SRC="bluprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A>
<A HREF="index.htm"><IMG SRC="blutoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A>
<A HREF="unx24.htm"><IMG SRC="blunext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A>
<A HREF="index.htm"><IMG SRC="bluprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Home"></A>
</P><UL>
<LI>
<A HREF="#I1">23 — Formatting Tables with tbl</A></LI>
<UL>
<UL>
<UL>
<UL>
<LI>
<A HREF="#I3">By Susan Peppard</A></LI></UL></UL>
<LI>
<A HREF="#I4">tbl Requirements</A></LI>
<UL>
<LI>
<A HREF="#I5">tbl Macros</A></LI>
<LI>
<A HREF="#I6">Minimal Format Options</A></LI>
<LI>
<A HREF="#I7">Table Data</A></LI></UL>
<LI>
<A HREF="#I8">tbl Options</A></LI>
<UL>
<LI>
<A HREF="#I9">Format Options</A></LI>
<LI>
<A HREF="#I10">Global Options</A></LI></UL>
<LI>
<A HREF="#I11">Advanced tbl Formatting</A></LI>
<UL>
<LI>
<A HREF="#I12">Column Headings</A></LI>
<LI>
<A HREF="#I13">Horizontal and Vertical Spanning</A></LI>
<LI>
<A HREF="#I14">Text Blocks</A></LI>
<LI>
<A HREF="#I15">Spacing and Lines</A></LI>
<LI>
<A HREF="#I16">Changing Format Options</A></LI>
<LI>
<A HREF="#I17">A Fancy Table</A></LI></UL>
<LI>
<A HREF="#I18">Troubleshooting</A></LI>
<LI>
<A HREF="#I19">Summary</A></LI></UL></UL></UL>
<H1 ALIGN="CENTER">
<CENTER><A ID="I1" NAME="I1">
<BR>
<FONT SIZE=5><A ID="I2" NAME="I2"></A><B>23 — Formatting Tables with </B><B><I>tbl</I></B>
<BR></FONT></A></CENTER></H1>
<H5 ALIGN="CENTER">
<CENTER><A ID="I3" NAME="I3">
<FONT SIZE=3><B>By Susan Peppard</B>
<BR></FONT></A></CENTER></H5>
<P>Meet tbl, the troff and nroff table preprocessor.
<BR></P>
<P>tbl is not a popular troff tool. It is cursed and vilified and mocked, but it is used. The people who hate tbl rarely take the time to learn even the basics of formatting a table. Instead, they have two or three reliable samples that they just keep
copying over and over, changing the data each time. When something goes wrong, they ask a friend or a colleague—maybe even an enemy—for help.
<BR></P>
<P>You don't have to do that. tbl isn't that hard to learn. As with all UNIX utilities and programs, you can learn as much or as little as you like. You can learn the rudiments of tbl in an hour. This chapter includes some sample tables to enlarge your
collection.
<BR></P>
<H3 ALIGN="CENTER">
<CENTER><A ID="I4" NAME="I4">
<FONT SIZE=4><B><I>tbl</I></B><B> Requirements</B>
<BR></FONT></A></CENTER></H3>
<P>tbl is a troff preprocessor. This means that the tbl code you write is processed by tbl before troff gets anywhere near it. Typically, you send a file through tbl and pipe the output to troff. The syntax is
<BR></P>
<PRE>tbl filename | troff options</PRE>
<H4 ALIGN="CENTER">
<CENTER><A ID="I5" NAME="I5">
<FONT SIZE=3><B><I>tbl</I></B><B> Macros</B>
<BR></FONT></A></CENTER></H4>
<P>troff recognizes tbl output by the macros that begin and end a table: .TS and .TE. (The .TS H and .TH macros are discussed in the "Column Headings" section later in this chapter.)
<BR></P>
<P>The code that produces a simple table is
<BR></P>
<PRE>.TS
box;
cB cB cB
l l l.
Days[TAB]Months[TAB]Years
Monday[TAB]January[TAB]1990
Tuesday[TAB]February[TAB]1991
Wednesday[TAB]March[TAB]1992
.TE</PRE>
<H4 ALIGN="CENTER">
<CENTER><A ID="I6" NAME="I6">
<FONT SIZE=3><B>Minimal Format Options</B>
<BR></FONT></A></CENTER></H4>
<P>In addition to the macros, a table must include at least one line of format options. These options tell tbl whether you want your columns left-justified, centered, or right-justified—in short, how you want each column to look. The rule is one
option per column. Therefore, a three-column table with all columns left-justified has the following format options:
<BR></P>
<PRE>l l l.</PRE>
<P>The last line of format options must end with a period.
<BR></P>
<P>These are the minimum options you need for a three-column table. With these options, every row in your table has left-justified columns. If you want centered column headings with left-justified data, then you need two rows of format options:
<BR></P>
<PRE>c c c
l l l.</PRE>
<P>tbl uses the first format line to format the first row of your table. It uses the second format line to format the second row of your table. When it runs out of format lines, as it does in row 3 in this example, tbl formats the rest of your table
according to the last format line.
<BR></P>
<P>You can't format a complex table with the bare minimum; you need to use more of the options that tbl provides.
<BR></P>
<H4 ALIGN="CENTER">
<CENTER><A ID="I7" NAME="I7">
<FONT SIZE=3><B>Table Data</B>
<BR></FONT></A></CENTER></H4>
<P>Table data is entered with a tab between each column. If a line is too long for your terminal and, therefore, wraps, you can use the continuation character (\). Each new line starts a new row in the table.
<BR></P>
<P>To leave a cell empty, use a tab. For example, to enter a row that contains no entries in the first, second, and fourth columns but contains an entry in the third column, do this:
<BR></P>
<PRE>[TAB][TAB]entry</PRE>
<P>You don't need to enter the last tab—the one for the fourth column. tbl doesn't mind if you specify too little data. It complains only if you specify too much.
<BR></P>
<H3 ALIGN="CENTER">
<CENTER><A ID="I8" NAME="I8">
<FONT SIZE=4><B><I>tbl</I></B><B> Options</B>
<BR></FONT></A></CENTER></H3>
<P>In addition to format options, tbl enables you to use global options. Global options affect the appearance of your table as a whole. tbl provides a wide range of format and global options.
<BR></P>
<H4 ALIGN="CENTER">
<CENTER><A ID="I9" NAME="I9">
<FONT SIZE=3><B>Format Options</B>
<BR></FONT></A></CENTER></H4>
<P>Format options include more than just instructions on how to justify columns. The tbl format options include
<BR></P>
<TABLE BORDER>
<TR>
<TD>
<P>l (or L)</P>
<TD>
<P>Left-justifies the column.</P>
<TR>
<TD>
<P>r (or R)</P>
<TD>
<P>Right-justifies the column.</P>
<TR>
<TD>
<P>c (or C)</P>
<TD>
<P>Centers the column.</P>
<TR>
<TD>
<P>n (or N)</P>
<TD>
<P>Specifies a numeric column. It lines up the column on decimal points—or units digits if there are no decimal points.</P>
<TR>
<TD>
<P>a (or A)</P>
<TD>
<P>Specifies an alphabetic column. It indents text one em.</P>
<TR>
<TD>
<P>s (or S)</P>
<TD>
<P>Spans the column horizontally.</P>
<TR>
<TD>
<P>^</P>
<TD>
<P>Spans the column vertically.</P></TABLE>
<P>Spanning is explained in the "Horizontal and Vertical Spanning" section later in this chapter.
<BR></P>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="imp.gif" WIDTH = 68 HEIGHT = 35><B>TIP: </B>The a option doesn't work properly in older versions of tbl. Use \0\0 instead.
<BR></NOTE>
<HR ALIGN=CENTER>
<P>Two more format options enable you to adjust the width of your columns. You can use them in conjunction with any of the options listed above.
<BR></P>
<TABLE BORDER>
<TR>
<TD>
<P>e</P>
<TD>
<P>Equalizes the width of the indicated columns.</P>
<TR>
<TD>
<P>w</P>
<TD>
<P>Enables you to specify a width for a column.</P></TABLE>
<P>lw(1i), for example, creates a left-justified column that is one inch wide.
<BR></P>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> Be sure to specify a unit with the w (width) option. The default unit is ens. The width you specify does not include the gap between columns. To set the column gap, refer to the "Spacing and
Lines" section later in this chapter.
<BR></NOTE>
<HR ALIGN=CENTER>
<P>Other format options enable you to change the font and the point size. You can also change vertical spacing.
<BR></P>
<TABLE BORDER>
<TR>
<TD>
<P>f (or F)</P>
<TD>
<P>Changes the font. The font is specified immediately after f, as in fCW.</P>
<TR>
<TD>
<P>b (or B)</P>
<TD>
<P>Changes to bold font.</P>
<TR>
<TD>
<P>i (or I)</P>
<TD>
<P>Changes to italic font.</P>
<TR>
<TD>
<P>p (or P)</P>
<TD>
<P>Changes the point size. The size is specified immediately after p, as in p9.</P>
<TR>
<TD>
<P>v (or V)</P>
<TD>
<P>Changes vertical spacing. The spacing is specified immediately after v, as in v10.</P></TABLE>
<H4 ALIGN="CENTER">
<CENTER><A ID="I10" NAME="I10">
<FONT SIZE=3><B>Global Options</B>
<BR></FONT></A></CENTER></H4>
<P>tbl's global options affect the placement of your table on the page and its appearance. They are specified on a single line that ends with a semicolon (;). The options must be separated by spaces, tabs, or commas.
<BR></P>
<P>One global option, for example, enables you to change the default column separator, the tab, to another character. The exclamation point is typically used in technical writing. Because there's nothing to get excited about in the text, tbl doesn't get
confused.
<BR></P>
<P>The global options are
<BR></P>
<TABLE BORDER>
<TR>
<TD>
<P>expand</P>
<TD>
<P>Expands the table to the full width of the page. It cannot be used with center.</P>
<TR>
<TD>
<P>box</P>
<TD>
<P>Puts a box around the table.</P>
<TR>
<TD>
<P>doublebox</P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -