1

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{}
\fancyhead[R]{\thepage}
\renewcommand{\headrulewidth}{0pt}

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{}
\fancyhead[R]{\thepage}
\renewcommand{\headrulewidth}{0pt}
\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}
  • 1
    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
2

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:

enter image description here

\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{}
  \fancyhead[R]{\thepage}
  \renewcommand{\headrulewidth}{0pt}
}
\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{}
  \fancyhead[R]{\thepage\hspace*{2.25in}}
  \renewcommand{\headrulewidth}{0pt}
}

\chapter{Introduction}

\chapter{Beautiful}

\end{document}
  • Thank you so much for your great answer – Aria Apr 30 at 2:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.