A macro example from Mother GooseIntroduction to LaTeX macrosWhat good are macros?

What good are macros?

What is the value in defining personal LaTeX macros?

Suppose that you are writing a document in which you are going to refer to Texas A&M University twenty-five times. To save typing, you might type "Texas A&M University" once and then copy it twenty-four times with the mouse.

Unfortunately, you find out when you run the document through LaTeX that LaTeX wanted you to type "A\&M" instead of "A&M". Or perhaps you decide later that you want to say "Texas A&M University System" instead of "Texas A&M University."

By defining a LaTeX macro, you can simultaneously save typing and protect yourself in the event of future revisions. If you type

     \newcommand{\tamu}{Texas A\&M University} 

then you can later type 

     Bonfire is a tradition at \tamu. 

LaTeX will translate this into

Bonfire is a tradition at Texas A\&M

Or you can type

     \tamu{} is a land-grant institution. 

TAMU is a land-grant institution.

The point of the empty braces in the preceding example is that LaTeX interprets white space immediately following a control sequence as a delimiter terminating the name of the control sequence. You have to fool LaTeX if you really want a space to appear in the output. There is a standard LaTeX package named  xspace that handles this annoying feature automatically. If you have a current LaTeX installation that includes the  tools bundle, then you can put \usepackage{xspace} in the preamble of your source document, change the macro definition to

     \newcommand{\tamu}{Texas A\&M University\xspace}

and you should not have to worry about putting empty braces at the end of the control sequence in text.

By using the \tamu macro, you save typing, and you also make it easy to revise your document. If you decide to change "Texas A&M University" to "Texas A&M University System", you need only make this change once, in the definition of the macro \tamu.

(Aside to former plain TeX users: LaTeX's \newcommand command is preferable to TeX's \def command, because \newcommand prevents you from accidentally redefining an existing macro. If you really do want to redefine an existing LaTeX command, and you are sure that no disaster will ensue, then you can use LaTeX's \renewcommand command.)

logo The Math 696 course pages were last modified April 5, 2005.
These pages are copyright © 1995-2005 by Harold P. Boas. All rights reserved.
A macro example from Mother GooseIntroduction to LaTeX macrosWhat good are macros?