TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Possible Duplicate:
Definining commands/abbreviations that contain numbers

How can I define macros as \text1,\text2...\text3 which, when invoked will produce text with different widths?

share|improve this question

marked as duplicate by Lev Bishop, lockstep, Loop Space, Alan Munn, Taco Hoekwater Mar 12 '11 at 20:14

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

You definitively can't change the catcodes of 0-9 to letter, because TeX doesn't see them as numbers anymore. – Martin Scharrer Mar 12 '11 at 18:34
I'd agree on the dupe – Joseph Wright Mar 12 '11 at 18:47
up vote 13 down vote accepted

There are many possible ways you can achieve this:

1) Provided, you only going to have text0...text9 the easiest would be to trick your users that you are providing a command \textn, whereas in reality you are only providing a one parameter macro, defined as follows:


Note text is defined in amsmath so you will be overriding it.

2) You can define different environments for your users. A little known fact is that a LaTeX environment can use any combination of characters. These can be called as:


3) The most difficult method to employ is through the control sequences


This is a common technique for macro writers, especially if you want to automate the process and have an algorithm to automatically define these commands. The disantvantage of using this type of definition is that you will not be able to call such a macro directly. You will need to use \csname...\endcsname to call it as well. LaTeX provides the equivalent wrappers \@namedef and \@nameuse, which I have used as the last definition in the minimal below.

Additional factoids. I have used TeX's \narrower in the code to increase the left and right indentation in the text by one additional \leftskip and \rightskip for every \narrower.

I also used \usepackage[latin]{babel} in order to hyphenate Latin correctly, as during the first run of the MWE some words were an eye-sore! The Latin are produced by the lipsum package, which provides sample paragraphs for testing.

Lastly, I wouldn't recommend the use of such macro names as they are not semantically correct, does \text1 represent a quote, a section or something else? Rather spent some time to determine the major structural components of your document and define them with appropriate names.

          \marginpar{Type 1} 
            \marginpar{Type 2}
{\bgroup \leavevmode \narrower\narrower\narrower
 \marginpar{Type 1 text}}
{\egroup }
 \marginpar{Type 2 text}}
{\egroup }
\expandafter\def\csname Text1\endcsname{\leavevmode
          \marginpar{csname Type 1}}
          \marginpar{defined using @namedef Type 2}}
{\csname Text1\endcsname

share|improve this answer

The UK TeX FAQ features an entry about Non-letters in macro names. (One of the recommendations is to "choose another name for your macro, one that contains only ordinary letters".)

share|improve this answer

You can't define macros with numbers in their names, because all parts of the macro name must consists with character from the category letter (category code 11) and the 0-9 digits are from category other (category code 12). TeX seems to only recognize them as numbers when they are in this category so changing this would break things.

As already mentioned by Yiannis Lazarides you can however define a macro which takes the number as first argument. As long it is only one digit (0-9) this works without the braces! In the macro you can then use \ifcase or use internal macro which use Roman numbers:


\newcommand*{\test}[1]{\csname test\romannumeral#1\endcsname}


The internal macros do not need to take an argument.

An example for \ifcase would be:

    \relax % 0
    \or % 1
    \or % 2
    \or % 3
    \or % 8
    \or % 9
    \else % everything else
share|improve this answer

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