TransWikia.com

Usage of different headings with fancyhdr in a thesis

TeX - LaTeX Asked on May 15, 2021

I have been trying to insert different headings to my thesis.

The requirement is to show the frontmatter with Roman letters and the name of the section or chapter. The heading of the main part shall show the number of the chapter, the chapter title and the page number. The backmatter again shall show the same as the main part, thus the number of the chapter, title and page number. Sounds pretty easy. However, I have my difficulties.

I have tried the following code. It works fine for the frontmatter. The main and backmatter do not show the expected result. In the main matter, the number of the chapter is not shown and on the "rightmark" page, the section is shown instead of the chapter details.

Same for the backmatter. It shows the title twice in the header, but not the chapter number or the page number.

Attached you will find the equivalent code.

usepackage{fancyhdr}
pagestyle{fancy}
fancypagestyle{main}{%
fancyhf{}
%fancyhead[EL,OR]{textsl{rightmark}}
%fancyhead[OL,ER]{textsl{rightmark}}
fancyhead[EL]{thepage} 
fancyhead[ER]{leftmark}
fancyhead[OL]{leftmark}
fancyhead[OR]{thepage}
renewcommand{chaptermark}[1]{%
     markboth{thechapter{} #1}{thechapter{}#1}}
}
fancypagestyle{plain}{%
fancyhf{}
fancyhead[L]{nouppercase{rightmark}}
fancyhead[R]{nouppercase{leftmark}}
fancyhead[EL]{thepage} 
fancyhead[OR]{thepage}

renewcommand{chaptermark}[1]{%
    markboth{#1}{#1}}
}
fancypagestyle{back}{%
fancyhf{}
fancyhead[EL]{thepage} 
fancyhead[ER]{leftmark}
fancyhead[OL]{leftmark}
fancyhead[OR]{thepage}

renewcommand{chaptermark}[1]{
    markboth{thechapter{} #1}{thechapter{}#1}}
}
apptofrontmatter{pagestyle{plain}}
apptomainmatter{pagestyle{main}}
apptobackmatter{pagestyle{back}}



%**Another way of doing it is the following:** 
documentclass[12pt,twoside]{book}
usepackage[utf8]{inputenc}
usepackage{graphicx}
usepackage[a4paper,width=150mm, top=25mm, bottom=25mm, bindingoffset=6mm]{geometry}
usepackage[ngerman]{babel}
usepackage[T1]{fontenc}
usepackage{fontspec}
%usepackage[scaled]{uarial}
setsansfont{Arial}
%usepackage{lmodern}
usepackage{amsmath}
usepackage{siunitx}
usepackage{amsfonts}
usepackage{amssymb}
usepackage[]{unicode-math}
setmainfont[Mapping=tex-text,Numbers={OldStyle,Proportional},Ligatures={TeX, Common}]{TeX Gyre Termes}
setmonofont[Mapping=tex-text,Scale=0.8]{TeX Gyre Cursor}
setsansfont[Mapping=tex-text,Numbers=OldStyle,Ligatures=Common]{TeX Gyre Heros}
setmathfont[math-style=ISO,bold-style=ISO,vargreek-shape=TeX]{TeX Gyre Pagella Math}
usepackage{makeidx}
usepackage{multirow}
usepackage{caption}
usepackage{subcaption}
usepackage{fancyhdr}
setlength{headheight}{15pt}
fancypagestyle{plain}{%
fancyhf{}
fancyhead[L]{nouppercase{leftmark}}
fancyhead[R]{nouppercase{leftmark}}
fancyhead[EL]{thepage} 
fancyhead[OR]{thepage}
renewcommand{chaptermark}[1]{%

    markboth{#1}{#1}}
}
%fancypagestyle{main}
pagestyle{fancy}
fancyhf{}
renewcommand{chaptermark}[1]{ 
    markboth{thechapter{} #1} {} }

fancyhead[LE,RO]{thepage}
fancyhead[RE]{nouppercase{leftmark} }
fancyhead[LO]{nouppercase{leftmark} 
}

graphicspath{images/}
usepackage[style=alphabetic]{biblatex}
addbibresource[style=alphabetic]{references.bib}
usepackage{setspace}
setstretch{1.1}
usepackage{acronym}

parindent0cm
renewcommand*{familydefault}{sfdefault}


makeatletter 
% Define frontmatter, mainmatter and backmatter if not defined 
@ifundefined{frontmatter}{% 
    newcommand{frontmatter}{% 
       %In Roemischen Buchstaben nummerieren (i, ii, iii) 
       pagenumbering{roman} 
    } 
}{} 
@ifundefined{mainmatter}{% 
    % scrpage2 benoetigt den folgenden switch 
    % wenn mainmatter definiert ist. 
%    newifif@mainmatter@mainmattertrue 
    newcommand{mainmatter}{% 
       % -- Seitennummerierung auf Arabische Zahlen zuruecksetzen (1,2,3) 
       pagenumbering{arabic}% 
       setcounter{page}{1}% 
    } 
}{} 
@ifundefined{backmatter}{% 
%    newifif@backmatter@backmattertrue 
    newcommand{backmatter}{ %
       %In Roemischen Buchstaben nummerieren (i, ii, iii) 
       pagenumbering{roman} 
    } 
}{} 
makeatother 

begin{document}
{sffamily
%maketitle
begin{titlepage}
   begin{center}
       vspace*{2cm}

begin{Huge}
{xx}
end{Huge}
       
       vspace{0.5cm}
        {Large xx}
            
       vspace{1cm}
textbf{xx}
        vspace{1cm}
zxx 

        vspace{1cm}
textbf{xx}

        vspace{0.5cm}
textbf{(xx)}
        
vspace{1cm}
          
                             
       von xx
       vspace{0.5cm}
xx 
        vspace{0.5cm}
xx
        vspace{0.5cm}
xx
        vspace{1.5cm}
xx: 
        vspace{0.5cm}
xx
        vspace{0.5cm}
xx
        vspace{1cm}
xx     
%       includegraphics[width=0.4textwidth]{university}
           
            
   end{center}
end{titlepage}
thispagestyle{empty}
frontmatter 
pagestyle{plain}

chapter{Vorwort}
chapter{Danksagung}
xx
chapter{Abstrakt}
xx
chapter{Abstract}
xx
chapter{Abkürzungsverzeichnis}
begin{acronym}[SEPSEP]
acro{abb}[Abb.]{Abbildung}
end{acronym}
newpage
tableofcontents
listoffigures
listoftables
mainmatter
pagestyle{fancy}
chapter{Einleitung}
xx

chapter{ssssssssss}
xx

chapter{eeeeeeeeee}
cccc

backmatter

appendix
chapter{Anhang}
ffffffffff
}

end{document}

2 Answers

Chapters in backmatter are unnumbered. So, I think they should be unnumbered in the page header, too. Therefore I would use if@mainmatter in a global redefinition of chaptermark:

makeatletter
renewcommand{chaptermark}[1]{markboth{if@mainmatterthechapter{} fi#1}{}}
makeatother

Example:

documentclass[12pt]{book}
usepackage[a4paper,width=150mm, top=25mm, bottom=25mm, bindingoffset=6mm]{geometry}
usepackage[ngerman]{babel}
usepackage[T1]{fontenc}
usepackage{fontspec}
usepackage{}
usepackage{fancyhdr}
setlength{headheight}{15pt}
pagestyle{fancy}% before the redefinition of chaptermark

makeatletter
renewcommand{chaptermark}[1]{markboth{if@mainmatterthechapter{} fi#1}{}}
makeatother

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

renewcommand*{familydefault}{sfdefault}

usepackage{lipsum}% nur für Fülltext
begin{document}
begin{titlepage}
begin{center}
  Huge Titelseitepar
  normalsize
  Weiterer Text auf der Titelseite
end{center}
end{titlepage}
thispagestyle{empty}
frontmatter 
chapter{Vorwort}
  lipsum[1]
chapter{Danksagung}
  lipsum[2]
chapter{Abstrakt}
  lipsum[3]
chapter{Abstract}
  lipsum
tableofcontents
listoffigures
listoftables
mainmatter

chapter{Einleitung}
lipsum
chapter{ssssssssss}
lipsum
chapter{eeeeeeeeee}
lipsum
backmatter
appendix
chapter{Anhang}
lipsum
end{document}

Note, that first page of each chapter uses pagestyle plain. If they should get the same layout, define fancypagestyle plain in the preamble:

fancypagestyle{plain}{%
  fancyhf{}%
  fancyhead[LE,RO]{thepage}%
  fancyhead[LO,RE]{nouppercase{leftmark}}%
}

or

fancypagestyle{plain}{}% change plain pagestyle to fancy

Correct answer by esdd on May 15, 2021

Since I had to create an MWE anyway, here it is.

documentclass{book}
usepackage{etoolbox}
usepackage{lipsum}

usepackage{fancyhdr}
pagestyle{fancy}
fancypagestyle{main}{%
fancyhf{}
%fancyhead[EL,OR]{textsl{rightmark}}
%fancyhead[OL,ER]{textsl{rightmark}}
fancyhead[EL]{thepage} 
fancyhead[ER]{leftmark}
fancyhead[OL]{leftmark}
fancyhead[OR]{thepage}
defchaptermark##1{markboth{thechapter{} ##1}{thechapter{}##1}}%
}
fancypagestyle{plain}{%
fancyhf{}
fancyhead[L]{nouppercase{rightmark}}
fancyhead[R]{nouppercase{leftmark}}
fancyhead[EL]{thepage} 
fancyhead[OR]{thepage}
renewcommand{chaptermark}[1]{%
    markboth{##1}{##1}}
}
fancypagestyle{back}{%
fancyhf{}
fancyhead[EL]{thepage} 
fancyhead[ER]{leftmark}
fancyhead[OL]{leftmark}
fancyhead[OR]{thepage}
renewcommand{chaptermark}[1]{
    markboth{thechapter{} ##1}{thechapter{}##1}}
}
apptofrontmatter{pagestyle{plain}}
apptomainmatter{pagestyle{main}}
apptobackmatter{pagestyle{back}}

begin{document}
frontmatter
chapter{chapter}
section{section}
lipsum[1-10]
mainmatter
chapter{chapter}
section{section}
lipsum[1-10]
backmatter
chapter{chapter}
section{section}
lipsum[1-10]
end{document}

Answered by John Kormylo on May 15, 2021

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