We want the section Preface to appear on the TOC, but as can be seen on the following screenshot, the section name does not appear to be visible, although the dotted line is appearing with page number as well.
We are using the following lines, when we try to make the section appear in the ToC:
\input{sections/0_Preface}
\addcontentsline{toc}{section}{\hspace*{2.2em}\nameref{sec:preface}} % ---- ADDS PREFACE TO THE TOC
What may cause this is that we are using both titlesec and fancyhdr together, although they are in conflict with each other. If we remove the titlesec package, the section name suddenly appears in the TOC:
How do we make the section name appear in the TOC when still using titlesec and fancyhdr? We need fancyhdr for header, footer etc. and titlesec for changing font size of sections.
MWE
\documentclass[11pt, report]{report}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\graphicspath{ {./figures/} }
\usepackage{lastpage}
\usepackage{array}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{float}
\usepackage[pagestyles]{titlesec} % <------ This here
\titleformat{\section}{\bfseries\LARGE}{\hspace{1ex}\Huge\thesection}{2ex}{} %<--- changes font size of sections
\usepackage{fancyhdr} %<---- Before that there
\usepackage{color}
\usepackage{hyperref}
\usepackage{lastpage,refcount,atbegshi}
\usepackage{lipsum}
\title{Header} % Title
\author {Ahmad Ahmadsen} % Author
\date{\today} % Date
\makeatletter
\let\mytitle\@title
\let\theauthor\@author
\let\thedate\@date
\makeatother
\pagestyle{fancy}
\fancyhf{}
\lhead{\mytitle}
\rfoot{\thepage \space}
\lfoot{University}
\begin{document}
\pagenumbering{Roman}
\input{sections/0_Preface}
\addcontentsline{toc}{section}{\hspace*{2.2em}\nameref{sec:preface}} % ---- ADDS PREFACE TO THE TOC
\cfoot{\thepage \space of \pageref{LastPage}}
\rfoot{}
\lfoot{}
\renewcommand \thesection{\arabic{section}}
\pagenumbering{gobble}
\pagenumbering{arabic}
\vspace*{-20em}\tableofcontents\numberline{}
%\tableofcontents
\thispagestyle{empty}
\clearpage
\setcounter{page}{1}
\setcounter{tocdepth}{3}
\newpage
% Add sections here!
\section{Introduction}
\subsection{Motivation}
\subsection{Research Question}
\newpage
\section{Analysis}
\subsection{analys 1}
\subsection{analys 2}
\subsection{analys 3}
\end{document}
0_Preface File:
\section*{Preface} \label{sec:preface}
\clearpage
pagestyles
option fromtitlesec
:\usepackage{titlesec}
. This removes the conflict withfancyhdr
and it brings thePreface
back to the ToC. The option doesn't serve anything in your document and only makes thing bad. And the other thing is to rename\thetitle
to something else, e.g. \TheTitle`.\label{sec:preface}
? I'll give you a working example in the answer.