3

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?

lockstep
  • 250,273
Andrew
  • 377
  • 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 05 '12 at 16:32

2 Answers2

7

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:

enter image description here

Notes:

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}
Peter Grill
  • 223,288
  • 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
  • 1
    @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
1

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

And of course LuaLaTeX offers arrays natively in Lua.