I'm writing a book and want my separate odd and even headers, with centered chapter/section titles on even/odd pages respectively. On both pages I want to print the section number on the outside of the page.
I thought this would be easy to do with, for example,
\fancyhead[LE]{\thesection}
\fancyhead[RO]{\thesection}
Unfortunately, this fails when a new section heading starts at the top of a page, in which case the previous page gets labelled with the new section number. I know this happens because \thesection is expanded too late, but I want an easy fix. Also, I can't use the \rightmark to store the section number because I'm using that to store the section name.
Here's a minimum working example that illustrates the problem:
\documentclass{article}
\usepackage{lipsum}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead{}
\fancyhead[L]{\leftmark}
\fancyhead[R]{\thesection}
\renewcommand{\sectionmark}[1]{\markboth{\thesection. #1}{}}
\begin{document}
\section{Introduction}
\lipsum[1-4]
\lipsum[2]
\section{Blah}
\lipsum[1]
\end{document}
article
. Basically you need to use\leftmark
or\rightmark
(I can't tell which because of the uncertainty about chapters) and just discard everything after the space if you only want the number and not the title.