Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
You can use \@namedef and \@nameuse. Or the package etoolbox provides similar commands. –  Marco Daniel Nov 10 '11 at 18:13
1  
Please see also: tex.ac.uk/cgi-bin/texfaq2html?label=linmacnames –  Marco Daniel Nov 10 '11 at 18:19
add comment

4 Answers

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.

share|improve this answer
add comment

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.

share|improve this answer
    
Thanks for the Roman numeral suggestion! I had been writing things like \fooOne, \fooTwo, \fooThree! –  JohnJamesSmith Nov 11 '11 at 4:29
    
@JohnJamesSmith - The idea is not mine: it is from plain.tex... –  Boris Nov 11 '11 at 4:31
add comment

There are two basic "flavors", to use Knuth's own terminology, of control sequences in TeX/LaTeX:

  1. 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.]
  2. 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.)

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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