Custom position of page numbers


I have a document with narrow margins (geometry: margin=1.5cm) which makes individual page number move outside of the page. How can I alter the position of page numbers so that they become visible? I had a hard time finding an easy and clear solution on the internet.



I am also wondering how can we change the position of page numbers. Fail to find useful links on the web. Anyone can help? Thanks!


Are you knitting to pdf? Are you using any LaTeX packages to set page numbers?

Check out the fancyhdr LaTeX package ( You can alter the position of the page numbers (or any footer content) by defining a new page style and adjusting the \setlegnth\footskip{..}. Here's an example.


Make sure you have the stringi package installed to run this demo as I'm generating lipsum paragraphs.

Install the fancyhdr and lastpage LaTeX packages using your preferred LaTeX utilities manager.


title: "Untitled Document"
output: pdf_document
geometry: margin=1.5cm
- \usepackage{fancyhdr}
- \usepackage{lastpage}

<!--- Define Headers and Footers --->
  \fancyfoot[C]{\footnotesize Page \thepage\, of\, \pageref*{LastPage}}

# Test

`r stringi::stri_rand_lipsum(25)`


  • I'm assuming the page numbers are placed at the bottom of the page. Change \fancyfoot[C] to \fancyhead[C] if the page numbers should be placed at the top of the page.
  • \renewcommand{\headrulewidth}{0pt}: gets rid of the package's default top rule
  • \fancyfoot[...]: takes L, C, and R (left, center, right).
  • The lastpage packages allows you to make fancy page numbers in the format of Page 1 of 5
  • Adjust \footskip{..} with your desired margin.