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.

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

3 Answers 3

up vote 12 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:

  \def\text#1{...}

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:

  \begin{text1}...\end{text1} 

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

  \expandafter\def\csname...\endcsname 

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.

\documentclass{article}
\usepackage{lipsum}
\usepackage[latin]{babel}
\begin{document}
\def\text#1{\ifnum#1=1
          \leavevmode
          \narrower\narrower\narrower\narrower 
          \marginpar{Type 1} 
       \else
          \ifnum#1=2 
            \leavevmode
            \narrower\narrower\narrower
            \narrower\narrower\narrower 
            \marginpar{Type 2}
          \else
            \relax
       \fi\fi}
{\text1
\lipsum[1]}
{\text2
\lipsum[1]}
\newenvironment{text1}
{\bgroup \leavevmode \narrower\narrower\narrower
 \marginpar{Type 1 text}}
{\egroup }
\newenvironment{text2}
{\bgroup\leavevmode 
 \narrower\narrower\narrower
 \narrower\narrower\narrower     
 \marginpar{Type 2 text}}
{\egroup }
\begin{text1}
\lipsum[1]
\end{text1}
\begin{text2}
\lipsum[1]
\end{text2}
\expandafter\def\csname Text1\endcsname{\leavevmode
          \narrower\narrower\narrower
          \marginpar{csname Type 1}}
\makeatletter
\@namedef{Text2}{\leavevmode
          \narrower\narrower\narrower
          \narrower\narrower\narrower
          \marginpar{defined using @namedef Type 2}}
{\csname Text1\endcsname
\lipsum[1]}
{\@nameuse{Text2}
\lipsum[1]}
\makeatother

\texttt{\meaning\narrower}
\end{document}
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:

\documentclass{article}

\newcommand*{\test}[1]{\csname test\romannumeral#1\endcsname}
\newcommand*{\testi}[1]{{\tiny#1}}
\newcommand*{\testii}[1]{{\scriptsize#1}}
\newcommand*{\testiii}[1]{{\footnotesize#1}}
\newcommand*{\testiv}[1]{{\small#1}}
\newcommand*{\testv}[1]{{{\normalsize#1}}
\newcommand*{\testvi}[1]{{\large#1}}
\newcommand*{\testvii}[1]{{\Large#1}}
\newcommand*{\testviii}[1]{{\LARGE#1}}
\newcommand*{\testix}[1]{{\Huge#1}}

\begin{document}
\par\test1{text}
\par\test2{text}
\par\test3{text}
\par\test4{text}
\par\test5{text}
\par\test6{text}
\par\test7{text}
\par\test8{text}
\par\test9{text}
\end{document}

The internal macros do not need to take an argument.

An example for \ifcase would be:

\newcommand*{\test}[1]{%
    \ifcase#1%
    \relax % 0
      \tiny
    \or % 1
      \scriptsize
    \or % 2
      ...
    \or % 3
      ...
      ...
      ...
    \or % 8
      ...
    \or % 9
      ...
    \else % everything else
      ...
    \fi
}
share|improve this answer

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