Page LayoutFancyhdr and \chapter{} page numbering

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
adkdk
Posts: 9
Joined: Sun Apr 18, 2010 11:39 am

Fancyhdr and \chapter{} page numbering

Post by adkdk »

To create fancy headers in my document, I'm using fancyhdr package and the following code:

Code: Select all

\pagestyle{fancy}
\fancyhead[LE]{\nouppercase{\leftmark}}
\fancyhead[RO]{\nouppercase{\rightmark}}
\fancyhead[CE,RE,CO,LO]{\ }
This leaves footers (with centered page number) untouched. However, on the page which opens new chapter, page number is missing. To restore number, I have to put

Code: Select all

\thispagestyle{plain}
after each \chapter{}.

Is there any way to do it better? Redefining "plain" style with fancyhdr package is not working - it looks like name of new chapter page style is different.
When fancyhdr is turned off, page numbers are shown on all pages, including chapter opening.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Fancyhdr and \chapter{} page numbering

Post by frabjous »

I don't understand why you'd be getting the empty page style on chapter pages, and I don't understand why redefining the plain page style using the methods described in the fancyhdr package documentation wouldn't work. Could you provide a MWE showing what goes wrong?

Another thing to try would be the \assignpagestyle command from titlesec to reassign \chapter to the fancy page style (or whatever):

Code: Select all

\usepackage{titlesec}
\assignpagestyle{\chapter}{fancy}
or

Code: Select all

\usepackage{titlesec}
\assignpagestyle{\chapter}{plain}
or whatever.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Fancyhdr and \chapter{} page numbering

Post by localghost »

adkdk wrote:[...] This leaves footers (with centered page number) untouched. However, on the page which opens new chapter, page number is missing. [...]
Always post full examples to enable others to comprehend problems clearly. I can't reproduce the undesired behaviour with your code snippet. On chapter pages the »plain« page style is used by default. The code below works flawlessly.

Code: Select all

\documentclass[11pt,a4paper,twoside,english]{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{fancyhdr}
\usepackage{blindtext}

% Page setup (fancyhdr)
\fancyhf{}
\fancyhead[LE]{\nouppercase{\leftmark}}
\fancyhead[RO]{\nouppercase{\rightmark}}
\fancyfoot[CE,CO]{\thepage}
\pagestyle{fancy}

\begin{document}
  \blinddocument
\end{document}
As frabjous already suggested, you can alternatively use the titlesec package. But don't use it in conjunction with fancyhdr. Decide to use on or the other. The code below shows a similar example.

Code: Select all

\documentclass[11pt,a4paper,twoside,english]{report}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage{babel}
\usepackage[pagestyles,raggedright]{titlesec}
\usepackage{blindtext}

% Page setup (titlesec)
\newpagestyle{main}{%
  \headrule
  \sethead[\chaptername~\thechapter\quad\chaptertitle][][]{}{}{\thesection\quad\sectiontitle}
  \setfoot[][\thepage][]{}{\thepage}{}
}
\pagestyle{main}

\begin{document}
  \blinddocument
\end{document}
I recommend to use titlesec because it is much more flexible.


Best regards
Thorsten
adkdk
Posts: 9
Joined: Sun Apr 18, 2010 11:39 am

Fancyhdr and \chapter{} page numbering

Post by adkdk »

The problem was with mwrep (a non-standard report) class. After doing some changes in mwrep.cls everything started to work.

I changed:

Code: Select all

 \let\ps@opening\hf@empty
 \let\ps@closing\hf@empty
to

Code: Select all

 \let\ps@opening\hf@plain
  let\ps@closing\hf@plain
and saved this with a custom class name.
Post Reply