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.

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?

share|improve this question
    
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
add comment

2 Answers

up vote 7 down vote accepted

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}
share|improve this answer
    
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
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
add comment

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

And of course LuaLaTeX offers arrays natively in Lua.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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