[Tex/LaTex] Why is rightmark not appearing in header

fancyhdrheader-footer

I am a complete newbie with LaTeX and I couldn't find a solution to the following problem: I want the section to appear on the left hand side of the header, the subsection in the middle and the page to the left. I wanted to use the following:

\usepackage{fancyhdr}  
\usepackage[a4paper,left=2.5cm,right=2.5cm,top=3.0cm,bottom=1.5cm,headsep=1cm{geometry}  
\pagestyle{fancy}  
\fancyhf{}  
\fancyhead[R]{\thepage}  
\fancyhead[C]{\nouppercase{\rightmark}}  
\fancyhead[L]{\nouppercase{\leftmark}}  
\fancyheadoffset[R,L]{1cm}  
\renewcommand{\headrulewidth}{0.5pt}  

It works perfectly for the section and the page but the subsection doesn't appear at all. I managed to redefine \rightmark via \renewcommand{\rightmark}{\thesubsection} so that at least the number of the subsection appears but how do I get the title of the subsection as well into the center of the header?

Best Answer

The macro \rightmark is automatically update by LaTeX when it processes a \markright instruction, so you mustn't redefine it yourself.

By default, with the article class, \section issues \markboth, which sets \leftmark (and \rightmark, but usually sets this to empty) via the macro \sectionmark that's automatically called by \section.

You have to redefine \subsectionmark that, by default, does nothing to issue \markright:

\renewcommand{\subsectionmark}[1]{\markright{\thesubsection\ #1}}

Full example:

\documentclass{article}
\usepackage[
  a4paper,
  left=2.5cm,
  right=2.5cm,
  top=3.0cm,
  bottom=1.5cm,
  headsep=1cm
]{geometry}
\usepackage{fancyhdr}

\usepackage{blindtext}

\pagestyle{fancy}
\fancyhf{}
\fancyhead[R]{\thepage}
\fancyhead[C]{\nouppercase{\rightmark}}
\fancyhead[L]{\nouppercase{\leftmark}}
\fancyheadoffset[R,L]{1cm}
\renewcommand{\headrulewidth}{0.5pt}

\renewcommand{\subsectionmark}[1]{\markright{\thesubsection\ #1}}

\begin{document}

\title{Title}
\author{Author}
\maketitle

\blinddocument

\end{document}

enter image description here

As Gustavo Mezzetti remarks, the macro \subsectionmark does execute \markright, provided twoside is in force and the page style is set to headings or myheadings. However, this is not relevant for the task at hand, since the pagestyle is declared to be fancy.