2

Possible Duplicate:
Definining commands/abbreviations that contain numbers
newcommand name cannot include numbers for example \Mycomand123

It seems like command names starting with a numeral are not valid when using \newcommand but are valid when using \def. Are there any reasons not to define commands starting with a numeral? For example:

\newcommand{\123MHz}{\SI{123}{MHz}\xspace}  % Causes errors
\def\123MHz{\SI{123}{MHz}}   % seems to work fine
10
  • 1
    they can not have a digit anywhere in the name, it is not just a restriction on starting with a digit. (It is possible to change the catcode of digit to 11 so they become letters then you can use them in names but you can not use them in lengths and other places.) Commented Sep 21, 2012 at 8:58
  • Is this only a restriction of \newcommand? This code works fine for \def.
    – Mike
    Commented Sep 21, 2012 at 8:59
  • 4
    Don't do it. Use only letters for command names. Your \def doesn't define \123MHz but \1 with special parameters. If you would add a definition for e.g. \145MHz the first definition will break. Commented Sep 21, 2012 at 9:01
  • 1
    @DavidCarlisle provides a good solution here at question 66666: newcommand name cannot include numbers for example \Mycomand123 for commands with numbers (although don't think they can start with a number). Commented Sep 21, 2012 at 9:11
  • 1
    @Mike: I would gave the numbers names that reflect their meaning. \pi is imho a much better command name than \3.14. Also in case you got the number wrong you won't have to change all the \123 to \132. Commented Sep 21, 2012 at 9:17

1 Answer 1

2

As far as I know, command names should use only letters, and as such, numbers are considered valid command delimiters.

For example:

\newcommand{\foo123}{blabla} % should raise an error
\newcommand{\foo}[1]{Argument is #1 ;} % typing \foo123 will be typeset as : "Argument is 1 ;23"

I can only assume the first definition is rejected by the compiler in order not to conflict with the second.

3
  • 1
    The first is rejected because it's syntactically incorrect. The first argument to \newcommand should be a backslash followed either by any number of letters (A-Za-z) or by a single nonletter.
    – egreg
    Commented Sep 21, 2012 at 9:09
  • @egreg: OP's question was about the reasons behind this syntactic restriction.
    – T. Verron
    Commented Sep 21, 2012 at 9:12
  • @t.verron should he perhaps be referred to the tex book? it was knuth's decision, so any rationalisation by us is probably useless. Commented Sep 21, 2012 at 9:56

Not the answer you're looking for? Browse other questions tagged .