在绝对位置添加页码(fancyhdr、tikz)

在绝对位置添加页码(fancyhdr、tikz)

我一直在为我们的部门设计信头类。我们希望在页眉上放两张图片,图片下面有一条线;在页脚上放地址,地址上面有一条线。这是一个可以正常工作的 MWE:

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}

我在标题中添加了一些图像,以防万一它们在这里: 标志1 标志2

mwe.tex:

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

使用 pdflatex 运行两次得到以下结果:

姆韦

看起来不错!但现在我想给每页添加一个数字。我可以在页脚的任何位置添加 \thepage,但我更希望将数字放在页脚的右侧:

我想要的地方

基于TikZ 整页带绝对节点定位我试图用

\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}}

这是 \fancypagestyle 的定义

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

(将这些添加到 mwe.cls)

结果几乎是我想要的:第 1-9 页看起来不错:

在此处输入图片描述

但是,一旦我得到两位数字,页面对齐就会变得很糟糕:

在此处输入图片描述

最后,还有以下问题:

  1. 我如何定义我的 tikzpicture 以便数字与页脚上方的水平线右对齐,即使我更改文本大小或字体?
  2. 是否可以这样定义 tikzpicture 中的值(锚点、移位等),以便如果我更改页边距或纸张大小,则无需更改这些值?例如,是否可以使 tikzpicture 相对于页脚上的线定位?
  3. 它有效但是看起来像一个丑陋的黑客,有没有更好/更简单/更强大的方法?

编辑-更新

这是可行的方法(基于不同的 tikz 问题在一个 TiKZ 矩形节点内左对齐和右对齐):

\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}}

有一个灰色背景,可以帮助可视化对齐:

在此处输入图片描述 在此处输入图片描述

问题 2 和 3 仍然存在,我不得不调整 xshift 参数直到得到我想要的结果——不确定它是否可靠。任何帮助都值得感激。

谢谢,拉斐尔

答案1

那这个呢:

     \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}

首先编译原始版本时,我收到一条错误消息;似乎

         \LoadClass{letter}

在 mwe.cls 中出现两次。

相关内容