I experienced some problems when creating \newcommand
that have some numbers in their names; I can't find docs about that, about the accepted charset for the commands in LaTeX.
4 Answers
You should use only the letters A-Za-z in the user commands. Commands defined in .sty file (or inside \makeatletter
/\makeatother
pair) may include @.
If you need numbers for a set of related commands, the usual practice is to use roman numerals, like \fooi
, \fooii
, \fooiii
, \fooiv
, etc.
-
Thanks for the Roman numeral suggestion! I had been writing things like
\fooOne
,\fooTwo
,\fooThree
! Nov 11, 2011 at 4:29 -
A command name can consist either of one non alphabetic character or a sequence of alphabetic (A-Za-z
) characters.
Actually the rule is a bit more complicated, but as far as the final user is concerned, this is can be considered the truth.
So you can't say \newcommand{\a1}{...}
because the name a1
is not a legal command name. Macho TeX programmers know ways to overcome that restriction, but it would be ininfluent for the final user that wouldn't be able to say \a1
, as distinct from \a232
, anyway.
If you have in mind some application of command names with numbers, please modify your question so that somebody can suggest you a better alternative.
There are two basic "flavors", to use Knuth's own terminology, of control sequences in TeX/LaTeX:
- A control symbol, consisting of a backslash,
\
, followed by a single nonletter character. Examples:\\
(newline),\'
(sharp accent in text mode). [For you TeX purists out there: I know that, strictly speaking, it's an "escape character" that starts the control sequence, not necessarily a backslash. However, it is near-universal practice to use the backslash character, and only the backslash character, as the escape character.] - A control word, consisting of a backslash and one or more alphabetic characters (i.e., no numerals, no other non-alphabetic characters). Examples:
\textbackslash
,\def
. Note that TeX and LaTeX are case-sensitive, hence\hello
and\Hello
won't be the same commands.
Control words may be further differentiated as those that include one or more "at", @
, characters and those that do not. The former control words are (and should be) used mainly by TeX and LaTeX programmers for macros and variables which users should not access or manipulate directly; this is achieved by making the @
character "special", in a well-defined sense. However, when one must change some existing macro that contains one or more @
characters, one can do so by first typing \makeatletter
to suspend the "specialness" of the @
character, performing whatever further instructions that are required, followed by a \makeatother
instruction to restore the "normal" behavior of the @
symbol.
Given the preceding explanations, it's clear that what you've run into is the fact that you should not be using numerals (or any other other nonletter characters) in a control word. (If you think you simply must use some numbers in the macro, you can write them out as, say, \choosefirstoftwo
, \numberten
, etc.)
You can only use characters in macro names which are officially marked as letters. By default only real letters a-z and A-Z are marked this way.
You can however, change this by changing the category code (catcode) for the character to 11 (letter). This is done to the @
character for all packages and class files. In user documents you can do this manually using \makeatletter
and switch back to normal using \makeatother
after you have defined all required macros. This is done to keep internal macros untouchable in normal user document to avoid accidental name clashes.
You could change the catcode of virtually any character to 11 and then use it in a macro name, but this may cause issues. (The LaTeX3 project for example uses :
and _
.) The best thing is to stick with normal letters and use 'a', b', etc. or roman numbers instead of numbers.
\@namedef
and\@nameuse
. Or the package etoolbox provides similar commands.