1

How can I display the section in the header of the page that starts a chapter?

On following pages the chapter and the sections are displayed but not on the page where the chapter starts.

If there is no good reason to not display the section as well, I would like it to be displayed.

Also. The chapter is not displayed on the 3rd page. Instead of the chapter and the section, "List of listings" is displayed.

    \documentclass[
   final,
   12pt,
   a4paper,
   oneside
]{book}

\usepackage[titles]{tocloft}% to alighn the TOC numbers on the right
\usepackage{setspace}
\usepackage{epstopdf}
\usepackage{textcomp}
\usepackage{listings}
\usepackage{etoolbox}

%%# IN NEW BEGIN
\usepackage{lipsum}
\usepackage[headheight=30pt,
            a4paper,
            left=42mm,
            right=29mm,
            top=32mm,
            bottom=38mm
]{geometry}  % or %\setlength{\headheight}{30pt}
%% IN NEW END

\usepackage{fancyhdr}
    %\pagestyle{fancy}
    %\pagestyle{fancyplain}
    %\fancyhead{}
    %\fancyfoot{}
    \fancyhead[L]{}
    \fancyhead[R]{\small\nouppercase\leftmark\\\small\nouppercase\rightmark}
    \fancyfoot[L]{\rule{2cm}{1cm}}
    \fancyfoot[R]{\thepage}
    \fancyfoot[C]{}
    \fancypagestyle{plain}{%
        \fancyhf{} % clear all header and footer fields
        \fancyhead[R]{\small\nouppercase\leftmark\\\small\nouppercase\rightmark}
        \fancyfoot[L]{\rule{2cm}{1cm}}
        \fancyfoot[R]{\thepage}
        \fancyfoot[C]{}
        \renewcommand{\headrule}{{%
        \hrule height 1.9pt width\headwidth
        \vspace{1.6pt}%
        \hrule height 0.9pt width\headwidth
    }}}
    \renewcommand{\headrule}{{%
        \hrule height 1.9pt width\headwidth
        \vspace{1.6pt}%
        \hrule height 0.9pt width\headwidth
    }}

\makeatletter
\patchcmd{\tableofcontents}
  {\@mkboth{\MakeUppercase\contentsname}{\MakeUppercase\contentsname}}
  {\@mkboth{\mbox{}}{\MakeUppercase\contentsname}}
  {}{}
\makeatother

\begin{document}
\renewcommand{\cftchapaftersnum}{.}

%%# IN NEW BEGIN
\pagestyle{fancy}
%%# IN NEW END

\frontmatter
%% OUT NEW BEGIN
%\pagenumbering{Roman}
%% OUT NEW BEGIN

%% OUT NEW BEGIN
%\fancyfoot[RO, LE] {\small\thepage}
%\thispagestyle{empty}
%\fancyfoot[R] {\thepage}
%% OUT NEW END

\setlength{\parindent}{0pt}
\setlength{\parskip}{12pt}

\renewcommand\lstlistingname{Code listing}
\renewcommand\lstlistlistingname{List of Listings}
\rhead{\small{List of Listings}}
\addcontentsline{toc}{chapter}{List of Listings}
\lstlistoflistings

\mainmatter

%% IN NEW BEGIN
\renewcommand{\sectionmark}[1]{\markright{\thesection~#1}}
%% IN NEW END

\pagenumbering{arabic}
\onehalfspacing

%% OUT NEW BEGIN
%\fancyhead{}
%\pagestyle{plain}
%\lhead{}
%\rhead{\small\nouppercase\leftmark\\\small\nouppercase\rightmark}
%\fancyfoot[R] {\thepage}
%% OUT NEW END

\setlength{\parindent}{0pt}
\setlength{\parskip}{12pt}

\chapter{Some Chapter}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

\section{Some section}

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

\section{Some section}

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.


\end{document}
  • You don't need to write those lot of dummy text. Just use lipsum package and write \lipsum[1] for example. – Sigur Jan 24 '15 at 18:18
  • The basic idea for fancyhdr is to do: \fancypagestyle{plain}{<put your definitions here; i.e., make them match your other definitions>}. This redefines the 'plain' style to match your 'fancy' style. In general, however, a page that starts a new chapter doesn't need a "fancy" header. – jon Jan 24 '15 at 18:37
  • It is the norm that a section title is not displayed on the first page of a chapter, since it aims at giving this information when it is not apparent. By definition, if a section begins on first page, it is apparent from the body of text, so it would be a redundant information. – Bernard Jan 24 '15 at 18:44
  • Oh, thanks to @bernard's formatting, I see the problem: \rhead{\small{List of Listings}}. This is manually overriding your earlier definitions. Comment it out. – jon Jan 24 '15 at 19:06
  • I have a one sided layout and would like to have both lines in the header fills with chapter and section all the time, also at the beginning of a chapter. – latenite Jan 24 '15 at 19:18
1

First of all remove the line \rhead{\small{List of Listings}}.

A mark command sets three variables: \topmark (last mark of the previous page), firstmark (first mark on the current page) and \botmark (last mark on the current page). \rightmark uses the \firstmark set by \markright or the second argument of \markboth. On a chapter page the first call of \markboth is done by \chapter, normally with an empty second argument to reset the header entry.

If it is alright to use the last section occurring on the chapter page you could define a command \rightbotmark using \botmark instead \firstmark

\makeatletter
\providecommand*{\rightbotmark}{\expandafter\@rightmark\botmark\@empty\@empty}
\makeatother

and then

\fancyhead[R]{\small\nouppercase\leftmark\\\small\nouppercase\rightbotmark}

at pagestyle plain and maybe also at pagestyle fancy.

enter image description here

Code:

\documentclass[
   final,
   12pt,
   a4paper,
   oneside
]{book}

\usepackage[titles]{tocloft}% to alighn the TOC numbers on the right
\usepackage{setspace}
\usepackage{epstopdf}
\usepackage{textcomp}
\usepackage{listings}
\usepackage{etoolbox}

%%# IN NEW BEGIN
\usepackage{lipsum}
\usepackage[headheight=30pt,
            a4paper,
            left=42mm,
            right=29mm,
            top=32mm,
            bottom=38mm
]{geometry}  % or %\setlength{\headheight}{30pt}
%% IN NEW END

\makeatletter
\providecommand*{\rightbotmark}{\expandafter\@rightmark\botmark\@empty\@empty}
\makeatother

\usepackage{fancyhdr}
    %\pagestyle{fancy}
    %\pagestyle{fancyplain}
    %\fancyhead{}
    %\fancyfoot{}
    \fancyhead[L]{}
    \fancyhead[R]{\small\nouppercase\leftmark\\\small\nouppercase\rightmark}
    \fancyfoot[L]{\rule{2cm}{1cm}}
    \fancyfoot[R]{\thepage}
    \fancyfoot[C]{}
    \fancypagestyle{plain}{%
        \fancyhf{} % clear all header and footer fields
        \fancyhead[R]{\small\nouppercase\leftmark\\\small\nouppercase\rightbotmark}
        \fancyfoot[L]{\rule{2cm}{1cm}}
        \fancyfoot[R]{\thepage}
        \fancyfoot[C]{}
        \renewcommand{\headrule}{{%
        \hrule height 1.9pt width\headwidth
        \vspace{1.6pt}%
        \hrule height 0.9pt width\headwidth
    }}}
    \renewcommand{\headrule}{{%
        \hrule height 1.9pt width\headwidth
        \vspace{1.6pt}%
        \hrule height 0.9pt width\headwidth
    }}

\makeatletter
\patchcmd{\tableofcontents}
  {\@mkboth{\MakeUppercase\contentsname}{\MakeUppercase\contentsname}}
  {\@mkboth{\mbox{}}{\MakeUppercase\contentsname}}
  {}{}
\makeatother

\usepackage{lipsum}% only for dummy text

\begin{document}
\renewcommand{\cftchapaftersnum}{.}

%%# IN NEW BEGIN
\pagestyle{fancy}
%%# IN NEW END

\frontmatter
%% OUT NEW BEGIN
%\pagenumbering{Roman}
%% OUT NEW BEGIN

%% OUT NEW BEGIN
%\fancyfoot[RO, LE] {\small\thepage}
%\thispagestyle{empty}
%\fancyfoot[R] {\thepage}
%% OUT NEW END

\setlength{\parindent}{0pt}
\setlength{\parskip}{12pt}

\renewcommand\lstlistingname{Code listing}
\renewcommand\lstlistlistingname{List of Listings}
%\rhead{\small{List of Listings}}% <- remove this
\addcontentsline{toc}{chapter}{List of Listings}
\lstlistoflistings

\mainmatter

%% IN NEW BEGIN
\renewcommand{\sectionmark}[1]{\markright{\thesection~#1}}
%% IN NEW END

\pagenumbering{arabic}
\onehalfspacing

%% OUT NEW BEGIN
%\fancyhead{}
%\pagestyle{plain}
%\lhead{}
%\rhead{\small\nouppercase\leftmark\\\small\nouppercase\rightmark}
%\fancyfoot[R] {\thepage}
%% OUT NEW END

\setlength{\parindent}{0pt}
\setlength{\parskip}{12pt}

\chapter{Some Chapter}
\lipsum[1]
\section{Some section}
\lipsum[1-5]
\section{Some section}
\lipsum[1-2]
\end{document}

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.