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

# Đánh số trang

Số trang của tài liệu có thể được dàn kiểu bằng một *style*, chẳng hạn bằng cách dùng chữ số Ả Rập hoặc số La Mã, và được dàn kiểu tại một vị trí trang *vị trí*—thường trong phần đầu trang hoặc chân trang. Bài viết này cho thấy cách thay đổi kiểu và vị trí của số trang:

* trang *style* của số trang có thể được thay đổi bằng `\pagenumbering` lệnh;
* trang *vị trí* của số trang có thể được thay đổi bằng [`fancyhdr` gói](https://ctan.org/pkg/fancyhdr?lang=en).

## Thiết lập kiểu số trang

Kiểu số trang có thể được thay đổi bằng lệnh

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

trong đó `⟨style⟩` là một trong

* `arabic`: dùng chữ số Ả Rập (1, 2, 3, ...)
* `alph`: dùng chữ cái thường (a, b, c, ...)
* `Alph`: dùng chữ cái in hoa (A, B, C, ...)
* `roman`: dùng số La Mã thường (i, ii, iii, ...)
* `Roman`: dùng số La Mã in hoa (I, II, III, ...)

### Ví dụ minh họa các kiểu số trang

Ví dụ sau dàn kiểu một mục lục, tiếp theo là 5 trang, mỗi trang minh họa một trong các `⟨style⟩` tùy chọn cho số trang. Bên trong, LaTeX sử dụng một cái gọi là *bộ đếm* để ghi lại số trang hiện tại. Một bộ đếm là tên của một biến LaTeX dùng để lưu một giá trị số nguyên—xem bài [bài viết Overleaf Counters](/latex/vi/dinh-dang/10-counters.md) để biết thêm chi tiết và ví dụ.

```latex
\begin{document}
% Chèn mục lục
\tableofcontents
\newpage
\section{Số La Mã in hoa}
\pagenumbering{Roman}% Chữ 'R' viết hoa: số La Mã in hoa
\blindtext[1]
\newpage
\section{Số La Mã in thường} % số La Mã in thường
\pagenumbering{roman}
\blindtext[1]
\newpage
\section{Chữ số Ả Rập}
\pagenumbering{arabic} % số trang Ả Rập/Ấn Độ
\blindtext[1]
\newpage
\section{Chữ cái thường}
\pagenumbering{alph} % "số" trang bằng chữ cái thường
\blindtext[1]
\newpage
\section{Chữ cái in hoa}
\pagenumbering{Alph} % "số" trang bằng chữ cái in hoa
\blindtext[1]
\end{document}
```

[Mở ví dụ này trong 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)

Hình tiếp theo cho thấy mục lục được tạo bởi tài liệu này. Hãy chú ý cách mà `\pagenumbering` lệnh đã đặt lại số trang bắt đầu về giá trị ban đầu cho mỗi kiểu: `i` cho `roman` kiểu, `Một` cho `Alph` kiểu, `I` cho `Roman` kiểu, và cứ thế:

![Ví dụ mục lục được dàn kiểu trong LaTeX](/files/c1e7d934440fa012713e6326238f732e3ae23224)

Sau đây là hai trang mẫu được tạo bởi ví dụ này; hình đầu tiên hiển thị số trang bằng số La Mã in hoa, hình thứ hai minh họa số trang bằng chữ cái in hoa:

![Ví dụ về số trang được dàn kiểu bằng số La Mã](/files/cb141d993e3e95fc2c3f69ccb22e5923457b4429)

![Ví dụ về số trang được dàn kiểu bằng chữ cái in hoa](/files/192aab94fa2726f26f5556b44aafec6514883961)

## Lớp Book: dùng hai kiểu số trang

Tính năng [vài trang đầu của một cuốn sách](https://en.wikipedia.org/wiki/Book_design#Structure), bao gồm trang bản quyền, trang nhan đề hoặc trang nhan đề phụ, bất kỳ Lời nói đầu hoặc Lời tựa nào và mục lục, được gọi chung là các *phần đầu* trang, hay gọi tắt là prelims—một thuật ngữ khác cho những trang này là [*phần đầu sách*](https://en.wikipedia.org/wiki/Book_design#Front_matter). Theo truyền thống, các trang prelim được đánh số bằng số La Mã thường, còn các trang chính (nội dung) được đánh số bằng chữ số Ả Rập.  `book` lớp tài liệu chứa các lệnh hỗ trợ việc này, như ví dụ sau minh họa.

```latex
\documentclass{book}
% Gói emptypage ngăn số trang và
% tiêu đề xuất hiện trên các trang trống.
\usepackage{emptypage}
\begin{document}
\frontmatter %Dùng số La Mã thường cho số trang
\chapter*{Lời nói đầu}
\addcontentsline{toc}{chapter}{Lời nói đầu}
Lời nói đầu do một người không phải là tác giả cuốn sách viết.

\chapter*{Lời tựa}
\addcontentsline{toc}{chapter}{Lời tựa}
Lời tựa do tác giả cuốn sách viết.

\tableofcontents

\mainmatter % Giờ dùng chữ số Ả Rập cho số trang

\chapter{Chương đầu tiên}
Đây sẽ là một chương trống...
\section{Phần thứ nhất}
Sẽ tốt nếu có vài dòng văn bản.
\chapter{Chương thứ hai}
\end{document}
```

[Mở ví dụ này trong 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)

Mục lục do ví dụ này tạo ra minh họa việc dùng số La Mã cho phần đầu sách và chữ số Ả Rập cho phần chính (nội dung):

![Hiển thị số trang La Mã và Ả Rập trong một cuốn sách](/files/e52e978a2af69ec8d85e2e07574927a3eb781fac)

Các lệnh điều khiển việc đánh số trang là:

* `\frontmatter`: Các trang sau lệnh này và trước lệnh `\mainmatter`, sẽ được đánh số bằng số La Mã thường.
* `\mainmatter`: Lệnh này sẽ khởi động lại bộ đếm trang và đổi kiểu sang chữ số Ả Rập.

## Lớp Article: dùng hai kiểu số trang

Như đã nêu ở trên, số trang hiện tại được lưu trong một [biến bộ đếm của LaTeX](/latex/vi/dinh-dang/10-counters.md#introduction-to-latex-counters) có tên là `page`. Giá trị được lưu trong `page`, hoặc bất kỳ biến bộ đếm nào khác, có thể được đặt thành một giá trị cụ thể bằng lệnh `\setcounter` lệnh:

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

trong đó biến bộ đếm `⟨countvar⟩` được đặt thành giá trị `⟨intval⟩`. Ví dụ, để đặt `page` bộ đếm thành `3` bạn sẽ viết

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

Các lệnh khác để thay đổi biến bộ đếm bao gồm `\addtocounter` và `\stepcounter`:

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

* `\addtocounter{⟨countvar⟩}{⟨increment⟩}` thêm một lượng `⟨increment⟩` vào biến bộ đếm `⟨countvar⟩`. **Lưu ý**: `⟨increment⟩` có thể là số dương để tăng giá trị bộ đếm, hoặc số âm để giảm giá trị đó.
* `\steptocounter{⟨countvar⟩}` thêm 1 vào biến bộ đếm `⟨countvar⟩`

Ví dụ giả định này sửa đổi bộ đếm trang để lập kế hoạch cho mục lục, bao gồm các mục chưa được viết và có số trang ước tính. Nó cũng dùng `\pagenumbering{roman}` và `\pagenumbering{Arabic}` để đặt kiểu số trang.

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

\section*{Lời nói đầu khách mời (TBD)}
\addcontentsline{toc}{section}{Lời nói đầu: 4 trang (TBD)}
Sẽ viết sau: dành 4 trang.
\newpage
\setcounter{page}{5}

\section*{Giới thiệu (2 trang)}
\addcontentsline{toc}{section}{Giới thiệu: 2 trang (TBD)}
Sẽ viết sau: cho phép 2 trang.
\newpage
\addtocounter{page}{1}

\section*{Tóm tắt chiến lược (2 trang)}
\addcontentsline{toc}{section}{Tóm tắt chiến lược: 2 trang (TBD)}
Sẽ viết sau: 2 trang.
\newpage
\stepcounter{page}

\tableofcontents

\newpage
\pagenumbering{arabic}

\section{Tiêu đề cấp 1}
Một đoạn văn bản trên trang này.
\newpage
\section{Một tiêu đề cấp 1 khác}
\end{document}
```

[Mở ví dụ này trong 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)

Ví dụ này tạo ra mục lục sau, cho thấy kết quả của việc thay đổi giá trị của `page` bộ đếm:

![Thay đổi bộ đếm trang trong LaTeX](/files/ced15c75090b9d5f65bf62b660604df970fd6c84)

## Tùy chỉnh số trang với gói fancyhdr

Tính năng `fancyhdr` gói có thể được dùng để tùy chỉnh vị trí và định dạng của số trang; chẳng hạn đặt chúng ở các vị trí cụ thể trong đầu trang hoặc chân trang. Bài viết Overleaf [Đầu trang và chân trang](/latex/vi/dinh-dang/02-headers-and-footers.md) khám phá điều này chi tiết, với nhiều ví dụ mà chúng tôi sẽ không lặp lại ở đây.

Ở đây chúng ta sẽ nêu một ví dụ về cách viết số trang hiện tại trong ngữ cảnh tổng số trang, chẳng hạn `Trang X trên Y` trong đó Y là tổng số trang của tài liệu thu được bằng lệnh [`lastpage` gói](https://ctan.org/pkg/lastpage?lang=en).

```latex
\documentclass{article}
% Dùng kích thước trang nhỏ để tránh
% cần nhiều văn bản để tạo ra
% nhiều trang
\usepackage[includefoot,
paperheight=10cm,
paperwidth=10cm,
textwidth=9cm,
textheight=8cm]{geometry}
\usepackage{blindtext}
\usepackage{lastpage}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{} % xóa các mục đầu/chân trang hiện có
% Đặt Trang X trên Y ở phía bên phải
% của chân trang
\fancyfoot[R]{Trang \thepage \hspace{1pt} trên \pageref{LastPage}}
\begin{document}
% Chèn mục lục
\tableofcontents
\newpage
\section{Một}
\blindtext[1]
\newpage
\section{Hai}
\blindtext[1]
\newpage
\section{Ba}
\blindtext[1]
\newpage
\section{Bốn}
\blindtext[1]
\newpage
\section{Năm}
\blindtext[1]
\end{document}
```

[Mở ví dụ này trong 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)

Hình sau cho thấy một trong các trang được tạo bởi ví dụ này—hãy chú ý `Trang 3 trên 6` ở phía bên phải chân trang:

![Thay đổi số trang với fancyhdr](/files/d268b2db58f41df32b9c24e97ad1a514a1b0f305)


---

# 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/vi/dinh-dang/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.
