2

I'm trying to print a counter whose name I receive in the argument #1. I tried \the#1, which doesn't work and also tried the solution given in this answer, which gives me the error "You can't use \let after \the".

Is there any way I can do what I want?

Here's an example of what I want:

\newcommand{\printcounter}[1]{\expandafter\the\csname #1\endcsname}
\begin{document}
\printcounter{section} % this doesn't work
  • \expandafter\the#1? – Bernard Sep 30 '17 at 21:55
  • @Bernard: That tells me "You can't use 'the letter t' after \the" – Runemoro Sep 30 '17 at 21:56
  • 1
    Could you post a full code, that we can play with? – Bernard Sep 30 '17 at 21:57
  • It depends what you mean by "counter". \csname the#1\endcsname could work. If not make a complete example – Ulrike Fischer Sep 30 '17 at 21:59
  • @UlrikeFischer: A counter, like section, subsection, etc. – Runemoro Sep 30 '17 at 22:01
2

If you want the abstract numeric value:

\newcommand{\printcounter}[1]{\the\value{#1}}

and

\printcounter{section}

If you want the representation,

\thesection

or, for a generic interface

\newcommand{\printcounter}{\csname the#1\endcsname}

\printcounter{section}

What's the difference? Under standard settings, if we are at section 2, subsection 3, the first \printcounter{subsection} would print 3, the second one would print 2.3 (the current representation). Take your pick.

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.