[Tex/LaTex] How to add horizontal line after header and before footer

header-footerrules

How can I add horizontal line after header and before footer?

header
---------

---------
footer

Best Answer

Your easiest approach here would be to include the fancyhdr package and set the header/footer rule widths using \renewcommand:

enter image description here

\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:

enter image description here

\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}