# Top-right margin displaying “Contents” instead of section title

I have a class MyReportClass where I define what has to be displayed in the margins of all pages of my document. In particular I would like the current section to appear in the top-right margin of each page. However, from the table of contents onwards I get "Contents" instead of the section number and title. When the references section is created through the \bibliographycommand, the top-right margin displays "References" and keeps that also in the appendixes that I have at the end of the document.

Is there something wrong in the commands of my class?

Below a MWE for my case.

\documentclass{MyReportClass}

\begin{document}

\reporttitle{My Title}
\coursecode{1234}
\coursename{Course Name}
\instructorname{Instructor Name}
\reportauthor{Name}{Surname}{1234}
\frontcoverpic{example-image-b}

\initialise

\section{Section 1}
\newpage\section{Section 2}

\end{document}


\makemarings command in MyReportClass:

\newcommand{\makemargins}{
\makenomenclature
\pagestyle{fancy}
\lhead{\includegraphics[width=0.15\textwidth]{./logos/logo.pdf}} % Set image at top of the page
\rfoot{\thepage}
\cfoot{\textbf{\reporttitle}}
\lfoot{\coursecode}
}


\initialize command in MyReportClass:

\newcommand{\initialise}{
\makemargins
\makecoverpage
\tableofcontents
\newpage
}


For reference, below is the entire MyReportClass.

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{MyReportClass}
\LoadClass[a4paper,12pt]{article} % Class of the document

%--------------------- Packages ------------------------

\RequirePackage[english]{babel} % Langueage of the document
\RequirePackage[utf8]{inputenc} % Special characters
\RequirePackage[section]{placeins} % For format of sections
\RequirePackage[T1]{fontenc} % Letters not included in UTF-8
\RequirePackage{mathtools} % Equations and mathematic symbols
\RequirePackage{siunitx} % Scientific notation
\RequirePackage{float} % Images formatting
\RequirePackage{graphicx} % Insert figures
\RequirePackage{tikz}
\RequirePackage[justification=centering]{caption} % Centred legends
\RequirePackage{subcaption}
\RequirePackage{wallpaper}
\RequirePackage{nomencl}
\RequirePackage{fancyhdr}
\RequirePackage{url}
\RequirePackage[pdfencoding=auto,psdextra,hidelinks]{hyperref} % Legends in sub-figures
\RequirePackage{amsmath,bm,amssymb}
\RequirePackage{amsfonts}
\RequirePackage{bookmark} % Faster updated bookmarks
\RequirePackage{textcomp}

% Package for nice tables
\RequirePackage{booktabs,makecell}
\sisetup{
%  output-decimal-marker = {,},
detect-weight,
mode = text
}
\newcommand*{\specialcell}[2][b]{%
\begin{tabular}[#1]{@{}c@{}}#2\end{tabular}%
}
\newcommand*{\specialcellbold}[2][b]{%
\bfseries
\sisetup{text-rm = \bfseries}%
\begin{tabular}[#1]{@{}c@{}}#2\end{tabular}%
}
\newcommand*{\leftspecialcell}[2][b]{%
\begin{tabular}[#1]{@{}l@{}}#2\end{tabular}%
}

%Package for labelling matrix rows and columns
\RequirePackage{blkarray}

% Package to adjust labelled matrices
\RequirePackage{makebox,bigstrut,relsize}

% Page configuration
\RequirePackage{geometry}
\geometry{a4paper,hscale=0.75,vscale=0.8}

%Package to modify chapter header
%\RequirePackage{titlesec}
%Color for sections head
\RequirePackage{color}
\definecolor{tdblue}{RGB}{0,174,239}
\RequirePackage{sectsty}
\sectionfont{\color{tdblue}}
\subsectionfont{\color{tdblue}}
\subsubsectionfont{\color{tdblue}}
\paragraphfont{\color{tdblue}}

%MATLAB command
\RequirePackage{xspace}
\newcommand{\MATLAB}{\textsc{Matlab}\xspace}

%Typesetting a MATLAB code
\RequirePackage[T1]{fontenc}
\RequirePackage{bigfoot} % to allow verbatim in footnote
\RequirePackage[numbered,framed]{matlab-prettifier}
\RequirePackage{filecontents}
\let\ph\mlplaceholder % shorter macro
\lstMakeShortInline"
\lstset{
style              = Matlab-editor,
basicstyle         = \mlttfamily,
escapechar         = ",
mlshowsectionrules = true,
}

% Per-section figure, equation, table and lstlisting numbering
\RequirePackage{chngcntr}
\counterwithin{figure}{section}
\counterwithin{equation}{section}
\counterwithin{table}{section}
\AtBeginDocument{\counterwithin{lstlisting}{section}}

% Package for references with numbers
\bibliographystyle{apsrev4-1}
\RequirePackage[numbers]{natbib}
\setcitestyle{numbers}

% Set graphics path
\graphicspath{{./figures/}}

%-------------- Define command for missing figures ------------
\let\StandardIncludeGraphics\includegraphics%
\renewcommand{\includegraphics}[2][]{%
\IfFileExists{./figures/#2}{%
\StandardIncludeGraphics[#1]{#2}%
}{%
\IfFileExists{#2}{%
\StandardIncludeGraphics[#1]{#2}%
}{%
\StandardIncludeGraphics[#1]{example-image-a}}}%
}% End of command

%-------------------- Report information ----------------------


\newcommand{\makemargins}{
\makenomenclature
\pagestyle{fancy}
\lhead{\includegraphics[width=0.25\textwidth]{./logos/logo.pdf}} % Set image at top of the page
\rfoot{\thepage}
\cfoot{\textbf{\reporttitle}}
\lfoot{\coursecode}
}

\newcommand{\makecoverpage}{
\begin{titlepage}

\centering % Center the content
\includegraphics[width=0.4\textwidth]{./logos/logo_blue.pdf}\par\vspace{1cm} % Insert logo
{\scshape\LARGE Delft University of Technology \par} % Name of the university
\vspace{1cm} % Vertical space of 1.5 cm
{\scshape\Large \coursename \\ \large \coursecode \\ \normalsize \instructorname \par}
\vspace{0.5cm} % Vertical space of 1 cm
\rule{\linewidth}{0.2 mm} \\[0.4 cm]
{\huge\bfseries \reporttitle \par} \
\rule{\linewidth}{0.2 mm} \\[1 cm]
\vspace{0.5cm}
\frontcoverpic\\[1.5 cm]
\normalsize \reportauthor\\
\vfill
{\large \today\par} % Visualization of the date

\end{titlepage}
}

\newcommand{\initialise}{
\makemargins
\makecoverpage
\tableofcontents
\newpage
}

% Define newcommand for norm
\newcommand{\norm}[1]{\left\lVert#1\right\rVert}

• \clearpage\pagestyle{fancy} – user31729 Feb 11 '19 at 16:09
• @ChristianHupfer I have substituted \pagestyle{fancy} in the \makemargins command with \clearpage\pagestyle{fancy}, but nothing changes. Do I need that line somewhere else? – fma Feb 11 '19 at 16:14
• You have a very messy (sorry!) .cls file and and there might be many possibilities that cause your issue. At first glance I cannot detect a starting point other than to debug the .cls file. – user31729 Feb 11 '19 at 16:25
• What would you have it display instead of 'Contents' - '0 Section 0'? – Pippip19 Feb 11 '19 at 16:42
• @ChristianHupfer I agree that my .cls is quite messy, sorry for that. Maybe I can try commenting out some parts of it and seeing what changes. – fma Feb 11 '19 at 16:51

After some debugging with the OP it I have found that using the 'lower-level' \rightmark command (see section 12 of the fancyhdr docs) gets things working. If you want to edit exactly how the section names appear, you can edit the \sectionmark command (see code).

\newcommand{\makemargins}{
\makenomenclature
\pagestyle{fancy}