3

I have a rather simple problem that I can not solve, I am only trying to get both the section and subsections in the header (using fancyhdr). However, I don't seem to be able to get that done. I would like to get on the left the section or subsection and on the right the page numbering. Currently I am using the following code:

\pagestyle{fancy}

\fancyhead[L]{\rightmark}
\fancyhead[R]{\thepage}
\fancyfoot[L]{}
\fancyfoot[C]{}
\fancyfoot[R]{}

But in the beginning, I am having 3 chapters with no subsection and hence the header only displays the page numbering. I tried different combinations of rightmark and leftmark but I didn't get it the way I want. So, I am trying to get the section in the header unless there is a subsection, then I want the subsection in the header. Any ideas how to get that done?

Thank you Jonas

2

You want to redefine \subsectionmark (that by default does nothing) to issue a mark:

\documentclass[a4paper,oneside]{report}

\usepackage{fancyhdr}

\usepackage{kantlipsum} % for mock text

\pagestyle{fancy}
\fancyhf{} % clear all fields
\fancyhead[L]{\rightmark}
\fancyhead[R]{\thepage}

\renewcommand{\subsectionmark}[1]{%
  \markright{\MakeUppercase{\thesubsection.\ #1}}}%
}

\begin{document}

\chapter{Chapter title}

\kant[1]

\section{Section title}

\kant[2-7]

\subsection{Subsection title A}

\kant[8-10]

\subsection{Subsection title B}

\kant[11-14]

\subsection{Subsection title C}

\kant[15-18]

\end{document}

enter image description here

| improve this answer | |
0

are you familiar with the package scrlayer-scrpage? This one allows you to modify your page layout a bit more in detail. It takes a while to read through the documentation and it is not too easy to find exactly what you are looking for. However, a good Google search in combination with scrlayer-scrpage will help you further.

I created a small example, which always put the "active" element in the Header, which is the {section, subsection, etc} mentioned last. I uncommented some lines (%lehead to %cofoot). You can use those if you want to write a specific term at a certain Position, like your Name for example. This is for documents like books where you have two pages next to each other. First letter can be l (left) c (Center) or r (right) than e (for even numbered page) or o (for odd numbered pages) and then head for the Header and foot for the foot part of the page.

Please check if you can compile it and if it is what you were looking for.

Best regards, Bastien

\documentclass[a4paper,11pt]{scrartcl}


\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage[automark, headsepline, footsepline]{scrlayer-scrpage}

\clearpairofpagestyles
\automark*{section}
\automark*{subsection}
\automark*{subsubsection}
\ihead{\headmark}
\ohead*{\pagemark}

%\lehead{}
%\rehead{}
%\lohead{}
%\rohead{}
%\cefoot{}
%\cofoot{}

\pagestyle{scrheadings} 

\begin{document}
    \section{Section A}
    \blindtext
    \subsection{Subsection a}
    \blindtext
    \blindtext
    \subsection{Subsection b}
    \blindtext
    \blindtext
    \newpage
    \subsection{Subsection c}
    \blindtext
    \section{Section B}
    \blindtext
    \subsection{Subsection c}
    \blindtext
    \blindtext
    \subsection{Subsection d}
    \blindtext
    \newpage
    \section{Section C}
    \blindtext
    \subsection{Subsection e}
    \blindtext
\end{document}
| improve this answer | |

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.