# How to force fancyhdr to mark the subsection on a page?

I am trying to use fancyhdr to create headings on some notes I am writing (article class, one sided). I would like to have the section name and number on the left and the subsection name and number on the right. My issue is that the subsection header does not appear on certain pages. Here is my code:

\usepackage[utf8]{inputenc}
\usepackage{amsthm, amsmath, amssymb, amsfonts, enumitem, mathtools, etoolbox, mathrsfs, fancyhdr}
\usepackage[margin=1in]{geometry}

\pagestyle{fancy}
\fancyfoot{}
\fancyfoot[C]{\thepage}


And here is the output. The first two images show how I would like every page to look:

These two show the problem I am having:

As you can see, in the last two images the subsection does not appear in the header even though a new subsection starts on those pages. Is there a way around this?

EDIT: Here is a working example of the issue:

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsthm, amsmath, amssymb, amsfonts, enumitem, mathtools, etoolbox, mathrsfs, fancyhdr, extramarks}
\usepackage[margin=1in]{geometry}
\usepackage{blindtext}

\pagestyle{fancy}
\fancyfoot{}
\fancyfoot[C]{\thepage}

\renewcommand{\subsectionmark}[1]{\markright{\thesubsection.\ #1}}

\title{test}
\date{September 2019}

\begin{document}
\section{Intro}
\subsection{Subsection 1}

\blindtext[3]
\pagebreak

\subsection{Subsection 2}

\blindtext[3]
\pagebreak

\section{Another section}
\subsection{Sub 3}
\blindtext[1]

\subsection{Sub 4}
\blindtext[1]
\end{document}


When compiled the first and third pages are missing the subsection header. It seems this only occurs when a new section is started first thing on the page.

• Welcome to TeX.SX! Please add a small but complete, compilable document to your question that can be used to reproduce the problem. Sep 30, 2019 at 3:09
• \rightmark shows the first right mark on the page, and the right mark on the problematic pages is filled by the \section command and so it is empty. You could try the extramarks package and use \lastrightmark instead. Sep 30, 2019 at 8:15

Unfortunately, the solution presented by @ejmastnak doesn't always work correctly. I have a couple of examples where it fails. The problem is that TeX's page breaking algorithm can process more text than fits on the page, and then it can later decide to put that text on the next page. Therefore setting 'variables' to communicate information to the headers can cause these variables to be set on the wrong page. In fact, this is exactly the reason that the 'marks' mechanism was developed. Only in some restricted cases with 'variables' work correctly.

Here is an example were that solution fails. I left out the commented version 2 code, and I added some \message statments in the \[sub]sectionmark commands to show what happens.

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{fancyhdr}
\usepackage{blindtext}

% begin header configuration
\pagestyle{fancy}

% Start fancyhdr settings
\fancyhf{}
\fancyfoot[C]{\thepage}
% End fancyhdr settings

% Redefine sectionmark to produce desired behavior --- VERSION 1
% This version sets the FIRST subsection on a page as the right mark,
%   even if there are multiple subsections on a page
\renewcommand{\sectionmark}[1]{%
\markboth{\thesection. \ #1}
{\noexpand\firstsubsectiontitle}%
\global\firstsubsectionmarktrue
\message{section #1 on page \thepage}} % first subsection mark set
\renewcommand{\subsectionmark}[1]{% ensures the first subsection is displayed even if multiple subsections occur on a given page
\markright{\thesubsection. \, #1}%
\iffirstsubsectionmark
\edef\firstsubsectiontitle{\thesubsection. \, #1}%
\fi
\message{subsection #1 on page \thepage}%
\global\firstsubsectionmarkfalse
}
\newif\iffirstsubsectionmark
\def\firstsubsectiontitle{}
% End VERSION 1

% End header configuration

\newcommand{\sometext}{Lots of text. Lots of text. Lots of text. Lots of text.
Lots of text. Lots of text.}
\newcommand{\lotsoftext}{\sometext \sometext \sometext
\sometext \sometext \sometext \par}

\begin{document}

\section{First section}

\subsection{First subsection}

\lotsoftext \lotsoftext \lotsoftext \lotsoftext \lotsoftext \lotsoftext
\lotsoftext \lotsoftext

\section{Second section}

\subsection{Second subsection}

\begin{figure}[h]
\centering
\fbox{\rule{0pt}{420pt}\hspace{10cm}}
\caption{A figure}
\end{figure}

\section{Third section}

\subsection{Third subsection}

\blindtext[3]

\end{document}


As you can see, page 2 gets the header of the first section of page 3. This is because both \section{Third section} and \subsection{Third subsection} are processed at page 2 (as can be seen in the messages in the log file), but then pushed to page 3 because they don't fit on page 2.

Now this is very difficult to solve with the current LaTeX. It is possible, but that would require extensive modifications to the LaTeX kernel. I have no idea when (or if) that is going to happen, but I hope it will.

There is one case where a solution with marks is possible, namely if every \section command is immediately followed by a \subsection command. Them LaTeX will never break the page between these two, so they always will stay together on the same page. Then you need only one 'mark' command for the two, which solves the problem.

What we do then is put the section title in a variable, and use that in the \subsectionmark to put it in the left mark.

\newcommand\sectiontitle{}

\renewcommand{\sectionmark}[1]{%
\renewcommand\sectiontitle{\thesection. \ #1}}
\renewcommand{\subsectionmark}[1]{%
\markboth{\sectiontitle}{\thesubsection. \, #1}%
}


But if there is more than one section on the page, this gives the last section title instead of the first one (which is also the case in @ejmastnak's solution). We can get the first leftmark with the \firstleftmark command from the extramarks package.

\usepackage{extramarks}
. . .


(\firstrightmark is a synonym for \rightmark)

There is only one case in which @ejmastnak's solution works reliably: when every \section command starts at a new page, and when it is immediately followed by a \subsection command (or at least very close to it). But in these cases my solution also works, and that works in more cases.

• I have this exact problem except i use subfiles, eg a main.tex which has \sections and then calls a file that has subsection and subsubsection. Any idea of a work around? every section is followed by a subsection which is followed by a subsub section. is \newpage this easiest workaounrd? Aug 27, 2021 at 8:30

Edit: Thanks to @Pieter van Oostrum for pointing out that this solution fails for non-trivial section/subsection arrangement. See his answer for a more robust solution.

I know that this is an old question, but seeing that it is still unanswered, I thought I would provide my solution for whomever might stumble upon this from a Google search with the same problem. The solution is based on egreg's excellent answer in Header not displaying correctly on chapter page due to subsection, which answers a similar problem, but for the report document class instead of the article class.

The code below produces the desired output, i.e. the subsection name displaying in the header even on section pages, for an article document class as in the OP's question. I have included two versions---the more robust first version displays the first subsection on a given page, even when there are multiple subsections on a single page (see second picture), while the second version displays the last subsection, which might be undesired for pages with multiple subsections. egreg's answer in Header not displaying correctly on chapter page due to subsection already explains the new commands, so I have not re-written the same explanation here.

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{fancyhdr}
\usepackage[margin=1in]{geometry}
\usepackage{blindtext}

% begin header configuration
\pagestyle{fancy}

% Start fancyhdr settings
\fancyhf{}
\fancyfoot[C]{\thepage}
% End fancyhdr settings

% Redefine sectionmark to produce desired behavior --- VERSION 1
% This version sets the FIRST subsection on a page as the right mark,
%   even if there are multiple subsections on a page
\renewcommand{\sectionmark}[1]{%
\markboth{\thesection. \ #1}
{\noexpand\firstsubsectiontitle}%
\global\firstsubsectionmarktrue} % first subsection mark set
\renewcommand{\subsectionmark}[1]{% ensures the first subsection is displayed even if multiple subsections occur on a given page
\markright{\thesubsection. \, #1}%
\iffirstsubsectionmark
\edef\firstsubsectiontitle{\thesubsection. \, #1}%
\fi
\global\firstsubsectionmarkfalse
}
\newif\iffirstsubsectionmark
\def\firstsubsectiontitle{}
% End VERSION 1

% Redefine sectionmark to produce desired behavior --- VERSION 2
% This version sets the LAST subsection on a page as the right mark
% If there is only one subsection on a page, it gives the same result as the more complicated version above
% Intentionally commented out because this is less robust, but feel free to experiment

% \renewcommand{\sectionmark}[1]{\markboth{\thesection. \ #1}
%   {\noexpand\firstsubsectiontitle}}
% \renewcommand{\subsectionmark}[1]{%
%   \markright{\thesubsection\ \, #1}\gdef\firstsubsectiontitle{#1}}
% \newcommand\firstsubsectiontitle{}

% End VERSION 2

% End header configuration

\begin{document}

\section{First section}
\subsection{Subsection on a section page}

\blindtext[3]
\pagebreak

\subsection{Second subsection}

\blindtext[3]
\pagebreak

\section{Another section}
\subsection{First subsection on a section page}
\blindtext[1]

\subsection{Second subsection on a section subsection}
\blindtext[1]

\end{document}


• Unfortunately, this solution doesn't always work correctly. See my answer for details. Feb 26, 2021 at 16:43