# Change /rightmark to show last header defined

How can I change the default behaviour of \rightmark to display the last defined section before the current page. The default behaviour appears to be the last defined section before the current page IF there are no new sections on the current page.

For example, the following produces a sample document with the default behaviour of \rightmark.

\documentclass[a4paper,oneside]{book}
\usepackage{lipsum}
\usepackage{fancyhdr}

\pagestyle{fancy}
\fancyhf{}

\begin{document}
\chapter{Lorem Ipsum :)}
\section{Paragraphs 1 - 5}
\lipsum[1-5]
\section{Paragraphs 6 - 10}
\lipsum[1-5]
\section{Paragraphs 11 - 15}
\lipsum[1-5]
\end{document}


It looks a bit like this

I would like to make it look a bit more like this. Notice the header now references the last defined section

• latex uses \firstmark and \botmark but you want \topmark – David Carlisle Sep 1 '18 at 15:58
• @DavidCarlisle Thank you for the comment. I managed to get it working with that information. Would you agree with the approach I took in the answer to achieve the result or is there a better way? – Dan Sep 1 '18 at 17:23
• @DavidCarlisle Thanks again for the advice, but do you know why \topmark might be prefixed by CONTENTS? I posted a new question about it here. I appreciate any help you can give – Dan Sep 1 '18 at 19:57

Credits to David Carlisle for pointing me to \topmark

As mentioned before, this can be achieved by using \topmark. However, \topmark returns the chapter as part of the value. So this can be got around by "silencing" the \chaptermark.

In my case, I used

%Header / Footer info
\pagestyle{fancy}

%Change commands so when topmark is used, the last defined section before the current page is used
\renewcommand{\chaptermark}[1]{\markright{#1}}
%\renewcommand{\sectionmark}[1]{\markright{#1}} %Uncomment this line if you don't like numbers