[Tex/LaTex] Add a headrule to the page style in a document class

header-footerrules

I'm writing a thesis using the thesis.cls document class supplied by my own university. The university's own guidelines state that headers may be used in the thesis ''as long as there is a line across the entire page to separate the header from the text''. But using \pagestyle{headings} (a page style defined in the university's thesis class) does not make a horizontal rule.

Below is the relevant snippet that defines the headings page style.

\if@twoside 
 \def\ps@headings{
    \def\@oddfoot{}\def\@evenfoot{}                      % No feet.
    \def\@evenhead{\rmfamily \thepage\hfil \slshape \leftmark}            % Heading on left for even pages.        .
    \def\@oddhead{{\slshape \rightmark}\hfil \rmfamily\thepage}           % Heading on right for odd pages. 
    \def\chaptermark##1{\markboth {\uppercase{\ifnum \c@secnumdepth >\m@ne
      \@chapapp\ \thechapter. \ \fi ##1}}{}}%
    \def\sectionmark##1{\markright {\uppercase{\ifnum \c@secnumdepth >\z@
    \thesection. \ \fi ##1}}}
   }
\else
   \def\ps@headings{
    \def\@oddfoot{}\def\@evenfoot{}                      % No feet.
    \def\@oddhead{{\slshape \rightmark} \hfil \rmfamily\thepage}          % Heading.
    \def\chaptermark##1{\markright {\uppercase{\ifnum \c@secnumdepth >\m@ne
    \@chapapp\ \thechapter. \ \fi ##1}}}
   }
\fi

How do I add a horizontal line under the headings?

Best Answer

You can update \@evenhead and \@oddhead after having set the your \pagestyle{headings}. That is, somewhere in your preamble, you should have the following:

\makeatletter
\newcommand{\insertheaderrule}{\rlap{\rule[-.3\normalbaselineskip]{\textwidth}{.4pt}}}
\let\old@evenhead\@evenhead \let\old@oddhead\@oddhead
\def\@evenhead{\insertheaderrule\old@evenhead}% Prepend \insertheaderrule
\def\@oddhead{\insertheaderrule\old@oddhead}% Prepend \insertheaderrule
\makeatother

Above we create \insertheaderrule and prepend it to \@evenhead and \@oddhead. It sets a rule of length \textwidth, width .4pt and dropped 30% below the normal baseline skip. We ensure it doesn't take up space horizontally using \rlap.