使用 tikz 和 fancyhdr 模仿帶有延續頁的信頭

使用 tikz 和 fancyhdr 模仿帶有延續頁的信頭

我正在嘗試模仿帶有續頁的信箋。信頭格式如下:首頁為彩色橫幅,右側為徽標,左側為部門名稱。第一頁底部附近還有部門地址。所有這些都必須絕對定位。續頁在相同位置有徽標,但沒有其他內容。

在我的無知中,我以為我會使用 tikz 使用頁面座標進行絕對定位。這適用於第一頁,但似乎不適用於任何後續頁面。也就是說,延續頁面應該包含徽標,並且僅包含徽標,但不包含任何內容。到目前為止,我還沒有找到任何完全解決這個問題的答案,而且我很確定我在 fancyhdr 中陷入了愚蠢的錯誤,並且可能以錯誤的方式啟動。

這是當前輸出的範例。第二頁和所有後續頁面應該在與第一頁完全相同的位置有一個演示圖像。

顯示問題的輸出影像

這是類別文件

% class file MWE
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{mweletter}

\DeclareOption*{\PassOptionsToClass{\CurrentOption}{letter}}

\ProcessOptions

\RequirePackage[demo]{graphicx}
\LoadClass[a4paper]{letter}
\RequirePackage[left=2.5cm,showframe]{geometry}
\RequirePackage{fancyhdr}
\RequirePackage{tikz}
%\RequirePackage{fontspec}
\usetikzlibrary{calc}

\newsavebox{\addressbox}
\savebox{\addressbox}{
  \sffamily\fontsize{8}{10}\selectfont
  \textcolor{blue}{\begin{minipage}{7cm}
      Block A Level 4, Simpson Street, Springfield USA  \\%
    Tel: (+1) 617 123 4567 \\
  \end{minipage}}
}

\renewcommand{\headrulewidth}{0pt}%
\renewcommand{\footrulewidth}{0pt}%

\pagestyle{plain}
\AtBeginDocument{\thispagestyle{firstpage}}

\fancypagestyle{firstpage}{%
  \fancyhf{}%
  \begin{tikzpicture}[overlay, remember picture]
    \fill [anchor=north west,inner sep=0pt,orange]
      (current page.north west)  rectangle ($(current page.north east) + (0,-3cm)$);
    \node[anchor=north east,inner sep=0pt] at ($(current page.north east) + (-1cm,-0.5cm)$)
      {\includegraphics[height = 1.6cm]{demo}};
    \node[anchor=north west,inner sep=0pt] at
     ($(current page.north west) + (2.5cm,-0.5cm)$) {
      \sffamily\fontsize{8}{12} Department
      of Excellent Science };
    \node[anchor=south west, inner sep=0pt] at
    ($(current page.south west) + (2.5cm,1.5cm)$) {\usebox{\addressbox}};
  \end{tikzpicture}
}

\fancypagestyle{plain}{%
  \fancyhf{}%
  \begin{tikzpicture}[overlay, remember picture]
    \node[anchor=north east,inner sep=0pt]
    at ($(current page.north east) + (-1cm,-0.5cm)$)
    {\includegraphics[height = 1.6cm]{demo}};
  \end{tikzpicture}
}

以及一個乳膠文件來運行它。

\documentclass[12pt]{mweletter}
\usepackage{blindtext}
\signature{%
  Johnathan "John" I.Q. Neidelbaum Frink, Jr., B.Sc., Ph.D. M.R.S.C. \\ 
  The Principal Professor \\
  Springfield Heights Institute of Technology \\
  Springfield, America
}
\date{\today}
\begin{document}
\begin{letter}{Prof Tan\\
    Beijing Da Xue}
\opening{Dear Prof Tan,}
\Blindtext[5]
\closing{Yours sincerely,}
\encl{mweletter.cls}
\ps{Please see the enclosed file.}
\cc{Lisa Simpson}
\end{letter}
\end{document}

答案1

fancyhdr土撥鼠的評論讓我意識到一開始使用它是多麼愚蠢。我完全沒有意識到eso-pic。這只是將元素放置在頁面上,並以不同的方式對待第一頁。

% mweletter.cls
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{mweletter}
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{letter}}
\ProcessOptions
\RequirePackage[demo]{graphicx}
\LoadClass[a4paper]{letter}
\RequirePackage[left=2.5cm,showframe]{geometry}
\RequirePackage{tikz}
\RequirePackage{eso-pic}
\usetikzlibrary{calc}

\AddToShipoutPicture{
  \begin{tikzpicture}[overlay, remember picture]
    \ifnum\value{page}=1 
    \fill [anchor=north west,inner sep=0pt,orange]
    (current page.north west)  rectangle ($(current page.north east) + (0,-3cm)$);
    \node[anchor=north west,inner sep=0pt] at
     ($(current page.north west) + (2.5cm,-0.5cm)$) {
      \sffamily\fontsize{8}{12} Department
      of Excellent Science };
    \node[anchor=south west, inner sep=0pt] at
    ($(current page.south west) + (2.5cm,1.5cm)$) {
      \sffamily\fontsize{8}{10}\selectfont
      \textcolor{blue}{
        \begin{minipage}{7cm}
          Block A Level 4, Simpson Street, Springfield USA  \\%
          Tel: (+1) 617 123 4567 \\
        \end{minipage}}};
    \fi
    \node[anchor=north east,inner sep=0pt] at ($(current page.north east) + (-1cm,-0.5cm)$)
    {\includegraphics[height = 1.6cm]{demo}};
  \end{tikzpicture}
}

這是輸入

\documentclass[12pt]{mweletter}
\usepackage{blindtext}
\signature{%
  Johnathan "John" I.Q. Neidelbaum Frink, Jr., B.Sc., Ph.D. M.R.S.C. \\ 
  The Principal Professor \\
  Springfield Heights Institute of Technology \\
  Springfield, America
}
\date{\today}
\begin{document}
\begin{letter}{Prof Tan\\
    Beijing Da Xue}
\opening{Dear Prof Tan,}
\Blindtext[4]
\closing{Yours sincerely,}
\encl{mweletter.cls}
\ps{Please see the enclosed file.}
\cc{Lisa Simpson}
\end{letter}
\end{document}

給予

正確2up

相關內容