1

I tried to define indexed counters, such as x1, x2, . . . etc, by defining them like this : \newcount\x1, \newcount\x2, . . . etc, but it didn't work. Is there a workaround for this in TeX or LaTeX ? I have written a small tex-file that illustrates what happens when you try it. Note added : With the remark made by @cfr, that in LaTex one may use the command \newcounter{name} the question has a good answer that cannot easily be found by using the search engine. (I have some good reference books for TeX, but not for LaTeX.)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%  Example tex-file that illustrates what happens  %%
%%  when you use a number in the name of a counter  %%
%%                                                  %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%         

\documentclass{article}
\usepackage[paperwidth=0.75in, paperheight=1.5in]{geometry}
\pagestyle{empty}

\begin{document}

\newcount\a  \newcount\b \newcount\c7

\a=1   \b=41 \c7=78

\noindent
a = \number\a  
\par \vskip 5pt \noindent
b = \number\b  
\par \vskip 5pt \noindent 
c7 = \number\c7 

\end{document}
  • Since you are using LaTeX, wouldn't it be better to use e.g. \newcounter rather than \newcount? – cfr Nov 9 '14 at 21:08
  • 2
    @cfr then you could use digits:-) \newcounter{a1} is OK:-) – David Carlisle Nov 9 '14 at 21:09
  • I didn't know there was a variant. But that would indeed be the workaround. Thanks ! With the new answer it isn't a duplicate after all, then ? – user35145 Nov 9 '14 at 21:15
  • 1
    @user35145 It should be reworded to specifically mention counters, at least – Sean Allred Nov 9 '14 at 21:43
  • I extended my answer with specifics about counters as the question is reopened. – David Carlisle Nov 10 '14 at 12:30
4

tex parsing is controlled by "catcodes" (\catcode) the catcode of any character is changeable, but control sequence names can be any number of catcode 11 characters (letters) or a single non-letter so \"o for example is \"{o} not a command called "o digits are non-letters so you can have \1 but not \a1. It is possible to make digits letters by making them catcode 11, but many things will break so it's not a good idea except in very controlled environments.

Note that this restriction just applies to parsing of a name after the escape character (normally \).

\csname name with 1 and 2\endcsname

accesses the command with name name with 1 and 2 which has digits and spaces in its name, you just can't access it via \name with 1 and 2. In particular latex has some constructs such as environments that are always accessed via name so \begin{tabular*} internally uses the macro with name tabular* even though that may not be accessed via \tabular*. LaTeX counters though have a mixed design, they are allocated by name, so you can perhaps go

\newcounter{c7}

but they are used via command tokens; in particular the above creates a command with name thec7 that expands to the print form of the counter, but this can not be accessed via \thec7 but only via \csname thec7\endcsname so it is best to avoid such constructs and only use counter names that generate legal command names (that is, just consists of letters).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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