如何引用显示部分编号的自定义计数器?

如何引用显示部分编号的自定义计数器?

我正在尝试使用软件包proof附带的环境amsthm。我希望proof环境被编号,例如theorem为了添加标签的环境,所以我在序言中添加了一个自定义计数器,并更改了环境\proofname中的嵌入proof。一切都运行良好,并在文本中正确显示我的计数器。但是,当我尝试引用我的证明时,只会显示证明计数器,而不是完整的计数器。

我的代码如下:

\documentclass[12pt,letterpaper]{article}

\usepackage{amsthm}
\usepackage{hyperref}

\newcounter{proof}[section] % adds a new counter for the proof environment included in the amsthm package that restarts for every new section
\renewcommand{\theproof}{\thesection.\arabic{proof}} % adds the section number before your proof counters
\renewcommand{\proofname}{\refstepcounter{proof}Proof \theproof} % the \proofname was embedded in your proof environment to output the italicized 'Proof', which is displayed at the beginning of every proof. This changes the \proofname to print 'Proof' and \theproof, which we defined as the section-proof counter above. This adds a counter to your proofs so you can hyperref them.

\newtheorem{definition}{Definition}[section]


\begin{document}
\section{One}
\begin{proof}
\label{p1}
this is proof one.
\end{proof}

This should reference proof \ref{p1}, which should display as proof 1.1.
\begin{definition}
\label{d1}
this is definition one.
\end{definition}
As we can see, this is not a problem when I reference definition \ref{d1}.

\section{Two}
\begin{proof}
\label{p2}
this is proof two.
\end{proof}
This should reference proof \ref{p2}, which should display as proof 2.1.

\begin{definition}
\label{d2}
this is definition two.
\end{definition}
As we can see, this is not a problem when I reference definition \ref{d2}.

\end{document}

但这是我得到的结果:

在此处输入图片描述

答案1

您的\refstepcounter是在 的可选参数中发出的,\item而 的对应值在 被处理\@currentlabel后就会被遗忘\item,因此您实际上获得了对节号的引用。您可以通过单击引用来检查它。

一种可能的解决方案是修补\proof(进入环境时执行的命令)以在之后和之前proof发出。\refstepcounter\trivlist\item

\documentclass[12pt,letterpaper]{article}

\usepackage{amsthm}
\usepackage{xpatch}
\usepackage{hyperref}

\newcounter{proof}[section]
\renewcommand{\theproof}{\thesection.\arabic{proof}}
\renewcommand{\proofname}{Proof \theproof}

\xpatchcmd{\proof}{\trivlist}{\trivlist\refstepcounter{proof}}{}{}

\newtheorem{definition}{Definition}[section]

\begin{document}
\section{One}
\begin{proof}
\label{p1}
this is proof one.
\end{proof}

This should reference proof \ref{p1}, which should display as proof 1.1.
\begin{definition}
\label{d1}
this is definition one.
\end{definition}
As we can see, this is not a problem when I reference definition \ref{d1}.

\section{Two}
\begin{proof}
\label{p2}
this is proof two.
\end{proof}
This should reference proof \ref{p2}, which should display as proof 2.1.

\begin{definition}
\label{d2}
this is definition two.
\end{definition}
As we can see, this is not a problem when I reference definition \ref{d2}.

\end{document}

在此处输入图片描述

您还可以通过在修补之前proof*保存一份副本来定义未编号证明的环境。\proof

\documentclass[12pt,letterpaper]{article}

\usepackage{amsthm}
\usepackage{xpatch}
\usepackage{hyperref}

% save \proof
\let\unnumberedproof\proof
\newenvironment{proof*}
 {\renewcommand\proofname{Proof}\unnumberedproof}
 {\endproof}

\newcounter{proof}[section]
\renewcommand{\theproof}{\thesection.\arabic{proof}}
\renewcommand{\proofname}{Proof \theproof}

\xpatchcmd{\proof}{\trivlist}{\trivlist\refstepcounter{proof}}{}{}

\newtheorem{definition}{Definition}[section]

\begin{document}
\section{One}
\begin{proof}
\label{p1}
this is proof one.
\end{proof}

This should reference proof \ref{p1}, which should display as proof 1.1.
\begin{definition}
\label{d1}
this is definition one.
\end{definition}
As we can see, this is not a problem when I reference definition \ref{d1}.

\section{Two}
\begin{proof}
\label{p2}
this is proof two.
\end{proof}
This should reference proof \ref{p2}, which should display as proof 2.1.

\begin{definition}
\label{d2}
this is definition two.
\end{definition}
As we can see, this is not a problem when I reference definition \ref{d2}.

\begin{proof*}
An unnumbered proof.
\end{proof*}

\end{document}

答案2

\@currentlabel您可以在验证环境开始时通过定义来手动设置标签值。

xpatch可以使用提供 (xpatch pre to command) 宏的包在命令开头添加内容\xpretocmd。这对于环境也同样适用,类似 proof 的环境定义了两个命令,一个命令通过 执行,\begin{proof}内部调用\proof,另一个命令通过 执行,\end{proof}内部调用\endproof

修补环境允许您使用常规\label命令,而不是新定义的标签命令。

因为名称中\@currentlabel有一个符号,所以重新定义应该用和括起来。@\makeatletter\makeatother

梅威瑟:

\documentclass[12pt,letterpaper]{article}

\usepackage{amsthm}
\usepackage{xpatch}
\usepackage{hyperref}
\makeatletter
\xpretocmd{\proof}{\def\@currentlabel{\theproof}}{}{}
\makeatother

\newcounter{proof}[section] % adds a new counter for the proof environment included in the amsthm package that restarts for every new section
\renewcommand{\theproof}{\thesection.\arabic{proof}} % adds the section number before your proof counters
\renewcommand{\proofname}{\refstepcounter{proof}Proof \theproof} % the \proofname was embedded in your proof environment to output the italicized 'Proof', which is displayed at the beginning of every proof. This changes the \proofname to print 'Proof' and \theproof, which we defined as the section-proof counter above. This adds a counter to your proofs so you can hyperref them. 

\newtheorem{definition}{Definition}[section]

\begin{document}
\section{One}
\begin{proof}
\label{p1}
this is proof one.
\end{proof}

This should reference proof \ref{p1}, which should display as proof 1.1.
\begin{definition}
\label{d1}
this is definition one.
\end{definition}
As we can see, this is not a problem when I reference definition \ref{d1}.

\section{Two}
\begin{proof}
\label{p2}
this is proof two.
\end{proof}
This should reference proof \ref{p2}, which should display as proof 2.1.

\begin{definition}
\label{d2}
this is definition two.
\end{definition}
As we can see, this is not a problem when I reference definition \ref{d2}.

\end{document}

结果:

在此处输入图片描述

答案3

这是一种可行的方法,但我不确定幕后发生了什么。我改编了以下已接受的答案:这个问题,虽然逐字复制它会proof在环境开始时增加计数器一次,在定义标签时增加一次,从而产生偏移编号,所以每次调用标签时我只是减少计数器。

定义计数器后proof,添加以下行:

\newcommand{\prooflabel}[1]{\addtocounter{proof}{-1}\refstepcounter{proof}\label{#1}}

然后,只要您有证明,\label请使用\prooflabel下面的方法,而不是 。

\section{One}
\begin{proof}
\prooflabel{p1}
this is proof one.
\end{proof}

This should reference proof \ref{p1}, which should display as proof 1.1.

结果如下:

在此处输入图片描述

也许比我更有知识的人可以调整\prooflabel和其他定义,这样就不会出现这种来回计算,但它似乎可以按照您希望的方式工作。

答案4

查看 -environment 的代码proof可发现,\proofname只有在未提供可选参数的情况下才会执行。在这种情况下,作为a\proofname的可选参数的一部分执行。因此,在本地范围内执行。来自执行的将重新定义,但重新定义也将限于该本地范围。\trivlist\item\proofname\refstepcounter\proofname\@currentlabel

我强烈建议不要重新定义,\proofname因为包和 LaTeX 基础设施依赖于它仅保存用于引入证明的单词。

我建议重新定义proof-environment,以便可选参数的默认值不是\proofname另一个宏,而是调用未修改的宏并\proofname调用\refstepcounter然后“全球化” \@currentlabel

如果您这样做,将可选参数传递给proof-environment 仍然不会触发计数器的递增。

您可以用作\proofname可选参数以便像往常一样获得未编号的证明。

\documentclass[12pt,letterpaper]{article}
\usepackage{xpatch}
\usepackage{amsthm}
\usepackage{hyperref}

\makeatletter
\newcounter{proof}[section]%
\renewcommand{\theproof}{\thesection.\arabic{proof}}%
\DeclareRobustCommand\prooflabel{%
  \refstepcounter{proof}%
  \global\let\@currentlabel\@currentlabel
  \lowercase\expandafter{%
    \expandafter\protected@xdef
    \expandafter\@currentlabelname\expandafter{\proofname\protect\nobreakspace\theproof}%
  }%
  \proofname~\theproof
}%
\xpatchcmd{\proof}{\itshape}{\itshape\gdef\@currentlabelname{#1}\csname phantomsection\endcsname}{}{}%
\expandafter\def\expandafter\proof\expandafter{%
  \expandafter\@protected@testopt\expandafter\proof\csname\string\proof\endcsname{\prooflabel}%
}%
\DeclareRobustCommand\LcUcfork[1]{#1}
\makeatother

\newtheorem{definition}{Definition}[section]

\begin{document}

\section{One}
\begin{proof}
\label{p1.1}
This is proof one.
\end{proof}

\begin{proof}[\LcUcfork{M}y special proof]%
\label{special}%
This is my special proof.
\end{proof}

\begin{proof}[\proofname]%
This proof is not numbered.
\end{proof}

\begin{proof}
\label{p1.2}
This is proof two.
\end{proof}

This should reference proof \ref{p1.1}, which should display as proof 1.1.

This should reference \nameref{p1.1}, which should display as proof 1.1.

This should reference {\let\LcUcfork=\lowercase\nameref{special}}, which should display as my special proof.


\begin{definition}
\label{d1.1}
This is definition one.
\end{definition}
As we can see, this is not a problem when I reference definition \ref{d1.1}.

\section{Two}
\begin{proof}
\label{p2.1}
This is proof two.
\end{proof}
This should reference proof \ref{p2.1}, which should display as proof 2.1.

This should reference \nameref{p2.1}, which should display as proof 2.1.

\begin{definition}
\label{d2.1}
This is definition two.
\end{definition}
As we can see, this is not a problem when I reference definition \ref{d2.1}.

\end{document}

在此处输入图片描述

相关内容