我正在尝试写一封正式信函。我遇到的问题是如何fancyhdr
正确使用。我希望实现以下目标:
- 如果只有一页:
- 仅页眉和页脚 - 没有页码。
- 如果超过一页:
- 第一页的特定页眉
- 最后一页的特定页脚
- 所有页面的页码
代码已包含在内。
% !TEX TS-program = lualatex
\documentclass{letter}
\usepackage{geometry}
\geometry{letterpaper}
\usepackage{setspace}
\usepackage[ddmmmyyyy]{datetime}
\usepackage{fancyhdr}
\usepackage{fontspec}
\usepackage{lipsum}
\fancypagestyle{first-page}{
\fancyhf{}% No header/footer
\fancyhead[C]{ABC}
\fancyfoot[R]{\thepage}% Right footer
\renewcommand{\headrulewidth}{0.4pt}% No header rule
\renewcommand{\footrulewidth}{0.4pt}% A footer rule
}
}
\fancypagestyle{default}{
\fancyhf{}% No header/footer
\fancyfoot[R]{\thepage}% Right footer
\renewcommand{\footrulewidth}{0.4pt}% A footer rule
}
\fancypagestyle{last-page}{
\fancyhf{}% No header/footer
\fancyfoot[C]{ABC}% Centre footer
\fancyfoot[R]{\thepage}% Right footer
\renewcommand{\footrulewidth}{0.4pt}% A footer rule
}
\AtBeginDocument{\thispagestyle{first-page}}
\AtEndDocument{\thispagestyle{last-page}}
\begin{document}
\raggedleft
\today
\normalsize Receiver\leavevmode \\ \small Address \leavevmode \\ \small Etc \leavevmode%
\raggedright
\normalsize Dear Receiver,
\lipsum[1-6]
\normalsize Sender \leavevmode \\
\small Sender Address \leavevmode \\ \small
Sender address etc \leavevmode \\ \small
\end{document}
2020.01.27 更新
我能够使用以下代码解决我的问题:
% !TEX TS-program = lualatex
\documentclass{letter}
\usepackage{lastpage}
\usepackage{refcount}
\usepackage{zref-lastpage}
\usepackage{hyperref}
\usepackage{etoolbox}
\usepackage{ifthen}
\usepackage{lipsum,fancyhdr}
\usepackage{geometry}
\geometry{letterpaper}
\usepackage{setspace}
\usepackage[ddmmmyyyy]{datetime}
\pagestyle{fancy}
\makeatletter
\newcommand{\LastPageNumber}{\zref@extractdefault{LastPage}{page}{0}}
\makeatother
\begin{document}
\ifnumcomp{\getpagerefnumber{LastPage}}{>}{1}
%for multiple pages header
{{\fancyhead[C]{\ifthenelse{\value{page}=1}{First Page of Multiples}{Other pages of Multiples}}}}
%for only one page
{\fancyhead[C]{Single Page Header}
}
\ifnumcomp{\getpagerefnumber{LastPage}}{>}{1}
%for multiple pages header
{{
\fancyfoot[R]{\thepage}
\fancyfoot[C]{\ifnum\value{page}<\LastPageNumber
\textit{Continue Multiple Pages}%
\else
\textit{Final Page End}
\fi
}}}
%for only one page
{\fancyfoot[C]{Single Page End}}
% If document is longer than 2 pages there will be page numeration, if not there will not be.
\lipsum[1-10] % automatically remove page number in a document with this line
\end{document}