6

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?

1
  • Welcome to TeX.SE. By default, \subsection does not set the marks for the book class, but it is unclear which document class your using. Please provide a compilable document, not just fragments. Here's a possible workaround: tex.stackexchange.com/questions/356565/…
    – user31729
    Commented Mar 13, 2017 at 22:27

1 Answer 1

4

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.

2
  • @GustavoMezzetti I added the information, but it's not really relevant, as the OP is doing \pagestyle{fancy}. As a linguistic remark, mustn't is strong enough to not require more emphasis.
    – egreg
    Commented Mar 13, 2017 at 23:34
  • I tried this approach. I replaced "\blinddocument" with some sections and subsections. I could not suceed in getting subsections to appear in hearder . Commented May 2, 2020 at 15:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .