1

In a two-sided article, I wish to show the current subsection name on the odd pages, and the current section name on even pages. Therefore I wrote

\fancyhead[RO]{\rightmark}
\fancyhead[LE]{\leftmark}
\renewcommand*{\sectionmark}[1]{\markboth{\uppercase{#1}}{}}
\renewcommand*{\subsectionmark}[1]{\markright{\thesubsection~~#1}}

However, if \section and \subsection is on the same page, then the right mark disappear. I searched on this site and found the reason (in this answer):

\rightmark always use the first mark, so if you have to sections or subsections on a page, you will get the first.

Is there anyway to change this behavior, making the right mark always shows up?

Below is a MWE, as you can see, the right mark on the first page disappear.

\documentclass[twoside]{article}

\usepackage{fancyhdr} \pagestyle{fancy} \fancyhf{} \fancyhead[RO]{\rightmark} \fancyhead[LE]{\leftmark} \renewcommand{\sectionmark}[1]{\markboth{\uppercase{#1}}{}} \renewcommand{\subsectionmark}[1]{\markright{\thesubsection~~#1}}

\usepackage{blindtext} \begin{document} \blindtext \section{Section One} \subsection{Subsection One} \blindtext[10] \section{Section Two} \blindtext[3] \subsection{Subsection Two} \blindtext[10] \end{document}

Jinwen
  • 8,518

1 Answers1

0

This is an often recurring problem, because the standard LaTeX marks interact with each other. Therefore the sectionmark hides the subsectionmark. I have written a new package extramarks2 to solve this. The package is not yet on CTAN, because I still have to write the documentation, and integrate that into the fancyhdr documentation. But you can download a beta version from Github: get the files README-extramarks2, extramarks2.sty and extramarks2-multicol.tex. With this package, the solution is

\documentclass[twoside]{article}

\usepackage{fancyhdr} \usepackage{extramarks2} \pagestyle{fancy} \fancyhf{} \fancyhead[RO]{\firstrightxmark} \fancyhead[LE]{\firstleftxmark} \fancyfoot[c]{\thepage} \renewcommand{\sectionmark}[1]{\extramarksleft{\uppercase{#1}}} \renewcommand{\subsectionmark}[1]{\extramarksright{\thesubsection~~#1}}

\usepackage{blindtext} \begin{document} \blindtext \section{Section One} \subsection{Subsection One} \blindtext[10] \section{Section Two} \blindtext[3] \subsection{Subsection Two} \blindtext[10] \end{document}

enter image description here