Page Layoutfancyhdr | Wrong Chapter Numbers in Headers

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
aless80
Posts: 5
Joined: Fri Dec 02, 2011 1:03 pm

fancyhdr | Wrong Chapter Numbers in Headers

Post by aless80 »

Hi,
I have a problem with the chapters in the headers. One or two pages before the start of the next chapter (e.g. we are in the last pages of Chapter 2), the header shows the name of the next chapter (e.g. Chapter 3).
Maybe it is \cleardoublepage?
Thanks

Here the code of the main file:

Code: Select all

\documentclass[12pt,b5paper]{book}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage{float}
\usepackage{setspace}
\usepackage{tocloft}
\geometry{verbose,a4paper,tmargin=15mm,bmargin=30mm,lmargin=30mm,rmargin=20mm}
\usepackage[font=small,labelfont=bf]{caption}
\usepackage{amssymb}
\usepackage{chapterbib}
\usepackage{fancyhdr}
\usepackage{ifthen} 
\usepackage[utf8x]{inputenc}
\usepackage{units}
\usepackage{sidecap}
\usepackage{xr}
\usepackage[round,numbers,sort&compress]{natbib} 
\defaultbibliographystyle{unsrt}
\setcounter{secnumdepth}{5}
\setcounter{tocdepth}{5}
\onehalfspacing
\usepackage{multirow}
\usepackage{xcolor,colortbl}
\usepackage[font=small,labelfont=bf]{caption}
\begin{document}
\newenvironment{mydef}[1]{\begin{definition} #1 \mbox{\\}
\rm}
{\end{definition}}
\cleardoublepage
\clearpage{\pagestyle{empty}\cleardoublepage}
\setcounter{tocdepth}{1}
\pagenumbering{arabic}
\cleardoublepage
\pagenumbering{arabic}
\input{ch1.tex}
\input{ch2.tex}
\end{document}
Here the code of Ch1 file:

Code: Select all

\pagestyle{fancy}
	\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
	\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}} \fancyhf{}
	\fancyhead[LE,RO]{\bfseries\thepage}
	\fancyhead[LO]{\bfseries\ Chapter 1}
	\fancyhead[RE]{\bfseries\ Chapter 1}
	\renewcommand{\headrulewidth}{0.5pt}
	\renewcommand{\footrulewidth}{0pt}
	\addtolength{\headheight}{0.5pt}
\chapter{Chapter1}
\newpage
aaaa aaaa bbb bbb bbb bbb aaaa aaaa bbb bbb bbb bbb aaaa aaaa bbb bbb bbb bbb aaaa aaaa bbb bbb bbb bbb aaaa aaaa bbb bbb bbb bbb aaaa aaaa bbb bbb bbb bbb 
\newpage
aaa
Here the code of Ch2 file:

Code: Select all

	\pagestyle{fancy}
	\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
	\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}} \fancyhf{}
	\fancyhead[LE,RO]{\bfseries\thepage}
	\fancyhead[LO]{\bfseries\ Chapter 2}
	\fancyhead[RE]{\bfseries\ Chapter 2}
	\renewcommand{\headrulewidth}{0.5pt}
	\renewcommand{\footrulewidth}{0pt}
	\addtolength{\headheight}{0.5pt}
\chapter{Chapter2}
\newpage
aaaa aaaa bbb bbb bbb bbb aaaa aaaa bbb bbb bbb bbb aaaa aaaa bbb bbb bbb bbb aaaa aaaa bbb bbb
Last edited by localghost on Fri Dec 02, 2011 5:47 pm, edited 2 times in total.

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

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

fancyhdr | Wrong Chapter Numbers in Headers

Post by localghost »

Your example does not compile. Please provide a proper minimal example that is reduced to only relevant code and furthermore compilable out of the box for everybody.


Best regards and welcome to the board
Thorsten
aless80
Posts: 5
Joined: Fri Dec 02, 2011 1:03 pm

fancyhdr | Wrong Chapter Numbers in Headers

Post by aless80 »

I am sorry, the main file should be:

Code: Select all

\documentclass[12pt,b5paper]{book}
\usepackage[english]{babel}
\usepackage{fancyhdr}
\begin{document}
\cleardoublepage
\clearpage{\pagestyle{empty}\cleardoublepage}
\setcounter{tocdepth}{1}
\pagenumbering{arabic}
\cleardoublepage
\input{ch1.tex}
\input{ch2.tex}
\end{document}
you will see that pages 3-4 have the header "Chapter 2" whereas it should be Chapter 1
meho_r
Posts: 823
Joined: Tue Aug 07, 2007 5:28 pm

fancyhdr | Wrong Chapter Numbers in Headers

Post by meho_r »

You don't need to set headers/footers again and again before every chapter. fancyhdr is there to take care of numbering automatically. So, instead of specifying fancyhdr-related commands before chapters, set headers/footers in your master file only. Also, use commands for automatic numbering; command for chapter numbers is \thechapter, for section numbers is \thesection, etc.

Take a look at the example in the attachment. It's the same code you provided, with these changes:

1. commands for setting headers/footers specified only in the master file (A1.tex) (lines 6–20)
2. \thechapter command used in line 12 instead of manual numbering
3. all fancyhdr-related commands in files Ch01.tex and Ch02.tex are disabled. Commands in the master file (A1.tex) are enough.
Attachments
Example.zip
(1.27 KiB) Downloaded 264 times
Post Reply