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

# 页码编号

文档页码可以使用特定的方式进行排版 *样式*，例如使用阿拉伯数字或罗马数字，并在特定的页面位置进行排版 *位置*——通常位于页眉或页脚中。本文介绍如何更改页码的样式和位置：

* 该 *样式* 页码的样式可以通过 `\pagenumbering` 命令更改；
* 该 *位置* 页码的位置可以使用 [`fancyhdr` 宏包](https://ctan.org/pkg/fancyhdr?lang=en).

## 设置页码样式

页码的样式可以使用命令

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

其中 `⟨样式⟩` 是以下之一

* `arabic`：使用阿拉伯数字（1，2，3，...）
* `alph`：使用小写字母（a，b，c，...）
* `Alph`：使用大写字母（A，B，C，...）
* `roman`：使用小写罗马数字（i，ii，iii，...）
* `Roman`：使用大写罗马数字（I，II，III，...）

### 页码样式演示示例

下面的示例排版了目录，随后是 5 页，每一页都演示页码的某一种 `⟨样式⟩` 选项。LaTeX 内部使用所谓的 *计数器* 来记录当前页码。计数器是 LaTeX 变量的名称，用于存储整数值——更多细节和示例请参见 [Overleaf 计数器文章](/latex/zh-cn/ge-shi-hua/10-counters.md) 。

```latex
\begin{document}
% 插入目录
\tableofcontents
\newpage
\section{大写罗马数字}
\pagenumbering{Roman}% 大写字母 R：大写罗马数字
\blindtext[1]
\newpage
\section{小写罗马数字} % 小写罗马数字
\pagenumbering{roman}
\blindtext[1]
\newpage
\section{阿拉伯数字}
\pagenumbering{arabic} % 阿拉伯/印度数字页码
\blindtext[1]
\newpage
\section{小写字母}
\pagenumbering{alph} % 小写字母页“数字”
\blindtext[1]
\newpage
\section{大写字母}
\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/a8e1b6d2d82c9ee161013f42844048082913e4c7)

下面是该示例生成的两页样例；第一张图显示的是大写罗马数字页码，第二张图显示的是大写字母页码：

![使用罗马数字排版的页码示例](/files/bad2c9809533aa93acab18412c539c6997c309f0)

![使用大写字母排版的页码示例](/files/02c1c01ca273cc6e06edef385bdd5932ee3455a2)

## 书籍文档类：使用两种页码样式

该 [书籍的前几页](https://en.wikipedia.org/wiki/Book_design#Structure)，包括版权页、扉页或半扉页、任何前言或序言以及目录，统称为 *前置* 页，简称 prelims——这些页面的另一个术语是 [*前置内容*](https://en.wikipedia.org/wiki/Book_design#Front_matter)。传统上，前置页使用小写罗马数字编号，而正文页面使用阿拉伯数字编号。 `book` 文档类包含有助于此的命令，如下面的示例所示。

```latex
\documentclass{book}
% emptypage 宏包可防止空白页上出现页码和
% 页眉。
\usepackage{emptypage}
\begin{document}
\frontmatter % 使用小写罗马数字作为页码
\chapter*{前言}
\addcontentsline{toc}{chapter}{前言}
前言由并非本书作者的人撰写。

\chapter*{序言}
\addcontentsline{toc}{chapter}{序言}
序言由本书作者撰写。

\tableofcontents

\mainmatter % 现在使用阿拉伯数字作为页码

\chapter{第一章}
这将是一个空章……
\section{第一节}
最好还是有些文字。
\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/1b5c40a8614ad594c85151b6e47846f023f63c5c)

控制页码编号的命令有：

* `\frontmatter`：此命令之后、命令 `\mainmatter`之前的页面将使用小写罗马数字编号。
* `\mainmatter`：这将重新开始页码计数器并将样式改为阿拉伯数字。

## 文章文档类：使用两种页码样式

如上所述，当前页码存储在一个 [LaTeX 计数器变量](/latex/zh-cn/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*{嘉宾前言（待定）}
\addcontentsline{toc}{section}{前言：4页（待定）}
待撰写：预留 4 页。
\newpage
\setcounter{page}{5}

\section*{引言（2页）}
\addcontentsline{toc}{section}{引言：2页（待定）}
待撰写：预留 2 页。
\newpage
\addtocounter{page}{1}

\section*{策略摘要（2页）}
\addcontentsline{toc}{section}{策略摘要：2页（待定）}
待撰写：2页。
\newpage
\stepcounter{page}

\tableofcontents

\newpage
\pagenumbering{arabic}

\section{一级标题}
本页上的一些文字。
\newpage
\section{另一个一级标题}
\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/8a7c74e8e08b08d55b15d263c88d16ab9346b1e1)

## 使用 fancyhdr 宏包自定义页码

该 `fancyhdr` 宏包可用于自定义页码的位置和格式；例如，将其放置在页眉或页脚中的特定位置。Overleaf 文章 [页眉和页脚](/latex/zh-cn/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{一}
\blindtext[1]
\newpage
\section{二}
\blindtext[1]
\newpage
\section{三}
\blindtext[1]
\newpage
\section{四}
\blindtext[1]
\newpage
\section{五}
\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/f94f6e3b543e1b35feb74ce5d1e8066e2e6d8318)


---

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