47

I have a problem with the fancy package: I use the report documentclass and I have a chapter, a lot of text and then a new page starting with a section (or the next page has no chapter starting on that particular page).

The problem be, that what I defined for my header and footer is NOT on the chapter-page but only on the non-chapter page. How come? What did I miss?

Minimal example:

\documentclass[]{report}

\usepackage{lastpage}
\usepackage{fancyhdr} % for use of \pageref{LastPage}

\fancypagestyle{IHA-fancy-style}{%
\fancyhf{} %Clear header and footer
\fancyhead[LE,RO]{\slshape \rightmark}
\fancyhead[LO,RE]{\slshape \leftmark}
\fancyfoot[C]{\thepage of \pageref{LastPage}} %Custom footer
\renewcommand{\headrulewidth}{0.4pt} %Line at the header visible
\renewcommand{\footrulewidth}{0.4pt}} %Line at the footer visible

\title{A short example}

\begin{document}

\maketitle
\tableofcontents

\pagestyle{IHA-fancy-style}
\chapter{A chapter}
Look at the footer

\newpage
\section{A section}
Look at the footer

\end{document}

Take notice: No line on page 2 at the bottom - a line on page 3 at the bottom.

Any clues?

1
  • 5
    In general one doesn't want headers in a chapter starting page: there's no need for them; moreover this will give a more distinctive look to those pages. In particular a rule above the chapter title doesn't look very nice.
    – egreg
    Jun 2, 2013 at 21:20

2 Answers 2

32

You can redefine the plain style that's issued by \chapter. Here's how you can do; I'd recommend not having headings in the chapter starting pages, nor the upper rule.

\documentclass[twoside]{report}

\usepackage{lastpage}
\usepackage{fancyhdr} % for use of \pageref{LastPage}

\fancypagestyle{IHA-fancy-style}{%
  \fancyhf{}% Clear header and footer
  \fancyhead[LE,RO]{\slshape \rightmark}
  \fancyhead[LO,RE]{\slshape \leftmark}
  \fancyfoot[C]{\thepage\ of \pageref{LastPage}}% Custom footer
  \renewcommand{\headrulewidth}{0.4pt}% Line at the header visible
  \renewcommand{\footrulewidth}{0.4pt}% Line at the footer visible
}

% Redefine the plain page style
\fancypagestyle{plain}{%
  \fancyhf{}%
  \fancyfoot[C]{\thepage\ of \pageref{LastPage}}%
  \renewcommand{\headrulewidth}{0pt}% Line at the header invisible
  \renewcommand{\footrulewidth}{0.4pt}% Line at the footer visible
}
\title{A short example}

\begin{document}

\maketitle
\tableofcontents

\pagestyle{IHA-fancy-style}
\chapter{A chapter}
Look at the footer

\newpage
\section{A section}
Look at the footer

\end{document}
4
  • @egreg--so this means that since \pagestyle changes before the chapter--it should affect the chapter also--am i right?? now if i comment out the line regarding \fancyfoot--the fancyfoot still appears in the chapter--why??
    – js bibra
    Sep 26, 2019 at 17:25
  • @jsbibra I'm not sure what you mean.
    – egreg
    Sep 26, 2019 at 17:37
  • \pagestyle{iha fancy style }------is mentioned just before chapter start--so will this command affect all following pages such as chapter and section---------or will this command only affect the toc and title page??
    – js bibra
    Sep 27, 2019 at 1:26
  • also kindly advise me if there is a particular method outlined somewhere for coding and typesetting------for example the beginsection should be indented once and the next line should be indented twice and for tables should have 4 inndentations and so on-------or are the indents are as per individual choice-----just want to make the coding /typesetting more neat ---------as in the image added
    – js bibra
    Sep 27, 2019 at 1:31
52

For the report document class, the command \chapter is defined in the file report.cls, starting on line 339 or so. Contained in the definition of the \chapter macro is the instruction \thispagestyle{plain}, instructing LaTeX to set the chapter's first page in the so-called "plain" style.

To switch the page style of the first page of a chapter from "plain" to "fancy", you could load the etoolbox package and patch the definition of the \chapter macro:

\documentclass{report}
% other preamble stuff...
\usepackage{etoolbox}
\patchcmd{\chapter}{\thispagestyle{plain}}{\thispagestyle{fancy}}{}{}
% rest of document ...
6
  • 4
    +1, very useful. This also works for the book document class.
    – BowPark
    Dec 6, 2018 at 10:43
  • What about book with different style for even/odd pages?
    – KcFnMi
    Jun 3, 2019 at 9:10
  • @KcFnMi - Not sure I understand your question. Are you trying to find out if the book document class inserts the instruction \thispagestyle{plain} on pages with part- and chapter-level headers? If so, the answer is "yes".
    – Mico
    Jun 3, 2019 at 11:55
  • Not sure I understand it. Though asked tex.stackexchange.com/questions/493945/…. Comment/answer will be much appreciated.
    – KcFnMi
    Jun 3, 2019 at 12:46
  • also kindly advise me if there is a particular method outlined somewhere for coding and typesetting------for example the beginsection should be indented once and the next line should be indented twice and for tables should have 4 inndentations and so on-------or are the indents are as per individual choice-----just want to make the coding /typesetting more neat ---------as in the image added
    – js bibra
    Sep 27, 2019 at 6:02

You must log in to answer this question.

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