2

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}

Should have a 1 on the right-hand side

1
  • 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. Commented Jun 27, 2013 at 20:54

3 Answers 3

3

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}
\fancyhead{}
\fancyhead[L]{\leftmark}
\fancyhead[R]{\leftmarksection}

\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}
1
  • 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
    Commented Jul 2, 2013 at 12:32
4

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{}
\fancyhead[L]{\leftmark}
\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}
2
  • 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
    Commented Jun 28, 2013 at 1:08
  • @PatMorin copy the definition of \numberonly but use #2 instead of #1 as the definition body Commented Jun 28, 2013 at 1:27
1

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}
\fancyhead{}
\fancyhead[L]{\StrBehind{\leftmark}{+}}
\fancyhead[R]{\StrBefore{\leftmark}{+}}

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

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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .