暂时修改计数器在现有环境中的显示方式

暂时修改计数器在现有环境中的显示方式

我的文档已将假设编号为 1.1、1.2 等。在文本的某个时候,我想定义先前陈述的假设的变体,比如假设 1.2,并将其计数器显示为 1.2b。

我试图通过定义一个新assumptionb环境来实现这一点,该新环境\theassumption通过使用作为参数传递的标签来获取正确的假设计数器值并将其附加b到它来重新定义。

这是我的尝试:

\documentclass{article}

\newtheorem{assumption}{Assumption}[section]

\usepackage{refcount}  % used to refer to a past assumption by label
\newcounter{assumptionbkup}
\newenvironment{assumptionb}[1]{%
  \setcounter{assumptionbkup}{\value{assumption}}  % save current value of Assumption counter
  \let\oldtheassumption\theassumption  % the current way assumption numbers are displayed
  \setcounterref{assumption}{#1}       % set value of Assumption counter based on reference
  \renewcommand{\theassumption}{{\oldtheassumption}b}
  \begin{assumption}
}{%
  \end{assumption}
  \setcounter{assumption}{\value{assumptionbkup}}  % restore value of Assumption counter
  \let\theassumption\oldtheassumption  % restore how assumption numbers should be displayed
}

\begin{document}

\section{Some section}

\begin{assumption}  % will display as Assumption 1.1
  \label{asm:1}
  First assumption.
\end{assumption}

\begin{assumption}  % will display as Assumption 1.2
  \label{asm:2}
  Second assumption.
\end{assumption}

\begin{assumption}  % will display as Assumption 1.3
  \label{asm:3}
  Third assumption.
\end{assumption}

\begin{assumptionb}{asm:3}  % should display as Assumption 1.3b
  First variant.
\end{assumptionb}

\end{document}

结果是,无论我将什么标签作为参数传递给assumptionb,变体假设都显示为“假设 1.2b”。此外,在环境之前会显示“.3”:

在此处输入图片描述

“.3”大概是指的参数,assumptionb但我不明白为什么它会被显示。

我希望assumptionb环境友好,\usepackage[nameinlink]{cleveref}以便\cref{asm:2b}显示“假设 1.2b”全部超链接。

答案1

您可以用一种更简单的方式来管理它:只需重新定义\theassumption并取消其效果,\refstepcounter以避免增加原始assumption计数器。

在此处输入图片描述

\documentclass{article}

\newtheorem{assumption}{Assumption}[section]

\usepackage{hyperref}
\usepackage[nameinlink]{cleveref}

\makeatletter
\newenvironment{assumptionb}[1]{%
  \renewcommand{\theassumption}{\ref*{#1}b}%
  \renewcommand{\@currentlabel}{\protect\ref*{#1}b}% Update reference stored
  \renewcommand{\refstepcounter}[1]{}% Remove functionality of \refstepcounter
  \csname phantomsection\endcsname
  \begin{assumption}
}{%
  \end{assumption}
}
\makeatother

\begin{document}

\section{Some section}

\begin{assumption}[abc]  % will display as Assumption 1.1
  \label{asm:1}
  First assumption.
\end{assumption}

\begin{assumption}  % will display as Assumption 1.2
  \label{asm:2}
  Second assumption.
\end{assumption}

\begin{assumption}  % will display as Assumption 1.3
  \label{asm:3}
  Third assumption.
\end{assumption}

\begin{assumptionb}{asm:3}  % should display as Assumption 1.3b
  \label{asm:b}
  First variant.
\end{assumptionb}

See \ref{asm:b}.

\begin{assumption}  % will display as Assumption 1.4
  \label{asm:4}
  Fourth assumption.
\end{assumption}

\end{document}

相关内容