0

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.

enter image description here

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}
Manj
  • 315
  • 1
    First and last are more meaningful concepts than "most of the page". You may need to swap back and forth between normal and revised \sectionmark and \subsectionmakr. A full MWE would be useful. – John Kormylo Mar 28 '20 at 20:37
  • @JohnKormylo How much more code should I add? – Manj Mar 29 '20 at 20:27
  • 1
    Your example should be able to be compiled so that the output exhibits the problam you describe, so it should begin with \documentclass and end with \end{document}. You can use lipsum to fill in dummy text. including enough \subsection entries 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:00
  • @barbarabeeton Done – Manj Mar 30 '20 at 13:40
  • 1
    Not quite. To compile this successfully, it's also necessary to know the document class and any necessary packages. It looks like book with a header-formatting package, but it's much better not to have to guess. – barbara beeton Mar 30 '20 at 13:49
  • The document class is article. – Manj Mar 30 '20 at 14:16
  • I'm not a user of fancyhdr so I am interpreting what it says in the user manual. I think that replacing \rightmark by \toprightmark should have the desired result. The distinction between \topmark (the desired condition) and \firstmark is 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
  • That didn't help. Also, I have another query. My current code (in particular, \renewcommand{\sectionmark}[1]{\xdef\leftmark{\thesection\quad#1}\xdef\rightmark{}} \renewcommand{\subsectionmark}[1]{\xdef\rightmark{\thesubsection\quad#1}}) cause an error if I use \mathcal in the subsection titles. Do you know how I can fix this? – Manj Mar 30 '20 at 16:23

0 Answers0