# How do I determine the name of the counter associated with a theorem-like environment

(A question related to this one and leading up to this one.)

Suppose I'm using ntheorem. I would like to have a command which determines the name (not the value) of the counter associated with a certain theorem-like environment. Thus, the following:

\documentclass{article}
\usepackage{ntheorem}
\newtheorem{theorem}{Theorem}[section]
\newtheorem{prop}[theorem]{Proposition}
\begin{document}
Propositions use the counter \getenvcounter{prop} while theorems use \getenvcounter{theorem}.
\end{document}


Should produce

Propositions use the counter theorem while theorems use theorem.

I can tolerate the use of cleveref if necessary...

-

## 1 Answer

\makeatletter
\def\getenvcounter#1{%
\expandafter\expandafter\expandafter\@getenvcounter\csname mkheader@#1\endcsname}
\def\@getenvcounter\csname#1\endcsname#2#3#4#5{#4}
\makeatother


Explanation: \show\prop gives

> \prop=macro:
->\let \thm@starredenv \@undefined \csname mkheader@prop\endcsname .


Then \show\mkheader@prop gives

> \mkheader@prop=macro:
->\csname setparms@prop\endcsname \@thm {prop}{theorem}{Proposition}.


So I expand twice \csname mkheader@#1\endcsname, which gives the token list shown last and define \@getenvcounter accordingly.

The version for the LaTeX kernel definition (kept by amsthm) would be

\makeatletter
\def\getenvcounter#1{%
\expandafter\expandafter\expandafter\@getenvcounter\csname#1\endcsname}
\def\@getenvcounter#1#2#3{#2}
\makeatother


## Common version

If you prefer a version that doesn't depend on package loading, here it is:

\makeatletter
\def\getenvcounter#1{%
\@ifundefined{mkheader@#1}
{\expandafter\expandafter\expandafter\@getenvcounterkernel\csname#1\endcsname}
{\expandafter\expandafter\expandafter\@getenvcounterntheorem\csname mkheader@#1\endcsname}}
\def\@getenvcounterkernel#1#2#3{#2}
\def\@getenvcounterntheorem\csname#1\endcsname#2#3#4#5{#4}
\makeatother


## Version supporting also thmtools

\makeatletter
\def\getenvcounter#1{%
\@ifundefined{mkheader@#1}
{\expandafter\expandafter\expandafter\@getenvcounterkernel
\csname\ifcsname thmt@original@#1\endcsname thmt@original@\fi#1\endcsname}
{\expandafter\expandafter\expandafter\@getenvcounterntheorem\csname mkheader@#1\endcsname}}
\def\@getenvcounterkernel#1#2#3{#2}
\def\@getenvcounterntheorem\csname#1\endcsname#2#3#4#5{#4}
\makeatother

-