# fancyhdr package does not put my page number all the way to the right for table of content

I am using fancyhdr package to put all the page numbers on the top right corners of the pages as below:

\documentclass[10pt,openany,oneside]{book}
\usepackage{fancyhdr}
\pagestyle{fancyplain}% <- use fancyplain instead fancy
\fancyhf{}


unfortunately on my table of contents this page number is not all the way on right it is on middle right. But for my chapters the page number is all the way on the right. Do you know how to fix this?

\documentclass[10pt,openany,oneside]{book}
\usepackage{fancyhdr}
\pagestyle{fancyplain}
\fancyhf{}
\usepackage{geometry}
\geometry{paperheight=9.8125in,paperwidth=8in, left=.5in,
right=.5in,top=.75in,bottom=.4375in }
\renewcommand{\rmdefault}{pag}
\begin{document}
\newcommand{\titlefontarash}{\usefont{OT1}{pag}{b}{n}\fontsize{25}
{\baselineskip} \selectfont}
\title{\titlefontarash  Good Boy}
\author{\titlefontarash good boy}
\date{\titlefontarash April 29, 2019}
\maketitle
\frontmatter
\vspace*{\fill}
\begin{center}
\titlefontarash To Good Manners
\linebreak
\titlefontarash To Nice Manners
\end{center}
\vspace*{\fill}
\tableofcontents
\newgeometry{ left=2.75in, right=.5in }
\mainmatter
\chapter{Introduction}
\chapter{Beautiful}
\end{document}

• Welcome to TeX SX! Could you post a small compilable code demonstratng the problem? – Bernard Apr 29 at 8:51
• Thanks Bernard and Kurt I added a compilable code demonstrating the problem as a minimal working example – Aria Apr 29 at 23:55
• @ArashRashidi: Load fancyhdr after geometry. – Werner Apr 30 at 0:35
• @Werner: Thanks for your comment but this removes the page numbers for the chapters as a drawback – Aria Apr 30 at 0:45

fancyhdr's positioning is dependent on the page geometry. As such, you should load fancyhdr after geometry. Additionally, the adjustment of the page geometry via \newgeometry is the main cause for the missing page numbers, since the layout is stuck using the older dimensions. You can adjust the fancyhdr layout mid-document, immediately after performing a \newgeometry:

\documentclass[10pt,openany,oneside]{book}

\usepackage{geometry}
\geometry{
paperheight = 9.8125in,
paperwidth  = 8in,
left        = .5in,
right       = .5in,
top         = .75in,
bottom      = .4375in
}

\usepackage{fancyhdr}
\fancypagestyle{plain}{
\fancyhf{}
}
\pagestyle{plain}

\renewcommand{\rmdefault}{pag}

\newcommand{\titlefontarash}{\usefont{OT1}{pag}{b}{n}\fontsize{25}{\baselineskip}\selectfont}

\title{\titlefontarash  Good Boy}
\author{\titlefontarash good boy}
\date{\titlefontarash April 29, 2019}

\begin{document}

\maketitle

\frontmatter

\vspace*{\fill}

\begin{center}
\titlefontarash To Good Manners
\linebreak
\titlefontarash To Nice Manners
\end{center}

\vspace*{\fill}

\tableofcontents

\newgeometry{
left  = 2.75in,
right = .5in
}

\mainmatter

\fancypagestyle{plain}{
\fancyhf{}