![如何将文本放在 fancyhdr 的顶部](https://linux22.com/image/267885/%E5%A6%82%E4%BD%95%E5%B0%86%E6%96%87%E6%9C%AC%E6%94%BE%E5%9C%A8%20fancyhdr%20%E7%9A%84%E9%A1%B6%E9%83%A8.png)
我制作了一个包含两页和标题的文档:
- 我可以将“第 2 页,共 2 页”放在第 2 页的顶部,以便与“商业发票”垂直对齐吗?
- 我该如何管理标题和“文本”之间的填充?
\vspace
如果可能的话,不要使用负数。
代码:
\documentclass{article}
\usepackage{lastpage}
\usepackage{fancyhdr}
\pagestyle{fancy}
% no line in header area
\renewcommand{\headrulewidth}{0pt}
\setlength{\headheight}{96pt}
\fancyhead[L]{
%Text must be shown only on the 1-st page
\count0 = \thepage
\ifnum \count0 = 1
\begin{center}\Large{\bf{Commercial invoice}}\end{center}
This invoice must be completed in English
\fi
%page number, aligned right on all the pages
\hfill Page \thepage~of~\pageref{LastPage}
}
%no page number at the bottom
\fancyfoot{}
\begin{document}
text
\clearpage
text
\end{document}
答案1
是的。请参阅下面的代码。
您可以使用
geometry
包及其headsep
具有适当值的键:
代码(我仅用于a5paper
示例):
\documentclass{article}
\usepackage[a5paper,headsep=3mm]{geometry}
\usepackage{lastpage}
\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\headrulewidth}{0pt}
\setlength{\headheight}{96pt}
\fancyhead[L]{%
\ifnum\value{page}=1
\begin{center}\Large\bfseries Commercial invoice\end{center}
This invoice must be completed in English\hfill
Page \thepage~of~\pageref{LastPage}
\else
\begin{center}\hfill Page \thepage~of~\pageref{LastPage}\end{center}
\hfill\phantom{P}
\fi
}
\fancyfoot{}
\begin{document}
text
\clearpage
text
\end{document}
答案2
调整页面大小\headsep
和其他可能的页面参数。框架只是为了更好地显示对齐。
\documentclass{article}
\usepackage[a5paper,
headsep=48pt,% adjust this space
headheight=14pt,% this is mandatory
includehead,
showframe,% this is for the example
]{geometry}
\usepackage{lastpage}
\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\headrulewidth}{0pt}
\newcommand\firstpagehead{%
\parbox[t][0pt]{\textwidth}{%
{\centering\Large\bfseries Commercial invoice\par}
\medskip
This invoice must be completed in English\hfill
Page \thepage~of~\pageref{LastPage}}%
}
\newcommand\otherpagehead{%
\parbox[t][0pt]{\textwidth}{%
\raggedleft
Page \thepage~of~\pageref{LastPage}}%
}
\fancyhead[L]{\ifnum\value{page}=1 \firstpagehead\fi}
\fancyhead[R]{\ifnum\value{page}=1\else \otherpagehead\fi}
\fancyfoot{}
\begin{document}
text
\clearpage
text
\end{document}