fancyhdr package not working
Use this order:
\usepackage[a4paper,width=150mm,top=25mm,bottom=25mm]{geometry}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
[...]
Then fancyhdr
will see the correct margin setting. For the first chapter page LaTeX
automatically uses pagestyle plain
which has no header and the pagenumber in the bottom centered. Redefine this pagestyle. See documentation of fancyhdr
for an example.
\documentclass{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[a4paper,width=150mm,top=25mm,bottom=25mm]{geometry}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[RE]{\leftmark}
\fancyhead[LO]{\rightmark}
\fancyfoot[RO,LE]{\thepage}
\fancypagestyle{plain}{% the preset of fancyhdr
\fancyhf{} % clear all header and footer fields
\fancyfoot[C]{\textbf{\thepage}} % except the center
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}}
\usepackage{blindtext}
\begin{document}
\blinddocument
\end{document}
Few things to know first#
\pagestyle{<style>}
is a LaTeX native command that sets the current style to<style>
.- LaTeX has four predefined styles:
plain
,empty
,headings
, andmyheadings
.
- Several packages extended the number of predefined styles, such as
fancyhdr
, andtitleps
. Further info in this thread.
- If not set by the class/package, the initial style is
plain
. \thispagestyle{<style>}
is a LaTeX native command that sets the style of the current page to<style>
.- Classes such as
article
,book
, orreport
make use of\thispagestyle{plain}
at\maketitle
, or\chapter
. - The commands from the
fancyhdr
package, e.g.\fancyhf{}
, act on the current style. \fancypagestyle{<style>}{<fancyhdr commands>}
creates a closed environment in which- It first copies the
fancy
style over<style>
, creating it if necessary; - It then sets
<style>
as the current style; - Finally it modifies
<style>
executing<fancyhdr commands>
. - Note that the current document style has not changed.
- BUG: Do not issue
\fancypagestyle{fancy}{<fancyhdr commands>}
, the LaTeX compiler will hang at the first\pagestyle{fancy}
call since it will enter in an infinite recursive loop. I just notified the author with regards to this.
Why your code does not what you intend to?
Once you call \pagestyle{fancy}
, you have set the current page style to fancy
, and the subsequent commands will modify this style only. The class you are using, report
, will issue repeatedly \thispagestyle{plain}
countering your previous changes.
How to do what you itend to?
Option 1 (recommended)
Simply edit the plain
style. Recommended if you intend to use only one style.
\pagestyle{plain} % this line is not necessary if previously no style has been set
\fancyhf{}
\fancyhead[RE]{\leftmark}
\fancyhead[LO]{\rightmark}
\fancyfoot[RO,LE]{\thepage}
Option 2
Edit fancy
and copy it to plain
. Edits to fancy
or plain
won't affect each other. Recommended if you intend to use different styles.
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[RE]{\leftmark}
\fancyhead[LO]{\rightmark}
\fancyfoot[RO,LE]{\thepage}
\fancypagestyle{plain}{% copies "fancy" over "plain"
\fancyfoot[C]{\thepage}% you can add edits that won't affect "fancy" but only "plain"
}