# Extract correct counter value of a reference

Somewhere in my document there is a theorem labelled theoremA that has the number 2.21. Later, I want to set another counter to that specific number 21.

First, I tried \setcounterref{newcounter}{reference} which does not work as I expected it to: theoremA has the counter value 2.21 and \setcounterref sets newcounter to 2 instead of 21.

To fix this, I tried \setcounter{theorem}{\StrBehind{\getrefnumber{orbitcomeagre}}{.}} which yields the error Missing number, treated as zeroeven though \StrBehind{\getrefnumber{orbitcomeagre}}{.}has the correct value of 21.

Does anyone have any suggestions how I can fix this?

• There's an important distinction to make:\thetheorem produces "2.21", but \value{theorem} produces "21". – Mico May 28 '16 at 18:19
• @Mico: True, but this is only correct (after and) close to the particular theorem, not as a reference in a completely different place in the document where other theorems may precede it. – Werner May 28 '16 at 19:11
• @Werner - Correct. The upshot, in my view, is that if the OP wants to store the value of the theorem counter for later re-use, it's most straightforward to do so by using \value{theorem} immediately after the theorem in question. – Mico May 28 '16 at 19:22

Maybe I'm missing an important complication, but I think all you need to do is (a) define a "scratch" counter variable named, say, scratchcounter, in the preamble and (b) use an instruction such as

\setcounter{scratchcounter}{\value{theorem}}


immediately after the theorem in question to store the number of the theorem (minus the "prefix") in the counter named scratchcounter. Its value may be accessed elsewhere in the document via \thescratchcounter.

\documentclass{article}
\usepackage{amsthm} % or: "\usepackage{ntheorem}"
\newtheorem{theorem}{Theorem}[section]

\newcounter{scratchcounter} % define the scratch counter

\begin{document}
\setcounter{section}{2}   % just for this example
\setcounter{theorem}{20}

\begin{theorem}[Pythagoras] \label{thm:pyth}
$a^2+b^2=c^2$.
\end{theorem}
\setcounter{scratchcounter}{\value{theorem}} % Store value of 'theorem' counter

\bigskip\noindent
Later on \dots\ the value of the scratch'' counter is \thescratchcounter.
\end{document}


The following solution implements \extractthmnum{<cnt>}{<ref>} in a similar way to refcount's \setcounterref:

\documentclass{article}

\newtheorem{theorem}{Theorem}[section]
\newcounter{mycount}

\makeatletter
\def\@extractthmnum#1.#2{#2}
\newcommand{\extractthmnum}[2]{% \extractthmnum{<cnt>}{<ref>}
\setcounter{#1}{0}% Default
\ifcsname r@#2\endcsname
\edef\@tempa{\csname r@#2\endcsname}% Extract complete reference
\edef\@tempa{\expandafter\@firstoftwo\@tempa}% Extract number
\setcounter{#1}{\expandafter\@extractthmnum\@tempa}% Strip number
\fi
}
\makeatother

\begin{document}

\setcounter{section}{1}
\section{A section}

\setcounter{theorem}{20}
\begin{theorem}\label{thm:theorem}
A theorem.
\end{theorem}

See Theorem~\ref{thm:theorem}.

\extractthmnum{mycount}{thm:theorem}%
See Theorem~\themycount.

‎\end{document}‎


The idea of \StrBehind is good, but you have to use it in a different way.

\StrBehind{\getrefnumber{orbitcomeagre}}{.}[\orbitcomeagrenumber]
\setcounter{theorem}{0\orbitcomeagrenumber}


The 0 is for avoiding issues when the reference is not yet defined and \StrBehind would output nothing.

\documentclass{article}

\usepackage{xstring,refcount}

\newtheorem{theorem}{Theorem}[section]
\newcounter{mycount}

\begin{document}

\setcounter{section}{1}
\section{A section}

\setcounter{theorem}{20}

\begin{theorem}\label{orbitcomeagre}
A theorem.
\end{theorem}

See Theorem~\ref{orbitcomeagre}.

\StrBehind{\getrefnumber{orbitcomeagre}}{.}[\orbitcomeagrenumber]
\setcounter{mycount}{0\orbitcomeagrenumber}

See Theorem~\themycount.

\end{document}


An “abstract” version:

\documentclass{article}

\usepackage{xstring,refcount}

\newtheorem{theorem}{Theorem}[section]

\newcounter{mycount}

\newcommand{\setcountertoref}[2]{%
\begingroup
\StrBehind{\getrefnumber{#2}}{.}[\setcountertoreftemp]%
\setcounter{#1}{0\setcountertoreftemp}%
\endgroup
}

\begin{document}

\setcounter{section}{1}
\section{A section}

\setcounter{theorem}{20}

\begin{theorem}\label{orbitcomeagre}
A theorem.
\end{theorem}

See Theorem~\ref{orbitcomeagre}.

\setcountertoref{mycount}{orbitcomeagre}

See Theorem~\themycount.

\end{document}‎