> 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/03-page-numbering.md).

# 頁碼編號

文件頁碼可以使用特定的 *style*，例如使用阿拉伯數字或羅馬數字，並在特定頁面 *位置*——通常位於頁首或頁尾。本文將說明如何變更頁碼的樣式和位置：

* 該 *style* 頁碼樣式可由以下 `\pagenumbering` 命令；
* 該 *位置* 頁碼位置可透過以下 [`fancyhdr` 套件](https://ctan.org/pkg/fancyhdr?lang=en).

## 設定頁碼樣式

頁碼樣式可以使用命令

```latex
\pagenumbering{⟨style⟩}
```

其中 `⟨style⟩` 是以下其中之一

* `arabic`：使用阿拉伯數字（1, 2, 3, ...）
* `alph`：使用小寫字母（a, b, c, ...）
* `Alph`：使用大寫字母（A, B, C, ...）
* `roman`：使用小寫羅馬數字（i, ii, iii, ...）
* `Roman`：使用大寫羅馬數字（I, II, III, ...）

### 示範頁碼樣式的範例

以下範例排版了一份目錄，後面接著 5 頁，每一頁都示範了其中一種 `⟨style⟩` 頁碼選項。LaTeX 內部使用一個所謂的 *計數器* 來記錄目前頁碼。計數器是 LaTeX 中用來儲存整數值的變數名稱——請參見 [Overleaf 計數器文章](/latex/zh-tw/ge-shi-hua/10-counters.md) 以了解更多細節與範例。

```latex
\begin{document}
% 插入目錄
\tableofcontents
\newpage
\section{Uppercase Roman}
\pagenumbering{Roman}% 大寫 'R'：大寫羅馬數字
\blindtext[1]
\newpage
\section{Lowercase Roman} % 小寫羅馬數字
\pagenumbering{roman}
\blindtext[1]
\newpage
\section{Arabic numbers}
\pagenumbering{arabic} % 阿拉伯／印度數字頁碼
\blindtext[1]
\newpage
\section{Lowercase alphabetic}
\pagenumbering{alph} % 小寫字母頁「數字」
\blindtext[1]
\newpage
\section{Uppercase alphabetic}
\pagenumbering{Alph} % 大寫字母頁「數字」
\blindtext[1]
\end{document}
```

[在 Overleaf 中開啟這個範例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Demonstrating+page+number+styles\&snip=%5Cdocumentclass%7Barticle%7D%0A%25+Use+a+small+page+size+to+avoid+%0A%25+needing+lots+of+text+to+create+%0A%25+several+pages%0A%5Cusepackage%5Bincludefoot%2C%0Apaperheight%3D10cm%2C%0Apaperwidth%3D10cm%2C%0Atextwidth%3D9cm%2C%0Atextheight%3D8cm%5D%7Bgeometry%7D%0A%5Cusepackage%7Bblindtext%7D%0A%5Cbegin%7Bdocument%7D%0A%25+Insert+a+table+of+contents%0A%5Ctableofcontents%0A%5Cnewpage%0A%5Csection%7BUppercase+Roman%7D%0A%5Cpagenumbering%7BRoman%7D%25+Capital+%27R%27%3A+uppercase+Roman+numerals%0A%5Cblindtext%5B1%5D%0A%5Cnewpage%0A%5Csection%7BLowercase+Roman%7D+%25+lowercase+Roman+numerals%0A%5Cpagenumbering%7Broman%7D%0A%5Cblindtext%5B1%5D%0A%5Cnewpage%0A%5Csection%7BArabic+numbers%7D%0A%5Cpagenumbering%7Barabic%7D+%25+Arabic%2FIndic+page+numbers%0A%5Cblindtext%5B1%5D%0A%5Cnewpage%0A%5Csection%7BLowercase+alphabetic%7D%0A%5Cpagenumbering%7Balph%7D+%25+Lowercase+alphabetic+page+%22numbers%22%0A%5Cblindtext%5B1%5D%0A%5Cnewpage%0A%5Csection%7BUppercase+alphabetic%7D%0A%5Cpagenumbering%7BAlph%7D+%25+Uppercase+alphabetic+page+%22numbers%22%0A%5Cblindtext%5B1%5D%0A%5Cend%7Bdocument%7D)

下一張圖顯示此文件產生的目錄。請注意， `\pagenumbering` 命令已將每種樣式的起始頁碼重設為初始值： `i` 適用於 `roman` 樣式， `一個` 適用於 `Alph` 樣式， `I` 適用於 `Roman` 樣式，依此類推：

![以 LaTeX 排版的目錄範例](/files/be29b845fe9a341c95b277d5e8ee8ab6f512e706)

以下是此範例產生的兩個示例頁面；第一張圖片顯示大寫羅馬數字頁碼，第二張示範大寫字母頁碼：

![使用羅馬數字排版的頁碼範例](/files/d156a900cf0b2a47d4365763aad7d8f8e8a95336)

![使用大寫字母排版的頁碼範例](/files/233e4ee373eb6dd591347a965de6855435728989)

## book 類別：使用兩種頁碼樣式

該 [一本書的前幾頁](https://en.wikipedia.org/wiki/Book_design#Structure)，其中包括版權頁、書名頁或半書名頁、任何序文或前言以及目錄，統稱為 *前置* 頁，或簡稱 prelims——那些頁面的另一個稱呼是 [*前置內容*](https://en.wikipedia.org/wiki/Book_design#Front_matter)。傳統上，前置頁面會使用小寫羅馬數字編號，而主文（內文）頁面則使用阿拉伯數字編號。The `book` document class 包含協助此操作的命令，如下例所示。

```latex
\documentclass{book}
% emptypage 套件可防止頁碼和
% 標題出現在空白頁面上。
\usepackage{emptypage}
\begin{document}
\frontmatter % 使用小寫羅馬數字作為頁碼
\chapter*{Foreword}
\addcontentsline{toc}{chapter}{Foreword}
序文是由非本書作者的人撰寫。

\chapter*{Preface}
\addcontentsline{toc}{chapter}{Preface}
前言由本書作者撰寫。

\tableofcontents

\mainmatter % 現在使用阿拉伯數字作為頁碼

\chapter{First Chapter}
這將會是一個空章節……
\section{第一節}
加一些文字會比較好。
\chapter{The second chapter}
\end{document}
```

[在 Overleaf 中開啟這個範例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Example+page+numbers+in+book+class\&snip=%5Cdocumentclass%7Bbook%7D%0A%25+The+emptypage+package+prevents+page+numbers+and%0A%25+headings+from+appearing+on+empty+pages.%0A%5Cusepackage%7Bemptypage%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cfrontmatter+%25Use+lowercase+Roman+numerals+for+page+numbers%0A%5Cchapter%2A%7BForeword%7D%0A%5Caddcontentsline%7Btoc%7D%7Bchapter%7D%7BForeword%7D%0AThe+Foreword+is+written+by+someone+who+is+not+the+book%27s+author.%0A%0A%5Cchapter%2A%7BPreface%7D%0A%5Caddcontentsline%7Btoc%7D%7Bchapter%7D%7BPreface%7D%0AThe+Preface+is+written+by+the+book%27s+author.%0A%0A%5Ctableofcontents%0A%0A%5Cmainmatter+%25+Now+Use+Arabic+numerals+for+page+numbers%0A%0A%5Cchapter%7BFirst+Chapter%7D%0AThis+will+be+an+empty+chapter...%0A%5Csection%7BFirst+section%7D%0ASome+text+would+be+good.%0A%5Cchapter%7BThe+second+chapter%7D%0A%5Cend%7Bdocument%7D)

此範例產生的目錄示範了前置部分使用羅馬數字，而主文（正文）部分使用阿拉伯數字：

![在書中顯示羅馬數字與阿拉伯數字頁碼](/files/47ffa3fffd6e0a0dd187e51afeed316d940358b3)

控制頁碼編排的命令如下：

* `\frontmatter`：此命令之後、以及命令之前的頁面 `\mainmatter`，將會以小寫羅馬數字編號。
* `\mainmatter`：這會重新啟動頁碼計數器並將樣式改為阿拉伯數字。

## article 類別：使用兩種頁碼樣式

如上所述，目前頁碼儲存在一個 [LaTeX 計數器變數](/latex/zh-tw/ge-shi-hua/10-counters.md#introduction-to-latex-counters) 其名稱為 `page`。儲存在 `page`，或任何其他計數器變數，也可以使用以下命令設定為特定值： `\setcounter` 命令：

```latex
\setcounter{⟨countvar⟩}{⟨intval⟩}
```

其中計數器變數 `⟨countvar⟩` 被設定為數值 `⟨intval⟩`。例如，要將 `page` 計數器設為 `3` 你可以寫

```latex
\setcounter{page}{3}
```

變更計數器變數的其他命令包括 `\addtocounter` 和 `\stepcounter`:

```latex
\addtocounter{⟨countvar⟩}{⟨increment⟩}
\stepcounter{⟨countvar⟩}
```

* `\addtocounter{⟨countvar⟩}{⟨increment⟩}` 會增加一個數值 `⟨increment⟩` 到該計數器變數中 `⟨countvar⟩`. **注意**: `⟨increment⟩` 可以是正值，用來增加計數器值，或是負值，用來減少它。
* `\steptocounter{⟨countvar⟩}` 會將 1 加到計數器變數 `⟨countvar⟩`

這個假設範例會修改頁碼計數器，以規劃目錄，包括尚未撰寫且頁數為估計值的章節。它也使用 `\pagenumbering{roman}` 和 `\pagenumbering{Arabic}` 來設定頁碼樣式。

```latex
\documentclass{article}
\pagenumbering{roman}
\begin{document}

\section*{Guest Foreword (TBD)}
\addcontentsline{toc}{section}{Foreword: 4 pages (TBD)}
待撰寫：預留 4 頁。
\newpage
\setcounter{page}{5}

\section*{Introduction (2 pages)}
\addcontentsline{toc}{section}{Introduction: 2 pages (TBD)}
待撰寫：允許 2 頁。
\newpage
\addtocounter{page}{1}

\section*{Strategy summary (2 pages)}
\addcontentsline{toc}{section}{Strategy summary: 2 pages (TBD)}
待撰寫：2 頁。
\newpage
\stepcounter{page}

\tableofcontents

\newpage
\pagenumbering{arabic}

\section{Level 1 heading}
本頁上的一些文字。
\newpage
\section{Another Level 1 heading}
\end{document}
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Mixing+page+number+formats+in+the+article+class\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cpagenumbering%7Broman%7D%0A%5Cbegin%7Bdocument%7D%0A%0A%5Csection%2A%7BGuest+Foreword+%28TBD%29%7D%0A%5Caddcontentsline%7Btoc%7D%7Bsection%7D%7BForeword%3A+4+pages+%28TBD%29%7D%0ATo+be+written%3A+Allowing+4+pages.%0A%5Cnewpage+%0A%5Csetcounter%7Bpage%7D%7B5%7D%0A%0A%5Csection%2A%7BIntroduction+%282+pages%29%7D%0A%5Caddcontentsline%7Btoc%7D%7Bsection%7D%7BIntroduction%3A+2+pages+%28TBD%29%7D%0ATo+be+written%3A+2+pages+allowed.%0A%5Cnewpage%0A%5Caddtocounter%7Bpage%7D%7B1%7D%0A%0A%5Csection%2A%7BStrategy+summary+%282+pages%29%7D%0A%5Caddcontentsline%7Btoc%7D%7Bsection%7D%7BStrategy+summary%3A+2+pages+%28TBD%29%7D%0ATo+be+written%3A+2+pages.%0A%5Cnewpage%0A%5Cstepcounter%7Bpage%7D%0A%0A%5Ctableofcontents%0A%0A%5Cnewpage%0A%5Cpagenumbering%7Barabic%7D%0A%0A%5Csection%7BLevel+1+heading%7D%0ASome+text+on+this+page.%0A%5Cnewpage%0A%5Csection%7BAnother+Level+1+heading%7D%0A%5Cend%7Bdocument%7D)

此範例會產生以下目錄，顯示變更 `page` 計數器值的結果：

![在 LaTeX 中變更頁碼計數器](/files/1787514e8cb68a8c60ec4e8fa9daffdeb26b87a5)

## 使用 fancyhdr 套件自訂頁碼

該 `fancyhdr` 套件可用來自訂頁碼的位置與格式；例如，將它們放在頁首或頁尾中的特定位置。Overleaf 文章 [頁首與頁尾](/latex/zh-tw/ge-shi-hua/02-headers-and-footers.md) 會深入探討這一點，並提供許多範例，這裡不再重現。

這裡我們將舉一個範例，說明如何在總頁數的脈絡中寫出目前頁碼，例如 `第 X 頁，共 Y 頁` 其中 Y 是透過以下方式取得的文件總頁數： [`lastpage` 套件](https://ctan.org/pkg/lastpage?lang=en).

```latex
\documentclass{article}
% 使用較小的頁面尺寸以避免
% 需要大量文字來產生
% 多個頁面
\usepackage[includefoot,
paperheight=10cm,
paperwidth=10cm,
textwidth=9cm,
textheight=8cm]{geometry}
\usepackage{blindtext}
\usepackage{lastpage}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{} % 清除現有頁首/頁尾項目
% 將第 X 頁，共 Y 頁放在頁尾的右側
% 頁尾
\fancyfoot[R]{第 \thepage \hspace{1pt} 頁，共 \pageref{LastPage}}
\begin{document}
% 插入目錄
\tableofcontents
\newpage
\section{One}
\blindtext[1]
\newpage
\section{Two}
\blindtext[1]
\newpage
\section{Three}
\blindtext[1]
\newpage
\section{Four}
\blindtext[1]
\newpage
\section{Five}
\blindtext[1]
\end{document}
```

[在 Overleaf 中開啟此範例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Modifying+page+numbers+with+fancyhdr\&snip=%5Cdocumentclass%7Barticle%7D%0A%25+Use+a+small+page+size+to+avoid+%0A%25+needing+lots+of+text+to+create+%0A%25+several+pages%0A%5Cusepackage%5Bincludefoot%2C%0Apaperheight%3D10cm%2C%0Apaperwidth%3D10cm%2C%0Atextwidth%3D9cm%2C%0Atextheight%3D8cm%5D%7Bgeometry%7D%0A%5Cusepackage%7Bblindtext%7D%0A%5Cusepackage%7Blastpage%7D%0A%5Cusepackage%7Bfancyhdr%7D%0A%5Cpagestyle%7Bfancy%7D%0A%5Cfancyhf%7B%7D+%25+clear+existing+header%2Ffooter+entries%0A%25+Place+Page+X+of+Y+on+the+right-hand%0A%25+side+of+the+footer%0A%5Cfancyfoot%5BR%5D%7BPage+%5Cthepage+%5Chspace%7B1pt%7D+of+%5Cpageref%7BLastPage%7D%7D%0A%5Cbegin%7Bdocument%7D%0A%25+Insert+a+table+of+contents%0A%5Ctableofcontents%0A%5Cnewpage%0A%5Csection%7BOne%7D%0A%5Cblindtext%5B1%5D%0A%5Cnewpage%0A%5Csection%7BTwo%7D%0A%5Cblindtext%5B1%5D%0A%5Cnewpage%0A%5Csection%7BThree%7D%0A%5Cblindtext%5B1%5D%0A%5Cnewpage%0A%5Csection%7BFour%7D%0A%5Cblindtext%5B1%5D%0A%5Cnewpage%0A%5Csection%7BFive%7D%0A%5Cblindtext%5B1%5D%0A%5Cend%7Bdocument%7D)

以下圖片顯示此範例產生的其中一頁——請注意 `第 3 頁，共 6 頁` 位於頁尾右側：

![使用 fancyhdr 變更頁碼](/files/b05b0e78d68d249005672ab158502d3085794d2f)


---

# 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/03-page-numbering.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.
