提取引用的正确计数器值

提取引用的正确计数器值

我的文档中某处有一个定理标记theoremA为带有数字2.21。稍后,我想将另一个计数器设置为该特定数字21

首先,我尝试了,\setcounterref{newcounter}{reference}但它没有按我预期的那样工作:theoremA具有计数器值2.21\setcounterref设置newcounter2而不是21

为了修复这个问题,我尝试了,虽然的值正确,但还是\setcounter{theorem}{\StrBehind{\getrefnumber{orbitcomeagre}}{.}}会出现错误。Missing number, treated as zero\StrBehind{\getrefnumber{orbitcomeagre}}{.}21

有人对我如何修复此问题有什么建议吗?

答案1

也许我忽略了一个重要的复杂因素,但我认为您需要做的就是(a)scratchcounter在序言中定义一个名为的“临时”计数器变量,以及(b)使用如下指令

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

紧接着所讨论的定理,将定理的编号(减去“前缀”)存储在名为 的计数器中scratchcounter。其值可通过 访问文档中的其他地方\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}

答案2

这个想法\StrBehind很好,但是你必须以不同的方式使用它。

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

0是为了避免在引用尚未定义且\StrBehind不会输出任何内容时出现问题。

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

在此处输入图片描述

“抽象”版本:

\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}‎

答案3

以下解决方案的\extractthmnum{<cnt>}{<ref>}实现方式与refcount\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}‎

相关内容