TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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}

\lhead{\thesubsection}

\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
share|improve this question

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.

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.
http://tex.stackexchange.com/questions/61615/using-thesubsection-in-header-references-last-instance-not-first
tex.stackexchange.com
crawl=CC-MAIN-2016-26
76728
application/http; msgtype=response
sha1:X5J3CLVAN54ZOZYIPUR7PHE4GVGXOQXA
<urn:uuid:4140a3bb-1805-4e71-8478-670811356a21>
2016-07-01T18:58:33Z
151.101.193.69
sha1:2OF5DLZUIOKKGQZSRPTYENYVAIDLVYK4
<urn:uuid:32778799-9d35-4217-bb0f-caf4675270ef>
http://tex.stackexchange.com/questions/61615/using-thesubsection-in-header-references-last-instance-not-first
response
<urn:uuid:dc0702f1-b2ce-4313-9a37-a43356d35c22>
robots: classic
hostname: ip-10-164-35-72.ec2.internal
software: Nutch 1.6 (CC)/CC WarcExport 1.0
isPartOf: CC-MAIN-2016-26
operator: CommonCrawl Admin
description: Wide crawl of the web for June 2016
publisher: CommonCrawl
format: WARC File Format 1.0
conformsTo: http://bibnum.bnf.fr/WARC/WARC_ISO_28500_version1_latestdraft.pdf