我正在使用 amsthm 和 fancyhdr 包以及书籍 LaTeX 文档类。所有定理类项目(定理、引理、定义、练习等)在每个章节内按顺序编号(通过通常的声明\newtheorem{theorem}{Theorem}[chapter]
)。
我怎样才能将当前页面上最后一个编号的定理类项目的编号和标签放入(正面)页眉中?
例如,如果页面上最后一个编号的定理类项目是“命题 2.11”,那么它就会出现在标题中。
[如果正面页面没有编号项,则页眉应为前一页的最后一个编号项 - 或者,如果前一页(反面页面)没有编号项,则为前一页(正面页面)。(我不需要比这更深的递归!)]
目的是帮助读者通过扫描页眉而不是扫描整个页面更容易地找到文本中的参考目标。
答案1
也许最简单的方法就是使用“并行”\mark
系统;我们已经学会了如何表现良好,所以我们现在用 分配\marks
数字\newmarks
。
\documentclass[a4paper,twoside]{article}
\usepackage[T1]{fontenc} % Not necessary, but recommended.
\usepackage{etoolbox}
% \usepackage{amsthm} % doesn't call "\@opargbegintheorem", though
\usepackage{fancyhdr}
\usepackage{lipsum}
\fancyhf{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[RE,LO]{\myBotmark} % "RE" just for testing purposes
\pagestyle{fancy}
\newtheorem{theorem}{Theorem}[section] % or "[chapter]", or whatever
\newtheorem{lemma} {Lemma} [section]
\makeatletter
\@ifdefinable\@my@claim@mark{\newmarks\@my@claim@mark}
\newcommand*\myMark[1]{\marks\@my@claim@mark{#1}}
\newcommand*\myBotmark{\botmarks\@my@claim@mark}
\apptocmd{\@begintheorem} {\myMark{#1\ #2}\ignorespaces}{}{}
\apptocmd{\@opargbegintheorem}{\myMark{%
#1\ #2%
\ (#3)% comment this line to leave "Mickey Mouse" out
\ignorespaces
}}{}{}
\makeatother
\begin{document}
\section{First section}
\lipsum[1]
\begin{theorem}
This is the first theorem of the first section.
\end{theorem}
\lipsum[2]
\begin{theorem}
This is the second theorem of the first section.
\end{theorem}
\begin{theorem}[Donald Duck]
This is the third theorem of the first section.
\end{theorem}
\lipsum[3-16]
\begin{theorem}
This is the fourth theorem of the first section.
\end{theorem}
\lipsum[17]
\begin{lemma}
This is the first lemma of the first section.
\end{lemma}
\lipsum[18]
\begin{theorem}
This is the fifth theorem of the first section.
\end{theorem}
\lipsum[19-20]
\begin{lemma}[Uncle Scrooge]
This is the second lemma of the first section.
\end{lemma}
\lipsum[21-32]
\begin{theorem}
This is the sixth theorem of the first section.
\end{theorem}
\lipsum[33-36]
\begin{theorem}
This is the seventh theorem of the first section.
\end{theorem}
\begin{lemma}
This is the third lemma of the first section.
\end{lemma}
\begin{theorem}[Mickey Mouse]
This is the eighth theorem of the first section.
\end{theorem}
\lipsum[37-40]
\begin{theorem}
This is the ninth theorem of the first section.
\end{theorem}
\lipsum[41-64]
\begin{theorem}[The last one]
This is the tenth theorem of the first section.
\end{theorem}
\lipsum[65-80]
\begin{lemma}
This is the fourth lemma of the first section.
\end{lemma}
\begin{lemma}
This is the fifth lemma of the first section.
\end{lemma}
\begin{lemma}
This is the sixth lemma of the first section.
\end{lemma}
\begin{lemma}
This is the seventh lemma of the first section.
\end{lemma}
\begin{lemma}
This is the eighth lemma of the first section.
\end{lemma}
\begin{lemma}
This is the ninth lemma of the first section.
\end{lemma}
\begin{lemma}[Deep breath]
This is the tenth lemma of the first section.
\end{lemma}
\lipsum[81-84]
\begin{lemma}
This is the eleventh lemma of the first section.
\end{lemma}
\begin{lemma}
This is the twelfth lemma of the first section.
\end{lemma}
\begin{lemma}
This is the thirteenth lemma of the first section.
\end{lemma}
\begin{lemma}
This is the fourteenth lemma of the first section.
\end{lemma}
\begin{lemma}
This is the fifteenth lemma of the first section.
\end{lemma}
\begin{lemma}[Hexadecimal deep breath]
This is the sixteenth lemma of the first section.
\end{lemma}
% \lipsum[85-96]
\end{document}
无法发布图像:编译并查看输出。
添加
实际上,原作者在问题中已经说得很清楚了,他/她正在使用这个amsthm
包,但我忽略了这句话。上面显示的代码在加载该包时也应该可以工作,但在这种情况下,运行头将省略“定理注释”,即给予“类定理环境”的可选参数(例如,在我们示例的第三条定理中,当出现时,将出现“唐老鸭”的情况。这是因为该amsthm
包不使用标准\@opargbegintheorem
钩子。
出于这个原因以及其他原因,最好设计另一个特别适合该amsthm
包的补丁,该补丁在宏的参数中提供标记\deferred@thm@head
,该宏在 内部排版;更准确地说,它是在或\box\@labels
调用 之前发出的,就在 的最开始处。请注意,此框随后被编辑,因此标记将出现在包含定理头的水平列表的最开始处,后跟定理声明;从那里,它将移动到同一段落的第一行之后,也就是它所属的确切位置。\swappedhead
\thmhead
\box\@labels
\unhbox
以下代码定义了一个名为的命令\IfAmsThm
,它接受两个参数,如果amsthm
包已加载,则执行其第一个参数中的代码,如果包未加载,则执行其第二个参数中的代码。当然,一旦您做出决定,您就不再需要这个设备:只需删除此命令和两个不适用的分支之一即可。
为了在您的文档中使用此代码,您只需执行以下三件事:
确保
etoolbox
在序言中尽早加载该软件包;将两个注释之间的代码复制
BEGIN WIZARDRY
到END WIZARDRY
你的序言中,在两个都 包裹etoolbox
和amsthm
包裹已经装载;在您的页面样式的定义中,放置
\myBotmark
您希望定理引用出现的任何位置(类似于放置\thepage
您希望页码出现的任何位置)。
修改后的代码如下:
\documentclass[a4paper,twoside]{article}
\usepackage[T1]{fontenc} % Not necessary, but recommended.
\usepackage{etoolbox}
\usepackage{amsthm} % comment or uncomment as you prefer
\usepackage{fancyhdr}
\usepackage{lipsum}
\fancyhf{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[RE,LO]{\myBotmark} % "RE" just for testing purposes
\pagestyle{fancy}
\newtheorem{theorem}{Theorem}[section] % or "[chapter]", or whatever
\newtheorem{lemma} {Lemma} [section]
% A little trick to make this example work both with amsthm and without it;
% once you've made your choice, you do not need this code.
\makeatletter
\@ifdefinable\IfAmsThm{}
\@ifpackageloaded{amsthm}{
\let \IfAmsThm = \@firstoftwo
}{
\let \IfAmsThm = \@secondoftwo
}
\makeatother
\IfAmsThm{
\newtheorem*{spclaim}{Special Claim}
\theoremstyle{definition}
}{}
\newtheorem{defin}{Definition}[section]
%%%%%%%% BEGIN WIZARDRY %%%%%%%%
\makeatletter
\@ifdefinable\@my@claim@mark{\newmarks\@my@claim@mark}
\newcommand*\myMark[1]{\marks\@my@claim@mark{#1}}
\newcommand*\myBotmark{\botmarks\@my@claim@mark}
\typeout{****************************************}
\@ifpackageloaded{amsthm}{\iftrue}{\iffalse}
\patchcmd{\@begintheorem}{% search for:
\thm@swap\swappedhead\thmhead % more specific than before
}{% replace with:
\myMark{#1\@ifnotempty{#2}{\ #2}\@ifnotempty{#3}{\ (#3)}}%
\thm@swap\swappedhead\thmhead
}{
\typeout{>>> Made patch specific for amsthm.}
}{
\typeout{>>> Patch specific for amsthm FAILED!}
}
\else
\apptocmd{\@begintheorem} {\myMark{#1\ #2}\ignorespaces}{}{}
\apptocmd{\@opargbegintheorem}{\myMark{%
#1\ #2%
\ (#3)% comment this line to leave "Mickey Mouse" out
\ignorespaces
}}{}{}
\typeout{>>> Made generic patch.}
\fi
\typeout{****************************************}
\makeatother
%%%%%%%% END WIZARDRY %%%%%%%%
\begin{document}
\section{First section}
\lipsum[1]
\begin{theorem}
This is the first theorem of the first section.
\end{theorem}
\lipsum[2]
\begin{theorem}
This is the second theorem of the first section.
\end{theorem}
\begin{theorem}[Donald Duck]
This is the third theorem of the first section.
\end{theorem}
\IfAmsThm{
\begin{spclaim}
This is a special claim.
\end{spclaim}
}{}
% \begingroup
% \showboxbreadth = 1000
% \showboxdepth = 10
% \tracingonline = 1
% \showlists
% \endgroup
\lipsum[3-16]
\begin{theorem}
This is the fourth theorem of the first section.
\end{theorem}
\begin{defin}[Something new]
This is the first definition of the first section.
\end{defin}
\begin{defin}
This is the second definition of the first section.
\end{defin}
\lipsum[17]
\begin{lemma}
This is the first lemma of the first section.
\end{lemma}
\lipsum[18]
\begin{theorem}
This is the fifth theorem of the first section.
\end{theorem}
\lipsum[19-20]
\begin{lemma}[Uncle Scrooge]
This is the second lemma of the first section.
\end{lemma}
\lipsum[21-32]
\begin{theorem}
This is the sixth theorem of the first section.
\end{theorem}
\lipsum[33-36]
\begin{theorem}
This is the seventh theorem of the first section.
\end{theorem}
\begin{lemma}
This is the third lemma of the first section.
\end{lemma}
\begin{theorem}[Mickey Mouse]
This is the eighth theorem of the first section.
\end{theorem}
\lipsum[37-40]
\begin{theorem}
This is the ninth theorem of the first section.
\end{theorem}
\lipsum[41-64]
\begin{theorem}[The last one]
This is the tenth theorem of the first section.
\end{theorem}
\lipsum[65-80]
\begin{lemma}
This is the fourth lemma of the first section.
\end{lemma}
\begin{lemma}
This is the fifth lemma of the first section.
\end{lemma}
\begin{lemma}
This is the sixth lemma of the first section.
\end{lemma}
\begin{lemma}
This is the seventh lemma of the first section.
\end{lemma}
\begin{lemma}
This is the eighth lemma of the first section.
\end{lemma}
\begin{lemma}
This is the ninth lemma of the first section.
\end{lemma}
\begin{lemma}[Deep breath]
This is the tenth lemma of the first section.
\end{lemma}
\lipsum[81-84]
\begin{lemma}
This is the eleventh lemma of the first section.
\end{lemma}
\begin{lemma}
This is the twelfth lemma of the first section.
\end{lemma}
\begin{lemma}
This is the thirteenth lemma of the first section.
\end{lemma}
\begin{lemma}
This is the fourteenth lemma of the first section.
\end{lemma}
\begin{lemma}
This is the fifteenth lemma of the first section.
\end{lemma}
\begin{lemma}[Hexadecimal deep breath]
This is the sixteenth lemma of the first section.
\end{lemma}
% \lipsum[85-96]
\end{document}
与以前一样,发布显示结果的图像没有什么意义:您必须自己编译代码并检查其是否按预期运行。
第二次添加
看我的答案针对这个问题位定理数前页眉中的定理标签以获得与该功能兼容的改进版本的代码\swapnumbers
。