📄 354-356.html
字号:
<HTML>
<HEAD>
<TITLE>Linux Unleashed, Third Edition:TeX and LaTeX</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=19//-->
<!--PAGES=354-356//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="351-354.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="357-359.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading11"></A><FONT COLOR="#000077">Macros</FONT></H4>
<P>Macros have made <TT>TeX</TT> a highly extendible system. They essentially enable you to create new commands by associating existing commands and text sequences to a macro name. After they are defined, these macros can be used in other parts of your document to replace repetitive pieces of text, or to encapsulate abstract operations.</P>
<P>A macro is defined once, using the following format:</P>
<!-- CODE SNIP //-->
<PRE>
\def <I>macroname</I> {<I>new text</I>}
</PRE>
<!-- END CODE SNIP //-->
<P>In this case, <I>macroname</I> is a name or <TT>TeX</TT> command preceded by a backslash character. Any reference to this macro name is replaced by the new text throughout the document. For example, the macro definition</P>
<!-- CODE SNIP //-->
<PRE>
\def\brg{burger}
Ham\brg, cheese\brg, lim\brg.
</PRE>
<!-- END CODE SNIP //-->
<P>is output as follows:
</P>
<!-- CODE SNIP //-->
<PRE>
Hamburger, cheeseburger, limburger.
</PRE>
<!-- END CODE SNIP //-->
<P>Macros can refer to other macros, as in
</P>
<!-- CODE SNIP //-->
<PRE>
\def\tig{a tigger }
\def\wond{a wonderful thing }
\def\pooh{\wond is \tig cause \tig is \wond}
\pooh\par
</PRE>
<!-- END CODE SNIP //-->
<P>which produces the following:
</P>
<!-- CODE SNIP //-->
<PRE>
a wonderful thing is a tigger cause a tigger is a wonderful thing
</PRE>
<!-- END CODE SNIP //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>Warning: </B><BR>You must be careful of recursive macro definitions: macros that refer to their own names within their definition. Such macro definitions cause <TT>TeX</TT> to continuously (and vainly) evaluate the macro, leading to an infinite loop. The following is an example of this:
<!-- CODE SNIP //-->
<PRE>
\def\itself{\itself}
\itself
</PRE>
<!-- END CODE SNIP //-->
<HR></FONT>
</BLOCKQUOTE>
<P><TT>TeX</TT> macros have the added feature of being able to accept parameters when expanded, if a list of formal parameters has been specified in the macro definition. To create a macro using parameters, you would use this format:</P>
<!-- CODE SNIP //-->
<PRE>
\def <I>macroname</I> (<I>list of formal parameters</I>) {<I>new text</I>}
</PRE>
<!-- END CODE SNIP //-->
<P>Here, the list of parameters is specified as <TT>#1</TT>, <TT>#1#2</TT>, <TT>#1#2#3</TT>, and so on. This is a powerful aspect of macros because it can change the output of an expanded macro based on the parameter in use. For example, the code</P>
<!-- CODE SNIP //-->
<PRE>
\def\parm#1{This is the #1 time I’ll say this.}
\parm{first}
\parm{second}
\parm{last}
</PRE>
<!-- END CODE SNIP //-->
<P>produces the following:
</P>
<!-- CODE SNIP //-->
<PRE>
This is the first time I’ll say this.
This is the second time I’ll say this.
This is the last time I’ll say this.
</PRE>
<!-- END CODE SNIP //-->
<P>Each parameter that is used must be passed separately by enclosing it in braces, as in
</P>
<!-- CODE SNIP //-->
<PRE>
\def\family#1#2{My #1 is #2.}
\family{wife}{Cindy}
\family{sister}{Sheila}
\family{father}{Myles}
</PRE>
<!-- END CODE SNIP //-->
<P>which makes the following output:
</P>
<!-- CODE SNIP //-->
<PRE>
My wife is Cindy.
My sister is Sheila.
My father is Myles.
</PRE>
<!-- END CODE SNIP //-->
<P>OK - tjp
</P>
<P>You must specify an appropriate number of parameters in your macro definition. The macro definition</P>
<!-- CODE SNIP //-->
<PRE>
\def\mistake#1{This is wrong because of #2.}
</PRE>
<!-- END CODE SNIP //-->
<P>is incorrect because it refers to a second parameter that is not specified in the formal parameter list.
</P>
<P>Macros can be redefined in your document, but you should be aware that only the most recent definition will be applied. Also, macros defined within groups are valid only within the scope of the group.</P>
<P>Macro definitions can be nested one within another, as in the following:</P>
<!-- CODE SNIP //-->
<PRE>
\def\hey{Hey\def\hey{hey}}
\hey, \hey, \hey.
</PRE>
<!-- END CODE SNIP //-->
<P>This has the following output:
</P>
<!-- CODE SNIP //-->
<PRE>
Hey, hey, hey.
</PRE>
<!-- END CODE SNIP //-->
<P>As with many topics within this book, we have examined only some of the highlights of <TT>TeX</TT>. There is much more to learn but, having covered the basics regarding macros, you can now look at the most popular extension of <TT>TeX</TT>, which uses macros to enhance the creation of documents. This extension is <TT>LaTeX</TT>.</P>
<H3><A NAME="Heading12"></A><FONT COLOR="#000077">LaTeX: An Enhancement of TeX</FONT></H3>
<P><TT>LaTeX</TT> is a collection of macros that build on the capabilities of <TT>TeX</TT> and provide a higher level of abstraction for the creation of documents. It is essentially a style library that encourages uniform formatting and typesetting across documents. <TT>LaTeX</TT> macros shift the emphasis away from the details of things such as “set text to 8-point slanted” to concepts that writers identify more readily with, such as the emphasis of a word or phrase. Thus, <TT>LaTeX</TT> macros have names that are more representative of the way writers <I>think</I> when they are writing.</P>
<P>Because <TT>LaTeX</TT> is an extension of <TT>TeX</TT>, you’ll find it easy to become quickly productive in <TT>LaTeX</TT>, assuming that you have some experience in <TT>TeX</TT>. Whitespace and spacing between paragraphs are handled in the same manner as in <TT>TeX</TT>. The special characters in <TT>TeX</TT> are the same in <TT>LaTeX</TT>, and comments are denoted using the <TT>%</TT> character.</P>
<P>The key differences between <TT>TeX</TT> and <TT>LaTeX</TT> become apparent as you learn more about the macros that define the layout of your document in a convenient fashion.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="351-354.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="357-359.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 + -