Report class formatting TOC. newbie questions

formattingreporttable of contents

I am creating a template for use in Report document class with pdfLaTeX compiler in Overleaf.

There are a lot of formatting details, but I think I have them all minimally reproduced in the code below.

I have five questions on formatting the table of contents; think they are all related, so I grouped them in the same post:

  1. How can I center, capitalize the word "Contents", and specify the font size? I am currently doing \renewcommand{\contentsname}{\centering\fontsize{18pt}{0pt}\MakeUppercase{\textbf{contents}}}, which does not apply the centering and is bad practice based on the answers here and here. I understand why this is bad practice, but I don't understand the correct way to do it. (I intend to apply this to have the same formatting for the "List of Figures" header.)
  2. The TOC currently makes most parts bold, which I like. How can I also make sections (but not subsections) bold in the TOC?
  3. How can I make chapter titles and sections (but not subsections) in all caps?
  4. How can I add page number dots to Abstract, List of Figures, etc. on the TOC, that are in the same format (spacing) as the other dots? Can I make all the dots denser? (to meet Word template requirements)
  5. Using \addcontentsline{toc}{chapter}{\MakeUppercase{Table of Contents}} works fine in this stand-alone example, but for some reason it throws an error in my real template. It compiles correctly, but I want to get rid of the error. I can't find any difference that would cause it. Error: Undefined control sequence. The compiler is having trouble understanding a command you have used. Check that the command is spelled correctly. If the command is part of a package, make sure you have included the package in your preamble using \usepackage{...}. Learn more **\MakeUppercase ...ppercaseUnsupportedInPdfStrings** The control sequence at the end of the top line of your error message was never \def'ed. If you have misspelled it (e.g., \hobx'), type I' and the correct spelling (e.g., I\hbox'). Otherwise just continue, and I'll forget about whatever was undefined. Based on the answer here, I think I need to use \texorpdfstring{}, but I tried some combinations with that and they didn't compile.
%-------------------------------------------------------
% Dummy Chapter 1
\begin{filecontents*}{ChapterA.tex}
\chapter{Introduction}
\lipsum[1]
\section{Section Name}
Here is some text, see Figure \ref{fig:a}.
\begin{figure}
    \centering
    \includegraphics[width = 0.5\textwidth]{example-image-a}
    \caption{An example figure.} 
    \label{fig:a}
\end{figure}
\subsection{Subsection Name}
Here is a subsection, see \cite{einstein1906new}.
\end{filecontents*}

%-------------------------------------------------------
% Dummy Abstract
\begin{filecontents*}{Abstract.tex}
\begin{center}
    {\fontsize{18pt}{0pt}\MakeUppercase{\textbf{abstract}}}
\vspace*{24pt}
\end{center}
\addcontentsline{toc}{chapter}{\MakeUppercase{Abstract}}

This is the abstract.
\end{filecontents*}

%-------------------------------------------------------
% Dummy References
\begin{filecontents*}{refs.bib}
@article{einstein1906new,
  title={A new determination of molecular dimensions},
  author={Einstein, Albert},
  journal={Ann. Phys.},
  volume={19},
  pages={289--306},
  year={1906}
}
\end{filecontents*}


%-------------------------------------------------------
% Preamble
\documentclass[12pt]{report}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{natbib}
\usepackage{lipsum}

% set page and paragraph dimensions
\setlength{\parindent}{0.5in}
\setlength{\parskip}{6pt}

% set report font
\usepackage[sfdefault]{carlito}

% format headers
\usepackage{titlesec}

\titleformat{\chapter}%
    {\normalfont\fontsize{22pt}{0pt}\bfseries}%
    {\thechapter}{0.5in}{}
\titlespacing*{\chapter}{0pt}{80pt}{40pt}

\titleformat{\section}%
    {\normalfont\fontsize{14pt}{0pt}\bfseries}%
    {\thesection}{0.5in}{\MakeUppercase}
\titlespacing*{\section}{0pt}{24pt}{12pt}

\titleformat{\subsection}%
    {\normalfont\fontsize{14pt}{0pt}\bfseries}%
    {\thesubsection}{0.5in}{}
\titlespacing*{\subsection}{0pt}{18pt}{12pt}

% set-up frontmatter, mainmatter, backmatter
\makeatletter
\newcommand\frontmatter{
    \cleardoublepage
    \pagenumbering{roman}}
\newcommand\mainmatter{
    \cleardoublepage
    \pagenumbering{arabic}}
\newcommand\backmatter{
    \if@openright
        \cleardoublepage
    \else
        \clearpage
    \fi
}
\makeatother

% format references fonts, etc.
\AtBeginEnvironment{thebibliography}{%
  \titleformat{\chapter}%
    {\normalfont\fontsize{14pt}{0pt}\bfseries\filcenter}%
    {\thechapter}{0.5in}{\MakeUppercase}%
    \titlespacing*{\chapter}{0pt}{0pt}{12pt}
}
\renewcommand{\bibname}{References}
\AddToHook{cmd/bibsection/after}{%
  \addcontentsline{toc}{chapter}{\bibname}%
}

\def\bibfont{\fontsize{11pt}{0pt}\selectfont\hyphenpenalty=10000}

% format TOC
\renewcommand{\contentsname}{%
    \centering\fontsize{18pt}{0pt}%
    \MakeUppercase{\textbf{contents}}}

%-------------------------------------------------------
% Begin

\begin{document}

\title{test}
\author{none}
\date{\today}

\frontmatter
\include{Title}
\include{Abstract}
\tableofcontents
\addcontentsline{toc}{chapter}{Contents}
\listoffigures
\addcontentsline{toc}{chapter}{List of Figures}

\mainmatter
\include{ChapterA}

\backmatter
\bibliographystyle{abbrvnat}
\bibliography{refs}

\end{document}
%-------------------------------------------------------

enter image description here

Best Answer

  1. The ToC is just like any other \chapter*. So if you want the formatting to be different to the regular chapter layout, you have to accommodate for that. In some cases it would be sufficient to adjust \contentsname to your liking, but since you're using titlesec that may not be the case. Instead I've added parameters so you can change the justification on-the-fly:

    • \chapterjustification (default is nothing, but can be changed to, say, \centering)

    • \chapterfont (default is \normalfont)

    • \chapterfontsize (default is \fontsize{22pt}{0pt}\selectfont)

  2. You can use the complementary titletoc package to format ToC-related entries. Specifically, using either \dottedcontents (for a sectional unit that needs a dotted ToC entry) or \titlecontents for a more general definition.

    I've used \dottedcontents{<section>}[<left>]{<above-code>}{<label width>}{<leader width>} since you want dotted lines.

    It's best to define each entry type (<section>) explicitly, otherwise it will resort to the default formatting for that document class.

  3. You'll need to use the more generalised \titlecontents{<section>}[<left>]{<above-code>}{<numbered-entry-format>}{<numberless-entry-format>}{<filler-page-format>}[<below-code>] with appropriate placement of \MakeUppercase as the last argument for both the <numbered-entry-format> and <numberless-entry-format>.

  4. This comes naturally from using \dottedcontents (or by specifying the \titlerule to have a . when using \titlecontents). You can adjust the 1pc measurement to whatever you like (for example, 5pt).

  5. It's hard to test this without seeing the actual document. However, the minimal example below formats both the ToC and in-text entry automatically, so there's no need to specifically pass formatting elements like \MakeUppercase.

General comments:

  • When you use \fontsize{<size>}{<baselineskip}, you also have to issue \selectfont in order for that specific font parameters to be used.

  • Using

    \listoffigures
    \addcontentsline{toc}{chapter}{List of Figures}
    

    won't achieve what you want, since the \addcontentsline will insert List of Figures in the toc (formatted like a chapter) pointing to the last page of the LoF. Why? \listoffigures will set the LoF, which may span multiple pages before calling \addcontentsline.

    The way around this is to force the appropriate page break (via \cleardoublepage), then issue the contents addition macro (which doesn't print anything), then issue to printing mechanism:

    \cleardoublepage
    \addcontentsline{toc}{chapter}{List of Figures}
    \listoffigures
    

Here is the complete minimal example:

enter image description here

%-------------------------------------------------------
% Dummy Chapter 1
\begin{filecontents*}[overwrite]{ChapterA.tex}
\chapter{Introduction}
\lipsum[1]
\section{Section Name}
Here is some text, see Figure \ref{fig:a}.
\begin{figure}
  \centering
  \includegraphics[width = 0.5\textwidth]{example-image-a}
  \caption{An example figure.} 
  \label{fig:a}
\end{figure}
\subsection{Subsection Name}
Here is a subsection.
\end{filecontents*}

%-------------------------------------------------------
% Dummy Abstract
\begin{filecontents*}[overwrite]{Abstract.tex}
\cleardoublepage
\addcontentsline{toc}{chapter}{Abstract}
\begin{center}
  \fontsize{18pt}{0pt}\selectfont\MakeUppercase{\textbf{abstract}}
\end{center}

\vspace*{24pt}

This is the abstract.
\end{filecontents*}

%-------------------------------------------------------
% Preamble
\documentclass[12pt]{report}

\usepackage{graphicx}
\usepackage{lipsum}

% set page and paragraph dimensions
\setlength{\parindent}{0.5in}
\setlength{\parskip}{6pt}

% set report font
\usepackage[sfdefault]{carlito}

% format headers
\usepackage{titlesec}
\usepackage{titletoc}

\newcommand{\chapterjustification}{}% Similar to \raggedright
\newcommand{\chapterfont}{\normalfont}
\newcommand{\chapterfontsize}{\fontsize{22pt}{0pt}\selectfont}
\titleformat{\chapter}%
    {\chapterjustification\chapterfont\chapterfontsize\bfseries\MakeUppercase}%
    {\thechapter}{0.5in}{}
\titlespacing*{\chapter}{0pt}{80pt}{40pt}

\titleformat{\section}%
    {\normalfont\fontsize{14pt}{0pt}\selectfont\bfseries}%
    {\thesection}{0.5in}{}
\titlespacing*{\section}{0pt}{24pt}{12pt}

\titleformat{\subsection}%
    {\normalfont\fontsize{14pt}{0pt}\selectfont\bfseries}%
    {\thesubsection}{0.5in}{}
\titlespacing*{\subsection}{0pt}{18pt}{12pt}

% set-up frontmatter, mainmatter
\newcommand\frontmatter{
    \cleardoublepage
    \pagenumbering{roman}}
\newcommand\mainmatter{
    \cleardoublepage
    \pagenumbering{arabic}}

% format TOC
\renewcommand{\contentsname}{\textbf{Contents}}

\contentsmargin{2.55em}
\titlecontents{chapter}[1.5em]{\addvspace{.5\baselineskip}\bfseries}{\contentslabel{2em}\MakeUppercase}{\hspace*{-2em}\MakeUppercase}{\titlerule*[1pc]{.}\contentspage}
\titlecontents{section}[3.8em]{\bfseries}{\contentslabel{2.3em}\MakeUppercase}{\hspace*{-2.3em}\MakeUppercase}{\titlerule*[1pc]{.}\contentspage}
\dottedcontents{subsection}[6.1em]{}{2.3em}{1pc}

%-------------------------------------------------------
% Begin

\begin{document}

\frontmatter

\include{Abstract}

\begingroup
\cleardoublepage
\addcontentsline{toc}{chapter}{CONTENTS}
\renewcommand{\chapterjustification}{\centering}
\renewcommand{\chapterfontsize}{\fontsize{18pt}{0pt}\selectfont}
\tableofcontents
\endgroup

\cleardoublepage
\addcontentsline{toc}{chapter}{LIST OF FIGURES}
\listoffigures

\mainmatter
\include{ChapterA}

\end{document}
Related Question