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

📄 unx27.htm

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

<P>Be careful when you use !. It must precede the expression being reversed. For example, to check for an unequal condition, you must write .if !\\na=\\nb. You cannot write .if \\na!=\\nb.

<BR></P>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="more.gif" WIDTH = 35 HEIGHT = 35><B>Units of Measurement</B>

<BR>

<BR>troff allows you to use just about any units you want (except rods and kilometers):

<BR>

<BR>i&#151;inch          p&#151;point      u&#151;troff unit

<BR>

<BR>c&#151;centimeter    m&#151;em         v&#151;verticaspace

<BR>

<BR>P&#151;Pica          n&#151;en

<BR>

<BR>Unfortunately, it is impossible to be 100 percent certain of the default units for any given primitive. For the most part, the troff default for horizontal measurements is the em and for vertical measurements is the line space. The nroff default for 
horizontal measurement is device-dependent, but it's usually 1/10 or 1/12 of an inch.

<BR>

<BR>If you use arithmetic expressions, you will soon find that none of those defaults work the way they are supposed to. The culprit is the troff unit (u). A troff unit is about 1/300 of an inch (for a 300 dpi printer). Because this is a very much smaller 

unit than any of the others troff accepts, you can expect loony output from time to time. (Your text will print, but not on the paper.)

<BR>

<BR>Always specify units.

<BR>

<BR>If you want to divide 37 inches by 2, you are far safer doing the arithmetic in your head and specifying 18.5P than letting troff decide how to process 37P/2. troff will not do what you expect. troff will divide 37 picas by 2 ems. You will not like the 

result. If, in desperation, you try 37/2P, you will still not like the result because troff will divide 37 ems by 2 picas. You have to specify 37P/2u. The u acts as a sort of pacifier and lets troff perform the arithmetic correctly.

<BR>

<BR>When you're unsure of the units, use troff units. It's sort of like adding backslashes. A few more will probably fix the problem.

<BR></NOTE>

<HR ALIGN=CENTER>

<H3 ALIGN="CENTER">

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

<FONT SIZE=4><B>Arithmetic and Logical Expressions</B>

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

<P>As you see, conditional statements are often combined with arithmetic expressions. You can also use logical expressions. troff understands all of the following:

<BR></P>

<TABLE BORDER>

<TR>

<TD>

<PRE>

<BR>+ - * /

<BR></PRE>

<TD>

<PRE>

<BR>plus, minus, multiplied by, divided by

<BR></PRE>

<TR>

<TD>

<PRE>

<BR>%

<BR></PRE>

<TD>

<PRE>

<BR>modulo

<BR></PRE>

<TR>

<TD>

<PRE>

<BR>&gt; &lt;

<BR></PRE>

<TD>

<PRE>

<BR>greater than, less than

<BR></PRE>

<TR>

<TD>

<PRE>

<BR>&gt;= &lt;=

<BR></PRE>

<TD>

<PRE>

<BR>greater than or equal to, less than or equal to

<BR></PRE>

<TR>

<TD>

<PRE>

<BR>=

<BR></PRE>

<TD>

<PRE>

<BR>equal (== is a synonym)

<BR></PRE>

<TR>

<TD>

<PRE>

<BR>&amp;

<BR></PRE>

<TD>

<PRE>

<BR>AND

<BR></PRE>

<TR>

<TD>

<PRE>

<BR>:

<BR></PRE>

<TD>

<PRE>

<BR>OR</PRE></TABLE>

<P>Unlike other UNIX programs, troff has no notion of precedence. An expression like \\$1+\\$2*\\$3-\\$4 is evaluated strictly from left to right. Thus, to troff, 2+3*5-10\2 equals 7.5. This is hard to get used to and easy to forget.

<BR></P>

<P>Always specify units.

<BR></P>

<H3 ALIGN="CENTER">

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

<FONT SIZE=4><B>Diversions</B>

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

<P>Diversions let you store text in a particular location (actually a macro that you define), from which the text can be retrieved when you need it. Diversions are used in the &quot;keep&quot; macros and in footnotes.

<BR></P>

<P>The diversion command is .di followed by the name of the macro in which the ensuing text is to be stored. A diversion is ended by .di on a line by itself.

<BR></P>

<P>Diverted text is processed (formatted) before it is stored, so when you want to print the stored text, all you have to do is specify the macro name. Since there is virtually no limit either to the number of diversions you can have in a file or to the 
length of any diversion, you can use diversions to store repeated text.

<BR></P>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> Storing repeated text in a diversion isn't necessarily a good idea. You can avoid typing the repeated text just as easily by putting it in a file and reading that file into your text file.

<BR></NOTE>

<HR ALIGN=CENTER>

<P>For example, suppose the following text is repeated many, many times in your document:

<BR></P>

<PRE>.AL 1

,LI

Log in as root.

.LI

Invoke the UNIX system administrative menu by

typing \f(CWsysadm\fP and pressing Enter.

.P

The system administrative menu is displayed.

.LI

Select \f(CWEquine Systems\fP by highlighting

the line and pressing Enter.

.P

The Equine Systems menu is displayed</PRE>

<P>You could store this text in .Em (for Equine Menu) by prefacing it with .diEm and ending it with .di.

<BR></P>

<P>Note that your diversion contains an unterminated list. If this is likely to cause problems, add .LE to the diverted text.

<BR></P>

<P>To print the Equine Systems text, just put .Em in your file.

<BR></P>

<P>In addition to .di, there is a .da (diversion append) primitive that works like .am. .da is used to add text to an existing diversion. It can be used over and over, each time adding more text to the diversion. (To overwrite the text in a given 
diversion, just define it again with a .diEm.) The .am primitive can be used, like .am, to create TOC data.

<BR></P>

<P>You can even have a diversion within a diversion. The &quot;inside&quot; diversion can be used on its own, as well.

<BR></P>

<H3 ALIGN="CENTER">

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

<FONT SIZE=4><B>Traps</B>

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

<P>troff provides several kinds of traps: page traps (.wh and .ch); diversion traps (.dt); and input line traps (.it).

<BR></P>

<P>Page traps usually invoke macros. For example, when troff gets near the bottom of a page, the trap that produces the page footer is sprung. A simple illustration of this is the following.

<BR></P>

<P>Suppose you wanted to print the current date one inch from the bottom of every page in your document. Use the .wh primitive:

<BR></P>

<PRE>.deDa                   \&quot;define date macro

\\n(mo/\\n(dy/18\\n(yr  \&quot;set date

..

.wh 1i Da               \&quot;set the trap</PRE>

<P>The order of the arguments is important.

<BR></P>

<P>To remove this kind of trap, invoke it with the position, but without the macro name: .wh 1i.

<BR></P>

<P>The .ch primitive changes a trap. If you wanted the date an inch from the bottom of the page on page 1 of your document, but an inch and a half from the bottom of the page on all subsequent pages, you could use .ch Da 1.5i.

<BR></P>

<P>(Note that the argument order is different.)

<BR></P>

<P>Diversion traps are set with the .dt primitive, for example:

<BR></P>

<PRE>.dt 1i Xx</PRE>

<P>This diversion trap, set within the diversion, invokes the .Xx macro when (if) the diversion comes within one inch of the bottom of the page.

<BR></P>

<P>Input text traps are set with the .it primitive. This trap is activated after a specified number of lines in your text file.

<BR></P>

<P>There is a fourth kind of trap, though it isn't usually thought of as a trap. This is the end macro (.em) primitive. .em is activated automatically at the end of your text file. It can be used to print overflow footnotes, TOCs, bibliographies, etc.

<BR></P>

<H3 ALIGN="CENTER">

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

<FONT SIZE=4><B>Environments</B>

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

<P>The .ev (environment) primitive gives you the ability to switch to a completely new and independent set of parameters, such as line length, point size, font, and so forth. It lets you return to your original set of parameters just as easily. This 
process is known as environment switching. The concept is used in page headers, for example, where the font and point size are always the same&#151;and always different from the font and point size in the rest of the document.

<BR></P>

<P>Three environments are available: ev 0 (the normal, or default, environment); ev 1; and ev 2.

<BR></P>

<P>To switch from the normal environment, just enter .ev 1 or .ev 2 on a line by itself and specify the new parameters. These new parameters will be in effect until you specify a different environment. To return to your normal environment, use .ev or .ev 
0.

<BR></P>

<P>You could use environment switching instead of writing the .Qb and .Qe macros. Here's how it would work:

<BR></P>

<PRE>.ev 1     \&quot;long quote begins

.sp

.ls 1

.in +1i

.ll -2i

text of quotation

.sp

.ev</PRE>

<P>Environments are often used with diversions or with footnotes where the text is set in a smaller point size than body type. It is to accommodate diversions within diversions that the third environment is provided.

<BR></P>

<H3 ALIGN="CENTER">

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

<FONT SIZE=4><B>Debugging</B>

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

<P>Debugging macros is slow and often painful. If you have a version of troff that includes a trace option, use it&#151;but be warned: It produces miles of paper. If you don't have a trace option, you can use the .tm primitive (for terminal message) to 
print the value of a number register at certain points in your file. The value is sent to standard error, which is probably your screen. Use .tm like this:

<BR></P>

<PRE>.tm Before calling the Xx macro, the value of xX is \n(xX.

.Xx

.tm After calling the Xx macro, the value of xX is \n(xX.</PRE>

<P>(Note that you don't hide the number register from copy mode because you put these lines right in your text file. Remember to delete them before the document goes to the printer.)

<BR></P>

<H4 ALIGN="CENTER">

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

<FONT SIZE=3><B><I>troff</I></B><B> Output</B>

<BR></FONT></A></CENTER></H4>

<P>Sometimes you have to look at troff output. It's not a pretty sight, but after the first few files, it begins to make sense. Here's the troff code produced by a file with two words in it: UNIX Unleashed.

<BR></P>

<P>(By the way, use troff -o &gt; outputfile to produce this output.)

<BR></P>

<PRE>x T post

x res 720 1 1

x init

v0

p1

x font 1 R

x font 2 I

x font 3 B

x font 4 BI

x font 5 CW

x font 6 H

x font 7 HI

x font 8 HB

x font 9 S1

x font 10 S

s10

f1

H720

V120

cU

72N72I33Xw97U72n50l28e44as39h50e44dn120 0

x trailer

v7920

x stop</PRE>

<P>If you look hard, you can pick out the text in the long line. The numbers are horizontal motions reflecting the width of the letters. You can also see where the font positions are defined. The s10 on a line by itself is the point size. f1 is the font in 

position 1 (in this case, Times-Roman). The H and V numbers following the font definition specify the starting horizontal and vertical position on the page.

<BR></P>

<H4 ALIGN="CENTER">

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

<FONT SIZE=3><B>PostScript Output</B>

<BR></FONT></A></CENTER></H4>

<P>PostScript output is a little easier to read, but the set-up lines are endless. Where UNIX Unleashed generates 24 lines of troff code, the same two words generate more than 800 lines of PostScript code. The significant lines are at the beginning and the 

end. The last 17 lines of the PostScript file are as follows:

<BR></P>

<PRE>setup

2 setdecoding

%%EndSetUp

%%Page: 1 1

/saveobj save def

mark

⌨️ 快捷键说明

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