4

I know that numbers cannot be used in macros, e.g., in \def/\newcommand definitions. Can anybody explain to me why this

\documentclass{article}

\def\i2{\frac{i}{2}}

\begin{document}
$\i2$
\end{document}

works?

enter image description here

1
  • Try to type in naïve and see what happens.
    – egreg
    Aug 12 at 7:52

1 Answer 1

9

From the TeX book, page 202:

TEX also allows you to define macros whose parameters are delimited in quite a general way; you needn’t always enclose arguments in braces. For example, \def\cs #1. #2\par{...} defines a control sequence \cs with two parameters, and its two arguments will be determined as follows: #1 will consist of all tokens between \cs and the next subsequent appearance of ‘. ’ (period and space); #2 will consist of all tokens between that ‘. ’ and the next \par token.

When you write

\def\i2{\frac{i}{2}}

you are not defining a macro i2 (I use a keyboard symbol to denote a control sequence token) but rather a delimited macro i which must always be followed by the number 2 (spaces are gobbled after \i in the usual way). Therefore from

\def\i2{i/2}
\i2\par
\i 2\par
\i3
\bye

you'll get i/2 twice, and an error

! Use of \i doesn't match its definition.
l.4 \i3

If you press h the help message says literally

If you say, e.g., `\def\a1{...}', then you must always
put `1' after `\a', since control sequence names are
made up of letters only. The macro here has not been
followed by the required stuff, so I'm ignoring it.

As egreg suggested, I add that using \def is always dangerous, because it does not check whether the macro is already defined. Simple example:

\documentclass{article}
\def\i2{\frac{i}{2}}
\begin{document}
Using \verb+\def+ is quite naïve\ldots
\end{document}

Running this code will also result in an error Use of \i doesn't match its definition. Why? The file ot1enc.dfu contains

\DeclareUnicodeCharacter{00EF}{\"\i}

(using a different font encoding works similarly), meaning that the Unicode character U+00EF (ï) is converted into \"\i, whose purpose is to put a dieresis \" onto a dotless i \i. But you've redefined \i to expect a 2 and to print a fraction! Chaos ensues.

Long story short: \defining macros with delimited arguments can be very useful (I use them a lot in my packages) but you really have to know what you are doing...


The preceding discussion holds under normal \catcode regime. You could of course change the \catcodeof 2 (or of all digits) from 12 (other) to 11 (letter) but, believe me, you don't want to do that...

You can define a macro i2 by something like

\expandafter\def\csname i2\endcsname{...}

but then of course you should write \csname i2\endcsname to use it...

3
  • I'd add the example of typing naïve that would also show why \def should be avoided.
    – egreg
    Aug 12 at 7:54
  • @egreg Added :-)
    – campa
    Aug 12 at 8:17
  • Can't upvote twice, sorry.
    – egreg
    Aug 12 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.