> For the complete documentation index, see [llms.txt](https://ayakaleaf-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://ayakaleaf-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-du-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-du-wen-zhang/23-how-does-latex-typeset-headers-and-footers.md) ，其中探讨并解释了 TeX 引擎用于创建页眉和页脚的基于标记的底层机制——以及为什么需要 `\mark` 命令。

### LaTeX 文档类

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

* `book`
* `report`
* `article`
* `letter`

它们通过 `\documentclass` 命令：

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

其中：

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

#### 单面或双面文档

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

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

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

或

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

#### 文档类的默认 ⟨options⟩

LaTeX 为每个标准文档类应用一组默认的 `⟨options⟩` 设置：

* `article`：默认 `⟨options⟩` 已 `letterpaper,10pt,oneside,onecolumn,final`
* `report`：默认 `⟨options⟩` 已 `letterpaper,10pt,oneside,onecolumn,final,openany`
* `book`：默认 `⟨options⟩` 已 `letterpaper,10pt,twoside,onecolumn,final,openright`
* `letter`：默认 `⟨options⟩` 已 `letterpaper,10pt,oneside,onecolumn,final`

文档类还提供命令，使用户能够将文档结构化为诸如部分、章、节、小节等逻辑层次。正如你所料， `book` 该类拥有一整套丰富的文档结构化命令。

### LaTeX 页面样式

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

* `empty`：页面上没有页眉或页脚
* `plain`：没有页眉，页脚为居中的页码
* `headings`：没有页脚，页眉包含与文档类相关的信息和页码
* `myheadings`：没有页脚，页眉包含页码和用户提供的信息

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

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

* 该 `article` 和 `report` 类使用 `plain`
* 该 `book` 类使用 `headings`

#### 设置页面样式

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

* `\pagestyle{⟨style⟩}`：将当前页以及后续所有页面的样式设置为 `⟨style⟩`
* `\thispagestyle{⟨style⟩}`：仅将当前页的样式设置为 `⟨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-du-wen-zhang/04-a-visual-guide-to-latex-s-page-layout-parameters.md).

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

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

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

```latex
\documentclass{article}
% Choose a conveniently small page size
\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]
```

[打开此 **`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}
% Choose a conveniently small page size
\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]
```

[打开此 **`report`** 在 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}
% Choose a conveniently small page size
\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]
```

[打开此 **`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}
```

其中 `⟨options⟩` 在 [包文档](http://mirrors.ctan.org/macros/latex/contrib/fancyhdr/fancyhdr.pdf).

### fancyhdr 页眉和页脚结构

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

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

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

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

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

* **`O`** 或 **`E`**：用于指定 \*\*`O`\*\*dd 或 \*\*`E`\*\*ven 页
* **`H`** 或 **`F`**：表示 \*\*`H`\*\*eader 或 \*\*`F`\*\*ooter
* **`L`**, **`C`** 或 **`R`**：表示 \*\*`L`\*\*eft、\*\*`C`\*\*entre 和 \*\*`R`\*\*ight” 区域（页眉或页脚）

### 命令简介

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

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

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

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

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

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

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

**双面文档示例**

以下示例取自 `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
This is page 2.
\end{document}
```

[打开此 `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`**ight**`O`**dd) 和 `LE` (**`L`**eft**`E`\*\*ven) 来放置内容 `\textbf{The performance of new graduates}`
* **`\fancyfoot[LE,RO]{\thepage}`**：使用页脚位置 **`LE`** (\*\*`L`**eft**`E`**ven) 和 `RO` (**`R`**ight**`O`\*\*dd) 来放置由 `\thepage`
* **`\fancyfoot[LO,CE]{From: K. Grant}`**：使用页脚位置 **`LO`** (\*\*`L`**eft**`O`**dd) 和 `CE` (**`C`**entre**`E`\*\*ven) 来放置内容 `发件人：K. Grant`
* **`\fancyfoot[CO,RE]{To: Dean A. Smith}`**：使用页脚位置 **`CO`** (\*\*`C`**entre**`O`**dd) 和 `RE` (**`R`**ight**`E`\*\*ven) 来放置内容 `收件人：Dean A. Smith`

**单面和双面文档**

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

**单面文档示例**

对于 *单面* 基于……的文档 `article` 或 `report` 类时，所有页面实际上都是右页，因此用于 `**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{August 2022}
\fancyhf{} % 清除现有页眉/页脚条目
% We don't need to specify the O coordinate
\fancyhead[R]{Hello}
\fancyfoot[L]{\thepage}
\maketitle
\section{Introduction}
一些内容。
\newpage
\section{Continued...}
\end{document}
```

[打开此 `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{Introduction}
一些内容。
\newpage
\section{Continued...}
\end{document}
```

[打开此 ***错误的*** `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
Package fancyhdr Warning: \fancyhead's `E' option without twoside option is useless on input line 23.
Package fancyhdr Warning: \fancyfoot's `E' option without twoside option is useless on input line 24.
```

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

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

**示例**

以下示例演示省略 **`E`** 和 **`O`** 来自 `*locations*` 中指定的 `\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
This is page 2.
```

[打开此 `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`\*\*dd 或 \*\*`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`**， `*content*` 单位定义拉伸分量的胶水都将 *放置在三个区域中* 位于该位置所指定的奇数页或偶数页上。

**示例**

以下示例为奇数页和偶数页设置页眉和页脚，但并未指定 `*locations*` 位于 `\fancyhead` 和 `\fancyfoot` 命令指定“区域”， **`L`**, **`C`** 或 **`R`**，其中 `*content*` 应放置内容。注意 `*content*` 会在三个区域中重复显示。

```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
This is page 2.
```

[打开此 `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*}`

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

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

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

请注意，坐标的顺序并不重要： **`EFC`** 产生与……相同的结果 **`FEC`** 依此类推。

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

```latex
\begin{document}
\pagestyle{fancy}
\fancyhf{}
\fancyhf[EHC]{Even+Header+Centre}
\fancyhf[EFC]{Even+Footer+Centre}
\fancyhf[OFC]{Odd+Footer+Centre}
\fancyhf[OHC]{Odd+Header+Centre}
\lipsum[1]\newpage\lipsum[1]
```

[打开此 `\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)

### 示例

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

以下与横线相关的命令由 4.x 版的……提供 `fancyhdr`:

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

**注释**:

* ……的默认值 `\headrulewidth` 是 `0.4pt` 以及……的默认值 `\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}% does not work
\setlength{\headruleskip}{10mm}% also will not work
```

下一个示例会更改横线宽度，并演示如何修改横线（线条）与页眉和页脚文本之间的距离——使用 `\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{Wrangling settings}
\section{Page 1}
这是第 1 页。
\newpage
\section{Page 2}
This is page 2.
\end{document}
```

[打开此 `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,% Set \headheight to 14pt to accommodate our fancy rule!
]{geometry}
\usepackage{fourier-orns}
\usepackage{fancyhdr}
\renewcommand{\headrule}{%
\vspace{-8pt}\hrulefill
\raisebox{-2.1pt}{\quad\decofourleft\decotwo\decofourright\quad}\hrulefill}
\title{Decorative line}
\author{Overleaf}
\date{August 2022}
\begin{document}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[LE]{\nouppercase{\rightmark\hfill\leftmark}}
\fancyhead[RO]{\nouppercase{\leftmark\hfill\rightmark}}
\fancyfoot[LE,RO]{\hfill\thepage\hfill}
\chapter{Fancy rules!}
\section{Introduction}
此示例取自 \texttt{fancyhdr} 文档。
\newpage
\section{See the header}
\newpage
\section{Yes, very neat!}
\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-du-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` 更改为 `*高度*`.

以下示例重新回顾我们前面的 book 类示例，并将……重设为 `\headheight` 通过……设为 2cm `headheight=2cm`:

```latex
\documentclass{book}
\usepackage[paperheight=16cm, paperwidth=12cm,% Set the height and width of the paper
includehead,% See below for an explanation
nomarginpar,% We don't want any margin paragraphs
textwidth=10cm,% Set \textwidth to 10cm
headheight=2cm,% Set \headheight to 2cm
showframe % Show the page layout
]{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`：为……设置一个值（10cm） `\textwidth`，这是 LaTeX 的其中一个 [页面布局参数](/latex/zh-cn/shen-du-wen-zhang/04-a-visual-guide-to-latex-s-page-layout-parameters.md)
* `headheight=2cm`：为……设置一个值（2mm） `\headheight`，这是 LaTeX 的其中一个 [页面布局参数](/latex/zh-cn/shen-du-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-du-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,% See below for an explanation
nomarginpar,% We don't want any margin paragraphs
textwidth=10cm,% Set \textwidth to 10cm
headheight=16pt % Set \headheight to 16pt to avoid fancyhr warnings
]{geometry}
\usepackage{lastpage}
\usepackage{fancyhdr}
\usepackage{lipsum} % just for mock text
\pagestyle{fancy}
\renewcommand{\headrulewidth}{0pt}
\fancyfoot[C]{\thepage\ of \pageref{LastPage}}
\fancypagestyle{plain}{%
  \renewcommand{\headrulewidth}{0pt}%
  \fancyhf{}%
  \fancyfoot[C]{\thepage\ of \pageref{LastPage}}}
\begin{document}
\title{Another fancyhdr demo}
\author{\texttt{tex.stackexchange} \textit{et al}}
\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` 选项，或者
* 该 `report` 带有……选项的类 `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}
```

[打开此 `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)

如下方带注释的图示所示，第 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.stackexchange 上](https://tex.stackexchange.com/questions/tagged/blank-page).

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

#### 更改单页的样式

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

```latex
\begin{document}
\maketitle
\chapter{Using different page styles}
\lipsum[1] % via \usepackage{lipsum}
\chapter{Sample Chapter}
\thispagestyle{empty} % remove headers/footers from the chapter page
\lipsum[1]
\clearpage
\section{New 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,% Set \headheight to 25pt to accommodate the multiline header
]{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{DRAFT} (Typeset \DTMnow)\\\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
This is page 2.
\end{document}
```

[打开此 `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://ayakaleaf-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.
