[Tex/LaTex] Writing chapter-name and section-name in header for report class

fancyhdrheader-footerreport

I need to write a LaTeX document that should have the following page specification:

  • Text Height: 240 mm & Text Width: 160 mm

  • Header with top Margin: 30 mm & Footer with Bottom margin: 27mm

  • Left Margin: 30 mm & Right Margin: 25 mm

Accordingly, I wrote the code which goes like this:

\documentclass[12pt]{report}
\usepackage[english]{babel}
\usepackage[breaklinks=true]{hyperref}
\usepackage{natbib,graphicx,mathtools,color,cleveref,xfrac,subcaption,multirow,multicol,mathptmx,lipsum}
\usepackage[shortlabels]{enumitem}
\usepackage{floatpag}\floatpagestyle{empty}
\crefname{equation}{Eq.}{Eqs.}
\crefname{figure}{Fig.}{Figs.}
\crefname{section}{Sect.}{Sects.}
\crefname{subsection}{Sect.}{Sects.}
\crefname{table}{Tab.}{Tabs.}
\linespread{1.5}

\usepackage{fancyhdr}
\pagestyle{fancy}

%\usepackage{fancyhdr}
%\pagestyle{fancy}
%\fancyhf{}
%\fancyhead[RO,RE]{\itshape \nouppercase Chapter \arabic{chapter} \itshape \nouppercase Section \arabic{section}}
%\rhead{\chaptername \thechapter \thesection}

\usepackage[textheight=280mm,textwidth=160mm,top=30mm,bottom=27mm,left=30mm,right=20mm]{geometry} % To set the page layout
\setlength{\parskip}{2.5pt}
\setlength{\parindent}{12mm}
\begin{document}
    \begin{titlepage}
    \begin{center}
        \vspace*{1cm}       
        \textbf{Thesis Title}
        \vspace{0.5cm}
        Thesis Subtitle
        \vspace{1.5cm}
        \textbf{Author Name}
        \vfill
        A thesis presented for the degree of Doctor of Philosophy
        \vspace{0.8cm}
        Department Name\\
        University Name\\
        Country\\
        Date
    \end{center}
    \end{titlepage}
    \addcontentsline{toc}{section}{\numberline{}Acknowledgement}
    \section*{Acknowledgement}  
    \lipsum[1-5]
    \tableofcontents
    \chapter{111111111} 
    \lipsum[1-5]
    \section{sec111111111-1}    
    \lipsum[1-20]
    \section{sec111111111-2}    
    \lipsum[1-20]   
    \chapter{222222222} 
    \lipsum[1-5]
    \section{sec222222222-1}    
    \lipsum[1-20]
    \section{sec222222222-2}    
    \lipsum[1-20]   
    \chapter{333333333} 
    \lipsum[1-5]
    \section{sec333333333-1}    
    \lipsum[1-20]
    \section{sec333333333-2}    
    \lipsum[1-20]   
    \chapter{444444444} 
    \lipsum[1-5]
    \section{sec444444444-1}    
    \lipsum[1-20]
    \section{sec444444444-2}    
    \lipsum[1-20]   
    \chapter{555555555} 
    \lipsum[1-5]
    \section{sec555555555-1}    
    \lipsum[1-20]
    \section{sec555555555-2}    
    \lipsum[1-20]   
    \chapter{666666666} 
    \lipsum[1-5]
    \section{sec666666666-1}    
    \lipsum[1-20]
    \section{sec666666666-2}    
    \lipsum[1-20]   
    \chapter{777777777} 
    \lipsum[1-5]
    \section{sec777777777-1}    
    \lipsum[1-20]
    \section{sec777777777-2}    
    \lipsum[1-20]
\end{document}
  1. I tried to vary Text Height and Text Width check whether any changes is occurring or not. But I could not find any. E.g. I changed text-height 180, 240 and 280 also I varied text-width to 120, 160 and 200. Is its because I have given header/footer size and left/right margin?

  2. When I am using: \usepackage{fancyhdr}, \pagestyle{fancy}, what I could see in the header is this:

q2

But what I would like to see is only like this: Chapter 1, Section 1.2 and that too in right side of header? How can I get this with the usage of \usepackage{fancyhdr}, \pagestyle{fancy}?

  1. When remove the above code and then introduce the code:

    \usepackage{fancyhdr}
    \pagestyle{fancy}
    \fancyhf{}
    \fancyhead[RO,RE]{\itshape \nouppercase Chapter \arabic{chapter} \itshape \nouppercase Section \arabic{section}}
    \rhead{\chaptername \thechapter \thesection}
    

the header get's modified to q3

Please note that there is no space between Chapter and number 1. How to add space in between them?

  1. Furthermore, this comes in acknowledgement page and table of contents also.

q4-1

q4-2

How to get rid of this thing?

  1. Additionally, I have added this line thinking that section number will appear. It appeared, but not I the fashion that I require (kindly see question 2), which also found it’s way into acknowledgments and table of contents:

    \usepackage{fancyhdr}
    \pagestyle{fancy}
    \fancyhf{}
    \rhead{\chaptername \thechapter \thesection}
    

q5

  1. The Chapter number and Section number (e.g., Chapter 2, Section 3) is to be printed on even numbered page headers and Section title on the odd numbered page header. How do we accomplish it? I tried with code:

    \usepackage{fancyhdr}
    \pagestyle{fancy}
    \fancyhf{}
    \fancyhead[RO,RE]{\itshape \nouppercase Chapter \arabic{chapter} \itshape \nouppercase Section \arabic{section}}
    

But this does not help me. Any help in this regard will be highly appreciated.

Best Answer

In order to get fancyhdr to differentiate between even and odd pages you need to specify a [twoside] document (default for report is oneside). Note that this also changes the margins. Alternatively you could do your own \ifodd\value{page} test.

\documentclass[12pt,twoside]{report}
\usepackage[english]{babel}
\usepackage[breaklinks=true]{hyperref}
\usepackage{natbib,graphicx,mathtools,color,cleveref,xfrac,subcaption,multirow,multicol,mathptmx,lipsum}
\usepackage[shortlabels]{enumitem}
\usepackage{floatpag}\floatpagestyle{empty}
\crefname{equation}{Eq.}{Eqs.}
\crefname{figure}{Fig.}{Figs.}
\crefname{section}{Sect.}{Sects.}
\crefname{subsection}{Sect.}{Sects.}
\crefname{table}{Tab.}{Tabs.}
\linespread{1.5}

\newcommand{\sectiontitle}{}% reserve global name

\usepackage{fancyhdr}
\fancyhead{}% clear default headers
\fancyhead[RE]{\ifnum\value{section}=0\relax
  \else \chaptername~\thechapter~Section~\thesection% No \sectionname defined
  \fi}
\fancyhead[LO]{\ifnum\value{section}=0\relax
  \else \sectiontitle
  \fi}
\pagestyle{fancy}

\makeatletter% modify section to record title
\let\oldsection=\section
\def\section{\@ifnextchar*{\oldsection}{\@section}}% handle \section*
\newcommand{\@section}[2][\empty]{% optional short title
  \ifx\empty#1\relax
    \def\sectiontitle{#2}%
    \oldsection{#2}%
  \else
    \def\sectiontitle{#1}%
    \oldsection[#1]{#2}%
  \fi}
\makeatother

\usepackage[textheight=280mm,textwidth=160mm,top=30mm,bottom=27mm,left=30mm,right=20mm]{geometry} % To set the page layout
\setlength{\parskip}{2.5pt}
\setlength{\parindent}{12mm}
\begin{document}
    \begin{titlepage}
    \begin{center}
        \vspace*{1cm}       
        \textbf{Thesis Title}
        \vspace{0.5cm}
        Thesis Subtitle
        \vspace{1.5cm}
        \textbf{Author Name}
        \vfill
        A thesis presented for the degree of Doctor of Philosophy
        \vspace{0.8cm}
        Department Name\\
        University Name\\
        Country\\
        Date
    \end{center}
    \end{titlepage}
    \addcontentsline{toc}{section}{\numberline{}Acknowledgement}
    \section*{Acknowledgement}  
    \lipsum[1-5]
    \tableofcontents
    \chapter{111111111}
    \lipsum[1-5]
    \section{sec111111111-1}   
    \lipsum[1-20]
    \section{sec111111111-2}    
    \lipsum[1-20]   
    \chapter{222222222} 
    \lipsum[1-5]
    \section{sec222222222-1}    
    \lipsum[1-20]
    \section{sec222222222-2}    
    \lipsum[1-20]   
    \chapter{333333333} 
    \lipsum[1-5]
    \section{sec333333333-1}    
    \lipsum[1-20]
    \section{sec333333333-2}    
    \lipsum[1-20]   
    \chapter{444444444} 
    \lipsum[1-5]
    \section{sec444444444-1}    
    \lipsum[1-20]
    \section{sec444444444-2}    
    \lipsum[1-20]   
    \chapter{555555555} 
    \lipsum[1-5]
    \section{sec555555555-1}    
    \lipsum[1-20]
    \section{sec555555555-2}    
    \lipsum[1-20]   
    \chapter{666666666} 
    \lipsum[1-5]
    \section{sec666666666-1}    
    \lipsum[1-20]
    \section{sec666666666-2}    
    \lipsum[1-20]   
    \chapter{777777777} 
    \lipsum[1-5]
    \section{sec777777777-1}    
    \lipsum[1-20]
    \section{sec777777777-2}    
    \lipsum[1-20]
\end{document}