Fancyhdr 和 \thesection 命令

Fancyhdr 和 \thesection 命令

我在 fancyhdr 包中对 \thechapter 的实现存在问题。正如我在帖子底部附上的照片中看到的,标题中的章节编号 (RO) 和标题 (LO) 不匹配:它应该显示“Propietats bàsiques de la divisibilitat”然后显示“1.3”,或者“Equacions diofantines lineals”然后显示“1.4”。如果必须选择,我更喜欢后者(不过我认为实现起来更复杂)。我的 pagestyle{fancy} 配置如下:


% Header
\pagestyle{fancy}
\renewcommand{\headrulewidth}{0.1pt}
\fancyhead[RE]{\nouppercase{\leftmark}}
\fancyhead[LO]{\nouppercase{\rightmark}}
\fancyhead[RO]{\thesection}
\fancyhead[LE]{\thechapter}
\fancyfoot[RO, LE]{\thepage}
\fancyfoot[CO, CE]{}
\fancyheadoffset[LE,RO]{+0.025\textwidth}
\renewcommand{\chaptermark}[1]{\markboth{ #1}{}}
\renewcommand{\sectionmark}[1]{\markright{ #1}}

如果能有帮助的话,我正在写一本书,双面环境。

\documentclass[a4paper,12pt,twoside]{book}

提前致谢!

输出截图

答案1

正如我在评论中所述,章节编号\thesection必须通过标记来传达(因为\thechapter章节总是从新页开始,所以这不是必需的)。但是普通的 LaTeX 没有额外的标记来表示这个章节编号,所以我使用extramarks带有\extramarks命令 and \firstrightxmarkand 的包\firstleftxmark

在此处输入图片描述

\documentclass{book}

\usepackage{fancyhdr}
\usepackage{extramarks}
\usepackage{lipsum}

% Header
\pagestyle{fancy}
\renewcommand{\headrulewidth}{0.1pt}
\fancyhead[RE]{\nouppercase{\leftmark}}
\fancyhead[LO]{\nouppercase{\firstrightxmark}}
\fancyhead[RO]{\firstleftxmark}
\fancyhead[LE]{\thechapter}
\fancyfoot[RO, LE]{\thepage}
\fancyfoot[CO, CE]{}
\fancyheadoffset[LE,RO]{+0.025\textwidth}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\extramarks{\thesection}{#1}}

\begin{document}
\chapter{First Chapter}

\lipsum

\newpage

\section{First section}

\lipsum[1]

\section{Second section}

\lipsum[2]

\end{document}

答案2

\sectionmark重新定义为仅使用奇数页的左页眉的解决方法:

\renewcommand{\sectionmark}[1]{\markright{#1\hfill \thesection}}

平均能量损失

\documentclass[a4paper,12pt,twoside]{book}
\usepackage{lipsum}
\usepackage{fancyhdr}
  \pagestyle{fancy}
  \renewcommand{\headrulewidth}{0.1pt}
  \renewcommand{\chaptermark}[1]{\markboth{\thechapter\hfill #1}{}}
  \renewcommand{\sectionmark}[1]{\markright{#1\hfill \thesection}}
  \fancyhf{}
  \fancyhead[LE]{\leftmark}
  \fancyhead[LO]{\rightmark}
  \fancyfoot[RO, LE]{\thepage}
  \fancyfoot[CO, CE]{}
  \fancyheadoffset[LE,RO]{+0.025\textwidth}

\begin{document}
 \chapter{Chapter title}
  \lipsum[1-8]
 \section{First section}
  \lipsum[1]
 \section{Second section}
  \lipsum 
\end{document}

在此处输入图片描述

相关内容