13

So I'm using the fancyhdr package and compiling my code and it gives me no error whatsoever but it's not working, it does not display any header or footer and keeps giving me number on the bottom center of the page... I don't understand what is wrong. Here is my code below:

\documentclass[10pt,twoside]{report}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[RE]{\leftmark}
\fancyhead[LO]{\rightmark}
\fancyfoot[RO,LE]{\thepage}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage[a4paper,width=150mm,top=25mm,bottom=25mm]{geometry}
\graphicspath{{Images/}}

\begin{document}
\chapter*{Acknowledgements}
\thispagestyle{empty}

\pagenumbering{arabic}

\tableofcontents
\addcontentsline{toc}{chapter}{\listfigurename}\addcontentsline{toc}chapter}{\listtablename}

\pagenumbering{roman}
\setcounter{page}{1}

\listoffigures
\listoftables

\clearpage
\pagenumbering{arabic}
\setcounter{page}{1}


\chapter{Introduction}
\input{chapter/introduction.tex}
\chapter{Bibliography work}
\input{chapter/bib_work.tex}
\chapter{Material and methods}
\input{chapter/mat_and_meth}
\chapter{Results}
\input{chapter/results}
\chapter{Discussion}
\input{chapter/discussion}
\chapter{Conclusion}
\input{chapter/conclusion}
\pagebreak
\nocite{benkaddour_salinite_2011}
\nocite{fetouani_assessing_2008}
\nocite{kihumba_modelling_2016}
\nocite{mattern_bayesian_2012}
\bibliographystyle{plain}
\bibliography{mybib}
\chapter*{Appendix}
\end{document}
murray
  • 7,944
A. Bohyn
  • 156
  • 1
    Your code is of course not compilable, since there are a lot of \input statements, for files we can't access! \pagenumbering{...} always resets the page counter to 1, no need to \setcounter{page}{1} ... and \chapter* before \begin{document} does not work! –  Jan 02 '17 at 16:48
  • Those files are empty, it's just for the structure of the thesis now – A. Bohyn Jan 02 '17 at 16:48
  • 1
    Try \input{Ishouldlearnhowtomakeanmwe} ... ;-) –  Jan 02 '17 at 16:51
  • But even without the input, it's not working and I don"t know why... – A. Bohyn Jan 02 '17 at 16:54
  • There is another typo: addcontentsline{toc}chapter}{\listtablename}!!! It should readaddcontentsline{toc}{chapter}{\listtablename}` –  Jan 02 '17 at 16:55

2 Answers2

9

Few things to know first

  1. \pagestyle{<style>} is a LaTeX native command that sets the current style to <style>.
    1. LaTeX has four predefined styles: plain, empty, headings, and myheadings.
    2. If not set by the class/package, the initial style is plain.
  2. \thispagestyle{<style>} is a LaTeX native command that sets the style of the current page to <style>.
    • Classes such as article, book, or report make use of \thispagestyle{plain} at \maketitle, or \chapter.
  3. The commands from the fancyhdr package, e.g. \fancyhf{}, act on the current style.
  4. \fancypagestyle{<style>}{<fancyhdr commands>} creates a closed environment in which
    1. It first copies the fancy style over <style>, creating it if necessary;
    2. It then sets <style> as the current style;
    3. Finally it modifies <style> executing <fancyhdr commands>.
    4. Note that the current document style has not changed.
    5. 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?

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"
}
8

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}