# Defining commands/abbreviations that contain numbers


• I think it is important to know what other commands \H... you're going to need. Is it as in Jan's answer? Then that one's perfect for you. If not, can you add some explanation to your question? – Hendrik Vogt Jan 26 '11 at 12:22
• Jan's answer works well for many of my use cases; it looks to me like other solutions are cumbersome or have side effects. I often use variables/identifiers with numbers when I compute things, and it is annoying that I cannot reuse these names in LaTeX -- that was my main motivation for asking the question. For the time being I think I'll stick with either not using numbers, or using command parameters as per Jan's suggestion. – Leo Alekseyev Jan 27 '11 at 5:33
• It would be really interesting for me to see other command with numbers that you'd like to use. – Hendrik Vogt Jan 27 '11 at 10:32
• @Hendrik: how about H2O? :) (I am not a chemist, so I actually don't care much about that one.) In truth, almost all my use cases involve subscripts and superscripts -- perhaps used as powers or to indicate matrix elements. So \newcommand parameters will work well. – Leo Alekseyev Jan 27 '11 at 18:26
• That's a good one, thanks. One could handle such cases, but it wouldn't be nice. Good that you mostly want sub- und superscripts only! – Hendrik Vogt Jan 27 '11 at 21:16

Defining commands with parameters would be the way I would deal with this.

\newcommand{\H}[1]{H^{(1)}_{#1}}


defines a command with one parameter. You can then do \H0, \H1, ..., \H9. For more than one digit, you will have to use it as \H{10}.

• Although this does address the particular example, a problem is that you may want the parameter to be omitted when it has a particular value, e.g. you want to write H2O but if 2 is 1 instead you want to write HO not H1O. Is there a way to achieve that with this approach? Sorry if this should be a different question. – kon psych May 22 '17 at 22:27
• @MaartenBodewes Thanks! – Jan Hlavacek Dec 13 '19 at 5:48

This faq answer discusses this issue, criticises 4 possible ways of using such a macro and ultimately comes to the conclusion don't do that.

New LaTeX users are often suprised that macro definitions containing non-letters, such as

\newcommand{\cul8r}{Goodbye!}


fail to compile. The reason is that the TeX macro language, unlike most programming languages, allows nothing but letters in macro names.

There are a number of techniques for defining a macro with a name like \cul8r. Unfortunately, none of the techniques is particularly good:

1. Use \csname\endcsname to define and invoke the macro:

\expandafter\newcommand\csname cul8r\endcsname{Goodbye!}
I said, \csname cul8r\endcsname''.

• Pro: No unexpected side effects
• Con: So verbose as to be unusable
2. Define a "special-command generator", and use the resulting commands:

\newcommand{\DefineRemark}[2]{%
\expandafter\newcommand\csname rmk-#1\endcsname{#2}%
}
\newcommand{\Remark}[1]{\csname rmk-#1\endcsname}
...
\DefineRemark{cul8r}{Goodbye!}
...
\Remark{cul8r}

• Pro: Straightforward to use, not too untidy
• Con: It's hardly doing what we set out to do (experts will see that you are defining a macro, but others likely won't)
3. Convince TeX that 8 is a letter:

\catcode8 = 11
\newcommand{\cul8r}{Goodbye!}
I said, \cul8r''.

• Pro: \cul8r can be used directly
• Con: Likely to break other uses of 8 (such as numbers or dimensions; so \setlength{\paperwidth}{8in} tells us:

! Missing number, treated as zero.
8


As a general rule, changing category codes is something to use in extremis, after detailed examination of options. It is conceivable that such drastic action could be useful for you, but most ordinary users are well advised not even to try such a technique.

4. Define a macro \cul which must always be followed by 8r:

\def\cul8r{Goodbye!}
I said, \cul8r''.

• Pro: \cul8r can be used directly
• Con #1: Breaks if \cul is followed by anything other than 8r, with a confusing diagnostic — \cul99 produces:

! Use of \cul doesn't match its definition.
<*> \cul9
9


(which would confuse someone who hadn't even realised there was a definition of \cul in the document).

• Con #2: Silently redefines existing \cul, if any; as a result, the technique cannot be used to define both a \cul8r and, say, a \cul123 macro in the same document.

Technique 3 is in fact commonly used — in a limited form — within most LaTeX packages and within LaTeX itself. The convention is to use @ within the names of internal macros to hide them from the user and thereby prevent naming conflicts. To this end, LaTeX automatically treats @ as a letter while processing classes and packages and as a non-letter while processing the user's document. The key to this technique is the separation: internally a non-letter is used for macro names, and the user doesn't see anything of it, while the status remains "frozen" in all the definitions created within the class or package. See \@ and @ in macro names for more information.

Note that analogous use of technique 3 in this example would give us

\begingroup
\catcode8 = 11
\gdef\cul8r{Goodbye!}
\gdef\later{\cul8r}
\endgroup
I said, \later''.


which works, but rather defeats the object of the exercise. (\later has the "frozen" catcode for "8", even though the value has reverted to normal by the time it's used; note, also, the use of the primitive command \gdef, since \newcommand can't make a macro that's available outside the group.)

Recommendation: Either choose another mechanism (such as \DefineRemark above), or choose another name for your macro, one that contains only ordinary letters. A common approach is to use roman numerals in place of arabic ones:

\newcommand{\culVIIIr}{Goodbye!}


which rather spoils the intent of the joke implicit in the example \cul8r!

In some cases, using Roman rather than Arabic numerals may help: \HI, \HII, \HIII, \HIV. This is also mentioned in Lev Bishop's link and helped in my case.

A bad hack (Jan's answer above is much better) :-):

\newcommand{\HH}{\afterassignment\HH@aux\count0=}
\newcommand{\HH@aux}{H^{(1)}_{\the\count0}}


between the usual \makeatletter and \makeatother. If you want to define the commands for various numbers separately, you can do

\makeatletter
\newcommand{\GG}{\afterassignment\GG@aux\count0=}
\newcommand{\GG@aux}{\csname GG\the\count0\endcsname}
\makeatother


and define the various \GG0, \GG1... as

\expandafter\newcommand\csname GG0\endcsname{%
Definition of GG0.}
\expandafter\newcommand\csname GG1\endcsname{%
Definition of GG1.}

\GG0 \GG1 \GG2 ...


Note that when we call \GG followed by 2, we don't get any error/warning: it is just ignored. This method could seem good, but it has many drawbacks, failing in unexpected cases. For instance, if \foo is say, 123, then

\GG1\foo...


will be seen as \GG1123, not the expected behaviour.

You can use \usepackage{numdef}:

http://web.archive.org/web/20130727172127/http://www.elec.ryukoku.ac.jp/~fujii/pub/ftp/incoming/styles/kth.se/numdef.sty

You can define \num\def\x1{x one} and use it as \x1.

Hope this helps! VIKI

• I get ! LaTeX Error: File numdef.sty'' not found. – Viesturs Dec 17 '17 at 14:55
• @Viesturs you've got to create a numdef.sty file that contains the code in the link provided. – Edoardo Serra May 16 '19 at 7:49

As a variable name cannot contain numbers or other symbols, the LaTex assignment is clearly incorrect: \setlength{\myT1Col1}{0.2\columnwidth}.

Here is how I propose to handle the problem (when I get around to it):

I will use numbers and underscore in variable names but with a switch \myVar. So the above statement would be written:

\setlength{\myVar{myT1_Col1}}{0.2\columnwidth}.

Next, I will preprocess the LaTex file (using my own code in ‘C’) replacing the numbers and underscores with some suitable alphabetical characters so that the above line becomes

\setlength{\myTOneUSColOne}{0.2\columnwidth}.

The problem still is that if I need to share the LaTex file, I shall have to send the preprocessed version, which does not help make things clear for others.

Wouldn’t it be great it a LaTeX Guru wrote a package to do the preprocessing?

• This looks to me more like a new question than like an answer to the question. – campa May 23 '18 at 14:46
• I felt it was related to the problem faced by the OP. I have proposed a possible solution. – Raviharry May 23 '18 at 14:50
• Welcome to TeX.SX! A tip: You can use backticks ` to mark your inline code as I did in my edit. – dexteritas May 23 '18 at 15:06