10

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

header
---------

---------
footer
  • I wonder what the solution for Plain TeX is. – Palec Apr 4 '15 at 9:51
17

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

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}

enter image description here

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}

enter image description here

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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.