# Adding page numbers in absolute position (fancyhdr, tikz)

I've been playing with the design of a letterhead class for our department. We want two pictures on the header, with a line under it; and the address on the footer, with a line over it. Here is a MWE that works OK:

mwe.cls:

% Class declaration
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{mwe}
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{letter}}
\ProcessOptions \relax

% Packages
\RequirePackage{tikz}
\RequirePackage{graphicx}
\RequirePackage[utf8]{inputenc}
\RequirePackage[brazil]{babel}
\RequirePackage{fancyhdr}
\RequirePackage{xcolor}

% Colors
\definecolor{xLine}{rgb}{.58,.80,.87}

% Change width and color of the lines under header and over footer
\renewcommand{\footrulewidth}{2pt}

\fancypagestyle{plain}{
\fancyhf{}
\fancyfoot[C]{
Our Department\\
Our University\\
Our Address -- a longer line\\
Our phone number -- Our e-mail}
}

\pagestyle{plain}


I've added some images to the header, just in case here they are:

mwe.tex:

\documentclass[12pt]{mwe}
\usepackage{lipsum}
\begin{document}
\begin{Large}Yay, a document!\end{Large}
\lipsum[3-12]
\end{document}


Two runs with pdflatex gives this:

Looking good! But now I want to add a number to each page. I could add a \thepage in any position on the footer, but I'd rather have the number on the right side of the footer:

Based on TikZ full page with absolute node positioning I've tried to define a tikz figure with

\newcommand{\mypagenumber}{
\begin{tikzpicture}[remember picture,overlay]
\node[anchor=north,minimum height=0.7cm, minimum width=2cm] (names)
at ([xshift=8.5cm,yshift=3cm]current page.south) {\color{xLine}\Large\thepage};
\end{tikzpicture}}


and this to the definition of \fancypagestyle

  \chead{\mypagenumber} % Why not? as long as it appears on every page


Results are almost what I want: pages 1-9 looks OK:

But as soon as I get two digits on the number page alignment gets bad:

Finally, here are the questions:

1. How can I define my tikzpicture so the number will be right-aligned with the horizontal line over the footer, even if I change the text size or font?
2. Is it possible to define the values (anchors, shifts, etc.) in tikzpicture in such a way that if I change the page margins or paper size I don't need to change these values? E.g. is it possible to make the tikzpicture position relative to the line over the footer?
3. It works but it seems a ugly hack, is there a better/simpler/more robust way?

EDIT -- UPDATE

Here's something that works (based on a different tikz issue in Left and right align inside one TiKZ rectangular node):

\newcommand{\mypagenumber}{
\begin{tikzpicture}[remember picture,overlay]
\node[fill=gray!20,anchor=north,minimum height=0.7cm,
text width=3cm,align=flush right] (names)
at ([xshift=6.97cm,yshift=3cm]current page.south)
{\color{xLine}\hfill\Large\bf\thepage};
\end{tikzpicture}}


There is a gray background to help visualize the alignment:

Still questions 2 and 3 remain, I had to adjust the xshift parameter until I got the results I wanted -- not sure it is robust. Any help appreciated.

Thanks, Rafael

• Does adding left to \node[anchor=north,...] help? – user31729 Feb 4 '18 at 21:12
• Change anchor=north to anchor=north east. However, this probably isn't the best way to do it. (I mean, this method generally.) – cfr Feb 4 '18 at 21:19
• @ChristianHupfer: adding just a "left" changed the position of the text. I've fixed it through the xshift and yshift parameters but the problem is still there: the page numbers are not aligned to the right. – Rafael Santos Feb 4 '18 at 22:25
• @cfr : results still not aligned. – Rafael Santos Feb 4 '18 at 22:28
• @RafaelSantos: Did you try cfr's suggestion then? – user31729 Feb 4 '18 at 22:28

     \documentclass[12pt]{mwe}
\usepackage{lipsum}
\usepackage{tikzpagenodes}
\newcommand{\mypagenumber}{
\begin{tikzpicture}[remember picture,overlay]
\node[fill=gray!20,anchor=east,minimum height=0.7cm,
text width=3cm,align=flush right] (names)
at (current page footer area.south east)
{\color{xLine}\hfill\Large\bf\thepage};
\end{tikzpicture}}
\begin{document}
\begin{Large}Yay, a document!\end{Large}
\lipsum[3-120]
\end{document}


When compiling the original first, I got an error message; it seems that

         \LoadClass{letter}


appears twice in mwe.cls.

• Much better, got rid of the {x,y}shifts! Thanks! – Rafael Santos Feb 5 '18 at 22:26