-->
Previous | Table of Contents | Next |
Macros have made TeX 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.
A macro is defined once, using the following format:
\def macroname {new text}
In this case, macroname is a name or TeX 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
\def\brg{burger} Ham\brg, cheese\brg, lim\brg.
is output as follows:
Hamburger, cheeseburger, limburger.
Macros can refer to other macros, as in
\def\tig{a tigger } \def\wond{a wonderful thing } \def\pooh{\wond is \tig cause \tig is \wond} \pooh\par
which produces the following:
a wonderful thing is a tigger cause a tigger is a wonderful thing
Warning:
You must be careful of recursive macro definitions: macros that refer to their own names within their definition. Such macro definitions cause TeX to continuously (and vainly) evaluate the macro, leading to an infinite loop. The following is an example of this:\def\itself{\itself} \itself
TeX 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:
\def macroname (list of formal parameters) {new text}
Here, the list of parameters is specified as #1, #1#2, #1#2#3, 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
\def\parm#1{This is the #1 time Ill say this.} \parm{first} \parm{second} \parm{last}
produces the following:
This is the first time Ill say this. This is the second time Ill say this. This is the last time Ill say this.
Each parameter that is used must be passed separately by enclosing it in braces, as in
\def\family#1#2{My #1 is #2.} \family{wife}{Cindy} \family{sister}{Sheila} \family{father}{Myles}
which makes the following output:
My wife is Cindy. My sister is Sheila. My father is Myles.
OK - tjp
You must specify an appropriate number of parameters in your macro definition. The macro definition
\def\mistake#1{This is wrong because of #2.}
is incorrect because it refers to a second parameter that is not specified in the formal parameter list.
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.
Macro definitions can be nested one within another, as in the following:
\def\hey{Hey\def\hey{hey}} \hey, \hey, \hey.
This has the following output:
Hey, hey, hey.
As with many topics within this book, we have examined only some of the highlights of TeX. There is much more to learn but, having covered the basics regarding macros, you can now look at the most popular extension of TeX, which uses macros to enhance the creation of documents. This extension is LaTeX.
LaTeX is a collection of macros that build on the capabilities of TeX 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. LaTeX 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, LaTeX macros have names that are more representative of the way writers think when they are writing.
Because LaTeX is an extension of TeX, youll find it easy to become quickly productive in LaTeX, assuming that you have some experience in TeX. Whitespace and spacing between paragraphs are handled in the same manner as in TeX. The special characters in TeX are the same in LaTeX, and comments are denoted using the % character.
The key differences between TeX and LaTeX become apparent as you learn more about the macros that define the layout of your document in a convenient fashion.
Previous | Table of Contents | Next |