在将当前定理类项目的名称/编号放在标题中,Gustavo Mezzetti 发布了一个答案,允许在页眉中包含当前类似定理的项目的名称和编号。
问题
\theorem
但是,当等采用包含某些数学字母字符的可选参数时,该答案会失败,包括\mathbb
(但对于普通数学字母来说是可以的)。
我怀疑需要进行额外的修复\protect
,但我不确定是什么。有什么建议吗?
测试示例
这个例子缩短了Mezzetti的代码,以消除不相关的内容(例如,测试是否amsthm
已加载)。
\documentclass{book}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amsfonts}
\newtheorem{theorem}{Theorem}
\newtheorem{corollary}[theorem]{Corollary}
\usepackage{etoolbox} % needed for headings wizardry, below
\usepackage{fancyhdr}
\fancyhf{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[RE,LO]{\myBotmark} % "RE" just for testing purposes
\pagestyle{fancy}
%%%%%%%% 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}
\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
}
\makeatother
%%%%%%%% END WIZARDRY %%%%%%%%
\begin{document}
Denote the reals by $\mathbb{R}$.
\begin{theorem}[about \protect $\mathbb{R}$] % gives error
%\begin{theorem}[about $R$] % so would this be OK
%\begin{theorem}[about reals] % this would be OK
Big result!
\end{theorem}
\newpage
%\begin{corollary}[special case for $\mathbb{Q}$]
\begin{corollary}[special case for $Q$]
%\begin{corollary}[special case for rationals]
Another assertion.
\end{corollary}
\end{document}
运行 pdflatex 时出现的错误是:
...42: Undefined control sequence.
\GenericError ...
#4 \errhelp \@err@ ...
l.42 \begin{theorem}[about \protect $\mathbb{R}$]
% gives error
最终错误原因的线索?
注释掉所有引用fancyhdr
并注释掉所有与此相关的补丁代码,但现在插入
\usepackage{thmtools}
在序言和
\listoftheorems
在文档主体中。那么$\mathbb{R}$
在定理的可选参数中不定理列表排版错误 — — 即使没有任何\protect
可选参数。
amsthm
所有这些都表明错误源于与或交互的代码“魔法”中的某些东西fancyhdr
。
答案1
在向您推荐我之前的答案,现在已更新请允许我提请大家注意以下事实:
该代码
\protect $\mathbb{R}$
是错误的(至少)有两个原因,第一个原因\protect
是仅有的到以下标记,在本例中为$
;但$
它本身是强大的,因为它是 TeX 的一个原始命令,因此是一个不可扩展的标记。无论如何,即使
$\protect\mathbb{R}$
是错误的,或者更确切地说,也是无用的,因为\mathbb
LaTeX 内核已经使其变得强大。\patchcmd
需要五参数:\patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
其中
<success>
和分别<failure>
是成功或失败时要执行的代码片段\patchcmd
。如果您不想执行任何代码,请将它们留空,但您不能简单地省略它们。在您的示例中,您非常幸运,a\par
和 the\makeatother
被吞噬了……
为了方便起见,我还将在此处保留精简的代码以用于该amsmath
软件包。我邀请您尝试一下:我\protect
在命令中添加了管理功能\myMark
。在以前的版本中省略了这一点肯定是一个错误:看看它现在是否有效。
\documentclass[a4paper,twoside]{article}
\usepackage[T1]{fontenc} % Not necessary, but recommended.
\usepackage{etoolbox}
\usepackage{amsthm} % we have decided to go for this one
\usepackage{amsfonts}
\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]
\newtheorem*{spclaim}{Special Claim}
\swapnumbers
\theoremstyle{definition}
\newtheorem{defin}{Definition}[section]
%%%%%%%% BEGIN WIZARDRY %%%%%%%%
\makeatletter
\@ifdefinable\@my@claim@mark{\newmarks\@my@claim@mark}
\newcommand*\myMark[1]{% <<< MODIFIED
\begingroup
\let\label\relax \let\index\relax \let\glossary\relax
\let\protect\@unexpandable@protect
\marks\@my@claim@mark{#1}%
\endgroup
}
\newcommand*\myBotmark{\botmarks\@my@claim@mark}
\typeout{****************************************}
\newcommand*\my@direct@mark[3]{%
\myMark{%
\thmname{#1}%
\thmnumber{\thmname{\ }#2}%
\thmnote{\ (#3)}%
}%
}
\newcommand*\my@swapped@mark[3]{%
\myMark{%
\thmnumber{#2}%
\thmname{\thmnumber{\ }#1}%
\thmnote{\ (#3)}%
}%
}
\patchcmd{\@begintheorem}{% search for:
\thm@swap\swappedhead\thmhead % more specific than before
}{% replace with:
\thm@swap\my@swapped@mark\my@direct@mark{#1}{#2}{#3}%
\thm@swap\swappedhead\thmhead
}{% execute if succeeded:
\typeout{>>> Made patch specific for amsthm.}
}{% execute if failed:
\typeout{>>> Patch specific for amsthm FAILED!}
}
\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 and~$\mathbb{Q}$]
This is the third theorem of the first section.
\end{theorem}
\begin{spclaim}
This is a special claim.
\end{spclaim}
\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 and~$\mathbb{R}$]
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 and~$\mathbb{C}$]
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-48]
\begin{defin}
This is the third definition of the first section.
\end{defin}
\lipsum[49-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}
请注意,一旦您通知我您已阅读该答案,我就会删除该答案:它实际上是我所引用的另一个答案的重复。