Can I write a command so LaTeX chooses the subsection which covers most of the page to be in the header? For example, see the image below.
How can I make it so that the header displays Section 2.2 rather than Section 2.3? I am currently using this code.
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{ragged2e}
\usepackage{lmodern}
\renewcommand*\familydefault{\sfdefault}
\usepackage{lipsum}
\usepackage{fancyhdr}
\begin{document}
\pagestyle{fancy}
\fancyhf{}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0.5pt}
\lhead{\textit{\leftmark}}
\rhead{\textit{\rightmark}}
\rfoot{\textit{Page \thepage}}
\renewcommand{\sectionmark}[1]{\xdef\leftmark{\thesection\quad#1}\xdef\rightmark{}}
\renewcommand{\subsectionmark}[1]{\xdef\rightmark{\thesubsection\quad#1}}
\section{Section 1}
\subsection{Subsection 1.1}
\lipsum[1]
\lipsum[2]
\lipsum[3]
\lipsum[4]
\subsection{Subsection 1.2}
\lipsum[1]
\end{document}

\documentclassand end with\end{document}. You can uselipsumto fill in dummy text. including enough\subsectionentries to make the transition clear. You will need to generate two pages, and maybe three, depending on whether the headers on left and right pages are different. Or maybe this can help: Change /rightmark to show last header defined – barbara beeton Mar 29 '20 at 21:00bookwith a header-formatting package, but it's much better not to have to guess. – barbara beeton Mar 30 '20 at 13:49article. – Manj Mar 30 '20 at 14:16fancyhdrso I am interpreting what it says in the user manual. I think that replacing\rightmarkby\toprightmarkshould have the desired result. The distinction between\topmark(the desired condition) and\firstmarkis what is important, but the documentation is a bit sparse on specifics (i.e., no prose description, only code). – barbara beeton Mar 30 '20 at 16:16\renewcommand{\sectionmark}[1]{\xdef\leftmark{\thesection\quad#1}\xdef\rightmark{}} \renewcommand{\subsectionmark}[1]{\xdef\rightmark{\thesubsection\quad#1}}) cause an error if I use\mathcalin the subsection titles. Do you know how I can fix this? – Manj Mar 30 '20 at 16:23