fancyhdr 和 extramarks 包:章节结束时的页眉不正确

fancyhdr 和 extramarks 包:章节结束时的页眉不正确

\fancyhdr我在使用和包时遇到了一个问题\extramarks。当一个章节在某一页结束时,它会显示错误的章节号。但是,如果新章节从该页开始,它会正确呈现。有人能帮我理解为什么\fancyhdr会这样吗?我已附上 MWE。

   \documentclass[letterpaper,12pt]{book}
\usepackage{zref-abspage}
\usepackage{ifthen}
\usepackage{fancyhdr}
\usepackage{extramarks}
\usepackage{setspace}
\usepackage{needspace}
\usepackage{lipsum}
    
% % Make Counters for Chapters and Verses
\newcounter{mychapter}
\newcounter{bibver}
\setcounter{mychapter}{0}
\setcounter{bibver}{0}

% % Make Bible Chapter and Verses
\newcommand{\mychaptermark}{}
\newcommand{\bibvermark}{}

\newcommand{\mychapter}[1]{%
  \stepcounter{mychapter}% Increment the mychapter counter
  \renewcommand{\mychaptermark}{#1}%
  %\chaptermark{#1 : \bibvermark}% Include the verse number in the chaptermark
  \needspace{2.4\baselineskip}
  \vspace{1.5cm}
  {\centering\Large\bfseries\MakeUppercase{\space\themychapter\space}}% Center the chapter title with bold chapter number
  \markboth{#1}{#1}
  }
\newcommand{\bibver}[1]{%
  \stepcounter{bibver}% Increment the bibver counter
  % Set both left and right marks to #1
  \textsuperscript{\textbf{#1 }}~%
  \extramarks{#1}{#1}
}  

\newcommand{\mymarks}{%
\ifthenelse{\equal{\leftmark}{\rightmark}}
{\leftmark:\firstleftxmark—–\lastleftxmark} % if equal
{\rightmark:\firstrightxmark ––\leftmark:\lastleftxmark}} % if not equal 

\pagestyle{fancy}
\fancyfoot{}
\fancyhead{} % Clear previous header settings
\fancyhead[RE,LO]{\thepage}
\fancyhead[RO,LE]{\mymarks} % Right on odd pages, Left on even pages
\setcounter{mychapter}{0}

\begin{document}

\mychapter{1}
\bibver{1} \lipsum[1]
\bibver{2} \lipsum[2]
\bibver{3} \lipsum[3]
\bibver{4} \lipsum[4]
\bibver{5} \lipsum[5]
\bibver{6} \lipsum[6]

\mychapter{2}
\bibver{1} \lipsum[1]
\bibver{2} \lipsum[2]

\mychapter{3}
\bibver{1} \lipsum[1]
\bibver{2} \lipsum[2]
\bibver{3} \lipsum[3]
\bibver{4} \lipsum[4]
\bibver{5} \lipsum[5]


\mychapter{4}
\bibver{1} \lipsum[1]
\bibver{2} \lipsum[2]
\bibver{3} \lipsum[3]
\bibver{4} \lipsum[4]


\mychapter{5}
\bibver{1} \lipsum[1]
\bibver{2} \lipsum[2]
\bibver{3} \lipsum[3]
\bibver{4} \lipsum[4]
\bibver{5} \lipsum[5]
\bibver{6} \lipsum[6]

\mychapter{6}
\bibver{1} \lipsum[1]
\bibver{2} \lipsum[2]
\bibver{3} \lipsum[3]
\bibver{4} \lipsum[4]

\end{document}

这是命令的新位置,但是当章节在某一页结束并在新页的顶部开始时,它仍然会给出错误的数字。

从这个例子可以看出,在某些页面上,它工作正常,但如果最后\bibver一个页面结束或结束在页面顶部,则标题会显示下一章。我不认为这是一个逻辑错误,因为它在其他所有情况下都会给出正确的响应——除非我忽略了什么。

感谢您的帮助。

答案1

为了避免章节在页面底部结束的边缘情况,有必要使用条件逻辑来解决顶部数字大于底部数字的情况。

\documentclass[letterpaper,12pt]{book}
\usepackage{zref-abspage}
\usepackage{ifthen}
\usepackage{fancyhdr}
\usepackage{extramarks}
\usepackage{setspace}
\usepackage{needspace}
\usepackage{lipsum}
    
% % Make Counters for Chapters and Verses
\newcounter{mychapter}
\newcounter{bibver}
\setcounter{mychapter}{0}
\setcounter{bibver}{0}

% % Make Bible Chapter and Verses
\newcommand{\mychaptermark}{}
\newcommand{\bibvermark}{}

\newcommand{\mychapter}[1]{%
  \stepcounter{mychapter}% Increment the mychapter counter
  \renewcommand{\mychaptermark}{#1}%
  %\chaptermark{#1 : \bibvermark}% Include the verse number in the chaptermark
  \needspace{2.4\baselineskip}
  \vspace{1.5cm}
  {\centering\Large\bfseries\MakeUppercase{\space\themychapter\space}}% Center the chapter title with bold chapter number
  \markboth{#1}{#1}
  }
\newcommand{\bibver}[1]{%
  \stepcounter{bibver}% Increment the bibver counter
  % Set both left and right marks to #1
  \textsuperscript{\textbf{#1 }}~%
  \extramarks{#1}{#1}
}  

\newcommand{\mymarks}{%
  \ifthenelse{\equal{\rightmark}{\leftmark} \AND \firstrightxmark > \lastleftxmark}{%
    \lastleftmark:1––\lastleftxmark % Code if both conditions are true
  }{%
    \ifthenelse{\equal{\rightmark}{\leftmark}}{%
      \lastleftmark:\firstrightxmark ––\lastleftxmark % Code if only the first condition is true
    }{%
      \firstrightmark:\firstrightxmark ––\lastleftmark:\lastleftxmark % Code if neither condition is true
    }
  }
}
 

\pagestyle{fancy}
\fancyfoot{}
\fancyhead{} % Clear previous header settings
\fancyhead[RE,LO]{\thepage}
\fancyhead[RO,LE]{\mymarks} % Right on odd pages, Left on even pages
\setcounter{mychapter}{0}

\begin{document}

\mychapter{1}
\bibver{1} \lipsum[1]
\bibver{2} \lipsum[2]
\bibver{3} \lipsum[3]
\bibver{4} \lipsum[4]
\bibver{5} \lipsum[5]
\bibver{6} \lipsum[6]

\mychapter{2}
\bibver{1} \lipsum[1]
\bibver{2} \lipsum[2]

\mychapter{3}
\bibver{1} \lipsum[1]
\bibver{2} \lipsum[2]
\bibver{3} \lipsum[3]
\bibver{4} \lipsum[4]
\bibver{5} \lipsum[5]


\mychapter{4}
\bibver{1} \lipsum[1]
\bibver{2} \lipsum[2]
\bibver{3} \lipsum[3]
\bibver{4} \lipsum[4]


\mychapter{5}
\bibver{1} \lipsum[1]
\bibver{2} \lipsum[2]
\bibver{3} \lipsum[3]
\bibver{4} \lipsum[4]
\bibver{5} \lipsum[5]
\bibver{6} \lipsum[6]

\mychapter{6}
\bibver{1} \lipsum[1]
\bibver{2} \lipsum[2]
\bibver{3} \lipsum[3]
\bibver{4} \lipsum[4]

\end{document}

相关内容