Delimited commands
15.1.8 Delimited commands
You can define new commands in TEX using characters and symbols to delimit argu- ments. Such delimited commands provide a way to write more readable source docu-
ments. First we have to learn how to define a command using TEX’s \def command.
Type \def, followed by the new command name (not in braces), then the definition in braces. For example, the first command defined in Section 15.1.1,
\newcommand{\larr}{\leftarrow} could be typed
\def\larr{\leftarrow} TEX’s \def command does not check whether a new command name is already
in use, so \def behaves differently from the L A TEX’s \newcommand, \renewcommand, and \providecommand (see Section 15.1.5). If the \larr command was defined pre- viously, the original definition is overwritten.
Tip It is your responsibility to ensure that your command name is unique when you define
a command using \def. L A TEX provides no protection. Use the techniques introduced
in Section 15.1.7 to check a name before you define a command with \def.
Now we can start discussing delimited commands with a simple example, defining
a command for vectors: \def\vect<#1>{\langle#1\rangle} Note that \vect is a command with one argument, #1. When invoked, it typesets h,
the argument, and then i. In the definition of \vect, the argument #1 is delimited by < and >. When the command is invoked, the argument must be delimited the same way. So to typeset the vector
ha, bi, we invoke \vect with \vect<a,b>
15.1 User-defined commands 379
This looks somewhat like a vector, and the name \vect serves as a reminder. You have to be careful with delimited commands because the math spacing rules (see Section 7.2) do not hold in either the definition or the invocation. So if there is a space before #1, in the definition of \vect,
\def\vect< #1>{\langle#1\rangle} then $\vect<a,b>$ results in the error message ! Use of \vect doesn’t match its definition.
l.12 $\vect<a ,b>$
which is clear enough. If the space is on the other side of the #1, as in \def\vect<#1 >{\langle#1\rangle} the error message is slightly more confusing:
Runaway argument? a,b>$ ! Paragraph ended before \vect was complete. <to be read again>
\par
The moral is that if you use delimited commands, you must be very careful that each invocation exactly matches the definition.
In Example 3 of Section 15.1.2, we introduced a command with three arguments for typing congruences:
\newcommand{\congr}[3]{#1\equiv#2\pod{#3}} $\congr{a}{b}{\theta}$ produces a ≡ b (θ). This command is easy to remem-
ber, but it does not make the source file more readable. For that, we use a delimited command.
Let us redo the congruence example with a delimited command \def\congr#1=#2(#3){#1\equiv#2\pod{#3}} so that $\congr a=b(\theta)$ produces a ≡ b (θ). In the source document, the
formula \congr a=b(\theta) looks a bit like the typeset congruence and it is easier to read. I included this definition in the newlattice.sty command file (see Sec- tion 15.3).
There is only one catch. Suppose you want to typeset the formula
x=a ≡ b (θ)
If you type $\congr x=a=b(\theta)$, L A TEX typesets it as x ≡ a = b (θ). Indeed, x is delimited on the right by the first =, so L A TEX believes that the first argument is x.
380 Chapter 15 Customizing L A TEX The second argument is delimited by the first = and the left parenthesis, so it is a=b. In
such cases, you can help L A TEX find the correct first argument by enclosing it in braces: $\congr{x=a}=b(\theta)$ Here is our final example. In Section 5.3.1 we discuss the problem of typing a
command such as \TeX (the example there was \today) in the form \TeX\ so that it is typeset as a separate word. The problem is that if you type \TeX without the trailing \, TEX is merged with the next word, and there is no error message to warn you. One solution is to use a delimited command:
\def\tex/{\TeX} Now to get TEX, type \tex/. If a space is needed after it, type \tex/ . If you forget
the closing /, you get an error message.
A better solution to this problem is the use of the xspace package—provided you do not want to typeset something like TEXbook (see Section 15.1.1). However, many
documents use the delimited construct (including the AMS documentation), so you should be familiar with it.