> 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-tw/ge-shi-hua/02-headers-and-footers.md).

# 頁首與頁尾

## 簡介與概述

本文示範如何使用 [fancyhdr 套件](https://ctan.org/pkg/fancyhdr?lang=en)。另一種自訂頁首和頁尾的方式，在姊妹文章中有討論， [*LaTeX 如何排版頁首和頁尾？*](/latex/zh-tw/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-tw/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 提供了幾種預先定義的頁面樣式：

* `empty`：頁面上沒有頁首或頁尾
* `plain`：沒有頁首；頁尾由置中的頁碼構成
* `標題式`：沒有頁尾；頁首包含類別特定資訊與頁碼
* `myheadings`：沒有頁尾；頁首包含頁碼與使用者提供的資訊

#### 標準文件類別的預設頁面樣式

除非使用者另行指定，標準文件類別會套用預設頁面樣式：

* 該 `article` 和 `報告` 類別使用 `plain`
* 該 `book` 類別使用 `標題式`

#### 設定頁面樣式

核心 LaTeX 提供兩個命令來變更（設定）頁面樣式：

* `\pagestyle{⟨style⟩}`：將目前頁面以及之後所有頁面的樣式設為 `⟨style⟩`
* `\thispagestyle{⟨style⟩}`：只將目前頁面的樣式設為 `⟨style⟩`

頁碼樣式是透過命令 `\pagenumbering{⟨numberstyle⟩}`；例如， `\pagenumbering{roman}` 會以小寫羅馬數字排版頁碼：i、ii、iii...

### LaTeX 頁面版面配置

因為頁首和頁尾是整體頁面版面／設計的一部分，了解多一點 LaTeX 的頁面版面模型會很有幫助；以下 Overleaf 文章可能會有興趣：

* 若要了解 LaTeX 頁面版面配置的入門介紹，Overleaf 文章 [頁面大小與邊界](/latex/zh-tw/ge-shi-hua/07-page-size-and-margins.md) 示範如何使用 [**`geometry`** 套件](https://ctan.org/pkg/geometry?lang=en);
* 如果你想了解更多細節，我們建議閱讀文章 [LaTeX 頁面版面參數視覺指南](/latex/zh-tw/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}% 用於一些假文
\title{article 類別範例}
\author{Overleaf}
\begin{document}
\maketitle

\section{起初……}
\lipsum[2]

\section{另一節}
\lipsum[1]

\section{又一節}
\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}% 用於一些假文
\title{report 類別範例}
\author{Overleaf}
\begin{document}
\maketitle
\chapter{一}
\section{起初……}
\lipsum[2]

\section{另一節}
\lipsum[1]

\section{又一節}
\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}% 用於一些假文
\title{\TeX{} 使用者回憶錄}
\author{Overleaf}
\begin{document}

\frontmatter
\maketitle
這是使用羅馬數字的前文。

\mainmatter
\chapter{我該從哪裡開始}
\lipsum[4]

\section{起初……}
\lipsum[1]

\section{又一節}
\lipsum[1]

\chapter{我記得的事情}

\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` 定義的雙面文件頁首與頁尾的一般結構：

* 頁首和頁尾可以在左頁（偶數頁）與右頁（奇數頁）包含不同內容
* 頁首和頁尾分成三個「區域」
  * **左***側*
  * **中***央*
  * **R***右*
* 頁首和頁尾可以包含一條可選的橫線

在 **左** 區域中，內容靠左排版；在 **中** 區域中則置中，而在 **R** 區域中則靠右排版。

![顯示 fancyhdr 頁首與頁尾結構的示意圖](/files/f15ecf819e58b1dd43cf9d3a20dd2834fd11928d)

由 `fancyhdr` 提供的命令使用單字母縮寫，稱為「座標」，用來識別特定的頁首／頁尾區域與頁面位置：

* **`O`** 或 **`E`**：用來指定 \*\*`O`奇數或 \*\*`E`偶數頁面
* **`H`** 或 **`F`**：用來表示 \*\*`H`頁首或 \*\*`F`頁尾
* **`左`**, **`中`** 或 **`R`**：針對 \*\*`左`左、\*\*`中`中與 \*\*`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,% 設定紙張的高度與寬度
includehead,
nomarginpar,% 我們不需要任何邊註段落
textwidth=10cm,% 將 \textwidth 設為 10cm
headheight=10mm,% 將 \headheight 設為 10mm
]{geometry}
\usepackage{fancyhdr}
\begin{document}
% 將頁面樣式設為 "fancy"...
\pagestyle{fancy}
%……然後進行設定。
\fancyhead{} % 清除所有頁首欄位
\fancyhead[RO,LE]{\textbf{新畢業生的表現}}
\fancyfoot{} % 清除所有頁尾欄位
\fancyfoot[LE,RO]{\thepage}
\fancyfoot[LO,CE]{來自：K. Grant}
\fancyfoot[CO,RE]{收件者：Dean A. Smith}
% 一些內容：
這是第 1 頁。\newpage
這是第 2 頁。
\end{document}
```

[在 Overleaf 中開啟這個 `fancyhdr` 範例](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/37ae26b61ae7916dad60bb4c4b061a7ad5871824)

**所用命令註解**

* **`\fancyhead{}`**：清除頁首設定
* **`\fancyfoot{}`**：清除頁尾設定
* **`\fancyhead[RO,LE]{\textbf{新畢業生的表現}}`**：使用頁首位置 **`RO`** (\*\*`R`**右**`O`**（奇數頁）與 `LE` (**`左`**側**`E`（偶數頁）以放置內容 `\textbf{新畢業生的表現}`
* **`\fancyfoot[LE,RO]{\thepage}`**：使用頁尾位置 **`LE`** (\*\*`左`**側**`E`**（偶數頁）與 `RO` (**`R`**右**`O`（奇數頁）以放置由 `\thepage`
* **`\fancyfoot[LO,CE]{來自：K. Grant}`**：使用頁尾位置 **`LO`** (\*\*`左`**側**`O`**（奇數頁）與 `CE` (**`中`**央**`E`（偶數頁）以放置內容 `來自：K. Grant`
* **`\fancyfoot[CO,RE]{收件者：Dean A. Smith}`**：使用頁尾位置 **`CO`** (\*\*`中`**央**`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,% 設定紙張的高度與寬度
includehead,
nomarginpar,% 我們不需要任何邊註段落
textwidth=10cm,% 將 \textwidth 設為 10cm
headheight=10mm,% 將 \headheight 設為 10mm
]{geometry}
\usepackage{fancyhdr}
\begin{document}
% 將頁面樣式設為 "fancy"...
\pagestyle{fancy}
\title{單面文件}
\author{Overleaf}
\date{2022 年 8 月}
\fancyhf{} % 清除現有頁首/頁尾項目
% 我們不需要指定 O 座標
\fancyhead[R]{你好}
\fancyfoot[L]{\thepage}
\maketitle
\section{簡介}
一些內容。
\newpage
\section{續篇……}
\end{document}
```

[在 Overleaf 中開啟這個 `fancyhdr` 範例](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,% 設定紙張的高度與寬度
includehead,
nomarginpar,% 我們不需要任何邊註段落
textwidth=10cm,% 將 \textwidth 設為 10cm
headheight=10mm,% 將 \headheight 設為 10mm
]{geometry}
\usepackage{fancyhdr}
\begin{document}
% 將頁面樣式設為 "fancy"...
\pagestyle{fancy}
\title{未使用 twoside 時的警告}
\author{Overleaf}
\date{2022 年 7 月}
\fancyhead[E]{你好}% 這會觸發警告！
\fancyfoot[E]{\thepage}% 這會觸發警告！
\maketitle
\section{簡介}
一些內容。
\newpage
\section{續篇……}
\end{document}
```

[在 Overleaf 中開啟這個 ***錯誤的*** `fancyhdr` 範例](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/a43997011c705a6a859d7a0aad7cb0abfdc04147)

上面的範例會產生以下警告：

```latex
套件 fancyhdr 警告：\fancyhead 的 `E` 選項在未使用 twoside 選項時是無用的。
套件 fancyhdr 警告：\fancyfoot 的 `E` 選項在未使用 twoside 選項時是無用的。
```

**在雙面文件中使用單一座標：省略 E 與 O**

如果你使用 `\fancyhead` 或 `\fancyfoot` 在雙面文件中，但省略 **`E`** 和 **`O`** 某位置中的 *只使用 1 個座標*, **`左`**, **`中`** 或 **`R`**，而 `*內容*` 就會被放在該位置的 *偶數與奇數頁上*.

**範例**

以下範例示範省略 **`E`** 和 **`O`** 來自 `*位置*` 在 `\fancyhead` 以及 \\\\`fancyfoot`。它也使用以下命令：

* `\pagestyle{fancy}` 用來將頁面樣式設定為由 `fancyhdr`
* `\fancyhead{}\fancyfoot{}` 用來清除所有頁首和頁尾（另見 `\fancyhf{}`)

```latex
\begin{document}
% 將頁面樣式設為 "fancy"...
\pagestyle{fancy}
%……然後進行設定。

% 清除所有頁首和頁尾（另見 \\fancyhf{}）
\fancyhead{}\fancyfoot{}

% 設定頁首置中位置，但不指定 O 或 E
\fancyhead[C]{在所有頁面頁首的中央：\thepage}

% 設定頁尾左側位置，但不指定 O 或 E
\fancyfoot[L]{在所有頁面頁尾的左側：\thepage}

% 一些內容：
這是第 1 頁。\newpage
這是第 2 頁。
```

[在 Overleaf 中開啟這個 `fancyhdr` 範例](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/a55e7d8c58ad918bfbeec0c2144af3dc14553160)

如圖所示，下列程式碼

```latex
\fancyhead[C]{在所有頁面頁首的中央：\thepage}
```

將 \*\*`中`置中的頁首位置，但未指定它是否應出現在 \*\*`O`奇數或 \*\*`E`偶數頁。省略 **`O`** 和 **`E`** 座標會導致`中`置中的區域頁首內容會出現在奇數與偶數頁上。

此外，這段程式碼

```latex
\fancyfoot[L]{在所有頁面頁尾的左側：\thepage}
```

將 \*\*`左`左側頁尾位置，但未指定它是否應出現在奇數或偶數頁。省略 **`O`** 和 **`E`** 座標會導致`左`左側區域內容會出現在頁尾的左側區域，並出現在奇數與偶數頁上。

**在雙面文件中使用單一座標：省略 L、C 與 R**

如果你省略 **`左`**, **`中`** 和 **`R`** 在雙面文件中的某個位置，並使用 *只使用 1 個座標*, **`E`** 或 **`O`**，而 `*內容*` 都會 *放在所有三個區域* 中，位於該位置所指定的偶數頁或奇數頁上。

**範例**

以下範例為奇數頁與偶數頁設定頁首和頁尾，但沒有任何 `*位置*` 在 `\fancyhead` 和 `\fancyfoot` 命令指定「區域」， **`左`**, **`中`** 或 **`R`**，其中 `*內容*` 應放置的內容。注意 `*內容*` 會在所有三個區域中重現。

```latex
\begin{document}
% 將頁面樣式設為 "fancy"...
\pagestyle{fancy}
%……然後進行設定。

% 清除所有頁首和頁尾（另見 \\fancyhf{}）
\fancyhead{}\fancyfoot{}

% 設定偶數頁的頁首與頁尾
% 頁面，但省略區域（L、C 或 R）
\fancyhead[E]{頁首：偶數頁 \thepage}
\fancyfoot[E]{頁尾：偶數頁 \thepage}

% 設定奇數頁的頁首與頁尾
% 頁面，但省略區域（L、C 或 R）
\fancyhead[O]{頁首：奇數頁 \thepage}
\fancyfoot[O]{頁尾：奇數頁 \thepage}

% 一些內容：
這是第 1 頁。\newpage
這是第 2 頁。
```

[在 Overleaf 中開啟這個 `fancyhdr` 範例](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/7fa1e124450be4bc45d1a0d3211d3c0396764b7a)

以下程式碼設定 **`E`偶數頁，但省略了一個區域座標（`左`**, **`中`** 或 **`R`**):

```latex
\fancyhead[E]{頁首：偶數頁 \thepage}
\fancyfoot[E]{頁尾：偶數頁 \thepage}
```

結果如下：

* **偶數頁頁首** 包含 `頁首：偶數頁 \thepage` 會出現在三個頁首區域（**`左`**, **`中`** 和 **`R`**)
* **偶數頁頁尾** 包含 `頁尾：偶數頁 \thepage` 會出現在三個頁尾區域（**`左`**, **`中`** 和 **`R`**)

以下程式碼設定 **`O`奇數頁面，但也省略了一個區域座標 (`左`**, **`中`** 或 **`R`**):

```latex
\fancyhead[O]{頁首：奇數頁 \thepage}
\fancyfoot[O]{頁尾：奇數頁 \thepage}
```

這會產生與偶數頁類似的結果：

* **奇數頁的頁首** 包含 `頁首：奇數頁 \thepage` 會出現在三個頁首區域（**`左`**, **`中`** 和 **`R`**)
* **奇數頁的頁尾** 包含 `頁尾：奇數頁 \thepage` 會出現在三個頁尾區域（**`左`**, **`中`** 和 **`R`**)

#### \fancyhf：最多使用 3 個座標

`\fancyhf` 是一個更通用的命令，可用來組合頁首與頁尾的設定。為此，它支援以下座標 **`H`** (用於 \*\*`H`\*\*頁首）與 **`F`** (用於 \*\*`F`\*\*頁尾）。此命令的一般形式為

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

其中 `*位置*` 是一個可選的、以逗號分隔的位置清單，由 2 或 3 個座標指定，用於放置 `*內容*`.

由於此命令可指定頁首的內容 *和* 頁尾，因此需要第三個座標， **`H`** 或 **`F`** 才能完整指定 `*位置*`。例如，若要使用（參照）左側（偶數）頁頁尾的中央區域，您可以將其指定為以下三個座標 **`EFC`**:

* \*\*`E`\*\*偶數編號頁面
* \*\*`F`頁尾
* \*\*`中`\*\*中央區域

請注意，座標的順序並不重要： **`EFC`** 產生相同的結果： **`FEC`** 以此類推。

以下範例使用 `\fancyhf` 以將內容放置於 \*\*`中`\*\*所有頁首與頁尾的中央區域：

```latex
\begin{document}
\pagestyle{fancy}
\fancyhf{}
\fancyhf[EHC]{偶數+頁首+中央}
\fancyhf[EFC]{偶數+頁尾+中央}
\fancyhf[OFC]{奇數+頁尾+中央}
\fancyhf[OHC]{奇數+頁首+中央}
\lipsum[1]\newpage\lipsum[1]
```

[在 Overleaf 中開啟這個 `\fancyhf` 範例](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/e0549a2f653691244efaa4b8e67fd45801aef2ca)

### 範例

#### 在頁首與頁尾中使用基本規則（線條）

以下與規則線相關的命令由 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}% 無法運作
\setlength{\headruleskip}{10mm}% 也無法運作
```

下一個範例會變更規則線寬度，並展示如何修改規則線（線條）與頁首、頁尾文字之間的距離——使用 `\headruleskip` 和 `\footruleskip`:

```latex
\begin{document}
% 將頁面樣式設為 "fancy"...
\pagestyle{fancy}
%……然後進行設定。
\renewcommand{\headruleskip}{10mm}
\renewcommand{\footruleskip}{10pt}
\renewcommand{\headrulewidth}{2pt}
\renewcommand{\footrulewidth}{4pt}
% 一些內容：
\chapter{調整設定}
\section{第 1 頁}
這是第 1 頁。
\newpage
\section{第 2 頁}
這是第 2 頁。
\end{document}
```

[在 Overleaf 中開啟這個 `fancyhdr` 範例](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,% 設定紙張的高度與寬度
includehead,
nomarginpar,% 我們不需要任何邊註段落
textwidth=10cm,% 將 \textwidth 設為 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/69cbe8e07c907c56dbb50820162de26050bdad4e)

* 本範例中命令的說明
  * **`\headrule`**：一個用來定義如何繪製頁首裝飾線條的巨集。此處它被重新定義為繪製一條包含來自以下套件飾物的線條： [`fourier-orns` 套件](https://ctan.org/pkg/fourier?lang=en).
  * **`\leftmark`** 和 **`\rightmark`**：這些命令用來取得由 LaTeX 分節命令插入的「標記資料」。若要更完整地說明這些命令，我們建議參考配套文章 [*LaTeX 如何排版頁首和頁尾？*](/latex/zh-tw/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` 透過下列方式重設為 2cm： `headheight=2cm`:

```latex
\documentclass{book}
\usepackage[paperheight=16cm, paperwidth=12cm,% 設定紙張的高度與寬度
includehead,% 說明如下
nomarginpar,% 我們不需要任何邊註段落
textwidth=10cm,% 將 \textwidth 設為 10cm
headheight=2cm,% 將 \headheight 設為 2cm
showframe % 顯示頁面版面配置
]{geometry}
\title{\TeX{} 使用者回憶錄}
\author{Overleaf}
\begin{document}

\frontmatter
\maketitle
這是使用羅馬數字的前文。

\mainmatter
\chapter{我該從哪裡開始}
第 1 章：一個在第 1 頁結束的短章節。

\chapter{我記得的事情}
第 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-tw/shen-ru-wen-zhang/04-a-visual-guide-to-latex-s-page-layout-parameters.md)
* `headheight=2cm`：為下列項目設定值（2mm）： `\headheight`，LaTeX 的其中一個 [頁面版面配置參數](/latex/zh-tw/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/9ac4ffcc0761345747c571bbefd0b7999300e8e1)

請注意， `geometry` 套件透過以下選項來表示文字區域的高度： `textheight` 而 LaTeX 使用同名相近的 [`\textheight` 頁面版面配置參數](/latex/zh-tw/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,% 設定紙張的高度與寬度
includehead,% 說明如下
nomarginpar,% 我們不需要任何邊註段落
textwidth=10cm,% 將 \textwidth 設為 10cm
headheight=16pt % 將 \headheight 設為 16pt，以避免 fancyhr 警告
]{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{我該從哪裡開始}
第 1 章：一個在第 1 頁結束的短章節。

\chapter{我記得的事情}
第 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)

如以下註解圖所示，第 2 章從第 3 頁開始，因為 LaTeX 插入了第 2 頁；該頁名義上是「空白」的，但仍包含一個不美觀的頁首。

![book 類別文件中的空白頁面範例](/files/d41afa4bcff4b7fb9b072e2d16789f947797d407)

可以使用各種機制——程式碼與套件——來確保插入的頁面（例如上面的第 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,% 設定紙張的高度與寬度
includehead,
nomarginpar,% 我們不需要任何邊註段落
textwidth=10cm,% 將 \textwidth 設為 10cm
headheight=25pt,% 將 \headheight 設為 25pt，以容納多行頁首
]{geometry}
\usepackage{fancyhdr}
\usepackage{datetime2}
\begin{document}
% 將頁面樣式設為 "fancy"...
\pagestyle{fancy}
%……然後進行設定。
\fancyhead{} % 清除所有頁首欄位
\fancyhead[RO,LE]{\textbf{草稿} (排版於 \DTMnow)\\\textbf{新畢業生的表現}}
\fancyfoot{} % 清除所有頁尾欄位
\fancyfoot[LE,RO]{\thepage}
\fancyfoot[LO,CE]{來自：K. Grant}
\fancyfoot[CO,RE]{收件者：Dean A. Smith}
% 一些內容：
這是第 1 頁。\newpage
這是第 2 頁。
\end{document}
```

[在 Overleaf 中開啟這個 `fancyhdr` 範例](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/d5507bdaae138f09e621f33c8ebeef3f863bddb4)


---

# 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-tw/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.
