Making the most of LaTeX commands
Posted Sat Mar 24 16:06:39 -0500 2007
The facilities in LaTeX for creating user-defined commands are often overlooked or ignored, but they can be extremely useful for saving time and improving flexibility, particularly in handwritten LaTeX code. Even better, they are very easy to use. The newcommand command can be used to define new commands (surprise!), with the following syntax. It is usually use in a document's preamble or in a separate package file; although the latter case isn't covered here.
\newcommand{name}[#args]{definition}
The number of arguments can be left out for simple cases such as
\newcommand{\ie}{\textit{i.e.}\ }
\newcommand{\eg}{\textit{e.g.}\ }
which define commands \ie and \eg for setting their respective abbreviations in italics. The backslash-space command at the end is needed to stop LaTeX from treating the last full stop as the end of a sentence and so creating extra space after it. (Note, however, that in this form the expanded command will always be followed by a space. The \xspace command is a cleverer alternative but it requires the xspace package.)
Why is this helpful? Well, firstly, if you or someone you are writing for want to set these abbreviations like this then it saves you from having to use a textit command every time. But in addition, if you later want or need to drop the italics, you can simply change the definition in the document preamble, rather than changing \textit{i.e.} to i.e. everywhere it occurs.
The same principle can be used for commonly used mathematical forms, for example
\newcommand{\infint}{\ensuremath{\int_{-\infty}^{\infty}}}
which defines a command for generating an integral with minus infinity and infinity as its limits. The ensuremath command is needed because \int is only defined within a mathematical environment. An example that uses arguments could be
\newcommand{\pfrac}[2]{\ensuremath{\frac{\partial #1}{\partial #2}}}
which sets a partial derivative. Note that #1 and #2 in the definition are replaced by the arguments when the command is used (there can be up to 9 such arguments), so the following two commands are now equivalent in the rest of the document.
\pfrac{r}{x}
\frac{\partial r}{\partial x}
Notice that almost any LaTeX commands can be included in the definition, so you can use \newcommand to wrap around another command that doesn't quite behave how you want. For example,
\newcommand{\eqn}[1]{Eq.\ (\ref{#1})}
will define a way to refer to equations in which the equation number is surrounded by parentheses and preceded by the abbreviation “Eq.”. Again, changing this behaviour is simply a matter of altering the definition accordingly.
Finally, it should be mentioned that LaTeX will not allow you to define a command that already exists: attempting to do so will create an error. However, if you're sure you want to override an existing command, you can use \renewcommand, which has exactly the same syntax as \newcommand, but is used for the particular case of redefining a command. For example, if you prefer the word “iff” to the double ended arrow that LaTeX uses by default to mean “if and only if” (i.e. symmetric logical dependence), you can use
\renewcommand{\iff}{\ensuremath{\mathrm{iff}}}
