> For the complete documentation index, see [llms.txt](https://overleaf-pro.ayaka.space/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://overleaf-pro.ayaka.space/latex/zh-cn/ge-shi-hua/02-headers-and-footers.md).

# 页眉和页脚

## 引言与概述

本文演示如何使用 [fancyhdr 宏包](https://ctan.org/pkg/fancyhdr?lang=en)。在配套文章中介绍的另一种自定义页眉和页脚的方法， [*LaTeX 如何排版页眉和页脚？*](/latex/zh-cn/shen-ru-wen-zhang/23-how-does-latex-typeset-headers-and-footers.md)，是深入研究 LaTeX 代码，以定义新的页面样式或重新定义相应的底层命令。

这里，我们先概述一些定义文档中默认页眉和页脚的基本 LaTeX 特性：

* [文档类](#document-classes)
* [LaTeX 页面样式](#latex-page-styles)

如果你想跳过导言部分，请直接跳到 [关于……的章节 `fancyhdr` 宏包](#using-the-fancyhdr-package)。如前所述，配套文章中包含更多技术细节 [*LaTeX 如何排版页眉和页脚？*](/latex/zh-cn/shen-ru-wen-zhang/23-how-does-latex-typeset-headers-and-footers.md) 其中探讨并解释了 TeX 引擎用于创建页眉和页脚的基于标记的底层机制，以及为什么需要 `\mark` 命令。

### LaTeX 文档类

不同类型的 LaTeX 文档是通过 *文档类*；例如，LaTeX 提供了若干标准文档类：

* `book`
* `报告`
* `article`
* `letter`

它们通过以下命令使用 `\documentclass` 命令设置为特定值：

```latex
\documentclass[⟨options⟩]{⟨class⟩}
```

其中：

* `⟨类⟩` 可以是标准类型之一（`book`, `报告`, `article`, `letter`）或由 [个人和机构贡献的类型](https://www.ctan.org/topic/class).
* `⟨选项⟩` 是用于配置文档某个实例的设置 `⟨类⟩`。可用的 `⟨选项⟩` 取决于 `⟨类⟩` 所使用的。

#### 单面或双面文档

如果某个 LaTeX 文档类支持左页（偶数页码）和右页（奇数页码）采用不同的版式，那么文档的页眉和页脚通常也会在左右页上有所不同。标准文档类的默认设置如下：

* `book`：双面
* `article` 和 `报告`：单面，但可通过使用选项 `twoside`；例如：

```latex
\documentclass[twoside]{article}
```

或

```latex
\documentclass[twoside]{report}
```

#### 文档类的默认⟨选项⟩

LaTeX 会应用一组默认 `⟨选项⟩` 到每个标准文档类：

* `article`：默认 `⟨选项⟩` 已 `letterpaper,10pt,oneside,onecolumn,final`
* `报告`：默认 `⟨选项⟩` 已 `letterpaper,10pt,oneside,onecolumn,final,openany`
* `book`：默认 `⟨选项⟩` 已 `letterpaper,10pt,twoside,onecolumn,final,openright`
* `letter`：默认 `⟨选项⟩` 已 `letterpaper,10pt,oneside,onecolumn,final`

文档类还提供命令，使用户能够将文档组织为诸如部分、章、节、小节等逻辑层次。正如你所 `book` 期望的那样，

### LaTeX 页面样式

在 LaTeX 中，文档页眉和页脚的默认格式由所使用的 *页面样式* 决定。LaTeX 提供了几种预定义页面样式：

* `空`：页面上没有页眉或页脚
* `plain`：没有页眉，页脚由居中的页码组成
* `headings`：没有页脚，页眉包含特定于文档类的信息和页码
* `myheadings`：没有页脚，页眉包含页码和用户提供的信息

#### 标准文档类的默认页面样式

除非用户另行指定，标准文档类会应用默认页面样式：

* 该 `article` 和 `报告` 各类使用 `plain`
* 该 `book` 该类使用 `headings`

#### 设置页面样式

LaTeX 核心提供两个命令来更改（设置）页面样式：

* `\pagestyle{⟨style⟩}`：将当前页面及其后所有页面的样式设置为 `⟨样式⟩`
* `\thispagestyle{⟨style⟩}`：仅将当前页面的样式设置为 `⟨样式⟩`

页码样式通过命令 `\pagenumbering{⟨numberstyle⟩}`；例如， `\pagenumbering{roman}` 将页码排版为小写罗马数字：i、ii、iii……

### LaTeX 页面布局

由于页眉和页脚是整体页面布局/设计的一部分，因此了解更多关于 LaTeX 页面布局模型的内容会很有帮助；以下 Overleaf 文章可能会让你感兴趣：

* 有关 LaTeX 页面布局的介绍，请参阅 Overleaf 文章 [页面大小和页边距](/latex/zh-cn/ge-shi-hua/07-page-size-and-margins.md) 演示如何使用 [**`geometry`** 宏包](https://ctan.org/pkg/geometry?lang=en);
* 如果你想了解更多细节，我们建议阅读文章 [LaTeX 页面布局参数可视化指南](/latex/zh-cn/shen-ru-wen-zhang/04-a-visual-guide-to-latex-s-page-layout-parameters.md).

### 标准 LaTeX 文档类和页面样式中的页眉和页脚

如前所述，LaTeX 提供了若干 [内置页面样式](#latex-page-styles) 它们被用作标准文档类的默认设置。以下简短示例演示标准文档类在使用 LaTeX 默认 `⟨选项⟩` 时所生成的默认页眉和页脚。

#### article 类的默认页眉和页脚

```latex
\documentclass{article}
% 选择一个方便的较小页面尺寸
\usepackage[paperheight=16cm,paperwidth=12cm,textwidth=10cm]{geometry}
\usepackage{lipsum}% for some dummy text
\title{An article class example}
\author{Overleaf}
\begin{document}
\maketitle

\section{In the beginning...}
\lipsum[2]

\section{Another section}
\lipsum[1]

\section{Yet another}
\lipsum[1]
```

[在 Overleaf 中打开此 **`article`** Overleaf 中的类示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=An+article+class+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%25+Choose+a+conveniently+small+page+size%0A%5Cusepackage%5Bpaperheight%3D16cm%2Cpaperwidth%3D12cm%2Ctextwidth%3D10cm%5D%7Bgeometry%7D%0A%5Cusepackage%7Blipsum%7D%25+for+some+dummy+text%0A%5Ctitle%7BAn+article+class+example%7D%0A%5Cauthor%7BOverleaf%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cmaketitle%0A%0A%5Csection%7BIn+the+beginning...%7D%0A%5Clipsum%5B2%5D%0A%0A%5Csection%7BAnother+section%7D%0A%5Clipsum%5B1%5D%0A%0A%5Csection%7BYet+another%7D%0A%5Clipsum%5B1%5D%0A%5Cend%7Bdocument%7D)

#### report 类的默认页眉和页脚

```latex
\documentclass{report}
% 选择一个方便的较小页面尺寸
\usepackage[paperheight=16cm,paperwidth=12cm,textwidth=10cm]{geometry}
\usepackage{lipsum}% for some dummy text
\title{A report class example}
\author{Overleaf}
\begin{document}
\maketitle
\chapter{One}
\section{In the beginning...}
\lipsum[2]

\section{Another section}
\lipsum[1]

\section{Yet another}
\lipsum[1]
```

[在 Overleaf 中打开此 **`报告`** Overleaf 中的类示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=A+report+class+example\&snip=%5Cdocumentclass%7Breport%7D%0A%25+Choose+a+conveniently+small+page+size%0A%5Cusepackage%5Bpaperheight%3D16cm%2Cpaperwidth%3D12cm%2Ctextwidth%3D10cm%5D%7Bgeometry%7D%0A%5Cusepackage%7Blipsum%7D%25+for+some+dummy+text%0A%5Ctitle%7BA+report+class+example%7D%0A%5Cauthor%7BOverleaf%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cmaketitle%0A%5Cchapter%7BOne%7D%0A%5Csection%7BIn+the+beginning...%7D%0A%5Clipsum%5B2%5D%0A%0A%5Csection%7BAnother+section%7D%0A%5Clipsum%5B1%5D%0A%0A%5Csection%7BYet+another%7D%0A%5Clipsum%5B1%5D%0A%5Cend%7Bdocument%7D)

#### book 类的默认页眉和页脚

```latex
\documentclass{book}
% 选择一个方便的较小页面尺寸
\usepackage[paperheight=16cm,paperwidth=12cm,textwidth=10cm]{geometry}
\usepackage{lipsum}% for some dummy text
\title{Memoirs of a \TeX{} user}
\author{Overleaf}
\begin{document}

\frontmatter
\maketitle
这是使用罗马数字的前言部分。

\mainmatter
\chapter{Where do I start}
\lipsum[4]

\section{In the beginning...}
\lipsum[1]

\section{Yet another section}
\lipsum[1]

\chapter{Things I remember}

\section{Oh and a further section}
\lipsum[1]
```

[在 Overleaf 中打开此 **`book`** Overleaf 中的类示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=A+book+class+example\&snip=%5Cdocumentclass%7Bbook%7D%0A%25+Choose+a+conveniently+small+page+size%0A%5Cusepackage%5Bpaperheight%3D16cm%2Cpaperwidth%3D12cm%2Ctextwidth%3D10cm%5D%7Bgeometry%7D%0A%5Cusepackage%7Blipsum%7D%25+for+some+dummy+text%0A%5Ctitle%7BMemoirs+of+a+%5CTeX%7B%7D+user%7D%0A%5Cauthor%7BOverleaf%7D%0A%5Cbegin%7Bdocument%7D%0A%0A%5Cfrontmatter%0A%5Cmaketitle%0AThis+is+frontmatter+which+uses+Roman+numerals.%0A%0A%5Cmainmatter%0A%5Cchapter%7BWhere+do+I+start%7D%0A%5Clipsum%5B4%5D%0A%0A%5Csection%7BIn+the+beginning...%7D%0A%5Clipsum%5B1%5D%0A%0A%5Csection%7BYet+another+section%7D%0A%5Clipsum%5B1%5D%0A%0A%5Cchapter%7BThings+I+remember%7D%0A%0A%5Csection%7BOh+and+a+further+section%7D%0A%5Clipsum%5B1%5D%0A%5Cend%7Bdocument%7D)

### 为“非标准” LaTeX 文档类更改页眉和页脚

更改页眉和页脚所需的宏包/命令会因所使用的文档类而异；例如， [TeX FAQ](https://texfaq.org/FAQ-fancyhdr#:~:text=The%20scrlayer%2Dscrpage%20package%20provides,and%20has%20several%20predefined%20styles) 指出使用了 [scrlayer-scrpage](https://ctan.org/pkg/scrlayer-scrpage?lang=en) 宏包，而不是 [fancyhdr](https://ctan.org/pkg/fancyhdr?lang=en)，以便与 [KOMA-script 文档类](https://ctan.org/pkg/koma-script)更好地集成。 [memoir 文档类](https://ctan.org/pkg/memoir?lang=en) 包含其自身用于更改页眉和页脚的功能（用户命令）。

## 使用 fancyhdr 宏包

要加载 `fancyhdr` 该宏包，请在文档导言区添加以下行：

```latex
\usepackage[⟨options⟩]{fancyhdr}
```

其中 `⟨选项⟩` 在……中进行了说明 [包文档](http://mirrors.ctan.org/macros/latex/contrib/fancyhdr/fancyhdr.pdf).

### fancyhdr 页眉和页脚的结构

下图概述了由……定义的页眉和页脚的一般结构 `fancyhdr` 在双面文档中的情况：

* 页眉和页脚可以在左页（偶数页）和右页（奇数页）包含不同内容
* 页眉和页脚被分为三个“区域”
  * **L***左*
  * **C***中*
  * **R***右*
* 页眉和页脚可以包含可选的横线（规则线）

在……中 **L** 区域中，内容左对齐排版；在 **C** 区域中，内容居中；在 **R** 区域中，内容右对齐排版。

![显示 fancyhdr 页眉和页脚结构的示意图](/files/5636f00d38cbeed6735d4df7484fd34bc1693bd5)

由……提供的命令 `fancyhdr` 使用单字母缩写，称为“坐标”，来标识特定的页眉/页脚区域和页面位置：

* **`O`** 或 **`E`**：用于指定 \*\*`O`\*\*奇\*\* 或 \*\*`E`\*\*偶\*\* 页
* **`H`** 或 **`F`**：表示 \*\*`H`\*\*页眉\*\* 或 \*\*`F`\*\*页脚\*\*
* **`L`**, **`C`** 或 **`R`**：用于 \*\*`L`\*\*左\*\*、\*\*`C`\*\*中\*\* 和 \*\*`R`\*\*右\*\*“区域”的页眉或页脚

### 命令简介

本节讨论若干 `fancyhdr` 用于定义页眉和页脚内容的命令：

* `\fancyhead[*locations*]{*content*}`
* `\fancyfoot[*locations*]{*content*}`
* `\fancyhf[*locations*]{*content*}`

这些命令都使用“坐标”来定义页眉和页脚的 `*位置*` 其中 `*内容*` 应该出现的位置。根据你使用的命令，可能需要（最多）2 个或（最多）3 个坐标才能完整指定所需的 `*位置*`.

#### 使用最多 2 个坐标的命令

以下命令需要最多 2 个坐标来指定页眉或页脚中的特定位置：

* `\fancyhead[*locations*]{*content*}`：假定 `**H**`页眉坐标
* `\fancyfoot[*locations*]{*content*}`：假定 `**F**`页脚坐标

其中 `*位置*` 是一个可选的、以逗号分隔的位置列表，由 1 个或 2 个坐标指定，用于放置 `*内容*`.

**双面文档示例**

以下示例取自 `fancyhdr` 文档：

```latex
\documentclass{book}
\usepackage[paperheight=16cm, paperwidth=12cm,% Set the height and width of the paper
includehead,
nomarginpar,% We don't want any margin paragraphs
textwidth=10cm,% Set \textwidth to 10cm
headheight=10mm,% Set \headheight to 10mm
]{geometry}
\usepackage{fancyhdr}
\begin{document}
% Set the page style to "fancy"...
\pagestyle{fancy}
%... then configure it.
\fancyhead{} % clear all header fields
\fancyhead[RO,LE]{\textbf{The performance of new graduates}}
\fancyfoot{} % clear all footer fields
\fancyfoot[LE,RO]{\thepage}
\fancyfoot[LO,CE]{From: K. Grant}
\fancyfoot[CO,RE]{To: Dean A. Smith}
% Some content:
This is page 1.\newpage
这是第 2 页。
\end{document}
```

[在 Overleaf 中打开此 `fancyhdr` Overleaf 中的示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=fancyhdr+package+example\&snip=%5Cdocumentclass%7Bbook%7D%0A%5Cusepackage%5Bpaperheight%3D16cm%2C+paperwidth%3D12cm%2C%25+Set+the+height+and+width+of+the+paper%0Aincludehead%2C%0Anomarginpar%2C%25+We+don%27t+want+any+margin+paragraphs%0Atextwidth%3D10cm%2C%25+Set+%5Ctextwidth+to+10cm%0Aheadheight%3D10mm%2C%25+Set+%5Cheadheight+to+10mm%0A%5D%7Bgeometry%7D%0A%5Cusepackage%7Bfancyhdr%7D%0A%5Cbegin%7Bdocument%7D%0A%25+Set+the+page+style+to+%22fancy%22...%0A%5Cpagestyle%7Bfancy%7D%0A%25...+then+configure+it.%0A%5Cfancyhead%7B%7D+%25+clear+all+header+fields%0A%5Cfancyhead%5BRO%2CLE%5D%7B%5Ctextbf%7BThe+performance+of+new+graduates%7D%7D%0A%5Cfancyfoot%7B%7D+%25+clear+all+footer+fields%0A%5Cfancyfoot%5BLE%2CRO%5D%7B%5Cthepage%7D%0A%5Cfancyfoot%5BLO%2CCE%5D%7BFrom%3A+K.+Grant%7D%0A%5Cfancyfoot%5BCO%2CRE%5D%7BTo%3A+Dean+A.+Smith%7D%0A%25+Some+content%3A%0AThis+is+page+1.%5Cnewpage%0AThis+is+page+2.%0A%5Cend%7Bdocument%7D)

下图显示了此示例产生的输出：

![使用 LaTeX fancyhdr 宏包创建的双面文档示例](/files/a691ecf1b827eec4b606437b60ae318e4ea27164)

**所用命令说明**

* **`\fancyhead{}`**：清除页眉设置
* **`\fancyfoot{}`**：清除页脚设置
* **`\fancyhead[RO,LE]{\textbf{The performance of new graduates}}`**：使用页眉位置 **`RO`** (\*\*`R`**右**`O`**页）和 `LE` (**`L`**左**`E`偶）来放置内容 `\textbf{The performance of new graduates}`
* **`\fancyfoot[LE,RO]{\thepage}`**：使用页脚位置 **`LE`** (\*\*`L`**左**`E`**偶）和 `RO` (**`R`**右**`O`页）来放置由……输出的页码 `\thepage`
* **`\fancyfoot[LO,CE]{From: K. Grant}`**：使用页脚位置 **`LO`** (\*\*`L`**左**`O`**页）和 `CE` (**`C`**中**`E`偶）来放置内容 `发件人：K. Grant`
* **`\fancyfoot[CO,RE]{To: Dean A. Smith}`**：使用页脚位置 **`CO`** (\*\*`C`**中**`O`**页）和 `RE` (**`R`**右**`E`偶）来放置内容 `收件人：Dean A. Smith`

**单面和双面文档**

该 [上图](#hfstructure) 展示了双面文档的页眉和页脚结构，例如 `book` 类，或 `article`/`报告` 使用该选项加载的类 `twoside`.

**单面文档示例**

对于 *单面* 基于……的文档 `article` 或 `报告` 类的文档中，实际上所有页面都是右页，因此用于……的“坐标” `**E**`页并不适用。我们也不 *需要* 需要指定 `**O**` 坐标，并且可以只使用一个 `**L**`, `**C**` 或 `**R**` 值：

```latex
\documentclass{article}
\usepackage[paperheight=16cm, paperwidth=12cm,% Set the height and width of the paper
includehead,
nomarginpar,% We don't want any margin paragraphs
textwidth=10cm,% Set \textwidth to 10cm
headheight=10mm,% Set \headheight to 10mm
]{geometry}
\usepackage{fancyhdr}
\begin{document}
% Set the page style to "fancy"...
\pagestyle{fancy}
\title{Single-sided document}
\author{Overleaf}
\date{2022 年 8 月}
\fancyhf{} % 清空现有页眉/页脚条目
% We don't need to specify the O coordinate
\fancyhead[R]{Hello}
\fancyfoot[L]{\thepage}
\maketitle
\section{引言}
一些内容。
\newpage
\section{Continued...}
\end{document}
```

[在 Overleaf 中打开此 `fancyhdr` Overleaf 中的示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Single-sided+fancyhdr+package+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%5Bpaperheight%3D16cm%2C+paperwidth%3D12cm%2C%25+Set+the+height+and+width+of+the+paper%0Aincludehead%2C%0Anomarginpar%2C%25+We+don%27t+want+any+margin+paragraphs%0Atextwidth%3D10cm%2C%25+Set+%5Ctextwidth+to+10cm%0Aheadheight%3D10mm%2C%25+Set+%5Cheadheight+to+10mm%0A%5D%7Bgeometry%7D%0A%5Cusepackage%7Bfancyhdr%7D%0A%5Cbegin%7Bdocument%7D%0A%25+Set+the+page+style+to+%22fancy%22...%0A%5Cpagestyle%7Bfancy%7D%0A%5Ctitle%7BSingle-sided+document%7D%0A%5Cauthor%7BOverleaf%7D%0A%5Cdate%7BAugust+2022%7D%0A%5Cfancyhf%7B%7D+%25+clear+existing+header%2Ffooter+entries%0A%25+We+don%27t+need+to+specify+the+O+coordinate%0A%5Cfancyhead%5BR%5D%7BHello%7D%0A%5Cfancyfoot%5BL%5D%7B%5Cthepage%7D%0A%5Cmaketitle%0A%5Csection%7BIntroduction%7D%0ASome+content.%0A%5Cnewpage%0A%5Csection%7BContinued...%7D%0A%5Cend%7Bdocument%7D)

该 `fancyhdr` 如果你尝试使用以下 `**E**` 坐标在单面文档中指定页眉或页脚，宏包会发出警告：

```latex
\documentclass{article}
\usepackage[paperheight=16cm, paperwidth=12cm,% Set the height and width of the paper
includehead,
nomarginpar,% We don't want any margin paragraphs
textwidth=10cm,% Set \textwidth to 10cm
headheight=10mm,% Set \headheight to 10mm
]{geometry}
\usepackage{fancyhdr}
\begin{document}
% Set the page style to "fancy"...
\pagestyle{fancy}
\title{Warning without twoside}
\author{Overleaf}
\date{July 2022}
\fancyhead[E]{Hello}% This triggers a warning!
\fancyfoot[E]{\thepage}% This triggers a warning!
\maketitle
\section{引言}
一些内容。
\newpage
\section{Continued...}
\end{document}
```

[在 Overleaf 中打开此 ***错误的*** `fancyhdr` Overleaf 中的示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=fancyhdr+package+warning+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%5Bpaperheight%3D16cm%2C+paperwidth%3D12cm%2C%25+Set+the+height+and+width+of+the+paper%0Aincludehead%2C%0Anomarginpar%2C%25+We+don%27t+want+any+margin+paragraphs%0Atextwidth%3D10cm%2C%25+Set+%5Ctextwidth+to+10cm%0Aheadheight%3D10mm%2C%25+Set+%5Cheadheight+to+10mm%0A%5D%7Bgeometry%7D%0A%5Cusepackage%7Bfancyhdr%7D%0A%5Cbegin%7Bdocument%7D%0A%25+Set+the+page+style+to+%22fancy%22...%0A%5Cpagestyle%7Bfancy%7D%0A%5Ctitle%7BWarning+without+twoside%7D%0A%5Cauthor%7BOverleaf%7D%0A%5Cdate%7BJuly+2022%7D%0A%5Cfancyhead%5BE%5D%7BHello%7D%25+This+triggers+a+warning%21%0A%5Cfancyfoot%5BE%5D%7B%5Cthepage%7D%25+This+triggers+a+warning%21%0A%5Cmaketitle%0A%5Csection%7BIntroduction%7D%0ASome+content.%0A%5Cnewpage%0A%5Csection%7BContinued...%7D%0A%5Cend%7Bdocument%7D)

如这张带注释的屏幕截图所示：

![来自 fancyhdr 宏包的警告示例](/files/7eb73edfada107a9103228dbf642099644cbb620)

上面的示例会产生以下警告：

```latex
fancyhdr 宏包警告：在输入行 23，\fancyhead 的 `E' 选项在未使用 twoside 选项时无效。
fancyhdr 宏包警告：在输入行 24，\fancyfoot 的 `E' 选项在未使用 twoside 选项时无效。
```

**在双面文档中使用单个坐标：省略 E 和 O**

如果你使用 `\fancyhead` 或 `\fancyfoot` 在双面文档中，但省略 **`E`** 和 **`O`** 某个位置的 *仅 1 个坐标*, **`L`**, **`C`** 或 **`R`**， `*内容*` 将被放置在该位置的 *偶数页和奇数页上*.

**示例**

以下示例演示省略 **`E`** 和 **`O`** 来自 `*位置*` 中指定的 `\fancyhead` 和 \\`fancyfoot`。它还使用以下命令：

* `\pagestyle{fancy}` 将页面样式设置为由……配置的样式 `fancyhdr`
* `\fancyhead{}\fancyfoot{}` 用于清除所有页眉和页脚（另见 `\fancyhf{}`)

```latex
\begin{document}
% Set the page style to "fancy"...
\pagestyle{fancy}
%... then configure it.

% Clear all headers and footers (see also \fancyhf{})
\fancyhead{}\fancyfoot{}

% Set the Centre header location but do not specify O or E
\fancyhead[C]{In the centre of the header on all pages: \thepage}

% Set the Left footer location but do not specify O or E
\fancyfoot[L]{On the left of the footer on all pages: \thepage}

% Some content:
This is page 1.\newpage
这是第 2 页。
```

[在 Overleaf 中打开此 `fancyhdr` Overleaf 中的示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=fancyhdr+package+example\&snip=%5Cdocumentclass%7Bbook%7D%0A%5Cusepackage%5Bpaperheight%3D16cm%2C+paperwidth%3D12cm%2C%25+Set+the+height+and+width+of+the+paper%0Aincludehead%2C%0Anomarginpar%2C%25+We+don%27t+want+any+margin+paragraphs%0Atextwidth%3D10cm%2C%25+Set+%5Ctextwidth+to+10cm%0Aheadheight%3D10mm%2C%25+Set+%5Cheadheight+to+10mm%0A%5D%7Bgeometry%7D%0A%5Cusepackage%7Bfancyhdr%7D%0A%5Cbegin%7Bdocument%7D%0A%25+Set+the+page+style+to+%22fancy%22...%0A%5Cpagestyle%7Bfancy%7D%0A%25...+then+configure+it.%0A%0A%25+Clear+all+headers+and+footers+%28see+also+%5Cfancyhf%7B%7D%29%0A%5Cfancyhead%7B%7D%5Cfancyfoot%7B%7D%0A%0A%25+Set+the+Centre+header+location+but+do+not+specify+O+or+E%0A%5Cfancyhead%5BC%5D%7BIn+the+centre+of+the+header+on+all+pages%3A+%5Cthepage%7D%0A%0A%25+Set+the+Left+footer+location+but+do+not+specify+O+or+E%0A%5Cfancyfoot%5BL%5D%7BOn+the+left+of+the+footer+on+all+pages%3A+%5Cthepage%7D%0A%0A%25+Some+content%3A%0AThis+is+page+1.%5Cnewpage%0AThis+is+page+2.%0A%5Cend%7Bdocument%7D)

下图是该代码生成输出的注释版本：

![在双面 LaTeX 文档中使用单个 fancyhdr 坐标](/files/9ffdeb3f49228f78865538e212c22964bc1d282d)

从这张图可以看出，以下代码

```latex
\fancyhead[C]{In the centre of the header on all pages: \thepage}
```

设置 \*\*`C`\*\*中\*\* 页眉位置，但未指定它是否应出现在 \*\*`O`\*\*奇\*\* 或 \*\*`E`\*\*偶数页。省略 **`O`** 和 **`E`** 坐标会导致 \*\*`C`\*\*中\*\* 区域的页眉内容同时出现在奇数页和偶数页上。

此外，这段代码片段

```latex
\fancyfoot[L]{On the left of the footer on all pages: \thepage}
```

设置 \*\*`L`\*\*左\*\* 页脚位置，但未指定其是否应出现在奇数页或偶数页上。省略 **`O`** 和 **`E`** 坐标会导致 \*\*`L`\*\*左\*\* 区域内容会同时出现在奇数页和偶数页的页脚左侧区域。

**在双面文档中使用单个坐标：省略 L、C 和 R**

如果你省略 **`L`**, **`C`** 和 **`R`** 在双面文档的某个位置，并使用 *仅 1 个坐标*, **`E`** 或 **`O`**， `*内容*` 将会 *放置在所有三个区域中* 在该位置指定的偶数页或奇数页上。

**示例**

以下示例为奇数页和偶数页设置了页眉和页脚，但没有任何 `*位置*` 文件夹中添加一个空文本文件。 `\fancyhead` 和 `\fancyfoot` 命令指定“区域”， **`L`**, **`C`** 或 **`R`**&#x5373; `*内容*` 应放置其中。注意 `*内容*` 会在所有三个区域中重复出现。

```latex
\begin{document}
% Set the page style to "fancy"...
\pagestyle{fancy}
%... then configure it.

% Clear all headers and footers (see also \fancyhf{})
\fancyhead{}\fancyfoot{}

% Set the header and footer for Even
% pages but omit the zone (L, C or R)
\fancyhead[E]{Header: even page \thepage}
\fancyfoot[E]{Footer: even page \thepage}

% Set the header and footer for Odd
% pages but omit the zone (L, C or R)
\fancyhead[O]{Header: odd page \thepage}
\fancyfoot[O]{Footer: odd page \thepage}

% Some content:
This is page 1.\newpage
这是第 2 页。
```

[在 Overleaf 中打开此 `fancyhdr` Overleaf 中的示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=fancyhdr+package+example\&snip=%5Cdocumentclass%7Bbook%7D%0A%5Cusepackage%5Bpaperheight%3D16cm%2C+paperwidth%3D12cm%2C%25+Set+the+height+and+width+of+the+paper%0Aincludehead%2C%0Anomarginpar%2C%25+We+don%27t+want+any+margin+paragraphs%0Atextwidth%3D10cm%2C%25+Set+%5Ctextwidth+to+10cm%0Aheadheight%3D10mm%2C%25+Set+%5Cheadheight+to+10mm%0A%5D%7Bgeometry%7D%0A%5Cusepackage%7Bfancyhdr%7D%0A%5Cbegin%7Bdocument%7D%0A%25+Set+the+page+style+to+%22fancy%22...%0A%5Cpagestyle%7Bfancy%7D%0A%25...+then+configure+it.%0A%0A%25+Clear+all+headers+and+footers+%28see+also+%5Cfancyhf%7B%7D%29%0A%5Cfancyhead%7B%7D%5Cfancyfoot%7B%7D%0A%0A%25+Set+the+header+and+footer+for+Even%0A%25+pages+but+omit+the+zone+%28L%2C+C+or+R%29%0A%5Cfancyhead%5BE%5D%7BHeader%3A+even+page+%5Cthepage%7D%0A%5Cfancyfoot%5BE%5D%7BFooter%3A+even+page+%5Cthepage%7D%0A%0A%25+Set+the+header+and+footer+for+Odd%0A%25+pages+but+omit+the+zone+%28L%2C+C+or+R%29%0A%5Cfancyhead%5BO%5D%7BHeader%3A+odd+page+%5Cthepage%7D%0A%5Cfancyfoot%5BO%5D%7BFooter%3A+odd+page+%5Cthepage%7D%0A%0A%25+Some+content%3A%0AThis+is+page+1.%5Cnewpage%0AThis+is+page+2.%0A%5Cend%7Bdocument%7D)

下图是该代码生成输出的注释版本：

![](/files/50c881b06ac52878cdb63da4b556553709cff69f)

以下代码为……设置页眉和页脚 **`E`偶数页，但省略了一个区域坐标（`L`**, **`C`** 或 **`R`**):

```latex
\fancyhead[E]{Header: even page \thepage}
\fancyfoot[E]{Footer: even page \thepage}
```

这会导致：

* **偶数页页眉** 包含 `Header: even page \thepage` 在所有三个页眉区域（**`L`**, **`C`** 和 **`R`**)
* **偶数页页脚** 包含 `Footer: even page \thepage` 在所有三个页脚区域（**`L`**, **`C`** 和 **`R`**)

以下代码为……设置页眉和页脚 **`O`dd 页，但也省略了一个区域坐标（`L`**, **`C`** 或 **`R`**):

```latex
\fancyhead[O]{Header: odd page \thepage}
\fancyfoot[O]{Footer: odd page \thepage}
```

这会产生与偶数页类似的结果：

* **奇数页页眉** 包含 `页眉：奇数页 \\thepage` 在所有三个页眉区域（**`L`**, **`C`** 和 **`R`**)
* **奇数页页脚** 包含 `页脚：奇数页 \\thepage` 在所有三个页脚区域（**`L`**, **`C`** 和 **`R`**)

#### \\\fancyhf：使用最多 3 个坐标

`\\fancyhf` 是一个更通用的命令，可用于组合页眉和页脚的设置。为此，它支持坐标 **`H`** （用于 \*\*`H`\*\*页眉）和 **`F`** （用于 \*\*`F`\*\*页脚）。命令的一般形式为

* `\fancyhf[*locations*]{*content*}`

其中 `*位置*` 是一个可选的、以逗号分隔的位置列表，通过 2 个或 3 个坐标指定，用于放置 `*内容*`.

由于该命令可以指定页眉的内容 *和* 和页脚的内容，因此你需要第三个坐标， **`H`** 或 **`F`** 才能完整指定 `*位置*`。例如，如果要使用（引用）左侧（偶数）页面页脚的中心区域，可以将其指定为三个坐标 **`EFC`**:

* \*\*`E`\*\*偶数页
* \*\*`F`\*\*页脚\*\*
* \*\*`C`\*\*中心区域

注意，坐标的顺序并不重要： **`EFC`** 产生的结果与 **`FEC`** 等等。

下面的示例使用 `\\fancyhf` 用于将内容放置在 \*\*`C`所有页眉和页脚的 \*\*中心区域：

```latex
\begin{document}
\pagestyle{fancy}
\fancyhf{}
\\fancyhf[EHC]{偶数+页眉+居中}
\\fancyhf[EFC]{偶数+页脚+居中}
\\fancyhf[OFC]{奇数+页脚+居中}
\\fancyhf[OHC]{奇数+页眉+居中}
\\lipsum[1]\\newpage\\lipsum[1]
```

[在 Overleaf 中打开此 `\\fancyhf` Overleaf 中的示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Example+of+the+fancyhf+command\&snip=%5Cdocumentclass%7Bbook%7D%0A%5Cusepackage%5Bpaperheight%3D16cm%2C+paperwidth%3D12cm%2C%25+Set+the+height+and+width+of+the+paper%0Aincludehead%2C%0Anomarginpar%2C%25+We+don%27t+want+any+margin+paragraphs%0Atextwidth%3D10cm%2C%25+Set+%5Ctextwidth+to+10cm%0Aheadheight%3D10mm%2C%25+Set+%5Cheadheight+to+10mm%0A%5D%7Bgeometry%7D%0A%5Cusepackage%7Blipsum%7D%0A%5Cusepackage%7Bfancyhdr%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cpagestyle%7Bfancy%7D%0A%5Cfancyhf%7B%7D%0A%5Cfancyhf%5BEHC%5D%7BEven%2BHeader%2BCentre%7D%0A%5Cfancyhf%5BEFC%5D%7BEven%2BFooter%2BCentre%7D%0A%5Cfancyhf%5BOFC%5D%7BOdd%2BFooter%2BCentre%7D%0A%5Cfancyhf%5BOHC%5D%7BOdd%2BHeader%2BCentre%7D%0A%5Clipsum%5B1%5D%5Cnewpage%5Clipsum%5B1%5D%0A%5Cend%7Bdocument%7D)

下面的图是对前一个示例输出的带注释版本：

![使用 fancyhdr \fancyhf 命令的示例](/files/f879e863ebce957e3ef65926b1614887cae8dd67)

### 示例

#### 在页眉和页脚中使用基本规则（线条）

以下与规则（线条）相关的命令由 `fancyhdr`:

* **`\\headrulewidth`**：定义页眉下方线条粗细的宏
* **`\\footrulewidth`**：定义页脚上方线条粗细的宏
* **`\\headruleskip`**：定义线条与页眉文本之间距离的宏（仅从 4.0 版开始可用）
* **`\\footruleskip`**：定义线条与页脚文本之间距离的宏
* **`\\headrule`** 完全重新定义页眉规则（线条）的宏
* **`\\footrule`** 完全重新定义页脚规则（线条）的宏
* **`\\headwidth`** 定义页眉和页脚总宽度的长度参数

**注**:

* \\\headrulewidth 的默认值 `\\headrulewidth` 是 `0.4pt` 以及\\\footrulewidth 的 `\\footrulewidth` 是 `0pt` 因此，默认情况下，该线不会显示在页脚中。
* 你必须使用 `\renewcommand` 来更改由 `\\headrulewidth` 或 `\\footrulewidth`
* 同样，使用 `\renewcommand` 来更改页眉或页脚文本之间的间距， `\\headruleskip` 或 `\\footruleskip` 分别

例如：

```latex
\\renewcommand{\\headrulewidth}{2pt}
\\renewcommand{\\footrulewidth}{4pt}
\\renewcommand{\\headruleskip}{10mm}
\\renewcommand{\\footruleskip}{10pt}
```

* **注意**：这是 *因为* 以下命令 *宏*
* * `\\headrulewidth`
  * `\\footrulewidth`
  * `\\headruleskip`
  * `\\footruleskip`

你 *必须* 使用 `\renewcommand` 来更改这些命令存储的值。以下代码 *将不起作用*:

```latex
\\setlength{\\headrulewidth}{2pt}% 不起作用
\\setlength{\\headruleskip}{10mm}% 同样也不起作用
```

下一个示例会更改规则线宽，并展示如何修改规则（线条）与页眉和页脚文本之间的距离——使用 `\\headruleskip` 和 `\\footruleskip`:

```latex
\begin{document}
% Set the page style to "fancy"...
\pagestyle{fancy}
%... then configure it.
\\renewcommand{\\headruleskip}{10mm}
\\renewcommand{\\footruleskip}{10pt}
\\renewcommand{\\headrulewidth}{2pt}
\\renewcommand{\\footrulewidth}{4pt}
% Some content:
\\chapter{整理设置}
\\section{第 1 页}
这是第 1 页。
\newpage
\\section{第 2 页}
这是第 2 页。
\end{document}
```

[在 Overleaf 中打开此 `fancyhdr` Overleaf 中的示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=fancyhdr+package+example\&snip=%5Cdocumentclass%7Bbook%7D%0A%5Cusepackage%5Bpaperheight%3D16cm%2Cpaperwidth%3D12cm%2C%25+Set+the+height+and+width+of+the+paper%0Aincludehead%2C%0Anomarginpar%2C%25+We+don%27t+want+any+margin+paragraphs%0Atextwidth%3D10cm%2C%25+Set+%5Ctextwidth+to+10cm%0A%25+Set+%5Cheadheight+to+15mm+to+accommodate%0A%25+value+of+%5Cheadruleskip%0Aheadheight%3D15mm%2C%0A%5D%7Bgeometry%7D%0A%5Cusepackage%7Bfancyhdr%7D%0A%5Cbegin%7Bdocument%7D%0A%25+Set+the+page+style+to+%22fancy%22...%0A%5Cpagestyle%7Bfancy%7D%0A%25...+then+configure+it.%0A%5Crenewcommand%7B%5Cheadruleskip%7D%7B10mm%7D%0A%5Crenewcommand%7B%5Cfootruleskip%7D%7B10pt%7D%0A%5Crenewcommand%7B%5Cheadrulewidth%7D%7B2pt%7D%0A%5Crenewcommand%7B%5Cfootrulewidth%7D%7B4pt%7D%0A%25+Some+content%3A%0A%5Cchapter%7BWrangling+settings%7D%0A%5Csection%7BPage+1%7D%0AThis+is+page+1.%0A%5Cnewpage%0A%5Csection%7BPage+2%7D%0AThis+is+page+2.%0A%5Cend%7Bdocument%7D)

#### 在页眉中使用花式规则

下面的示例改编自 `fancyhdr` 宏包文档中的一个示例，该示例使用了 [`fourier-orns` 宏包](https://ctan.org/pkg/fourier?lang=en).

```latex
\documentclass{book}
\usepackage[paperheight=16cm, paperwidth=12cm,% Set the height and width of the paper
includehead,
nomarginpar,% We don't want any margin paragraphs
textwidth=10cm,% Set \textwidth to 10cm
headheight=14pt,% 将 \\headheight 设为 14pt，以容纳我们的花式规则！
]{geometry}
\\usepackage{fourier-orns}
\usepackage{fancyhdr}
\\renewcommand{\\headrule}{%
\\vspace{-8pt}\\hrulefill
\\raisebox{-2.1pt}{\\quad\\decofourleft\\decotwo\\decofourright\\quad}\\hrulefill}
\\title{装饰线}
\author{Overleaf}
\date{2022 年 8 月}
\begin{document}
\pagestyle{fancy}
\fancyhf{}
\\fancyhead[LE]{\\nouppercase{\\rightmark\\hfill\\leftmark}}
\\fancyhead[RO]{\\nouppercase{\\leftmark\\hfill\\rightmark}}
\\fancyfoot[LE,RO]{\\hfill\\thepage\\hfill}
\\chapter{花式规则！}
\section{引言}
此示例取自 \\texttt{fancyhdr} 文档。
\newpage
\\section{查看页眉}
\newpage
\\section{是的，非常整洁！}
\end{document}
```

[在 Overleaf 中打开此示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Drawing+fancy+rules+in+headers\&snip=%5Cdocumentclass%7Bbook%7D%0A%5Cusepackage%5Bpaperheight%3D16cm%2C+paperwidth%3D12cm%2C%25+Set+the+height+and+width+of+the+paper%0Aincludehead%2C%0Anomarginpar%2C%25+We+don%27t+want+any+margin+paragraphs%0Atextwidth%3D10cm%2C%25+Set+%5Ctextwidth+to+10cm%0Aheadheight%3D14pt%2C%25+Set+%5Cheadheight+to+14pt+to+accommodate+our+fancy+rule%21%0A%5D%7Bgeometry%7D%0A%5Cusepackage%7Bfourier-orns%7D%0A%5Cusepackage%7Bfancyhdr%7D%0A%5Crenewcommand%7B%5Cheadrule%7D%7B%25%0A%5Cvspace%7B-8pt%7D%5Chrulefill%0A%5Craisebox%7B-2.1pt%7D%7B%5Cquad%5Cdecofourleft%5Cdecotwo%5Cdecofourright%5Cquad%7D%5Chrulefill%7D%0A%5Ctitle%7BDecorative+line%7D%0A%5Cauthor%7BOverleaf%7D%0A%5Cdate%7BAugust+2022%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cpagestyle%7Bfancy%7D%0A%5Cfancyhf%7B%7D%0A%5Cfancyhead%5BLE%5D%7B%5Cnouppercase%7B%5Crightmark%5Chfill%5Cleftmark%7D%7D%0A%5Cfancyhead%5BRO%5D%7B%5Cnouppercase%7B%5Cleftmark%5Chfill%5Crightmark%7D%7D%0A%5Cfancyfoot%5BLE%2CRO%5D%7B%5Chfill%5Cthepage%5Chfill%7D%0A%5Cchapter%7BFancy+rules%21%7D%0A%5Csection%7BIntroduction%7D%0AThis+example+is+taken+from+the+%5Ctexttt%7Bfancyhdr%7D+documentation.%0A%5Cnewpage%0A%5Csection%7BSee+the+header%7D%0A%5Cnewpage%0A%5Csection%7BYes%2C+very+neat%21%7D%0A%5Cend%7Bdocument%7D)

此示例生成以下输出：

![通过 fourier-orns 宏包在页眉中创建花式规则的示例](/files/e52461b9ad988e3c2b1bc3c5cd2b73ca45b8e3ee)

* 本示例中命令的说明
  * **`\\headrule`**：定义如何绘制页眉中的装饰线的宏。这里将其重新定义为绘制一条包含来自 [`fourier-orns` 宏包](https://ctan.org/pkg/fourier?lang=en).
  * **`\\leftmark`** 和 **`\\rightmark`**：这些命令用于获取由 LaTeX 的分节命令插入的“标记数据”。如需更完整地解释这些命令，我们推荐配套文章 [*LaTeX 如何排版页眉和页脚？*](/latex/zh-cn/shen-ru-wen-zhang/23-how-does-latex-typeset-headers-and-footers.md)
  * **`\\nouppercase`**：由 `fancyhdr` 宏包提供，用于防止页眉或页脚文本以大写排版，这是 LaTeX 页眉和页脚的默认样式

#### fancyhdr 警告：\\\headheight 过小

如果你收到 `fancyhdr` 警告，称 `\\headheight` 过小，你可以 *可以* 将其重设为某个值 `高度` ，通过编写

```latex
\\setlength{\\headheight}{height}
```

但 [推荐的解决方案是使用 `geometry` 宏包](https://tex.stackexchange.com/a/100408) 并应用其 `headheight=*height*` 选项来设置 `\\headheight` 更改为 `*height*`.

下面的示例重新使用我们前面的 book 类示例，并通过 `\\headheight` 将 `headheight=2cm`:

```latex
\documentclass{book}
\usepackage[paperheight=16cm, paperwidth=12cm,% Set the height and width of the paper
includehead,% 见下文解释
nomarginpar,% We don't want any margin paragraphs
textwidth=10cm,% Set \textwidth to 10cm
headheight=2cm,% 将 \\headheight 设为 2cm
showframe % 显示页面布局
]{geometry}
\title{Memoirs of a \TeX{} user}
\author{Overleaf}
\begin{document}

\frontmatter
\maketitle
这是使用罗马数字的前言部分。

\mainmatter
\chapter{Where do I start}
第 1 章：一个在第 1 页结束的短章节。

\chapter{Things I remember}
第 2 章：从第 3 页开始，因此 \\LaTeX{} 会插入第 2 页这个“空白”页。
\end{document}
```

[在 Overleaf 中打开：设置 `\\headheight` 使用 geometry 宏包的示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Setting+headheight+using+the+geometry+package\&snip=%5Cdocumentclass%7Bbook%7D%0A%5Cusepackage%5Bpaperheight%3D16cm%2C+paperwidth%3D12cm%2C%25+Set+the+height+and+width+of+the+paper%0Aincludehead%2C%25+See+below+for+an+explanation%0Anomarginpar%2C%25+We+don%27t+want+any+margin+paragraphs%0Atextwidth%3D10cm%2C%25+Set+%5Ctextwidth+to+10cm%0Aheadheight%3D2cm%2C%25+Set+%5Cheadheight+to+2cm%0Ashowframe+%25+Show+the+page+layout%0A%5D%7Bgeometry%7D%0A%5Ctitle%7BMemoirs+of+a+%5CTeX%7B%7D+user%7D%0A%5Cauthor%7BOverleaf%7D%0A%5Cbegin%7Bdocument%7D%0A%0A%5Cfrontmatter%0A%5Cmaketitle%0AThis+is+frontmatter+which+uses+Roman+numerals.%0A%0A%5Cmainmatter%0A%5Cchapter%7BWhere+do+I+start%7D%0AChapter+1%3A+A+short+chapter+that+ends+on+page+1.%0A%0A%5Cchapter%7BThings+I+remember%7D%0AChapter+2%3A+Starts+on+page+3%2C+so+%5CLaTeX%7B%7D+inserts+a+%60%60blank%27%27+page+2.%0A%5Cend%7Bdocument%7D)

**关于 geometry 宏包的说明**

上面的示例使用了 `geometry` 宏包的示例代码：

* `nomarginpar`：表示我们不希望有任何边注段落
* `textwidth=10cm`：为 `\textwidth`，LaTeX 的一项 [页面布局参数](/latex/zh-cn/shen-ru-wen-zhang/04-a-visual-guide-to-latex-s-page-layout-parameters.md)
* `headheight=2cm`：为其设置一个值（2mm） `\\headheight`，LaTeX 的一项 [页面布局参数](/latex/zh-cn/shen-ru-wen-zhang/04-a-visual-guide-to-latex-s-page-layout-parameters.md)
* `showframe`：绘制页面布局

该 `geometry` 宏包还提供一些选项，会影响它如何计算 LaTeX 页面布局参数中的某些值；例如：

* `includehead`
* `includefoot`

我们上面的示例使用了 `includehead` ，以确保文本区域的高度会随着 `headheight`值的变化而直接受到影响。下面的示意图摘自 `geometry` 宏包文档，展示了 `includehead` 和 `includefoot` 选项如何影响文档文本高度的计算：

![来自 geometry 宏包文档的图片，展示 includehead 和 includefoot 选项如何影响文档文本高度的计算](/files/7fa581f219ffc6e0c053b6d7febfebbc8b2f1225)

请注意， `geometry` 宏包通过选项 `textheight` 来指代文本区域的高度，而 LaTeX 使用同名的 [`\\textheight` 页面布局参数](/latex/zh-cn/shen-ru-wen-zhang/04-a-visual-guide-to-latex-s-page-layout-parameters.md#notes-on-the-page-layout-parameters-and-equations).

#### 包含第 X 页/共 N 页的页脚

来源：tex.stackexchange 上发布的代码的编辑版： <https://tex.stackexchange.com/a/282840>.

此示例创建一个页脚，显示当前页码和总页数。它使用 [`lastpage` 宏包](https://ctan.org/pkg/lastpage?lang=en) 与修改版的 `plain` 页面样式一起，通过 `\\fancypagestyle` 由 `fancyhdr`.

```latex
\documentclass[12pt]{article}
\usepackage[paperheight=16cm, paperwidth=12cm,% Set the height and width of the paper
includehead,% 见下文解释
nomarginpar,% We don't want any margin paragraphs
textwidth=10cm,% Set \textwidth to 10cm
headheight=16pt % 将 \\headheight 设为 16pt，以避免 fancyhdr 警告
]{geometry}
\usepackage{lastpage}
\usepackage{fancyhdr}
\\usepackage{lipsum} % 仅用于示例文本
\pagestyle{fancy}
\\renewcommand{\\headrulewidth}{0pt}
\\fancyfoot[C]{第 \\thepage 页，共 \\pageref{LastPage} 页}
\\fancypagestyle{plain}{%
  \\renewcommand{\\headrulewidth}{0pt}%
  \\fancyhf{}%
  \\fancyfoot[C]{第 \\thepage 页，共 \\pageref{LastPage} 页}}
\begin{document}
\\title{另一个 fancyhdr 演示}
\\author{\\texttt{tex.stackexchange} \\textit{等}}
\maketitle

\\lipsum
\end{document}
```

[在 Overleaf 中打开此示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Footers+with+page+X+of+Y\&snip=%5Cdocumentclass%5B12pt%5D%7Barticle%7D%0A%5Cusepackage%5Bpaperheight%3D16cm%2C+paperwidth%3D12cm%2C%25+Set+the+height+and+width+of+the+paper%0Aincludehead%2C%25+See+below+for+an+explanation%0Anomarginpar%2C%25+We+don%27t+want+any+margin+paragraphs%0Atextwidth%3D10cm%2C%25+Set+%5Ctextwidth+to+10cm%0Aheadheight%3D16pt+%25+Set+%5Cheadheight+to+16pt+to+avoid+fancyhr+warnings%0A%5D%7Bgeometry%7D%0A%5Cusepackage%7Blastpage%7D%0A%5Cusepackage%7Bfancyhdr%7D%0A%5Cusepackage%7Blipsum%7D+%25+just+for+mock+text%0A%5Cpagestyle%7Bfancy%7D%0A%5Crenewcommand%7B%5Cheadrulewidth%7D%7B0pt%7D%0A%5Cfancyfoot%5BC%5D%7B%5Cthepage%5C+of+%5Cpageref%7BLastPage%7D%7D%0A%5Cfancypagestyle%7Bplain%7D%7B%25%0A++%5Crenewcommand%7B%5Cheadrulewidth%7D%7B0pt%7D%25%0A++%5Cfancyhf%7B%7D%25%0A++%5Cfancyfoot%5BC%5D%7B%5Cthepage%5C+of+%5Cpageref%7BLastPage%7D%7D%7D%0A%5Cbegin%7Bdocument%7D%0A%5Ctitle%7BAnother+fancyhdr+demo%7D%0A%5Cauthor%7B%5Ctexttt%7Btex.stackexchange%7D+%5Ctextit%7Bet+al%7D%7D%0A%5Cmaketitle%0A%0A%5Clipsum%0A%5Cend%7Bdocument%7D)

#### 创建完全空白的页面：没有页眉或页脚

如 [`fancyhdr` 包文档](http://mirrors.ctan.org/macros/latex/contrib/fancyhdr/fancyhdr.pdf)中所述，

* 该 `book` 类 *没有* 该 `openany` 选项，或者
* 该 `报告` 使用带有 `openright` 选项

任何部分（`\\part{...}`）以及章节（`\\chapter{...}`）页面将从奇数页（右手页）开始。

有时，LaTeX 需要插入一个“空白”页，以确保下一章或下一部分从右手页开始——但那个“空白”页仍会包含页眉，许多人认为这很难看。

**book 类文档中的“空白”页示例**

以下 `book` 该类示例会创建 5 页，其中包括 2 个右手章节页。

```latex
\begin{document}

\frontmatter
\maketitle
这是使用罗马数字的前言部分。

\mainmatter
\chapter{Where do I start}
第 1 章：一个在第 1 页结束的短章节。

\chapter{Things I remember}
第 2 章：从第 3 页开始，因此 \\LaTeX{} 会插入第 2 页这个“空白”页。

\end{document}
```

[在 Overleaf 中打开此 `book` Overleaf 中的类示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Demonstrating+blank+pages\&snip=%5Cdocumentclass%7Bbook%7D%0A%25+Choose+a+conveniently+small+page+size%0A%5Cusepackage%5Bpaperheight%3D16cm%2Cpaperwidth%3D12cm%2Ctextwidth%3D10cm%5D%7Bgeometry%7D%0A%5Ctitle%7BMemoirs+of+a+%5CTeX%7B%7D+user%7D%0A%5Cauthor%7BOverleaf%7D%0A%5Cbegin%7Bdocument%7D%0A%0A%5Cfrontmatter%0A%5Cmaketitle%0AThis+is+frontmatter+which+uses+Roman+numerals.%0A%0A%5Cmainmatter%0A%5Cchapter%7BWhere+do+I+start%7D%0AChapter+1%3A+A+short+chapter+that+ends+on+page+1.%0A%0A%5Cchapter%7BThings+I+remember%7D%0AChapter+2%3A+Starts+on+page+3%2C+so+%5CLaTeX%7B%7D+inserts+a+%60%60blank%27%27+page+2.%0A%0A%5Cend%7Bdocument%7D)

如下图注释所示，chapter 2 从第 3 页开始，因为 LaTeX 插入了第 2 页，这一页名义上是“空白”的，但却带有一个难看的页眉。

![book 类文档中的空白页示例](/files/099fbf5a8e096c686370432f489b9317fd880462)

各种机制——代码和宏包——都可用于确保插入的页面，例如上面的第 2 页，完全空白。下面是一些示例：

* 使用 `emptypage` 宏包，只需在文档导言区添加以下行：

```latex
\usepackage{emptypage}
```

* 在第 1 章末尾手动插入一个空白页，这样第 2 章就会自然从右手页开始，从而无需 LaTeX 强制新起一页。宏包文档中建议使用以下代码： `fancyhdr` 宏包文档：

```latex
\clearpage
\begingroup
\\pagestyle{empty}\\cleardoublepage
\endgroup
```

* 同样，你可以在第 1 章末尾创建一个新页，并使用以下方式将其样式设为空： `\thispagestyle{empty}`:

```latex
\\newpage\\thispagestyle{empty}
```

有关使用真正空白页的更多信息和示例，请查阅 TUG FAQ，或者在 [空白页](https://texfaq.org/FAQ-reallyblank) 或搜索 [带有标签的问题 `blank-page` 在 TeX.stackechange 上](https://tex.stackexchange.com/questions/tagged/blank-page).

* **提示**：TeX FAQ 中有一条题为 [“去掉页码”](https://texfaq.org/FAQ-nopageno%7CHow) 的条目，其中包含一些有用的建议。

#### 更改单个页面的样式

有时，修改特定页面的样式很方便；例如，移除当前章节页的页眉和页脚：

```latex
\begin{document}
\maketitle
\\chapter{使用不同的页面样式}
\\lipsum[1] % 通过 \\usepackage{lipsum}
\\chapter{示例章节}
\\thispagestyle{empty} % 移除章节页的页眉/页脚
\lipsum[1]
\clearpage
\\section{新节}
\lipsum[1]
\end{document}
```

[在 Overleaf 中打开此示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Setting+the+style+of+a+single+page\&snip=%5Cdocumentclass%7Bbook%7D%0A%5Cusepackage%7Blipsum%7D%0A%5Ctitle%7BChanging+the+style+of+a+single+page%7D%0A%5Cauthor%7BOverleaf%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cmaketitle%0A%5Cchapter%7BUsing+different+page+styles%7D%0A%5Clipsum%5B1%5D+%25+via+%5Cusepackage%7Blipsum%7D%0A%5Cchapter%7BSample+Chapter%7D%0A%5Cthispagestyle%7Bempty%7D+%25+remove+headers%2Ffooters+from+the+chapter+page%0A%5Clipsum%5B1%5D%0A%5Cclearpage%0A%5Csection%7BNew+section%7D%0A%5Clipsum%5B1%5D%0A%5Cend%7Bdocument%7D)

#### 多行页眉或页脚

由 `fancyhdr` 可以包含多行。下面的示例基于 `fancyhdr` 文档，但已扩展以包含一个多行页眉。此示例使用 `datetime2` 包命令 `\\DTMnow` 来插入草稿文档排版时的日期。命令 `\\` 用于结束页眉的第一行。

```latex
\documentclass{book}
\usepackage[paperheight=16cm, paperwidth=12cm,% Set the height and width of the paper
includehead,
nomarginpar,% We don't want any margin paragraphs
textwidth=10cm,% Set \textwidth to 10cm
headheight=25pt,% 将 \\headheight 设为 25pt，以容纳多行页眉
]{geometry}
\usepackage{fancyhdr}
\\usepackage{datetime2}
\begin{document}
% Set the page style to "fancy"...
\pagestyle{fancy}
%... then configure it.
\fancyhead{} % clear all header fields
\\fancyhead[RO,LE]{\\textbf{草稿}（排版于 \\DTMnow）\\\\\\textbf{应届毕业生的表现}}
\fancyfoot{} % clear all footer fields
\fancyfoot[LE,RO]{\thepage}
\fancyfoot[LO,CE]{From: K. Grant}
\fancyfoot[CO,RE]{To: Dean A. Smith}
% Some content:
This is page 1.\newpage
这是第 2 页。
\end{document}
```

[在 Overleaf 中打开此 `fancyhdr` Overleaf 中的示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Multiline+header+fancyhdr+package+example\&snip=%5Cdocumentclass%7Bbook%7D%0A%5Cusepackage%5Bpaperheight%3D16cm%2C+paperwidth%3D12cm%2C%25+Set+the+height+and+width+of+the+paper%0Aincludehead%2C%0Anomarginpar%2C%25+We+don%27t+want+any+margin+paragraphs%0Atextwidth%3D10cm%2C%25+Set+%5Ctextwidth+to+10cm%0Aheadheight%3D25pt%2C%25+Set+%5Cheadheight+to+25pt+to+accommodate+the+multiline+header%0A%5D%7Bgeometry%7D%0A%5Cusepackage%7Bfancyhdr%7D%0A%5Cusepackage%7Bdatetime2%7D%0A%5Cbegin%7Bdocument%7D%0A%25+Set+the+page+style+to+%22fancy%22...%0A%5Cpagestyle%7Bfancy%7D%0A%25...+then+configure+it.%0A%5Cfancyhead%7B%7D+%25+clear+all+header+fields%0A%5Cfancyhead%5BRO%2CLE%5D%7B%5Ctextbf%7BDRAFT%7D+%28Typeset+%5CDTMnow%29%5C%5C%5Ctextbf%7BThe+performance+of+new+graduates%7D%7D%0A%5Cfancyfoot%7B%7D+%25+clear+all+footer+fields%0A%5Cfancyfoot%5BLE%2CRO%5D%7B%5Cthepage%7D%0A%5Cfancyfoot%5BLO%2CCE%5D%7BFrom%3A+K.+Grant%7D%0A%5Cfancyfoot%5BCO%2CRE%5D%7BTo%3A+Dean+A.+Smith%7D%0A%25+Some+content%3A%0AThis+is+page+1.%5Cnewpage%0AThis+is+page+2.%0A%5Cend%7Bdocument%7D)

下图显示了此示例产生的输出：

![fancyhdr 生成的多行页眉示例](/files/e183f5f1a78a2e9e487b5d1a6e226c47b974ad4b)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://overleaf-pro.ayaka.space/latex/zh-cn/ge-shi-hua/02-headers-and-footers.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
