1

I would like to use fancyhdr to have even page headers like this (page number on left, part name on right): enter image description here and odd page headers like this (section number $\blacklozenge$ section name on the left and page number on the right.): enter image description here My code looks like this:

\pagenumbering{arabic}
\setcounter{page}{1}
\pagestyle{fancy}
\let\Oldpart\part
\newcommand{\parttitle}{}
\renewcommand{\part}[1]{\Oldpart{#1}\renewcommand{\parttitle}{#1}}
\renewcommand{\sectionmark}[1]{\markright{#1}}
\renewcommand{\headrulewidth}{0.4pt}
\fancyhead[OR,EL]{\thepage}
\fancyhead[LO]{\fancyplain{}{\thesection\ $\blacklozenge$ \textsl{\textsc{\rightmark}}}}
\fancyhead[RE]{\fancyplain{}{\textsl{\textsc{\parttitle}}}}
\fancyfoot[C]{}
\setlength{\headheight}{14.5pt}

The problem that I am having is that occasionally, the section number on the odd page heading does not match the section name next to it. enter image description here As you can see, this is section 2.3 content on the page and the section name is that of 2.3 but the section number has increased to 2.4 when really it should not, in my opinion have increased until page 65 (and even if it was to increase here, the section name in the header should reflect this too.).

Thank you in advance for your help!


My minimal example is:

\documentclass[11pt, letterpaper,twoside,openright]{report}
\usepackage[sc]{mathpazo}
\linespread{1.05}  
\usepackage[T1]{fontenc}
\usepackage{fancyhdr}
\usepackage[margin=1.54in]{geometry}
\usepackage{amssymb}
\usepackage{lipsum}

\pagenumbering{arabic}
\setcounter{page}{1}
\pagestyle{fancy}
\let\Oldpart\part
\newcommand{\parttitle}{}
\renewcommand{\part}[1]{\Oldpart{#1}\renewcommand{\parttitle}{#1}}
\renewcommand{\sectionmark}[1]{\markright{#1}}
\renewcommand{\headrulewidth}{0.4pt}
\fancyhead[OR,EL]{\thepage}
\fancyhead[LO]{\fancyplain{}{\thesection\ $\blacklozenge$ \textsc{\rightmark}}}
\fancyhead[RE]{\fancyplain{}{\textsc{\parttitle}}}
\fancyfoot[C]{}
\setlength{\headheight}{14.5pt}

\begin{document}
    \part{Part One}
    \chapter{Ch01}
    \section{One Point One}
    \lipsum[1-5]
    \section{One Point Two}
    \lipsum[9-11] \vspace{3in} \lipsum[12-13] 
    \section{One Point Three}
    \lipsum[18-20]
\end{document}
  • I see no reason for this, so a minimal example is needed. In the meantime, \fancyplain{}{...} should just be ... and \textsl{\textsc{...}} should just be \textsc{...} (\textsl does nothing at all if the fonts don't have slanted small caps). – egreg Jul 21 '17 at 20:59
  • I will try to recreate this in a minimal example and update my question accordingly. – Jacob House Jul 21 '17 at 21:03
  • @JacobHouse Check if section 2.4 is actually available at the bottom of the page. If this is the case, then this behavior is normal. If not, then you have something wrong in the code or packages conflict. We need a minimal working example that is self-contained and allow us to run the code successfully getting the output you posted in the picture. – Al-Motasem Aldaoudeyeh Jul 21 '17 at 21:08
2

From the code snippet you posted and the outputs you provided, I guess that you have another section beginning close to the page where the incorrect header is shown. This is a common issue in LaTeX. You should not use \thesection in your headers because of this.

This might happen when LaTeX decides to break a page after the next section command took effect but does go on the next page.

To circumvent this problem, you should put \thesection into your \sectionmark definition:

\documentclass[11pt, letterpaper,twoside,openright]{report}
\usepackage[sc]{mathpazo}
\linespread{1.05}  
\usepackage[T1]{fontenc}
\usepackage{fancyhdr}
\usepackage[margin=1.54in]{geometry}
\usepackage{amssymb}
\usepackage{lipsum}


\pagenumbering{arabic}
\setcounter{page}{1}
\pagestyle{fancy}
\let\Oldpart\part
\newcommand{\parttitle}{}
\renewcommand{\part}[1]{\Oldpart{#1}\renewcommand{\parttitle}{#1}}
\renewcommand{\sectionmark}[1]{%
    \markright{\thesection\ $\blacklozenge$ \textsc{#1}}}
\renewcommand{\headrulewidth}{0.4pt}
\fancyhead[OR,EL]{\thepage}
\fancyhead[LO]{\fancyplain{}{\rightmark}}
\fancyhead[RE]{\fancyplain{}{\textsc{\parttitle}}}
\fancyfoot[C]{}
\setlength{\headheight}{14.5pt}

\begin{document}
    \part{Part One}
    \chapter{Ch01}
    \section{One Point One}
    \lipsum[1-5]
    \section{One Point Two}
    \lipsum[9-11] \vspace{3in} \lipsum[12-13] 
    \section{One Point Three}
    \lipsum[18-20]
\end{document}
  • Thank you. You were right, the first thing on the next page was the beginning of section 2.4. Your solution seems to have worked perfectly. – Jacob House Jul 21 '17 at 21:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.