How to add horizontal line after header and before footer?
Your easiest approach here would be to include the fancyhdr
package and set the header/footer rule widths using \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}
Headers and footers are simultaneously cleared using \fancyhf{}
, and individually set using \fancyhead[<pos>]{<stuff>}
and \fancyfoot[<pos>]{<stuff>}
. See the fancyhdr
documentation.
Modifying the colour of the header/footer rules is a bit more tricky, since fancyhdr
doesn't supply this modification by default. etoolbox
can be used to update the two rule macros \headrule
and \footrule
to insert the colour as needed. They are defined as follows:
\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}}
It is clear that the horizontal rule is drawn using \hrule
, so we can patch these commands and insert \color{<colour>}
just before drawing it by means of the following convenient helper macros:
\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}{}{}}
This allows you to use \headrulecolor{<colour>}
or \footrulecolor{<colour>}
in order to change them individually. The above patch is standard for etoolbox
, so see the etoolbox
documentation on how \patchcmd
works. Here's a complete 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}
Here's another option using the titleps
package:
\documentclass{article}
\usepackage{titleps}
\newpagestyle{ruled}
{\sethead{}{Header}{}\headrule
\setfoot{}{Footer}{}\footrule}
\pagestyle{ruled}
\begin{document}
Here is some text.
\end{document}
Redefining \makeheadrule
and \makefootrule
, you can change the attributes; for example, to change the color:
\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}
Of course, you can switch to other page style at any moment:
\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}