[Tex/LaTex] Including section titles in fancyhf headers

fancyhdrheader-footer

I would like to include the section name of the page's displayed section in my header tags in the fancyhdr package, something like

\fancyhf[HR]{SECTION NAME}
\fancyhf[HL]{SUBSECTION NAME}

How can I go about doing this?

Best Answer

titleps provides this out-of-the-box and is not that different in interface/usage from fancyhdr. You might be interested in reading titleps for fancyhdr users.

If you wish to do this straight with fancyhdr, you'll have to patch the sectional unit constructions to store the titles, or use the title marks with some massaging.

You would use

\usepackage{titleps}% http://ctan.org/pkg/titleps
\newpagestyle{main}{
\setheadrule{.4pt}% Header rule
\sethead{\subsectiontitle}% left
        {}%                 center
        {\sectiontitle}%    right
}
\pagestyle{main}

Here's a very minimal example with the addition of the sectional unit number as well:

enter image description here

\documentclass{article}
\usepackage{titleps}% http://ctan.org/pkg/titleps
\newpagestyle{main}{
\setheadrule{.4pt}% Header rule
\sethead{\thesubsection\ \subsectiontitle}% left
        {}%                                 center
        {\thesection\ \sectiontitle}%       right
}
\pagestyle{main}
\begin{document}
\section{A section}
\subsection{A subsection}
\subsubsection{A subsubsection}
\end{document}