Configure page numbers with fancyhdr in LaTeX.
Calling \fancyhf{} clears headers and footers completely, which wipes out the default page number. Restoring and positioning your page number requires placing \thepage into a specific header or footer slot such as \fancyfoot[C]{\thepage} or \fancyhead[RO,LE]{\thepage}.
The short answer: load fancyhdr, call \pagestyle{fancy}, wipe the defaults with \fancyhf{}, then assign \thepage to your chosen slot with \fancyfoot[C]{\thepage} or \fancyhead[R]{\thepage}. If chapter opening pages ignore your layout, redefine the plain style using \fancypagestyle{plain}.
Basic placement in headers and footers
The fancyhdr package assigns headers and footers through six standard positions: Left, Center, and Right for both head and foot.
| Target position | fancyhdr command | Typical use case |
|---|---|---|
Ready-to-use configurations
Choose the layout that matches your document structure and paste it directly into your preamble.
Single-sided document with bottom-right numbering
This setup places your name or project title in the top left, section titles in the top right, and the page number in the bottom right corner.
\documentclass{article} \usepackage{fancyhdr} \pagestyle{fancy} \fancyhf{} \fancyhead[L]{Project Report} \fancyhead[R]{\nouppercase{\rightmark}} \fancyfoot[R]{\thepage} \renewcommand{\headrulewidth}{0.4pt} \renewcommand{\footrulewidth}{0pt}Two-sided book layout with outer corner numbers
In books and duplex prints, numbers belong on the outer margins: the right side on odd pages and the left side on even pages. Use the E (even) and O (odd) selectors.
\documentclass[twoside]{book} \usepackage{fancyhdr} \pagestyle{fancy} \fancyhf{} \fancyhead[LE,RO]{\thepage} \fancyhead[RE]{\nouppercase{\leftmark}} \fancyhead[LO]{\nouppercase{\rightmark}} \renewcommand{\headrulewidth}{0.4pt}Total page count: Page X of Y
To display progress such as Page 3 of 12, combine fancyhdr with the lastpage package. The \pageref{LastPage} command reads the label generated on the final compilation pass.
\documentclass{article} \usepackage{lastpage} \usepackage{fancyhdr} \pagestyle{fancy} \fancyhf{} \fancyfoot[C]{Page \thepage\ of \pageref{LastPage}} \renewcommand{\headrulewidth}{0pt}
Fixing page numbers on chapter opening pages
A frequent source of confusion occurs when chapter start pages ignore your fancy settings and revert to bottom-center numbering.
Changing the numeral style and resetting counters
The \thepage macro prints the current value formatted by the active \pagenumbering scheme.
| Command | Format produced | Sample output |
|---|---|---|
| \pagenumbering{arabic} | Standard Arabic numbers | 1, 2, 3, 4 |
| \pagenumbering{roman} | Lowercase Roman numerals | i, ii, iii, iv |
| \pagenumbering{Roman} | Uppercase Roman numerals | I, II, III, IV |
| \pagenumbering{alph} | Lowercase Latin letters |
Troubleshooting common fancyhdr issues
When page numbers disappear or land in unexpected spots, the cause is usually counter resets, package conflicts, or header height warnings.
Page number vanished after adding fancyhdr
If you invoked \fancyhf{} but forgot to define \fancyfoot or \fancyhead with \thepage, your headers and footers remain blank. Add \fancyfoot[C]{\thepage} to bring it back.
Package fancyhdr Warning: \headheight is too small
When headers include text and numbers, the default 12pt headheight is often insufficient. Add \setlength{\headheight}{14.5pt} or pass headheight=15pt to geometry in your preamble.
\usepackage[headheight=15pt]{geometry}Unwanted headers on blank pages before new chapters
In two-sided documents, clearpage calls can insert blank pages carrying running headers. Load the emptypage package to keep automatic blank pages clean.
\usepackage{emptypage}Page X of Y prints as Page X of ??
The lastpage package relies on cross-referencing. You must run pdfLaTeX or your chosen compiler at least twice to resolve the final page label.
よくある質問
Why did my page numbers disappear when I added fancyhdr?
Calling \fancyhf{} clears every header and footer field in the document. To show page numbers again, explicitly assign \thepage to a field such as \fancyfoot[C]{\thepage} or \fancyhead[R]{\thepage}.
How do I put page numbers in the top-right corner?
Clear the default layout using \fancyhf{}, then insert \fancyhead[R]{\thepage}. Make sure \pagestyle{fancy} is active in your preamble.
How do I format page numbers as Page X of Y?
Load the lastpage package with \usepackage{lastpage}, then define your footer with \fancyfoot[C]{Page \thepage\ of \pageref{LastPage}}. Compile the document twice so LaTeX can resolve the final page reference.
あなたの論文を Texeno で試してみましょう。
無料で始めて、プロジェクトを .zip でアップロードし、リコンパイルを押すだけ。コンパイル時間を延ばしたくなったらアップグレードしてください。
書き始める