0

I am using book class. My margins have to be 3cm at top, bottom, left, right. The code I have right now is this:

\usepackage[a4paper,left=3cm,right=3cm,top=3cm,bottom=3cm]{geometry} %for margins
\usepackage{fancyhdr} %for page number
\pagestyle{fancy}
\fancyhf{}
\fancyhead[R]{\thepage}

This gives me accurate margins and places the page number at the top right, but not in the proper place as specified. Thank you for your help!

1

1 Answer 1

1

(1) Using the tikz package it is possible to position material using absolute page coordinates, regardless of the geometry used for the text.

It only requires a single line (\node at ...) to position the page number at certain distance from the top and left of the upper right corner of the page (named current page.north east).

(Strictly speaking it will position the center of the box containing the page number).

a

\documentclass{book}

\usepackage[a4paper,left=3cm,right=3cm,top=3cm,bottom=3cm]{geometry} 

%\newgeometry{a4paper,left=3cm,right=1cm,top=1cm,bottom=3cm, showframe}%USE TEST the numpber position <<<
    
\usepackage{fancyhdr} %for page number
\pagestyle{fancy}
\fancyhf{}
\fancyhead[R]{\tikz[remember picture,overlay]\node at ([xshift=-1cm, yshift=-1cm] current page.north east){\thepage};}
    
\usepackage{tikz}% ****** added <<<<

\begin{document}
     Some words.        
\end{document}

Uncomment

\newgeometry{a4paper,left=3cm,right=1cm,top=1cm,bottom=3cm, showframe}

to test the correct placement of the page number.

b

(2) A similar result is obtained with the eso-pic package.

\documentclass{book}

\usepackage[a4paper,left=3cm,right=3cm,top=3cm,bottom=3cm]{geometry} 

%\newgeometry{a4paper,left=3cm,right=1cm,top=1cm,bottom=3cm, showframe}%USE TEST the numpber position <<<

\usepackage{fancyhdr} %for page number
\pagestyle{fancy}
\fancyhf{}
\fancyhead[R]{}

% ************** added <<<<<<<<<<<<<<<<<<<<
\usepackage{eso-pic}    

\AddToShipoutPictureFG{%
    \put(\paperwidth-1cm,\paperheight-1cm)
    {\makebox[0pt][c]{\raisebox{-0.5\height}{\thepage}}}
}
% **************

\begin{document}
     Some words.        
\end{document}

(3) And the simplest solution, without additional packages using the recent macro \AddToHook{shipout/foreground}

\documentclass{book}

\usepackage[a4paper,left=3cm,right=3cm,top=3cm,bottom=3cm]{geometry} 

%\newgeometry{a4paper,left=3cm,right=1cm,top=1cm,bottom=3cm, showframe}%USE TEST the numpber position <<<

\AddToHook{shipout/foreground}{\put(\paperwidth-1cm,-1cm){% added <<<<<<<<<<<<<<
\makebox[0pt][c]{\raisebox{-0.5\height}{\thepage}}
}}

\usepackage{fancyhdr} %for page number
\pagestyle{fancy}
\fancyhf{}
\fancyhead[R]{}

     
\begin{document}
Some words.
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .