[Tex/LaTex] Extract correct counter value of a reference

counterscross-referencingxstring

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?

Best Answer

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.

enter image description here

\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}
Related Question