# get the value of the current counter

I'd like to recover the value of the current environment counter and store it for my own uses, e.g. in the example below I'd like to store (not print) the number 1. With \@currenvir I can get the name of the current environment. What is the most convenient way of getting the value of the counter of the environment that I'm in?

If I know that I'm in a lemma, like below, then the solution is easy, but I'm looking for a solution that is independent of the type of environment that I'm in.

(EDIT:) preferably something that works with the various packages that mess with the labeling system (hyperref, cleveref, autoref, etc.)

\documentclass{article}

\newtheorem{lemma}{Lemma}
\newcommand{\storecurrentlabelvalue}[1]{}

\begin{document}

\begin{lemma}
blabla\storecurrentlabelvalue{\mylabelvalue}
\end{lemma}
\end{document}

• You can't count (!) on the environment name to have the same like the counter – user31729 Nov 6 '15 at 14:32
• »the current environment counter« – not every environment has a counter associated! And it's \@currenvir. Something like the following? \edef\foo{\the\value{\@currenvir}} (or with \xdef for a global assignment) or \setcounter{mycounter}{\value{\@currenvir}} – cgnieder Nov 6 '15 at 14:39
• But you probably want \@currentlabel which holds the current label text – cgnieder Nov 6 '15 at 14:41
• @clemens: I rather believe it's the real counter value – user31729 Nov 6 '15 at 14:42
• I don't mind knowing both! – JPi Nov 6 '15 at 14:56

Not every environment has a counter and even if it has got a counter, there is no guarantee that the counter has the same name like the environment (and vice versa).

This uses \xpatchcmd{} to hack into \refstepcounter (which is the labelling one) and defines \mylabelvalue as \number\value{#1}, #1 being the counter name.

It does not interfere with hyperref etc.

\documentclass{article}

\usepackage{xpatch}
\newtheorem{lemma}{Lemma}

\xdef\mylabelvalue{}

\xpatchcmd{\refstepcounter}{%
\stepcounter{#1}%
}{%
\stepcounter{#1}%
\xdef\mylabelvalue{\number\value{#1}}%
}{\typeout{success}}{\typeout{failure}}

\begin{document}

\begin{lemma}
blabla
\end{lemma}

Value is \mylabelvalue

$$E = mc^2$$

$$E = mc^2$$

$$E = mc^2$$

Value is \mylabelvalue

\end{document}


Here is another version

It uses my assoccnt package and the command \LastRefSteppedCounter and there's \LastSteppedCounter too.

\documentclass{article}

\usepackage{assoccnt}
\newtheorem{lemma}{Lemma}

\begin{document}

\begin{lemma}
blabla
\end{lemma}

The last counter refstepped was \LastRefSteppedCounter\ and its value is  \number\value{\LastRefSteppedCounter}

\section{A section}

\section{Another section}

The last counter refstepped was \LastRefSteppedCounter\ and its value is  \number\value{\LastRefSteppedCounter}

\end{document}


Edit -- the successor package of assoccnt is xassoccnt and provides \LastCounterValue as new feature. I've uploaded xassoccnt v0.6 to CTAN (recently), current version is v1.5 already.

\documentclass{article}

\usepackage{xassoccnt}
\newtheorem{lemma}{Lemma}

\begin{document}

\begin{lemma}
blabla
\end{lemma}

\begin{lemma}
Other one
\end{lemma}

\begin{lemma}
Yet another one
\end{lemma}

The last counter refstepped was \LastRefSteppedCounter\ and its value is \LastCounterValue

\edef\storedlastvalue{\LastCounterValue}

\section{A section}

\section{Another section}

The last counter refstepped was \LastRefSteppedCounter\ and its value is \LastCounterValue

\subsection{Even deeper sectioning}

The last counter refstepped was \LastRefSteppedCounter\ and its value is \LastCounterValue\ contrary to \storedlastvalue

\end{document}


• +1 for the solution that uses the assoccnt package. – Mico Nov 6 '15 at 18:58
• By the way: congrats on passing the 50K reputation points mark!!! Very well earned!! – Mico Nov 6 '15 at 19:14
• @Mico: Thanks. Now I will edit your and egreg's answers (and his questions :-)) – user31729 Nov 6 '15 at 19:17
• Having unlimited editing rights has its advantages! Just don't get carried away... :-) – Mico Nov 6 '15 at 19:35
• Glad if I pushed you over the top; congrats regardless. – JPi Nov 7 '15 at 1:35