# How to create counters with index?

I need to create counters with names like mycounter1, mycounter2,... Basically this should be a counter-valued data array. The code like

\newcounter{index}
\newcounter{\csname mycounter\the\value{index}\endcsname}


does not work. It seems that it is impossible to use \csname...\endcsname for counter names. Using \expandafter with different variants also does not help. Is there a way to resolve this problem?

-
Could you give an example application? Perhaps there are more efficient ways to cope with the problem. –  egreg Sep 24 '12 at 17:15
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). –  Martin Schröder Oct 5 '12 at 16:32

Counters do not have a leading backslash which is what the \csname would put in it. So

\newcounter{mycounter\Alph{index}}


does the job. With counters it appears that numbers also work:

\newcounter{mycounter\arabic{index}}.


The MWE below yields:

## Notes:

• Even though you can have numerals in the counter names, it is not a good idea to have numbers in name of macros, although the second link here does provide a way to do that:

## Code:

\documentclass{article}

\newcounter{index}
\setcounter{index}{1}

\newcounter{mycounter\arabic{index}}
\newcounter{mycounter\Alph{index}}

\begin{document}
Counter\arabic{index} = \arabic{mycounter\arabic{index}}

Counter\Alph{index} = \arabic{mycounter\arabic{index}}
\end{document}

-
Thank you very much. It is easy and elegant! –  Andrew Sep 24 '12 at 16:44
Yes, also numbers work, because \newcounter{<string>} does \expandafter\newcount\csname c@<string>\endcsname. So even \newcounter{@#^&*({})} would work. :-) For renewing the \the... command associated to it, do \expandafter\renewcommand\csname @#^&*({})\endcsname{\alph{@#^&*({})}}. ;-) –  egreg Sep 24 '12 at 17:19
@egreg those are the names that LaTeX counters should have; I don't know why Lamport et al. chose such exotic names like page or chapter for counters ;-) –  Gonzalo Medina Sep 24 '12 at 20:10

There are the forarray and the arrayjobx packages for handling arrays with LaTeX.

And of course LuaLaTeX offers arrays natively in Lua.

-