0

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.

enter image description here

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:

enter image description here

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
5
  • Just like in your previous question: remove the pagestyles option from titlesec: \usepackage{titlesec}. This removes the conflict with fancyhdr and it brings the Preface 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`. Commented Dec 13, 2021 at 11:16
  • @PietervanOostrum Thank you for your reply once again! Unfortunately, removing pagestyles from the package so it only says: \usepackage{titlesec} , does not add the preface to the ToC. Also, i am not sure where it says \thetitle in my MWE. Should have been changed to \mytitle
    – Buster3650
    Commented Dec 13, 2021 at 11:22
  • Does your include file set the label \label{sec:preface}? I'll give you a working example in the answer. Commented Dec 13, 2021 at 11:30
  • Yes, it is included, but just found out that it might be due to that it is says: \section*{Preface} \label{sec:preface} and the * should ensure that there is no number included in the section, but instead it is the reason for not appearing in the ToC
    – Buster3650
    Commented Dec 13, 2021 at 11:34
  • @PietervanOostrum i updated the question with the preface file
    – Buster3650
    Commented Dec 13, 2021 at 11:35

1 Answer 1

1

Update:

This appears to be a conflict between the packages hyperref, nameref (loaded by hyperref) and titlesec (see https://tex.stackexchange.com/a/397032/113546). So I used the solution given there to resolve the conflict.

\makeatletter
\newcommand*{\starsection}[1]{%
  \section*{#1}%
  \NR@gettitle{#1}%
}
\makeatother

and then use \starsection instead of \section*

  • Removed the pagestyles option
  • Replaced the \input{sections/0_Preface} with
\chapter*{Preface}
\label{sec:preface}
  • renamed \thetitle to \TheTitle.
  • removed the redefinitions of the footer fields.
  • And, by the way, this problem would have disappeared if you had written Preface instead of \nameref{sec:preface} in \addcontentsline. You can still make it easier by adding the \addcontentsline call to the \starsection macro and using #1 there instead of the \nameref{sec:preface}. See the second solution below.
\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{titlesec}
\titleformat{\section}{\bfseries\LARGE}{\hspace{1ex}\Huge\thesection}{2ex}{} %<--- changes font size of sections

\usepackage{fancyhdr}
\usepackage{color}
\usepackage{hyperref}
\usepackage{lastpage,refcount,atbegshi}
\usepackage{lipsum}

\title{Header}   % Title
\author {Ahmad Ahmadsen} % Author
\date{\today} % Date

\makeatletter
\newcommand*{\starsection}[1]{%
  \section*{#1}%
  \NR@gettitle{#1}%
}
\let\TheTitle\@title
\let\TheAuthor\@author
\let\TheDate\@date
\makeatother

\pagestyle{fancy} 
\fancyhf{} 
\lhead{\TheTitle} 

\rfoot{\thepage \space} 
\lfoot{University} 

\begin{document}

\pagenumbering{Roman} 

%\input{sections/0_Preface}
\starsection{Preface} 
\label{sec: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}

Version with \contentsline added to \starsection macro:

\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{titlesec}
\titleformat{\section}{\bfseries\LARGE}{\hspace{1ex}\Huge\thesection}{2ex}{} %<--- changes font size of sections

\usepackage{fancyhdr}
\usepackage{color}
\usepackage{hyperref}
\usepackage{lastpage,refcount,atbegshi}
\usepackage{lipsum}

\title{Header}   % Title
\author {Ahmad Ahmadsen} % Author
\date{\today} % Date

\makeatletter
\newcommand*{\starsection}[1]{%
  \section*{#1}%
  \NR@gettitle{#1}%
  \addcontentsline{toc}{section}{\hspace*{2.2em}#1}%
}
\let\TheTitle\@title
\let\TheAuthor\@author
\let\TheDate\@date
\makeatother

\pagestyle{fancy} 
\fancyhf{} 
\lhead{\TheTitle} 

\rfoot{\thepage \space} 
\lfoot{University} 

\begin{document}

\pagenumbering{Roman} 

%\input{sections/0_Preface}
\starsection{Preface} 
\label{sec:preface}

% \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}

enter image description here

4
  • That works like a charm. Thank you very much!
    – Buster3650
    Commented Dec 13, 2021 at 11:45
  • although one last thing is bugging, and that is the \rfoot{\thepage \space} and \lfoot{University}. After changing preface from section to chapter, it appeared on the ToC, but removed the rfoot and lfoot , and instead added the roman number to the center , how do i fix this?
    – Buster3650
    Commented Dec 13, 2021 at 11:48
  • See updated solution nr. 2 Commented Dec 13, 2021 at 13:38
  • You have also definitions of \lfoot, \cfoot and \rfoot later in the document. These override the earlier ones. Commented Dec 13, 2021 at 14:37

You must log in to answer this question.

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