TransWikia.com

Headers and footers using fancyhdr displays table of contents on even pages

TeX - LaTeX Asked by bjornvandijkman on December 3, 2020

For my thesis I wish to alternate the header for even and odd pages like this template. I looked at the code of that template, but it was a bit too complicated for me to apply it to my own file.

On the even pages I want the page number on the left side and the subsection name on the right side. On the odd pages I want the page number on the right side and the section name on the left side. Lastly, I want pages where a new section starts to not have a header and to be able to specify other pages where I do not wish to have a header.

I tried the following using fancyhdr with the article as documentclass:

usepackage{fancyhdr}
fancyhf{}
fancyhead[LO]{nouppercase{rightmark}}
fancyhead[RE]{nouppercase{leftmark}}
fancyhead[LE,RO]{thepage}
pagestyle{fancy}

However, instead of the name of the subsection it returns ‘Table of Contents’ for the even pages until the page where the references start. For the odd pages it works fine for the section name. How can I change this behavior? Full preamble looks like this:

usepackage[utf8]{inputenc}
usepackage[T1]{fontenc}

usepackage{amsmath}

% For tables
usepackage{array}
usepackage{booktabs}

% for having numbers aligned to the decimal point
usepackage{siunitx}

% For captions
usepackage[labelfont=bf]{caption}

% Include graphics from pdf or jpg file
usepackage{graphicx} 

% For citations
usepackage{apacite}
bibliographystyle{apacite}

graphicspath{{Images/}}

usepackage{xstring}
usepackage{titleps}
usepackage{subcaption} %Side by side table alignment
usepackage{makecell} %Split table headers into multiple lines

% bold math symbols
usepackage{bm}

usepackage{tikz}
usepackage[titletoc]{appendix}

% easily write third, fourth, fifth etc.
usepackage[super]{nth}

usepackage{tikz}
usepackage{mathdots}
usepackage{yhmath}
usepackage{cancel}
usepackage{color}
usepackage{multirow}
usepackage{amssymb}
usepackage{gensymb}
usepackage{tabularx}
usetikzlibrary{fadings}

% Change contents to table of contents
renewcommand{contentsname}{Table of Contents}
renewcommandlabelitemi{$cdot$}
newcommand{HRule}{rule{linewidth}{0.5mm}}
raggedbottom

usepackage{fancyhdr}
fancyhf{}
fancyhead[LO]{nouppercase{rightmark}}
fancyhead[RE]{nouppercase{leftmark}}
fancyhead[LE,RO]{thepage}

pagestyle{fancy}

One Answer

Hello bjornvandijkman,

I cannot really reproduce your problem without having a minimal working example. It works fine for me (see my code using your preample).

Two notes:

  1. You switched the marks. So leftmark and rightmark have to be switched.
  2. Did you consider using report respectively scrreprt with chapters (that would automatically enable plain page style on pages on which the chapter starts).

It would be helpful to see your code with some of the content because I assume the error is there. Also what TeX version and OS do you use?

My examples with article

documentclass[12pt, twoside]{article}

usepackage[utf8]{inputenc}
usepackage[T1]{fontenc}

usepackage{amsmath}

% For tables
usepackage{array}
usepackage{booktabs}

% for having numbers aligned to the decimal point
usepackage{siunitx}

% For captions
usepackage[labelfont=bf]{caption}

% Include graphics from pdf or jpg file
usepackage{graphicx}

% For citations
usepackage{apacite}
bibliographystyle{apacite}

graphicspath{{Images/}}

usepackage{xstring}
usepackage{titleps}
usepackage{subcaption} %Side by side table alignment
usepackage{makecell} %Split table headers into multiple lines

% bold math symbols
usepackage{bm}

usepackage{tikz}
usepackage[titletoc]{appendix}

% easily write third, fourth, fifth etc.
usepackage[super]{nth}

usepackage{tikz}
usepackage{mathdots}
usepackage{yhmath}
usepackage{cancel}
usepackage{color}
usepackage{multirow}
usepackage{amssymb}
usepackage{gensymb}
usepackage{tabularx}
usetikzlibrary{fadings}

% Change contents to table of contents
renewcommand{contentsname}{Table of Contents}
renewcommandlabelitemi{$cdot$}
newcommand{HRule}{rule{linewidth}{0.5mm}}
raggedbottom

usepackage{fancyhdr}
fancyhf{}
fancyhead[LO]{nouppercase{leftmark}}
fancyhead[RE]{nouppercase{rightmark}}
fancyhead[LE,RO]{thepage}

pagestyle{fancy}

begin{document}
Empty page
newpage
tableofcontents
newpage
section{Introduction}

subsection{Sub Introduction 1}

newpage
newpage

subsection{Sub Introduction 2}

newpage
newpage

section{Material and Methods}

subsection{MM 1}
newpage
foo
newpage
foo
newpage

subsection{MM2}

newpage

section{Results}

newpage
newpage
foo
newpage
foo
newpage

section{Discussion}
newpage
foo
newpage
foo
newpage
end{document}

and with scrreprt

documentclass[12pt, twoside]{scrreprt}

usepackage[utf8]{inputenc}
usepackage[T1]{fontenc}

usepackage{amsmath}

% For tables
usepackage{array}
usepackage{booktabs}

% for having numbers aligned to the decimal point
usepackage{siunitx}

% For captions
usepackage[labelfont=bf]{caption}

% Include graphics from pdf or jpg file
usepackage{graphicx}

% For citations
usepackage{apacite}
bibliographystyle{apacite}

graphicspath{{Images/}}

usepackage{xstring}
usepackage{titleps}
usepackage{subcaption} %Side by side table alignment
usepackage{makecell} %Split table headers into multiple lines

% bold math symbols
usepackage{bm}

usepackage{tikz}
usepackage[titletoc]{appendix}

% easily write third, fourth, fifth etc.
usepackage[super]{nth}

usepackage{tikz}
usepackage{mathdots}
usepackage{yhmath}
usepackage{cancel}
usepackage{color}
usepackage{multirow}
usepackage{amssymb}
usepackage{gensymb}
usepackage{tabularx}
usetikzlibrary{fadings}

% Change contents to table of contents
renewcommand{contentsname}{Table of Contents}
renewcommandlabelitemi{$cdot$}
newcommand{HRule}{rule{linewidth}{0.5mm}}
raggedbottom

usepackage{fancyhdr}
fancyhf{}
fancyhead[LO]{nouppercase{leftmark}}
fancyhead[RE]{nouppercase{rightmark}}
fancyhead[LE,RO]{thepage}

fancypagestyle{plain}{%
fancyhf{}
}


pagestyle{fancy}

begin{document}
Empty page
newpage
tableofcontents
newpage
chapter{Introduction}

section{Sub Introduction 1}

newpage
newpage

section{Sub Introduction 2}

newpage
newpage

chapter{Material and Methods}

section{MM 1}
newpage
foo
newpage
foo
newpage

section{MM2}

newpage

chapter{Results}

newpage
newpage
foo
newpage
foo
newpage

chapter{Discussion}
newpage
foo
newpage
foo
newpage
end{document}

Best regards

Jeroen

Answered by J-Schaefer on December 3, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP