语境
在我编写的文档中,我重新定义了每个环境,并添加了一个名为 的新计数器counter
,以帮助对环境进行编号。我的目的是让每个定义、命题、定理等都使用相同的计数器,该计数器显示章节,然后显示计数器编号,并为每个章节重置计数器。例如,在第 2 节中,第一个定义为“定义 2.1”,接下来的命题则为“命题 2.2”。
计数器定义如下。
\newcounter{counter}
\renewcommand\thecounter{\thesection.\arabic{counter}}
环境定义与下列类似。
\newenvironment{definition}[1][]{%
\refstepcounter{counter}%
\textsc{Definition}~\thecounter. #1
}%
{}%
每个环境都有插入锚点\label{}
后的位置。\begin{<environment>}
为了重置每个部分的计数器,我在\setcounter{counter}{0}
之后包含了\section{}
。重新定义\thecounter
为包含\thesection
意味着交叉引用会\ref{}
显示每个数字的正确部分,并且所有环境本身都会显示正确的编号。
问题
我遇到的问题是,当我单击环境 2.X 的链接时,我会转到环境 1.X,对于环境 3.X 也是如此。即使它显示了具有正确部分的环境的编号,引用似乎也没有读取部分锚点。
我猜是重新定义\thecounter
导致了这个问题。我该如何解决这个问题?
答案1
你的问题应该使用
\newcounter{counter}[section]
将counter
计数器与计数器相链接,使得计数器每移动一步(在 时)section
它都会重置(设置为) 。0
section
\section{...}
以下是提供正确跳转的代码的完整示例:
\documentclass{article}
\usepackage{hyperref}
\newcounter{counter}[section]
\renewcommand{\thecounter}{\thesection.\arabic{counter}}
\newenvironment{definition}[1][]{%
\refstepcounter{counter}%
\noindent\textsc{Definition}~\thecounter. #1
}%
{\par}%
\begin{document}
See Definitions~\ref{def:first}, \ref{def:second}, \ref{def:third} and \ref{def:last}.
\section{First section}
\begin{definition}\label{def:first}Some definition\end{definition}
\begin{definition}Some definition\end{definition}
\begin{definition}Some definition\end{definition}
\section{Second section}
\begin{definition}\label{def:second}Some definition\end{definition}
\begin{definition}Some definition\end{definition}
\begin{definition}Some definition\end{definition}
\section{Third section}
\begin{definition}\label{def:third}Some definition\end{definition}
\begin{definition}Some definition\end{definition}
\begin{definition}Some definition\end{definition}
\section{Last section}
\begin{definition}\label{def:last}Some definition\end{definition}
\begin{definition}Some definition\end{definition}
\begin{definition}Some definition\end{definition}
\end{document}
答案2
使用更标准的框架,hyperref
开箱即用。
\documentclass{article}
\usepackage{amsmath,amsthm}
\usepackage{hyperref}
\usepackage{lipsum} % for context
\newtheoremstyle{wallisdefinition}
{0pt} % ABOVESPACE
{0pt} % BELOWSPACE
{\upshape} % BODYFONT
{0pt} % INDENT (empty value is the same as 0pt)
{\upshape} % HEADFONT
{} % HEADPUNCT
{ } % HEADSPACE
% CUSTOM-HEAD-SPEC follows
{\thmname{\textsc{#1}}\thmnumber{ #2}.\thmnote{ #3}}
\theoremstyle{wallisdefinition}
\newtheorem{definition}{Definition}[section]
\begin{document}
\section{Test}
See definition~\ref{def:foo}.
\clearpage
\lipsum[2]
\begin{definition}[Attribution]\label{def:foo}
A \emph{foo} is something very useful. We will use foos all the
time in this paper. This should be enough to wrap.
\end{definition}
\lipsum[3]
\end{document}
不过,我会添加一些垂直空间。
\documentclass{article}
\usepackage{amsmath,amsthm}
\usepackage{hyperref}
\usepackage{lipsum} % for context
\newtheoremstyle{wallisdefinition}
{\topsep} % ABOVESPACE
{\topsep} % BELOWSPACE
{\upshape} % BODYFONT
{0pt} % INDENT (empty value is the same as 0pt)
{\upshape} % HEADFONT
{} % HEADPUNCT
{ } % HEADSPACE
% CUSTOM-HEAD-SPEC follows
{\thmname{\textsc{#1}}\thmnumber{ #2}.\thmnote{ #3}}
\theoremstyle{wallisdefinition}
\newtheorem{definition}{Definition}[section]
\begin{document}
\section{Test}
See definition~\ref{def:foo}.
\clearpage
\lipsum[2]
\begin{definition}[Attribution]\label{def:foo}
A \emph{foo} is something very useful. We will use foos all the
time in this paper. This should be enough to wrap.
\end{definition}
\lipsum[3]
\end{document}
答案3
如果想用另一个计数器重置计数器,请使用 \newcounter 的可选参数。下面这个方法很好用:
\documentclass{article}
\newcounter{counter}[section]
\renewcommand\thecounter{\thesection.\arabic{counter}}
\newenvironment{definition}[1][]{%
\refstepcounter{counter}%
\textsc{Definition}~\thecounter. #1
}%
{}%
\usepackage{hyperref}
\begin{document}
\section{abc}
\begin{definition}
\label{a}
abc
\end{definition}
\newpage
\section{b}
\begin{definition}
\label{b}
abc
\end{definition}
\newpage
\ref{a} \ref{b}
\end{document}
答案4
让我们看一下您的帖子:
在我编写的文档中,我重新定义了每个环境,并添加了一个名为 的新计数器
counter
,以帮助对环境进行编号。我的目的是让每个定义、命题、定理等都使用相同的计数器,该计数器显示章节,然后显示计数器编号,并为每个章节重置计数器。例如,在第 2 节中,第一个定义为“定义 2.1”,接下来的命题则为“命题 2.2”。
除了这样做之外,您还可以根据 来定义环境\newtheorem
。
语法\newtheorem
为:
\newtheorem{⟨Name of the environment and — in case no already
existing counter is to be used — name of
newly to allocate underlying counter.⟩}%
[⟨Name of already existing counter to be used for
numbering the environment.⟩]%
{⟨Textual phrase denoting the title/heading/the kind of
item of sectioning produced by an instance of the
environment.⟩}%
[⟨Name of superordinate sectioning counter whose value is
part of the number of the sectioning-item provided by
this environment, and whose incrementing via `\refstepcounter`
triggers resetting the counter in use for numbering the
environment.⟩]
因此你可以这样做:
\newtheorem{theorem}{Theorem}[section]%
\newtheorem{proposition}[theorem]{Proposition}%
\newtheorem{definition}[theorem]{Definition}%
如果您希望通过这种方法使用超链接包裹要使用其\autoref
功能,您还需要加载别名包裹然后执行:
\documentclass...
...
\usepackage{hyperref}
\usepackage{aliascnt}
...
\newtheorem{theorem}{Theorem}[section]%
%
\newaliascnt{proposition}{theorem}
\newtheorem{proposition}[proposition]{Proposition}%
\aliascntresetthe{proposition}
\newcommand\propositionautorefname{Proposition}
%
\newaliascnt{definition}{theorem}
\newtheorem{definition}[definition]{Definition}%
\aliascntresetthe{definition}
\newcommand\definitionnautorefname{Definition}
除此之外,aliascnt 包还解决了与\autoref
hyperref 包的功能相关的问题。它还提供了为超链接使用不同锚点名称模式的可能性。(有关此内容的更多信息,请参阅hyperref 包手册,第 4 部分:附加用户宏,\autoref
。
例如,定理2.4的锚点名称不会是#counter.2.4
但#theorem.2.4
。
例如,命题2.5的锚点名称不会是#counter.2.5
但#proposition.2.5
。
例如,定义2.6的锚点名称不会是#counter.2.6
但#definition.2.6
。
计数器定义如下。
\newcounter{counter} \renewcommand\thecounter{\thesection.\arabic{counter}}
如果您希望每次通过 增加计数器时将计数器counter
重置为值 0 (反过来,这又被 sectioning-command 使用),请按如下方式应用:section
\refstepcounter
\section
\newcounter
\newcounter{counter}[section]
每个环境都有插入锚点
\label{}
后的位置。\begin{<environment>}
\label
认为-command 会放置一个锚点的假设是一个常见的误解。
\section
锚点由分段命令(如或\begin{theorem}
或\item
(枚举环境))放置。它们在内部的使用方式\refstepcounter
与您对环境的使用方式相同。
\refstepcounter
做
增加计数器,
创建一个锚点名称,
放置一个以该名字命名的锚,
使刚刚放置的锚点的名称可供
\label
-macro 作为宏的扩展使用\@currentHref
,使刚刚增加的计数器的值可供
\label
宏作为宏的扩展使用\@currentlabel
。
\label
本身既不创建锚点名称也不放置锚点。\label
仅将由分段命令提供的信息(以重新定义特定名称的宏(\@currentlabel
,,\@currentHref
...)的形式)写入 .aux 文件中,作为这些不错的\newlabel
条目。
如果我不关心超链接的锚点名称和\autoref
功能,我可能会按如下方式执行操作:
\documentclass{article}
\usepackage{hyperref}
\newcounter{counter}[section]
\renewcommand\thecounter{\thesection.\arabic{counter}}
\renewcommand\theHcounter{\theHsection.\arabic{counter}}
\newcommand\underlyingformatting[1]{%
\refstepcounter{counter}%
\par\noindent
\textsc{#1}~\thecounter. #1
}%
\newcommand\counterautorefname{Theorem/Definition/Proposition}
\newenvironment{theorem}[1][]{\underlyingformatting{Theorem}}{}%
\newenvironment{definition}[1][]{\underlyingformatting{Definition}}{}%
\newenvironment{proposition}[1][]{\underlyingformatting{Proposition}}{}%
\begin{document}
\section{A section}
\ref{thm1}
\autoref{thm1}
\ref{def1}
\autoref{def1}
\ref{pro1}
\autoref{pro1}
\ref{thm2}
\autoref{thm2}
\ref{def2}
\autoref{def2}
\ref{pro2}
\autoref{pro2}
\begin{theorem}\label{thm1}
A theorem
\end{theorem}
\begin{definition}\label{def1}
A definition
\end{definition}
\begin{proposition}\label{pro1}
A proposition
\end{proposition}
\begin{theorem}\label{thm2}
Another theorem
\end{theorem}
\begin{definition}\label{def2}
Another definition
\end{definition}
\begin{proposition}\label{pro2}
Another proposition
\end{proposition}
\end{document}
如果关心锚点名称等 \autoref
,我会按如下方式进行 - 我\theoremstyle
从 egreg “借用”了 -code ;-) :
\documentclass{article}
\usepackage{amsmath,amsthm}
\usepackage{hyperref}
\usepackage{aliascnt}
\usepackage{lipsum} % for context
\newtheoremstyle{wallisdefinition}
{\topsep} % ABOVESPACE
{\topsep} % BELOWSPACE
{\upshape} % BODYFONT
{0pt} % INDENT (empty value is the same as 0pt)
{\upshape} % HEADFONT
{} % HEADPUNCT
{ } % HEADSPACE
% CUSTOM-HEAD-SPEC follows
{\thmname{\textsc{#1}}\thmnumber{ #2}.\thmnote{ #3}}
\theoremstyle{wallisdefinition}
\newtheorem{theorem}{Theorem}[section]%
\newaliascnt{proposition}{theorem}
\newtheorem{proposition}[proposition]{Proposition}%
\aliascntresetthe{proposition}
\newcommand\propositionautorefname{Proposition}
\newaliascnt{definition}{theorem}
\newtheorem{definition}[definition]{Definition}%
\aliascntresetthe{definition}
\newcommand\definitionautorefname{Definition}
\begin{document}
\section{Test}
See definition~\ref{def:foo}.
See \autoref{def:foo}.
See theorem~\ref{thm:foo}.
See \autoref{thm:foo}.
See proposition~\ref{prp:foo}.
See \autoref{prp:foo}.
See definition~\ref{def:bar}.
See \autoref{def:bar}.
See theorem~\ref{thm:bar}.
See \autoref{thm:bar}.
See proposition~\ref{prp:bar}.
See \autoref{prp:bar}.
\clearpage
\lipsum[2]
\begin{definition}[(Foo-attribution.)]\label{def:foo}
A \emph{foo} is something very useful. Therefore we will not use
foos all the time in this paper. This should be enough to wrap.
\end{definition}
\lipsum[3]
\begin{theorem}[(Foo's bar-associativity.)]\label{thm:foo}
Foo is bar-associative. The proof is left to a drunken monkey.
\end{theorem}
\lipsum[4]
\begin{proposition}[(Foo's foobar-associativity.)]\label{prp:foo}
Foo is also foobar-associative. Before the break we announce proving
after the break. After the break we proclaim having proven just
before the break and continue with other things.
\end{proposition}
\lipsum[5]
\begin{definition}[(Bar-attribution.)]\label{def:bar}
A \emph{bar} is something very useful but we will never really
use bars in this paper. This should be enough to wrap.
\end{definition}
\lipsum[6]
\begin{theorem}[(Bar's foo-associativity.)]\label{thm:bar}
Bar is foo-associative. The proof is left to a drunken donkey.
\end{theorem}
\lipsum[7]
\begin{proposition}[(Bar's barfoo-assoviativity.)]\label{prp:bar}
Bar is also barfoo-associative. We proclaim that this is obvious
and cause fear of attracting attention in an embarrassing way on
the side of those people that don't agree.
\end{proposition}
\end{document}