假设我有一个环境theorem
。我想在每一页的顶部打印,Theorems 1-3
因此我需要知道如何访问环境的起始和结束计数器theorem
。我该怎么做?
现在我只能打印运行定理编号。
\documentclass[12pt]{book}
\usepackage{fancyhdr,lipsum,blindtext,amsthm}
\newtheorem{theorem}{Theorem}
\renewcommand{\chaptermark}[1]{\markboth{#1}{#1}}
\fancypagestyle{fancy}{
\fancyhf{}
\fancyhead[LE]{\thepage}
\fancyhead[C]{\textsc{Theorem} \arabic{theorem}}
%\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
}
\begin{document}
\tableofcontents
\pagenumbering{arabic}
\clearpage
\pagestyle{fancy}
%\addcontentsline{toc}{part}{\protect\centering{}Chapter I}
\part{One}
\chapter{The Greatest Common Divisor of Two Numbers}
\lipsum
\begin{theorem}
\end{theorem}
\blinddocument
\section{Section of first}
\section{Another Section of First}
\part{Two}
\chapter*{Unnumbered Chapter}
\lipsum
\chapter{Prime Numbers and Factorization into Prime Factors}
\section*{Unnumbered Section}
\section{Test Section}
\end{document}
答案1
您需要使用标记来解决这个问题。这是一个简单的解决方案。它使用包extramarks
。它不会在第一个定理之前打印标题。使用旧版本(版本 4 或更早版本)extramarks
有时会与由部分生成的标记发生不必要的交互(不在以下示例中)。在这种情况下,您应该使用extramarks
版本 5(下面的链接)。
我不喜欢在很多页面的页眉中都显示“定理 xx”,而这个定理比这个定理早几页。为此,我还给出了第二个更复杂的解决方案,即只有当页面上确实存在(部分)定理时才打印页眉。任你选择。
解决方案 1
\documentclass[12pt]{book}
\usepackage{fancyhdr,lipsum,blindtext,amsthm}
\usepackage{extramarks}
\usepackage{ifthen}
% If you want to number theorems within chapters, add '[chapter]'
\newtheorem{theorem}{Theorem}
% We add a new mark containing the theorem number at the beginning of a
% theorem. As the theorem number hasn't been incremented yet at the
% beginning, we increment it first, set the mark and then decrement the
% theorem number.
\AtBeginEnvironment{theorem}{\addtocounter{theorem}{1}%
\extramarks{\thetheorem}{}\addtocounter{theorem}{-1}}
\renewcommand{\chaptermark}[1]{\markboth{#1}{#1}}
\fancypagestyle{fancy}{
\fancyhf{}
\fancyhead[LE]{\thepage}
\fancyhead[C]{\ifthenelse{0\firstleftxmark=0}
{}% no theorems yet, so no header
{\textsc{Theorem} \ifthenelse{\equal{\firstleftxmark}{\lastleftxmark}}
{\firstleftxmark}% Only one theorem on the page
{\firstleftxmark–\lastleftxmark}}}% Multiple theorems on the page
%\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
}
\setlength{\headheight}{15pt}
\begin{document}
\pagestyle{plain} % This avoids a bug in extramarks - may also be headings
\tableofcontents
\pagenumbering{arabic}
\clearpage
\pagestyle{fancy}
%\addcontentsline{toc}{part}{\protect\centering{}Chapter I}
\part{One}
\chapter{The Greatest Common Divisor of Two Numbers}
\lipsum
\begin{theorem}
one
\end{theorem}
Some text
\begin{theorem}
two
\end{theorem}
\newpage
\begin{theorem}
three
\end{theorem}
\blinddocument
\section{Section of first}
\section{Another Section of First}
\part{Two}
\chapter*{Unnumbered Chapter}
\lipsum
\chapter{Prime Numbers and Factorization into Prime Factors}
\section*{Unnumbered Section}
\lipsum
\begin{theorem}
three
\end{theorem}
\section{Test Section}
\begin{theorem}
four
\end{theorem}
\begin{theorem}
five
\end{theorem}
\begin{theorem}
This is a multi-page theorem. \\
\lipsum
\end{theorem}
\end{document}
解决方案 2
此解决方案需要extramarks
版本 5,可在以下位置找到
https://github.com/pietvo/fancyhdr/tree/5.0beta
仅当页面上确实存在(部分)定理时,此解决方案才会打印标题。
\documentclass[12pt]{book}
\usepackage{fancyhdr,lipsum,blindtext,amsthm}
\usepackage{extramarks} % Need version 5
\usepackage{ifthen}
% If you want to number theorems within chapters, add '[chapter]'
\newtheorem{theorem}{Theorem}
% We add two new marks at the beginning and the end of a theorem.
% The left one has the theorem number. As the theorem number hasn't been
% incremented yet at the beginning, we increment it first, set the mark
% and then decrement the theorem number. We need this mark both at the
% beginning and the end, so that we can use the number on both pages if
% a theorem crosses a page boundary
% The right mark is 1 at the beginning and 0 at the end of a theorem.
% This allows us to check if we are inside a multipage theorem.
\AtBeginEnvironment{theorem}{\addtocounter{theorem}{1}%
\extramarks{\thetheorem}{1}\addtocounter{theorem}{-1}}
\AtEndEnvironment{theorem}{\extramarks{\thetheorem}{0}}
\renewcommand{\chaptermark}[1]{\markboth{#1}{#1}}
\fancypagestyle{fancy}{
\fancyhf{}
\fancyhead[LE]{\thepage}
\fancyhead[C]{\ifextramarksmissing{left} % no theorem marks on this page
% Check if we are in the middle of a multipage theorem
{\ifthenelse{\equal{\firstrightxmark}{1}}{\textsc{Theorem} \firstleftxmark}{}}
{\textsc{Theorem} \ifthenelse{\equal{\firstleftxmark}{\lastleftxmark}}
{\firstleftxmark} % Only one theorem on the page
{\firstleftxmark–\lastleftxmark}}} % Multiple theorems on the page
%\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
}
\setlength{\headheight}{15pt}
\begin{document}
\tableofcontents
\pagenumbering{arabic}
\clearpage
\pagestyle{fancy}
%\addcontentsline{toc}{part}{\protect\centering{}Chapter I}
\part{One}
\chapter{The Greatest Common Divisor of Two Numbers}
\lipsum
\begin{theorem}
one
\end{theorem}
Some text
\begin{theorem}
two
\end{theorem}
\newpage
\begin{theorem}
three
\end{theorem}
\blinddocument
\section{Section of first}
\section{Another Section of First}
\part{Two}
\chapter*{Unnumbered Chapter}
\lipsum
\chapter{Prime Numbers and Factorization into Prime Factors}
\section*{Unnumbered Section}
\lipsum
\begin{theorem}
three
\end{theorem}
\section{Test Section}
\begin{theorem}
four
\end{theorem}
\begin{theorem}
five
\end{theorem}
\begin{theorem}
This is a multi-page theorem. \\
\lipsum
\end{theorem}
\end{document}