5

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}
  • 1
    You can't count (!) on the environment name to have the same like the counter – user31729 Nov 6 '15 at 14:32
  • 2
    »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}} – clemens Nov 6 '15 at 14:39
  • 1
    But you probably want \@currentlabel which holds the current label text – clemens Nov 6 '15 at 14:41
  • 1
    @clemens: I rather believe it's the real counter value – user31729 Nov 6 '15 at 14:42
  • 1
    I don't mind knowing both! – JPi Nov 6 '15 at 14:56
6

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


\begin{equation}
   E = mc^2
\end{equation}


\begin{equation}
   E = mc^2
\end{equation}

\begin{equation}
   E = mc^2
\end{equation}


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}

enter image description here

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}

enter image description here

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

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.