2

I use fancyhdr for my headers and footers. This works well on all pages except for the Contents page. Here the header displays leftmark and rightmark, Contents is displayed twice. Any idea why this is the case?

\documentclass[12pt,twoside]{book}

% set margin

\usepackage[paper=a4paper,left=25mm,right=25mm,top=25mm,bottom=40mm,bindingoffset=6mm]{geometry}

\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{totcount}
\usepackage{lastpage}
\usepackage{float}

% headers and footers (https://www.youtube.com/watch?v=mZcV1wIPCBo)

\usepackage{fancyhdr}

% beautiful cross-references with hyperref

\usepackage{xcolor,colortbl}
\definecolor{Green}{RGB}{121,141,33}
\usepackage{hyperref} % http://ctan.org/pkg/hyperref

\hypersetup{
  colorlinks,
  linkcolor=Green,
  citecolor=Green,
  filecolor=Green,
  urlcolor=Green,
}

% change font

\usepackage[default,osfigures,scale=0.95]{opensans}
\usepackage[T1]{fontenc}
% \usepackage[sfdefault,light,condensed]{roboto}  %% Option 'sfdefault' only if the base font of the document is to be sans serif
% \usepackage[sfdefault]{AlegreyaSans}
% \renewcommand*\oldstylenums[1]{{\AlegreyaSansOsF #1}}

%disable indent globally

\setlength{\parindent}{0pt}

\begin{document}
  \input{"0"}

  \pagestyle{fancyplain}
  \fancyhf{}
  \fancyhead{}
  \fancyfoot{}
  \fancyhead[LO,RE]{\rightmark}

  \tableofcontents

  \fancyhead{}
  \fancyfoot{}
  \fancyhead[LE,RO]{\leftmark}
  \fancyhead[LO,RE]{\rightmark}
  \fancyfoot[LE,RO]{my footer\\~\\Page~\thepage~of~\getpagerefnumber{LastPage}}
  \fancyfoot[LO,RE]{my footer}
  \renewcommand{\headrulewidth}{0.4pt}
  \renewcommand{\footrulewidth}{0.4pt}
  \makeatletter
  \let\ps@plain\ps@fancy
  \makeatother

  \input{"Chapters/1"}
  \input{"Chapters/2"}
  \input{"Chapters/3"}
  \input{"Chapters/4"}
  \input{"Chapters/5"}
  \input{"Chapters/6"}
\end{document}
  • Issue \cleardoublepage after \tableofcontents – egreg Nov 21 '15 at 10:06
1

It's a problem of timing, due to the changes of page style you're doing. Issuing

\tableofcontents
\cleardoublepage

will fix the problem.

enter image description here

Your main problem is, however, confusing the various planes and of giving contradictory instructions. It's better sticking with simple page styles and not defining them inside the document.

Here's a full version with changes. Note the option heightrounded to geometry, with also a setting for headheight as requested by fancyhdr.

I define two page styles: one for the front matter, one for the main matter, which are automatically selected by the corresponding commands. The initial part (your \input{0}) uses the empty page style. The \chapter command is modified so it doesn't issue the \thispagestyle{plain} instruction.

\documentclass[12pt,twoside]{book}

% set margin
\usepackage[
  paper=a4paper,
  left=25mm,
  right=25mm,
  top=25mm,
  bottom=40mm,
  heightrounded,
  headheight=14.5pt,
  bindingoffset=6mm,
]{geometry}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{totcount}
\usepackage{lastpage}
\usepackage{float}
\usepackage{etoolbox} % for \patchcmd
% headers and footers (https://www.youtube.com/watch?v=mZcV1wIPCBo)
\usepackage{fancyhdr}

\usepackage[default,osfigures,scale=0.95]{opensans}

\usepackage{lipsum} % just for this example

% beautiful cross-references with hyperref
\usepackage{xcolor,colortbl}

\usepackage{hyperref} % http://ctan.org/pkg/hyperref

\hypersetup{
  colorlinks,
  linkcolor=Green,
  citecolor=Green,
  filecolor=Green,
  urlcolor=Green,
}

%%% customizations
\definecolor{Green}{RGB}{121,141,33}

\pagestyle{fancy}

\fancypagestyle{frontmatter}{%
  \fancyhf{}%
  \fancyhead[RO,LE]{\rightmark}%
}
\fancypagestyle{body}{%
  \fancyhf{}%
  \fancyhead[LE,RO]{\leftmark}%
  \fancyhead[LO,RE]{\rightmark}%
  \fancyfoot[LE,RO]{my footer\\~\\Page~\thepage~of~\getpagerefnumber{LastPage}}%
  \fancyfoot[LO,RE]{my footer}%
  \renewcommand{\headrulewidth}{0.4pt}%
  \renewcommand{\footrulewidth}{0.4pt}%
}

\patchcmd{\chapter}{\thispagestyle{plain}}{}{}{} % remove \thispagestyle{plain}
%%% continuous numbering
\makeatletter
\renewcommand\frontmatter{%
  \cleardoublepage
  \pagestyle{frontmatter}%
  \@mainmatterfalse
}
\renewcommand\mainmatter{%
  \cleardoublepage
  \pagestyle{body}%
  \@mainmattertrue
}
\makeatother

\begin{document}

\pagestyle{empty}% titlepage
%  \input{"0"}
\lipsum[1]

\frontmatter
\tableofcontents

\mainmatter

\chapter{Title}
\lipsum
\chapter{Title}
\lipsum
\chapter{Title}
\lipsum
\chapter{Title}
\lipsum

%  \input{"Chapters/1"}
%  \input{"Chapters/2"}
%  \input{"Chapters/3"}
%  \input{"Chapters/4"}
%  \input{"Chapters/5"}
%  \input{"Chapters/6"}
\end{document}
  • Yes, now it is only displayed once but on the wrong side, see your example. Contents shall be placed on the right side, not on the left. Any idea? – Michael S. Nov 22 '15 at 7:39
  • 1
    @gentlesea It was you who told it so: I just left \fancyhead[LO,RE]{\rightmark} from your code. Change it into \fancyhead[RO,LE]{\rightmark}. However, I'll put in a renewed version, with some advice. – egreg Nov 22 '15 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.