1

So the committee at my institution has some absurd formatting requirements on the thesis, and I am suppose to make corrections per their specifications to the default Table of Contents generated by LaTeX. They are:

Instead of Contents, the heading must state: Table of Contents (currently, it reads Contents)

Every entry in the TOC, even the chapter titles, must have leading dots to their page numbers (currently, the pages corresponding to the chapter titles are not by the dots)

The page numbers, all of them, must be not in boldface (currently, the only pages corresponding to the chapter titles are boldfaced in mine)

The chapter titles must be referenced in whole, meaning if it's Chapter 1: Introduction, as opposed to simply the entry saying "Introduction," it must read: Chater 1: Introduction.

I am fairly clueless as to how to proceed, since I am very unfamiliar with those packages that customize the TOC. Please help!

2
  • Does your institution uses a custom format or class or are you making this on the defaults? (e.g., article, book, etc) I don't know much about packages that work with Tables of Contents, but if it's on the plain ones, maybe me or someone else can come up with something.
    – user300379
    Jul 13, 2023 at 20:50
  • @MananIntindinichu, thanks for your comment. It's on the book format.
    – user134070
    Jul 13, 2023 at 20:52

3 Answers 3

0

I’m sure this is not the best possible solution, and then again, here are more savvy people that can point to a proper package that can handle stuff globally. However, this one works fine for good ol’ regular book class, which is the one that requires the patch, so here it goes!

\documentclass{book}

\makeatletter
\renewcommand*{\l@chapter}[2]{%         % Chapter label format
  \ifnum \c@tocdepth > \m@ne            % Check: Maximum depth > -1
    \addpenalty{-\@highpenalty}%        % Encourage prior line breaking
    \vskip 1.0em \@plus\p@%             % Prior vertical whitespace
    \setlength\@tempdima{1.5em}%        % Set temporary length
    \begingroup                         % Begin closed group
      \parindent\z@                     % No paragraph indentation
      \rightskip\@tocrmarg              % Reserved right whitespace
      \parfillskip -\@tocrmarg          % Prevent box from moving
      \leavevmode                       % Force horizontal mode
      \advance\leftskip\@tempdima       % Left whitespace
      \hskip -\leftskip                 % First line alignment
      {\bfseries Chapter #1}%           % Chapter name and format
      \nobreak                          % Avoid lineskip
      \leaders\hbox{$%                  % Command to replicate dots
        \m@th                           % Math mode for alignment
        \mkern\@dotsep mu               % Whitespace before a dot
        \hbox{.}%                       % Dot separator
        \mkern\@dotsep mu               % Whitespace after a dot
      $}%
      \hfill                            % Flush to the right
      \nobreak\hb@xt@\@pnumwidth{%      % Chapter page’s number box
        \hss #2%                        % Page number with buffer
      }%
      \par                              % Insert line skip
      \penalty\@highpenalty             % Return to default penalty
    \endgroup                           % End closed group
  \fi
}
\makeatother
\renewcommand\contentsname{Table of Contents}   % Table name

\begin{document}

\tableofcontents

\chapter{One}
\section{One-one}
\subsection{One-one-one}

\chapter{Two}
\section{Two-one}
\subsection{Two-one-one}

\chapter{Three}
\section{Three-one}
\subsection{Three-one-one}

\end{document}

Which produces the following!

Output1

Now, this huge blob of code is just a patch to the \l@chapter command which is the responsible of creating the table of contents entry. All is commented so you can follow along, but the notable mentions are as follows.

Line 16 holds the entry and its format, this is inside brackets so the bold does not escape to the page number.

Line 21 has the separator set as the dot. You can change it, but if we do that, we have to do that for all the other entries so… a dot it stays.

Line 26 holds the page number, if you need to format it, here is where.

Line 34, which is outside the big blob, just redefines the contents name to Table of Contents. Again, you can change it to whatever.

Something that I can’t figure out yet but I’m trying to: The proper “Chapter #: Something” with the colon. In this solution this is not as straight-forward to implement since the argument #1 will load “[Number] [Chapter name]” as a whole block. I’m working on it, but hopefully for the little time I had this does what you need to do!

Edit

I found the solution to the colon problem! However… this means we got to patch another mouthful of code, so here it goes the full version!

\documentclass{book}

\makeatletter
\def\@chapter[#1]#2{%                       % Regular chapter command
  \ifnum \c@secnumdepth > \m@ne             % Check: Maximum depth > -1
    \if@mainmatter                          % Verify if at the main matter
      \refstepcounter{chapter}              % Ch. counter +1. Nested to 0
      \addcontentsline{toc}{chapter}{%      % Add to the Table of Contents
        \protect\numberline{\thechapter:}#1%% Contents and format
      }
    \else                                   % Not at the main matter
      \addcontentsline{toc}{chapter}{%      % Add to the Table of Contents
        #1%                                 % Only optional text
      }
    \fi
  \else                                     % If above maximum depth
    \addcontentsline{toc}{chapter}{%        % Add to the Table of Contents
      #1%                                   % Only optional text
    }
  \fi
  \chaptermark{#1}%                         % Store optional title
  \addtocontents{lof}{%                     % Add to the List of Figures
    \protect\addvspace{10\p@}%              % Vertical whitespace
  }
  \addtocontents{lot}{%                     % Add to the List of Tables
    \protect\addvspace{10\p@}%              % Vertical whitespace
  }
  \if@twocolumn                             % For two-columned documents
    \@topnewpage[%                          % Occupy both columns
      \@makechapterhead{#2}%                % Generate the contents
    ]
  \else                                     % For one-columned documents
    \@makechapterhead{#2}%                  % Generate the contents
    \@afterheading                          % Line skip after a heading
  \fi
}

\renewcommand*{\l@chapter}[2]{%         % Chapter label format
  \ifnum \c@tocdepth > \m@ne            % Check: Maximum depth > -1
    \addpenalty{-\@highpenalty}%        % Encourage prior line breaking
    \vskip 1.0em \@plus\p@%             % Prior vertical whitespace
    \setlength\@tempdima{1.5em}%        % Set temporary length
    \begingroup                         % Begin closed group
      \parindent\z@                     % No paragraph indentation
      \rightskip\@tocrmarg              % Reserved right whitespace
      \parfillskip -\@tocrmarg          % Prevent box from moving
      \leavevmode                       % Force horizontal mode
      \advance\leftskip\@tempdima       % Left whitespace
      \hskip -\leftskip                 % First line alignment
      {\bfseries Chapter #1}%           % Chapter name and format
      \nobreak                          % Avoid lineskip
      \leaders\hbox{$%                  % Command to replicate dots
        \m@th                           % Math mode for alignment
        \mkern\@dotsep mu               % Whitespace before a dot
        \hbox{.}%                       % Dot separator
        \mkern\@dotsep mu               % Whitespace after a dot
      $}%
      \hfill                            % Flush to the right
      \nobreak\hb@xt@\@pnumwidth{%      % Chapter page’s number box
        \hss #2%                        % Page number with buffer
      }%
      \par                              % Insert line skip
      \penalty\@highpenalty             % Return to default penalty
    \endgroup                           % End closed group
  \fi
}
\makeatother
\renewcommand\contentsname{Table of Contents}   % Table name

\begin{document}

\tableofcontents

\chapter{One}
\section{One-one}
\subsection{One-one-one}

\chapter{Two}
\section{Two-one}
\subsection{Two-one-one}

\chapter{Three}
\section{Three-one}
\subsection{Three-one-one}

\end{document}

Which now yields this!

Output2

Now, all this weird stuff that comes from lines 4 to 36 is directly from the source of the default LaTeX2e classes documentation. Most of it is untouched, just pasted as-is to avoid crashing the intended functionality.

The only patch on this blob is line 9, which as you can see, contains the colon! (The original one didn’t, that’s why in the last code #1 called the chapter as “[Number] [Chapter name]” while now the colon is between them!

Again, apologies if this seems a bit messy, but with all the functions at hand, maybe you can modify this to your needs!

1
  • Rather than copy all of the code, you could patch the relevant commands with the etoolbox package: \makeatetter\patchcmd{\l@chapter}{#1}{\chaptername{} #1}{}{}\patchcmd{\l@chapter}{\nobreak}{\nobreak\normalfont\leaders\hbox{$\m@th\mkern\@dotsep mu\hbox{.}\mkern\@dotsep mu$}}{}{\fail}\patchcmd{\@chapter}{\numberline{\thechapter}}{\numberline{\thechapter:}}{}{\fail}\makeatother
    – Alan Munn
    Jul 14, 2023 at 21:45
0

One of the great advantages of LaTeX is that if there's something that you need to do, chances are someone has written a package to do it. In this case, the tocloft package provides a full set of interfaces to the contents code so that modifying it to your needs is quite simple. This is really the preferred route compared to hacking large chunks of code.

\documentclass{book}
\usepackage{tocloft}
\usepackage{calc}
\renewcommand{\contentsname}{Table of Contents}
% if using babel (substitute english with the main language name)
%\addto\extrasenglish{\renewcommand{\contentsname}{Table of Contents}}
\renewcommand\cftchappresnum{Chapter\space} % name before title
\setlength{\cftchapnumwidth}{\widthof{Chapter XX: }}% calculate width to leave enough room
\renewcommand{\cftchapdotsep}{\cftdotsep}% add dots to chapter level
\renewcommand\cftchapaftersnum{:}% add colon after chapter number
\renewcommand\cftchappagefont{\normalfont}% make chapter page numbers not bold
\renewcommand{\cftchapleader}{\cftdotfill{\cftchapdotsep}}% make chapter dots not bold
\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\chapter{One}
\section{One-one}
\subsection{One-one-one}

\chapter{Two}
\section{Two-one}
\subsection{Two-one-one}

\chapter{Three}
\section{Three-one}
\subsection{Three-one-one}

\end{document}

output of code

0

Another package that gives you full control of the format of the table of contents is the titletoc package, part of the titlesec and titleps suite of packages. The titlesec package is very popular for changing the format of section headings, and the titletoc package interfaces very nicely with that, so I'm adding this as a separate answer, even though titlesec is not being used in the example code.

\documentclass{book}
\usepackage{titletoc}
\usepackage{calc}
\renewcommand{\contentsname}{Table of Contents}
% if using babel (substitute english with the main language name)
%\addto\extrasenglish{\renewcommand{\contentsname}{Table of Contents}}
\dottedcontents{section}[3.8em]{}{2.3em}{.7pc}
\dottedcontents{subsection}[6.1em]{}{3.2em}{.7pc}
\contentsmargin{2.5em}
\titlecontents{chapter}
                  [0em]
                  {}
                  {\bfseries Chapter \thecontentslabel: }
                  {}
                  {\titlerule*[.7pc]{.}\contentspage}

\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\chapter{One}
\section{One-one}
\subsection{One-one-one}

\chapter{Two}
\section{Two-one}
\subsection{Two-one-one}

\chapter{Three}
\section{Three-one}
\subsection{Three-one-one}

\end{document}

output of code

You must log in to answer this question.

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