# Using \thesubsection in header references last instance - not first

I would like to include \thesubsection in my header, referring to the first instance of subsection, and not the last as seems to be the default.

The following MWE says it all:

\documentclass[a5paper]{article}

\usepackage{fancyhdr}
\usepackage{lipsum}
\usepackage[a5paper]{geometry}

\pagestyle{fancy}

\begin{document}

\section{Section 1}
\subsection{Subsection 1}
\lipsum[1]
\section{Section 2}
\subsection{Subsection 1}
\lipsum[2]

\pagebreak

\subsection{Subsection 2}
\lipsum[3]
\subsection{Subsection 3}
\lipsum[4]

\section{Section 3}
\subsection{Subsection 1}
\lipsum[5]
\subsection{Subsection 2}
\lipsum[6]
\subsection{Subsection 3}
\lipsum[7]

\end{document}


Results:

Page      What I want    What I get
1         1.1            2.1
2         2.2            3.1
3         3.2            3.3

-

You should never refer to counters directly in the page head. You are not getting the last value on the page, you are getting the value at the point in the document that TeX decided to try breaking up pages. This could be any number of pages ahead of the final chosen page break.

The \mark system is designed to reliably get values that are correct for a given page. Unfortunately LaTex's standard usage of marks in its section headings is designed mainly for two sided use with section values going on the left side of a spread and subsection on the right.

\lhead{\rightmark}


Almost does what you want, but the left part of the heading is blank on the first page as this uses \firstmark internally which is the value carried over from the previous page. You could initialise things as

\lhead{\rightmark}\markright{1.1 Subsection 1}


which gets the result you want.

You could use \topmark (which LaTeX doesn't normally use) which might be better generally if you want to pick up the first section on a page even if the top text on a page is in the previous section. However doing a full job would take more extensive changes as the mark for \section mask those from \subsection at the top of the page, as the intention was that only one is used, or they affect the headings on opposite sides of the spread in twosided mode.

-
This works, but not as well as I'd like: 1) The first page doesn't get anything, so I have to "hard-code" the first page mark. I might add unmarked pages later, so I don't want to do that. 2) I don't want the title of the subsection to be in the header - just the number. 3) In my actual document, I have redefined \thesection and \thesubsection to enumerate the greek alphabet. I don't know how to alter the output of the mark commands to do that. 4) \topmark is just downright ugly - it gives me "2 SECTION 22.1 Subsection 1" on second page... – Tomas Lycken Jun 30 '12 at 18:21
I've done some more work on this, and I'd like whatever solution we come up with to work equally well with \arabic{section} and the like, if possible. Would that make it easier than using \rightmark? – Tomas Lycken Jul 2 '12 at 13:32
\topmark is a tex primitive you are not supposed to use it directly in latex. It stores 2 (or 4 if you use hyperref) different fields, If you use it directly you get all of them. latex's \leftmark and \rightmark extract just one of the fields. If you have defined \thesection to be greek rather than arabic I would have expected that to "just work" in marks. – David Carlisle Jul 2 '12 at 13:50