> 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/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-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{First 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://ayakaleaf-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.
