如何在页眉后和页脚前添加水平线?
header
---------
---------
footer
答案1
最简单的方法是包括fancyhdr
包裹并使用设置页眉/页脚规则宽度\renewcommand
:
\documentclass{article}
\usepackage{fancyhdr}% http://ctan.org/pkg/fancyhdr
\pagestyle{fancy}% Change page style to fancy
\fancyhf{}% Clear header/footer
\fancyhead[C]{Header}
\fancyfoot[C]{Footer}% \fancyfoot[R]{\thepage}
\renewcommand{\headrulewidth}{0.4pt}% Default \headrulewidth is 0.4pt
\renewcommand{\footrulewidth}{0.4pt}% Default \footrulewidth is 0pt
\begin{document}
Here is some text.
\end{document}
使用 可同时清除页眉和页脚,并使用和\fancyhf{}
可分别设置页眉和页脚。请参阅\fancyhead[<pos>]{<stuff>}
\fancyfoot[<pos>]{<stuff>}
fancyhdr
文档。
修改页眉/页脚规则的颜色有点棘手,因为fancyhdr
默认情况下不提供这种修改。etoolbox
可用于更新两个规则宏\headrule
并\footrule
根据需要插入颜色。它们定义如下:
\def\headrule{{\if@fancyplain\let\headrulewidth\plainheadrulewidth\fi
\hrule\@height\headrulewidth\@width\headwidth \vskip-\headrulewidth}}
\def\footrule{{\if@fancyplain\let\footrulewidth\plainfootrulewidth\fi
\vskip-\footruleskip\vskip-\footrulewidth
\hrule\@width\headwidth\@height\footrulewidth\vskip\footruleskip}}
很明显,水平线是使用绘制的\hrule
,因此我们可以修补这些命令,并\color{<colour>}
通过以下方便的辅助宏在绘制之前插入:
\usepackage{etoolbox,fancyhdr,xcolor}% http://ctan.org/pkg/{etoolbox,fancyhdr,xcolor}
% \patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
\newcommand{\headrulecolor}[1]{\patchcmd{\headrule}{\hrule}{\color{#1}\hrule}{}{}}
\newcommand{\footrulecolor}[1]{\patchcmd{\footrule}{\hrule}{\color{#1}\hrule}{}{}}
这允许您使用\headrulecolor{<colour>}
或\footrulecolor{<colour>}
来单独更改它们。以上补丁是 的标准etoolbox
,因此请参阅etoolbox
文档工作原理\patchcmd
。以下是完整的 MWE:
\documentclass{article}
\usepackage{etoolbox,fancyhdr,xcolor}% http://ctan.org/pkg/{etoolbox,fancyhdr,xcolor}
% \patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
\newcommand{\headrulecolor}[1]{\patchcmd{\headrule}{\hrule}{\color{#1}\hrule}{}{}}
\newcommand{\footrulecolor}[1]{\patchcmd{\footrule}{\hrule}{\color{#1}\hrule}{}{}}
\pagestyle{fancy}
\fancyhf{}% Clear header/footer
\fancyhead[C]{Header}
\fancyfoot[C]{Footer}% \fancyfoot[R]{\thepage}
\renewcommand{\headrulewidth}{0.4pt}% Default \headrulewidth is 0.4pt
\renewcommand{\footrulewidth}{0.4pt}% Default \footrulewidth is 0pt
\headrulecolor{red!70}% Set header rule colour to 70% red.
\begin{document}
Here is some text.
\end{document}
答案2
这是另一个选项,使用titleps
包裹:
\documentclass{article}
\usepackage{titleps}
\newpagestyle{ruled}
{\sethead{}{Header}{}\headrule
\setfoot{}{Footer}{}\footrule}
\pagestyle{ruled}
\begin{document}
Here is some text.
\end{document}
重新定义\makeheadrule
和\makefootrule
,您可以更改属性;例如,更改颜色:
\documentclass{article}
\usepackage{xcolor}
\usepackage{titleps}
\newpagestyle{ruled}
{\sethead{}{Header}{}\headrule
\setfoot{}{Footer}{}\footrule}
\pagestyle{ruled}
\renewcommand\makeheadrule{\color{cyan}\rule[-.3\baselineskip]{\linewidth}{0.4pt}}
\renewcommand\makefootrule{\color{cyan}\rule[\baselineskip]{\linewidth}{0.4pt}}
\begin{document}
Here is some text.
\end{document}
当然,您可以随时切换到其他页面风格:
\documentclass{article}
\usepackage{titleps}
\newpagestyle{ruled}
{\sethead{}{Header}{}\headrule
\setfoot{}{Footer}{}\footrule}
\pagestyle{ruled}
\begin{document}
Here is some text.\newpage
Here is some text.\newpage
\pagestyle{plain}
text
\end{document}