很難使用 fancyhdr 和目錄

很難使用 fancyhdr 和目錄

前提:我是 fancyhdr 的新手。我定義了以下花式風格:

\lhead{}
\chead{\slshape\leftmark}
\rhead{}
\lfoot{}
\cfoot{\thepage}
\rfoot{}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}

正確繪製每章第一頁之外的所有頁面中的章節號和名稱、頂線、底線和頁碼。

因此,我透過以下方式重新定義了 plain 樣式:

\fancypagestyle{plain}{
\lhead{}
\chead{}
\rhead{}
\lfoot{}
\cfoot{\thepage}
\rfoot{}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0.4pt}}

正確繪製了每章第一頁的頂線、底線和頁碼(註:普通樣式缺少花式樣式的章節號和名稱)

現在,我對目錄有疑問。目錄的第一頁具有普通樣式(沒有章節號和名稱),但以下頁面具有花式樣式(具有章節號和名稱)。如何將普通樣式套用至目錄的所有頁面?

答案1

由於您正在使用該類book,我認為前面的所有內容都應位於plain頁面樣式下:

\documentclass[a4paper,oneside]{book}
\usepackage{fancyhdr}
\usepackage{etoolbox}
\usepackage{lipsum} % just for the example

\pagestyle{fancy}
\fancypagestyle{main}{%
  \fancyhf{}%
  \fancyhead[C]{\slshape\leftmark}%
  \fancyfoot[C]{\thepage}%
  \renewcommand{\headrulewidth}{0.4pt}%
  \renewcommand{\footrulewidth}{0.4pt}%
}
\fancypagestyle{plain}{%
  \fancyhf{}%
  \fancyfoot[C]{\thepage}%
  \renewcommand{\headrulewidth}{0pt}%
  \renewcommand{\footrulewidth}{0.4pt}%
}

\appto\frontmatter{\pagestyle{plain}}
\appto\mainmatter{\pagestyle{main}}

\begin{document}

\frontmatter
\tableofcontents

\mainmatter

%%% fill with some mock text
\def\testsec{\section{B}\lipsum}
\def\test{\chapter{A}
  \testsec
  \testsec
  \testsec
  \testsec
  \testsec
  \testsec
  \testsec
  \testsec
  \testsec
  \testsec
}
\test
\test
\test
\test
\test
\test
\test
\test
\end{document}

但是,我建議反對腳規則。

答案2

在真實文檔的序言中嘗試一下

\usepackage{etoc}
\renewcommand{\etocaftertitlehook}{\pagestyle{plain}}
\renewcommand{\etocaftertochook}{\thispagestyle{plain}}

(編輯替換\pagestyle上面\thispagestyle行的)

微量元素:

\documentclass{book}

\usepackage{fancyhdr}
\lhead{}
\chead{\slshape\leftmark}
\rhead{}
\lfoot{}
\cfoot{\thepage}
\rfoot{}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}
\fancypagestyle{plain}{
\lhead{}
\chead{}
\rhead{}
\lfoot{}
\cfoot{\thepage}
\rfoot{}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0.4pt}}


\usepackage{etoc}
\renewcommand{\etocaftertitlehook}{\pagestyle{plain}}
\renewcommand{\etocaftertochook}{\thispagestyle{plain}}

\begin{document}

\tableofcontents

\chapter{ONE}

\section {A}

\chapter{TW0}

\section {B}

\newcount\cnta
\loop
\chapter{\the\cnta}

\ifnum\cnta<50
\advance\cnta 1
\repeat
\end{document}

相關內容