⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unx21.htm

📁 Linux Unix揭密.高质量电子书籍.对学习Linux有大帮助,欢迎下载学习.
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<HTML>

<HEAD>

<TITLE>UNIX Unleashed unx21.htm</TITLE>

<LINK REL="ToC" HREF="index.htm">

<LINK REL="Next" HREF="unx22.htm">

<LINK REL="Previous" HREF="unxpt5.htm"></HEAD>

<BODY TEXT="#000000" LINK="#0000FF" VLINK="#800080" bgcolor=white>

<P><A HREF="unxpt5.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="unx22.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">21 &#151; Basic Formatting with troff/nroff</A></LI>

<UL>

<UL>

<UL>

<UL>

<LI>

<A HREF="#I3">By James C. Armstrong</A></LI></UL></UL>

<LI>

<A HREF="#I4">Formatting with nroff/troff An Overview</A></LI>

<LI>

<A HREF="#I5">Printing nroff and troff Files</A></LI>

<LI>

<A HREF="#I6">Text Filling and Adjusting</A></LI>

<LI>

<A HREF="#I7">Vertical Spacing</A></LI>

<LI>

<A HREF="#I8">Line Controls</A></LI>

<LI>

<A HREF="#I9">Page Control</A></LI>

<LI>

<A HREF="#I10">Fonts and Style Controls</A></LI>

<LI>

<A HREF="#I11">In-Line Escape Sequences</A></LI>

<LI>

<A HREF="#I12">Special Characters</A></LI>

<LI>

<A HREF="#I13">Strings and Macros</A></LI>

<LI>

<A HREF="#I14">Number Registers</A></LI>

<LI>

<A HREF="#I15">Traps and Diversions</A></LI>

<LI>

<A HREF="#I16">Tabs, Character Conversions, and Controls</A></LI>

<LI>

<A HREF="#I17">Local Motions</A></LI>

<LI>

<A HREF="#I18">Overstrikes, Lines, and Arcs</A></LI>

<LI>

<A HREF="#I19">Conditional Text</A></LI>

<LI>

<A HREF="#I20">File Switching and Environments</A></LI>

<LI>

<A HREF="#I21">Flotsam and Jetsam</A></LI>

<LI>

<A HREF="#I22">Quick Reference of nroff/troff Requests</A></LI>

<LI>

<A HREF="#I23">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>21 &#151; Basic Formatting with </B><B><I>troff</I></B><B>/</B><B><I>nroff</I></B>

<BR></FONT></A></CENTER></H1>

<H5 ALIGN="CENTER">

<CENTER><A ID="I3" NAME="I3">

<FONT SIZE=3><B>By James C. Armstrong</B>

<BR></FONT></A></CENTER></H5>

<P>One of the most common uses for any computer is text processing, and in this respect machines running UNIX are no different than any others.

<BR></P>

<P>Many products exist for UNIX machines, including WordPerfect, Microsoft Word, IslandWrite, and FrameMaker. Although very powerful, these text processors might not be available on every machine. The best solution for almost every platform is the troff 
text processor and its cousin, nroff. troff is short for &quot;text run-off&quot; and was originally developed by Joseph Ossanna in the early 1970s, for use with the Graphics Systems CAT typesetter. This was the typesetter in use at the Murray Hill 
Computer Center of Bell Labs, where UNIX was first developed. In 1979, in response to the need to support more typesetters, Brian Kernighan rewrote troff. Although alternatives were investigated, UNIX had already invested a large amount of effort in troff, 

including macro packages and preprocessors for the utility. You have already learned about manual pages, which are written in troff using a specialized macro package.

<BR></P>

<P>Closely associated with troff is nroff. They both use the same set of commands to format text, the biggest exception being that nroff does not support commands for changing point sizes and supports only a limited number of character set changes. nroff 
also provides ASCII output, so you can see the results of your nroff command on your screen. Although third-party products can show the results of your troff command on screen if they have graphics capabilities, on a standard UNIX system, the only way to 
see the troff output is to send it to a printer.

<BR></P>

<H3 ALIGN="CENTER">

<CENTER><A ID="I4" NAME="I4">

<FONT SIZE=4><B>Formatting with </B><B><I>nroff</I></B><B>/</B><B><I>troff</I></B><B> An Overview</B>

<BR></FONT></A></CENTER></H3>

<P>Many word processors such as Microsoft Word and WordPerfect are WYSIWYG processors (what you see is what you get). With those word processors, you choose menu items or press key combinations that cause the formatting to occur right on the screen, but 
the formatting codes do not appear in your document. Text processors like nroff and troff are called descriptive markup languages, which means that you enter the formatting codes into your document like any other text, and you don't see the effects of 
those instructions until you print the file.

<BR></P>

<P>Several building blocks are available for formatting files using nroff and troff, including the following:

<BR></P>

<PRE>

<BR>Primitive requests The standard command in troff is called a primitive request and has the form of a period followed by two lowercase letters. The period must appear in the first column, and any text after the request is an argument. Primitives are 
used to do all kinds of formatting, such as indenting paragraphs, adding space between paragraphs, changing fonts, and centering text. This chapter provides examples of using the more common primitives and a quick reference that briefly describes all 
primitives. If you are new to nroff/troff, you might want to try using a macro package before you dive into primitives.



<BR>Macros Most UNIX systems provide standard macro packages, which enable you to format documents more easily than with primitives. Macros perform operations more or less automatically, such as formatting bulleted lists, headings, and indented paragraphs. 

Four macro packages, mm, ms, me, and man are described in detail in Chapter 22, &quot;Formatting with Macro Packages.&quot; Chapter 27, &quot;Writing Your Own Macros,&quot; shows you the ins and outs of creating macros. You can create a file using only 
macro package commands, or you can mix macros and primitives in the same file.



<BR>Preprocessors Most UNIX systems provide standard preprocessors. Each preprocessor is a set of commands devoted to a special task. You can format tables with the tbl preprocessor (Chapter 23), equations with eqn (Chapter 24), line drawings with pic 
(Chapter 25), and graphs with grap (Chapter 26). You can create a file containing only preprocessor commands, and you can embed preprocessor commands in regular documents formatted with primitives, a macro package, or both.



<BR>Strings Strings can be defined, just as macros can. For example, if you were writing about a new product whose name hadn't been decided yet, you could define a string for the temporary name &quot;Hot New Product.&quot; When the name was finally chosen, 

you wouldn't have to do a global search and replace for &quot;Hot New Product.&quot; You could just redefine the string to produce &quot;XYZZY Thingo.&quot; Specific instructions for defining strings are in the section titled &quot;Strings and Macros&quot; 

later in this chapter, and in Chapter 26, &quot;Writing Your Own Macros.&quot;



<BR>Number registers Number registers are used to keep track of values like your current font and point size, your current indentation, and the current list item. They are really nothing more than storage locations. Some are read-only; others can be 
manipulated. You can define your own number registers and use them in macros. Specific examples are given in the section titled &quot;Number Registers&quot; later in this chapter, and in Chapter 26, &quot;Writing Your Own Macros.&quot;</PRE>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> troff insists on calling these registers &quot;number registers,&quot; but in fact they don't need to contain numbers; they can&#151;and often do&#151;contain alphabetic characters.

<BR></NOTE>

<HR ALIGN=CENTER>

<PRE>

<BR>Escape sequences Escape sequences (backslash-character or backslash-open-parenthesis-character-character) can be used to change fonts and point sizes and for many other tasks. Some escape sequences enable you to enter troff primitives in-line with the 

text they affect rather than on lines by themselves. Specific examples are given in the section titled &quot;In-Line Escape Sequences&quot; later in this chapter and in other chapters where they apply.



<BR>Special characters Although these are system-dependent, there are a number of special characters that are usually available on all systems, such as a long dash, a degree symbol, and a copyright symbol. Specific examples are given in the section titled 

&quot;Special Characters&quot; later in this chapter and in other chapters where they apply.</PRE>

<P>Processing troff and nroff files for printing is discussed in Chapter 29, &quot;Processing, Printing, and Troubleshooting Formatted Files.&quot;

<BR></P>

<P>Chapters 21 through 29 give you a good start on using the nroff/troff family of text processing tools, but there is much more that you can do with them. For additional information, you can consult the nroff and troff man pages online or your local 
bookstore for books dedicated to this subject.

<BR></P>

<H3 ALIGN="CENTER">

<CENTER><A ID="I5" NAME="I5">

<FONT SIZE=4><B>Printing </B><B><I>nroff</I></B><B> and </B><B><I>troff</I></B><B> Files</B>

<BR></FONT></A></CENTER></H3>

<P>UNIX offers a selection of commands and utilities for printing. Printing nroff and troff output files is covered in detail in Chapter 29. It describes the role of the preprocessors, nroff and troff, the role of the postprocessors, and introduces you to 

PostScript. It also reviews raw printing (just dumping the contents of your file with no formatting at all) because sometimes you'll want to print a file without formatting it. 

<BR></P>

<H3 ALIGN="CENTER">

<CENTER><A ID="I6" NAME="I6">

<FONT SIZE=4><B>Text Filling and Adjusting</B>

<BR></FONT></A></CENTER></H3>

<P>The cleanest look to any document is when the text looks symmetric, with one or two smooth margins, and spread across the page&#151;like this paragraph. The default settings for nroff and troff are to &quot;fill&quot; each line with text, and to 
&quot;adjust&quot; the position of the text so that all lines begin at the left margin, and the right margin is justified. In the simplest case, the input file does not need any troff requests to format a basic document. Listing 21.1 illustrates a basic 
input file, and Figure 21.1 illustrates the output produced by nroff.

<BR></P>

<P>

<BR><B><A HREF="21unx01.gif">Figure 21.1. </B><B><I>roff</I></B><B> output with no requests.</A></B>

<BR></P>

<UL>

<LH><B>Listing 21.1. Basic </B><B>nroff</B><B>/</B><B>troff</B><B> source with no requests.</B></LH></UL>

<PRE>We, the people of the United States, in order

to form a more perfect Union, establish justice, insure

domestic tranquility, provide for the common defense, promote

the general welfare,

and secure the blessing of liberty to ourselves and our posterity do

ordain and establish this Constitution for the United States of

America.</PRE>

<P>The raw text file has a ragged right margin, with some lines very short, and one line longer than desired. By putting the text through nroff, the lines are set to an even length, and the margins are smooth. Two words were broken across lines. If you 
look closely at the output, you'll see that nroff justifies the right margin by inserting extra spaces between words, at alternating ends of each line. The first line needed no extra spaces, but to even the margin on the second line, an extra space was 
included between &quot;perfect&quot; and &quot;Union,&quot; and the third line needed four extra spaces.

<BR></P>

<P>troff output of the same text, shown in Figure 21.2, shows that the lines are expanded to justify the margins by changing the spacing of letters across the entire line.

<BR></P>

<P>

<BR><B><A HREF="21unx02.gif">Figure 21.2. </B><B><I>troff</I></B><B> output of Figure 21.1.</A></B>

<BR></P>

<P>The ability to fill the text can be set with two requests. The first, .fi, tells troff that you want the text to be filled with input. This is the default setting. The request .nf tells troff that you don't want text filled, that you want the right 
margin to be ragged. This is useful for cases where a block of text is inappropriate, such as a return address or poetry. Listing 21.2 shows a sample input file for a letter, with no fill in places, and fill in places. Figure 21.3 shows the output.

<BR></P>

<P>

<BR><B><A HREF="21unx03.gif">Figure 21.3. </B><B><I>troff</I></B><B> output showing filled and </B><B>nonfilled text.</A></B>

<BR></P>

<UL>

<LH><B>Listing 21.2. </B><B>troff</B><B> source illustrating the fill requests.</B></LH></UL>

<PRE>.nf

101 Main Street

Morristown, NJ  07960

15 March, 1994

Dear Sir,

.fi

I just wanted to drop you a note to thank you for spending the

time to give me a tour of your facilities. I found the experience

both educational and enjoyable. I hope that we can work together

to produce a product we can sell.</PRE>

<P>Note that a blank line is used to separate blocks of text. On a longer document, these blank lines can be used to separate paragraphs. Another way to separate blocks is to use .br. This interrupts the filling of the current line and starts a new block 
of text. The same can be done by starting a line of text with a space. Figure 21.4 shows the output of Listing 21.1, but includes a break after &quot;the general welfare.&quot;

<BR></P>

<P>

<BR><B><A HREF="21unx04.gif">Figure 21.4. </B><B><I>troff</I></B><B> output showing the effect of a </B><B>break in midsentence.</A></B>

<BR></P>

<P>Although smooth margins are the default, this is also something under the control of the writer. The .ad command controls adjustment. It can take the following as arguments: l means to adjust the left margin only; r is to adjust the right margin only; c 

is to center each line; and b or n means to adjust both margins. Figure 21.5 shows the effects of .ad l, .ad r, and .ad c on the first text sample. .ad b is the default starting value and is effectively demonstrated in the first example.

<BR></P>

<P>

<BR><B><A HREF="21unx05.gif">Figure 21.5. </B><B><I>troff</I></B><B> output showing the effects of </B><B>different line adjustments.</A></B>

<BR></P>

<P>Obviously, adjustment makes no sense if the text is not filled. Right margin adjustment can also be turned off with .na. The adjustment mode is not changed.

<BR></P>

<P>The last type of text adjustment is centering. This is a bit different than .ad c, which continues to fill lines before centering the text, but only if .fi is specified. The centering request is .ce and can be followed by a number. This centers the next 

line or lines, without filling text. If the text is being filled, each input line is treated as if it is followed by a break. Non-filled lines would be treated the same as .ce. Chapter titles are an example of text centering. Listing 21.3 is the source for 

a centering command, and the output is illustrated in Figure 21.6.

<BR></P>

<P>

<BR><B><A HREF="21unx06.gif">Figure 21.6. The effects of the centering </B><B>request.</A></B>

<BR></P>

<UL>

<LH><B>Listing 21.3. </B><B>troff</B><B> source for the centering command.</B></LH></UL>

<PRE>.ce 3

Scientific Methods of Computing

A Simulation

by John Smith</PRE>

<H3 ALIGN="CENTER">

<CENTER><A ID="I7" NAME="I7">

<FONT SIZE=4><B>Vertical Spacing</B>

<BR></FONT></A></CENTER></H3>

<P>There are three types of vertical space controls in troff. Baseline spacing controls the basic spacing between consecutive lines of text. The next type is extra line spacing; this is the ability to double-space text, or more, both on a regular basis, 
and on a per case basis. The last is a block of vertical space.

<BR></P>

<P>Space measurements have different scales. When a request needs a distance, you can use the default type or modify the number with an indicator. The measurement types are inches, centimeters, Picas, Ems, Ens, points, units, and vertical line spaces. A 
Pica is 1/6 of an inch. An em is the width of the letter m and is dependent on the font used in troff. An en is half an em. The modifiers are listed in Table 21.1.

<BR></P>

<UL>

<LH><B>Table 21.1. </B><B>troff</B><B> space measurement modifiers.</B>

<BR></LH></UL>

<TABLE BORDER>

<TR>

<TD>

<PRE><I>Measurement Option</I>

<BR></PRE>

<TD>

<PRE><I>Description</I>

<BR></PRE>

<TR>

<TD>

<P>i</P>

<TD>

<P>inch</P>

<TR>

<TD>

<P>c</P>

<TD>

<P>centimeter</P>

<TR>

<TD>

<P>p</P>

<TD>

<P>Pica</P>

<TR>

<TD>

<P>m</P>

<TD>

<P>Em</P>

<TR>

<TD>

<P>n</P>

<TD>

<P>En</P>

<TR>

<TD>

<P>p</P>

<TD>

<P>point</P>

<TR>

<TD>

<P>u</P>

<TD>

<P>unit</P>

<TR>

<TD>

<P>v</P>

<TD>

<P>vertical space</P></TABLE>

<P>The default vertical spacing between lines of text is dependent on the text processor used. For nroff, it is 1/6 of an inch. For troff, it is 12 points. This can be changed with .vs. For nroff, the command argument is rounded to Picas, so if extra space 

is needed

<BR>regularly, .ls is clearer. With troff, the default space measurement is points, although any measurement type can be used. An example of different spacings is given in Figure 21.7, using the initial text sample.

<BR></P>

<P>

<BR><B><A HREF="21unx07.gif">Figure 21.7. Different vertical spacing using </B><B><I>troff</I></B><B>.</A></B>

<BR></P>

<P>The .ls request, mentioned previously, is used to indicate the number of blank lines between each line of text. The default value is 1, for single spacing. Double-spacing text is accomplished with .ls 2. Figure 21.8 shows the first text sample, but with 

.ls 2.

<BR></P>

<P>

<BR><B><A HREF="21unx08.gif">Figure 21.8. Different line spacing using </B><B><I>troff</I></B><B>.</A></B>

<BR></P>

<P>Block spacing can be achieved with the .sp request. With no arguments, this gives a single blank line. It can take arguments of any size, with the default unit being the vertical spacing. Negative numbers space back up the page; positive numbers head 
down the page. Spacing changes requested here will not leave the page&#151;if the requested space is beyond the bottom of the page, the text will start at the top of the next page. Using the sample letter, you can leave an inch of space between the date 
and the salutation. The source is changed in Listing 21.4, with the output in Figure 21.9.

<BR></P>

<P>

<BR><B><A HREF="21unx09.gif">Figure 21.9. </B><B><I>troff</I></B><B> output with a block of </B><B>space.</A></B>

<BR></P>

<UL>

<LH><B>Listing 21.4. </B><B>troff</B><B> source for block spacing.</B></LH></UL>

<PRE>.nf

101 Main Street

Morristown, NJ  07960

15 March, 1994

.sp 1i

Dear Sir,

.fi

I just wanted to drop you a note to thank you for spending the

time to give me a tour of your facilities. I found the experience

both educational and enjoyable. I hope that we can work together

to produce a product we can sell.</PRE>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -