# Asymmetric treatment of \mark commands on left and right pages

I work on a dictionary like index with longtable. Using /mark for setting headers on facing pages in a book manually I observe a strange asymmetry, which was explained in a blog as

You should note that a left-hand heading is generated by the last \markboth command before the end of the page, while a right-hand heading is generated by the first \markboth or \markright that comes on the page if there is one, otherwise by the last one before the page.

I tried to see if this is indeed the source of my issue with the following MVE:

%\documentclass{article}
\documentclass{scrbook}
\usepackage[german]{babel}
\usepackage{booktabs,array,microtype, longtable}
\usepackage{alltt}  % to use input with commands
\usepackage{fontspec}
\newfontfamily{\tablefont}{ebgaramond}
\usepackage{lmodern} % to avoid scalable font error

\begin{document}
some text 0

\markboth{firstLeft}{firstRight}  % on first page right (first seen on page)
some text 1

\markboth{secondLeft}{secondRight} % no effect

\clearpage
some text 2

\markboth{thirdLeft}{thirdRight} % on second right  (first seen on page)
% with scrbook: fourth left (last seen on page)

some text3

\markboth{fourthLeft}{fourthRight}
some text4

\clearpage
some text 5         % with header fourthRight )  -- last one seen

\end{document}


The output changes when setting the document class to a book style with facing pages: the header is not the first, but the last header marked on the page. This is incorrect for an index: the header on each page (left or right) should be the first entry (the first \markboth) value.

Is there a way to correct this?

If you are using KOMA-Script, as in your MWE, you can just use \leftfirstmark instead of \leftmark in your header:

\documentclass{scrbook}

\begin{document}

some text 0

\markboth{firstLeft}{firstRight}
some text 1

\markboth{secondLeft}{secondRight}

\clearpage
some text 2

\markboth{thirdLeft}{thirdRight}

some text 3

\markboth{fourthLeft}{fourthRight}
some text 4

\clearpage
some text 5

\end{document}


If you are not using KOMA-Script, you can still load scrlayer-scrpage and use the solution above. By default, the running head is printed on the outer side of the page here, so you will have to redefine \rehead instead of lehead.

\documentclass{book}



If you do not want to use scrlayer-scrpage, you can patch \leftmark to use \firstmark instead of \botmark.

\usepackage{etoolbox}
\patchcmd\leftmark{\botmark}{\firstmark}{}{}


If \leftmark has not been changed by the document class or a package you loaded, this is equivalent to

\makeatletter
\renewcommand*\leftmark{\expandafter\@leftmark\firstmark\@empty\@empty}
\makeatother