1

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
\LoadClass{letter}

% Packages
\RequirePackage{tikz}
\RequirePackage{graphicx}
\RequirePackage[utf8]{inputenc}
\RequirePackage[brazil]{babel}
\RequirePackage[a4paper,left=1.9cm,right=1.9cm,top=3.2cm,bottom=3.3cm,headheight=56pt,headsep=0.1in]{geometry}
\RequirePackage{fancyhdr}
\RequirePackage{xcolor}

% Colors
\definecolor{xLine}{rgb}{.58,.80,.87} 
\definecolor{xAddr}{rgb}{.19,.52,.61} 

% Change width and color of the lines under header and over footer
\renewcommand{\headrulewidth}{2pt}
\renewcommand{\headrule}{\hbox to\headwidth{\color{xLine}\leaders\hrule height \headrulewidth\hfill}}
\renewcommand{\footrulewidth}{2pt}
\renewcommand{\footrule}{\hbox to\headwidth{\color{xLine}\leaders\hrule height \footrulewidth\hfill}}


\fancypagestyle{plain}{
  \fancyhf{}
  \lhead{\includegraphics[height=1.5cm]{logo1.png}\vspace{-3mm}}
  \rhead{\includegraphics[height=1.5cm]{logo2.png}\vspace{-3mm}}
  \fancyfoot[C]{
  \color{xAddr}\small\vspace{-3mm}
  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: logo1 logo2

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:

mwe

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:

where I want it

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

(added these to mwe.cls)

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

enter image description here

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

enter image description here

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:

enter image description here enter image description here

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

16
  • 1
    Does adding left to \node[anchor=north,...] help?
    – user31729
    Commented Feb 4, 2018 at 21:12
  • 1
    Change anchor=north to anchor=north east. However, this probably isn't the best way to do it. (I mean, this method generally.)
    – cfr
    Commented Feb 4, 2018 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. Commented Feb 4, 2018 at 22:25
  • @cfr : results still not aligned. Commented Feb 4, 2018 at 22:28
  • @RafaelSantos: Did you try cfr's suggestion then?
    – user31729
    Commented Feb 4, 2018 at 22:28

1 Answer 1

2

What about this:

     \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}}
     \chead{\mypagenumber}
     \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.

1
  • Much better, got rid of the {x,y}shifts! Thanks! Commented Feb 5, 2018 at 22:26

You must log in to answer this question.

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