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[margin=1in, headheight=15pt]{geometry}
\usepackage{blindtext}
% begin header configuration
\pagestyle{fancy}
% Start fancyhdr settings
\fancyhf{}
\fancyhead[L]{\leftmark} % section
\fancyhead[R]{\rightmark} % subsection
\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}
. . .
\fancyhead[L]{\firstleftmark} % section
\fancyhead[R]{\firstrightmark} % subsection
(\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.
\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.