7

I have a counterA and a counterB. I want to define a command \PrintCounter, which gets either A or B as argument, and then prints the value of the corresponding counter.

I wrote the following code:

\newcommand{\PrintCounter}[1]{
    \thecounter#1
}

which results in an error. I need a way to tell LaTeX to evaluate counter#1 before evaluating \the. Unfortunately, I couldn't make it work using \expandafter.

10

If \counterA and \counterB are \count registers, i.e. TeX counters, then you can use \csname ..\endcsname together with \expandafter:

\newcommand{\PrintCounter}[1]{%
    \expandafter\the\csname counter#1\endcsname
}

If they are however defined using \newcounter, i.e. LaTeX counters, then simply use \arabic{counter#1}.

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.