# Separate section number in headers

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}


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}

\renewcommand{\sectionmark}[1]{\markboth{\thesection. #1}{}}

\begin{document}
\section{Introduction}
\lipsum[1-4]
\lipsum[2]

\section{Blah}
\lipsum[1]
\end{document}


• It isn't clear what headings you want, you mention chapters but your example uses 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. – David Carlisle Jun 27 '13 at 20:54

The following extracts the section number from \leftmark that you are using for the full section title. The section number is identified as anything at the beginning until .␣ is seen. Otherwise nothing is set.

\documentclass{article}
\usepackage{lipsum}

\usepackage{fancyhdr}
\pagestyle{fancy}

\renewcommand{\sectionmark}[1]{\markboth{\thesection. #1}{}}

\makeatletter
\newcommand*{\leftmarksection}{%
\begingroup
\protected@edef\temp{\leftmark}%
\expandafter\@leftmarksection\temp. \@nil
\endgroup
}
\def\@leftmarksection#1. #2\@nil{%
\def\temp{#2}%
\ifx\temp\@empty
\else
#1%
\fi
}
\makeatother

\begin{document}
Title page
\newpage
\section{Introduction}
\lipsum[1-4]
\lipsum[2]

\section{Blah}
\lipsum[1]
\end{document}

• I accepted this answer because it really works the best. Both David's and my own answer run into problems if the section name is at all fancy. – Pat Morin Jul 2 '13 at 12:32

Perhaps you want this, I'm not sure which mark you need in your real case.

\documentclass{article}
\usepackage{lipsum}

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead[R]{\expandafter\numberonly\romannumeral\^^@\leftmark!!}
\def\numberonly#1. #2!!{#1}

\renewcommand{\sectionmark}[1]{\markboth{\thesection. #1}{}}

\begin{document}
\section{Introduction}
\lipsum[1-4]
\lipsum[2]

\section{Blah}
\lipsum[1]
\end{document}

• This, and @Heiko Oberdiek's answer suggest that I extract the section number from \leftmark. Could one of you show me how to extract just the section title from \leftmark as well. I only included the section number in \leftmark in my example to help illustrate the problem. What I'm really after is an even-side header with <section-number> <chapter-title> and an odd-side header with <section-title> <section-number>. – Pat Morin Jun 28 '13 at 1:08
• @PatMorin copy the definition of \numberonly but use #2 instead of #1 as the definition body – David Carlisle Jun 28 '13 at 1:27

Thanks to both David and Heiko for your answers. I wanted a solution that I could understand, so I used the xstring package to implement unpacking the section number and title from the \leftmark:

\documentclass{article}
\usepackage{lipsum}
\usepackage{xstring}

\usepackage{fancyhdr}
\pagestyle{fancy}
`