如何使用 fancyhdr 向页脚添加定理编号?

如何使用 fancyhdr 向页脚添加定理编号?

我必须使用 fancyhdr 包将定理编号添加到页脚。如果我只添加该页面上提到的最后一个定理编号(见下面的示例),它就可以正常工作。但是,我想添加特定页面上提到的**所有*定理的编号。因此,在下面的示例中,我需要第 1 页的页脚为

定理1,2

代替

定理 2

在第 2 页,我需要页脚

定理 2, 3

代替

定理3。

有解决方案吗?

以下是示例代码:

\documentclass{article}

\usepackage{fancyhdr}
\usepackage{amsthm}

\newtheorem{Theorem}{Theorem}

\textheight7cm

\begin{document}


\pagestyle{fancy}
\fancyfoot[C]{Theorem \theTheorem}


\begin{Theorem}
  This is the first theorem on page 1.
\end{Theorem}

\begin{Theorem}
  This is the second theorem on page 1.
\end{Theorem}
Here comes a proof of the second theorem which is continued on page 2. \\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.

\begin{Theorem}
  This is the third theorem, which starts on page 2.
\end{Theorem}
\end{document}

答案1

您可以使用 xpatch 自动添加\addtheorem到 Theorem 环境。

\documentclass{article}
\textheight7cm % MWE only, I assume
\usepackage{fancyhdr}
\usepackage{amsthm}

\newtheorem{Theorem}{Theorem}

\newcommand*{\theoremname}{Theorem}% used in \ifx
\newcommand*{\theoremlist}{Theorem}% reserve global name

\newcommand{\addtheorem}{\ifx\theoremname\theoremlist\relax
  \xdef\theoremlist{\theoremname~\theTheorem}%
\else
  \xdef\theoremlist{\theoremlist, \theTheorem}%
\fi}

\makeatletter
\fancyfoot[C]{\ifx\theoremname\theoremlist \else
  \theoremlist
  \global\let\theoremlist=\theoremname
\fi
\ifx\@currenvir\theoremname\relax
  \xdef\theoremlist{\theoremname~\theTheorem}%
\fi}
\makeatother

\begin{document}

\pagestyle{fancy}

\begin{Theorem}\addtheorem
  This is the first theorem on page 1.
\end{Theorem}

\begin{Theorem}\addtheorem
  This is the second theorem on page 1.\\
    Since this theorem is rather long, it is continued on page 2. \\
    Text of this theorem.\\
    Text of this theorem.\\
    Text of this theorem.\\
    Text of this theorem.\\
    Text of this theorem.\\
    Text of this theorem.\\
    Text of this theorem.\\
    Text of this theorem.\\
    Text of this theorem.\\
    Text of this theorem.\\
    Text of this theorem.\\
    Text of this theorem.\\
    Text of this theorem.\\
    Text of this theorem.\\
    Text of this theorem.\\
    Text of this theorem.\\
\end{Theorem}

\end{document}

此版本允许(要求)您使用 任意设置定理的结尾\Theoremfalse

\documentclass{article}
\textheight7cm % MWE only, I assume
\usepackage{fancyhdr}
\usepackage{amsthm}

\newtheorem{Theorem}{Theorem}

\newcommand*{\theoremname}{Theorem}% used in \ifx
\newcommand*{\theoremlist}{Theorem}% reserve global name
\newif\ifTheorem

\newcommand{\addtheorem}{\ifx\theoremname\theoremlist\relax
  \xdef\theoremlist{\theoremname~\theTheorem}%
\else
  \xdef\theoremlist{\theoremlist, \theTheorem}%
\fi
\global\Theoremtrue}

\fancyfoot[C]{\ifx\theoremname\theoremlist \else
  \theoremlist
  \global\let\theoremlist=\theoremname
\fi
\ifTheorem
  \xdef\theoremlist{\theoremname~\theTheorem}%
\fi}

\begin{document}

\pagestyle{fancy}

\begin{Theorem}\addtheorem
  This is the first theorem on page 1.
\end{Theorem}\Theoremfalse

\begin{Theorem}\addtheorem
  This is the second theorem on page 1.
\end{Theorem}
Here comes a proof of the second theorem which is continued on page 2. \\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.
\par\Theoremfalse

\begin{Theorem}\addtheorem
  This is the third theorem, which starts on page 2.
\end{Theorem}\Theoremfalse
\end{document}

此版本使用计数器(firstthm)来跟踪页面上的第一个定理(如果没有则为 0)。

\documentclass{article}
\textheight7cm % MWE only, I assume
\usepackage{fancyhdr}
\usepackage{amsthm}

\newtheorem{Theorem}{Theorem}

\newcounter{firstthm}
\newif\ifTheorem

\newcommand{\addtheorem}{\ifnum\value{firstthm}=0\relax
  \setcounter{firstthm}{\theTheorem}%
\fi
\global\Theoremtrue}

\fancyfoot[C]{\ifnum\value{firstthm}=0\relax
\else
  \ifnum\value{firstthm}=\value{Theorem}%
    Theorem \theTheorem
  \else
    \ifnum\value{firstthm}=\numexpr\value{Theorem}-1\relax
      Theorem \thefirstthm, \theTheorem
    \else
      Theorem \thefirstthm--\theTheorem
    \fi
  \fi
\fi
\ifTheorem
  \setcounter{firstthm}{\value{Theorem}}%
\else
  \setcounter{firstthm}{0}%
\fi}

\begin{document}

\pagestyle{fancy}

\begin{Theorem}\addtheorem
  This is the first theorem on page 1.
\end{Theorem}\Theoremfalse

\begin{Theorem}\addtheorem
  This is the second theorem on page 1.
\end{Theorem}
Here comes a proof of the second theorem which is continued on page 2. \\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.\\
Proof of this theorem.
\par\Theoremfalse

\begin{Theorem}\addtheorem
  This is the third theorem, which starts on page 2.
\end{Theorem}\Theoremfalse

\begin{Theorem}\addtheorem
  This is the fourth theorem, which starts on page 2.
\end{Theorem}\Theoremfalse

\begin{Theorem}\addtheorem
  This is the fifth theorem, which starts on page 2.
\end{Theorem}\Theoremfalse
\end{document}

相关内容