0

I am writing my thesis report and I want to keep the section names in the header of the pages. Right now, the page number is at the center in the footer and I want to keep it in the same way. I have tried using fancyhdr for keeping the section name, but the problem is, it will show the header for content and list of figures also, which are in \frontmatter. Any suggestions would be helpful.

%\documentclass[12pt,a4paper]{article}
\documentclass[12pt,a4paper]{article}   
\setcounter{tocdepth}{3}
\setcounter{secnumdepth}{3}

%function definition for starting intro from first section.
\def\frontmatter{%
    \pagenumbering{roman}
    \setcounter{page}{1}
    \renewcommand{\thesection}{\Roman{section}}
}%

\def\mainmatter{%
    \pagenumbering{arabic}
    \setcounter{page}{1}
    \setcounter{section}{0}
    \renewcommand{\thesection}{\arabic{section}}
}%
\usepackage{fontspec}
%\setmainfont{Times New Roman}

\usepackage{makecell}
\usepackage{graphicx}

The expected would seem something like this: enter image description here

2
  • You could use \pagestyle={plain} for the frontmatter pages, and \pagestyle={fancy} when the mainmatter start
    – Tom
    May 7, 2022 at 15:17
  • \pagestyle works actually. Thank you! @Tom May 9, 2022 at 7:53

2 Answers 2

1

You could use fancyhdr and define your own pagestyles and use \pagestyle{...} at the end of the definitions for \mainmatter and \frontmatter.
Setup of pagestyles

% Page Layout
\setlength{\headheight}{15pt} % otherwise fancyhdr gives warning
\fancyhf{}

\fancypagestyle{frontmatter}{
    \fancyhf{} % clearing headers and footers
    \fancyfoot[C]{\thepage} % page number centered in the footer
    \renewcommand{\headrulewidth}{0pt} % Create an invisible header ruler line
}

\fancypagestyle{mainmatter}{
    \fancyhf{}
    \fancyhead[C]{\leftmark} % \leftmark gives current section with its number you may want to use \nouppercase
    \fancyfoot[C]{\thepage}
    \renewcommand{\headrulewidth}{0.3pt} % Create an header ruler line
}
1

Base on your requirements, this can be achieved without the fancyhdr package. Here is a the example code:

\documentclass[12pt,a4paper]{article}   
\setcounter{tocdepth}{3}
\setcounter{secnumdepth}{3}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% pagestyle `headings` setting %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatletter
\def\ps@headings{%
    \def\@oddfoot{\hfil\thepage\hfil}
    \def\@oddhead{\hfil\slshape\rightmark\hfil}%
    \def\@evenhead{\hfil\slshape\rightmark\hfil}%
    \let\@mkboth\markboth
    \def\sectionmark##1{%
      \markright {%
        \ifnum \c@secnumdepth >\m@ne
          \thesection\quad
        \fi
        ##1}}}
\makeatother

%%%%%%%%%%%%%%%%%%
% other packages %
%%%%%%%%%%%%%%%%%%
\usepackage{fontspec}
%\setmainfont{Times New Roman}
\usepackage{makecell}
\usepackage{graphicx}
\usepackage{lipsum}
\usepackage{mwe}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
%%%%%%%%%%%%%%
% title page %
%%%%%%%%%%%%%%
\thispagestyle{empty}
\null\vfil
\begin{center}
\textbf{My Title}
\vskip 3em
\textbf{My Name}
\end{center}
\vfil\null
\clearpage

%%%%%%%%%%%%%%%
% frontmatter %
%%%%%%%%%%%%%%%
\pagenumbering{roman}
\tableofcontents
\clearpage

\listoffigures
\clearpage

\listoftables
\clearpage

%%%%%%%%%%%%%%
% mainmatter %
%%%%%%%%%%%%%%
\pagenumbering{arabic}
\pagestyle{headings}
\section{Introduction}
\lipsum[1]
\clearpage

\section{Analysis}
\lipsum[2]
\begin{figure}[ht]
    \centering
    \includegraphics{example-image-a}
    \caption{figure 1}
    \label{fig:my_label}
\end{figure}
\clearpage

\section{Results}
\begin{table}[ht]
    \centering
    \begin{tabular}{c|c}
         1&2  \\
         3&4 
    \end{tabular}
    \caption{table 1}
    \label{tab:my_label}
\end{table}
\lipsum[3]

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .