2

I wish to make a Table of Contents like this one:

enter image description here

This is what I have achieved so far (still lack the - I -\newline and \fbox{\thechapter}):

enter image description here

How can I make such a TOC?

Below is a MWE.

\documentclass{book}

\usepackage{titletoc}

\titlecontents{part}
    [0em]
    {\addvspace{1.5pc}\filcenter}
    {\partname~\thecontentslabel}
    {\uppercase}
    {} % without page number
    [\addvspace{.5pc}]
\titlecontents{chapter}
    [2em] % i.e., 0em (part) + 2em
    {\addvspace{.5pc}}
    {\contentslabel{2em}}
    {\hspace*{-2em}}
    {\hfill\contentspage}
\titlecontents{section}
    [4em] % i.e., 2em (chapter) + 2em
    {}
    {\contentslabel{2em}}
    {\hspace*{-2em}}
    {\titlerule*[1em]{.}\contentspage}

\titlecontents{subsection}
    [7em] % i.e., 4em (section) + 3em
    {}
    {\contentslabel{3em}}
    {\hspace*{-3em}}
    {\titlerule*[1em]{.}\contentspage}

\renewcommand{\thesection}{\arabic{section}}
\renewcommand{\thesubsection}{\arabic{section}.\arabic{subsection}}

\def\quad{\ \ }
\def\qquad{\ \ \ \ }

\usepackage[hidelinks,linktoc=all]{hyperref}

\begin{document}

\ttfamily

%% Hard coded
%%=====================================
% \begin{center}
%     - I - \\FIRST PART
% \end{center}
% \fbox{1}\quad First chapter \hfill 1\\
% \null\qquad 1 \quad First section\ \ . . . . . . . . . . . . . . . . . .\hfill 1\\
% \null\qquad 2 \quad Second section . . . . . . . . . . . . . . . . . .\hfill 3\\[.5pc]
% \fbox{2}\quad Second chapter \hfill 4\\
% \null\qquad 1 \quad First section\ \ . . . . . . . . . . . . . . . . . .\hfill 4\\
% \null\qquad\qquad 1.1 \quad First subsection\ \ \,. . . . . . . . . . . . . .\hfill 4\\
% \null\qquad 2 \quad Second section . . . . . . . . . . . . . . . . . .\hfill 5\\

\tableofcontents

\part{First part}
\chapter{First chapter}
\section{First section}
\section{Second section}
\chapter{Second chapter}
\section{First section}
\subsection{First subsection}
\section{Second section}

\end{document}

2 Answers 2

4

Here is a tocloft implementation to achieve what you're going for:

enter image description here

\documentclass{book}

\usepackage{tocloft,etoolbox}
\usepackage[hidelinks,linktoc=all]{hyperref}

% \parts in ToC
\renewcommand{\cftpartfont}{\large\bfseries\ttfamily}
\makeatletter
% \patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
\patchcmd{\H@old@part}% <cmd>
  {\thepart\hspace{1em}#1}% <search>
  {\protect\setpartToC{\thepart}{#1}}% <replace>
  {}{}% <success><failure>
\makeatother
\newcommand{\setpartToC}[2]{%
  \texorpdfstring{\parbox{\linewidth}{\centering -\space #1\space- \\ #2}}{#1\space #2}%
}
\renewcommand{\cftpartfillnum}[1]{\par}
% \chapters in ToC
\renewcommand{\cftchapfont}{\bfseries\ttfamily}
\renewcommand{\cftchappresnum}{\fbox}
\addtolength{\cftchapnumwidth}{0.5em}
\renewcommand{\cftchappagefont}{\cftchapfont}
% \sections in ToC
\renewcommand{\cftsecfont}{\ttfamily}
\addtolength{\cftsecindent}{0.5em}% Because of \cftchapnumwidth increment
\renewcommand{\cftsecpagefont}{\cftsecfont}
% \subsections in ToC
\renewcommand{\cftsubsecfont}{\ttfamily}
\addtolength{\cftsubsecindent}{0.5em}% Because of \cftchapnumwidth increment
\renewcommand{\cftsubsecpagefont}{\cftsubsecfont}

% Update numbering of \section and \subsection (remove \chapter reference)
\renewcommand{\thesection}{\arabic{section}}
\renewcommand{\thesubsection}{\arabic{section}.\arabic{subsection}}

\begin{document}

\tableofcontents

\part{First part}
\chapter{First chapter}
\section{First section}
\section{Second section}
\chapter{Second chapter}
\section{First section}
\subsection{First subsection}
\section{Second section}

\end{document}
4
  • Thanks for this! Unfortunately, the setting for part (\setpartToC) seems to have to no effect if the title format of \part has been modified by titlesec, even with the option newparttoc added. Do you have any suggestions?
    – Jinwen
    Feb 8, 2021 at 0:27
  • @Jinwen: titlesec works with titletoc, but you can't mix those two with tocloft. They're competing packages, so its one or the other. It depends on how you also want to format the sectional units within the text.
    – Werner
    Feb 8, 2021 at 1:52
  • May I ask which package should be used with tocloft to format the sectional units?
    – Jinwen
    Feb 8, 2021 at 1:56
  • @Jinwen: It depends on what you want to do. Minor font-related changes can be done with sectsty. So, how intricate are the required changes? Of course, that falls out of scope in terms of this question.
    – Werner
    Feb 8, 2021 at 2:12
3

Based on the patch from this answer, I was finally able to achieve it:

enter image description here

Full code:

\documentclass{book}

% book -- titletoc patch: https://tex.stackexchange.com/a/454553/134574
\usepackage{etoolbox}
\makeatletter
\patchcmd\@part% change the part style
  {\addcontentsline{toc}{part}{\thepart\hspace{1em}#1}}
  {\addcontentsline{toc}{part}{\thepart\\\texorpdfstring{\MakeUppercase{#1}}{#1}}}
  {}{\FAIL}
\patchcmd\@part% let titletoc works normally
  {\thepart}
  {\protect\numberline{- \thepart\ -}}
  {}{\FAIL}
\patchcmd\@chapter% change the chapter style
  {\protect\numberline{\thechapter}}
  {\protect\numberline{\fbox{\thechapter}}}
  {}{\FAIL}
\makeatother

\usepackage{titletoc}

\titlecontents{part}
    [0em]
    {\addvspace{1.5pc}\filcenter}
    {\thecontentslabel}
    {}
    {} % without page number
    [\addvspace{.5pc}]
\titlecontents{chapter}
    [2em] % i.e., 0em (part) + 2em
    {\addvspace{.5pc}}
    {\contentslabel{2em}}
    {\hspace*{-2em}}
    {\hfill\contentspage}
\titlecontents{section}
    [4em] % i.e., 2em (chapter) + 2em
    {}
    {\contentslabel{2em}}
    {\hspace*{-2em}}
    {\titlerule*[1em]{.}\contentspage}

\titlecontents{subsection}
    [7em] % i.e., 4em (section) + 3em
    {}
    {\contentslabel{3em}}
    {\hspace*{-3em}}
    {\titlerule*[1em]{.}\contentspage}

\renewcommand{\thesection}{\arabic{section}}
\renewcommand{\thesubsection}{\arabic{section}.\arabic{subsection}}

\usepackage[hidelinks,linktoc=all]{hyperref}

\begin{document}

\ttfamily

\tableofcontents

\part{First part}
\chapter{First chapter}
\section{First section}
\section{Second section}
\chapter{Second chapter}
\section{First section}
\subsection{First subsection}
\section{Second section}

\end{document}

You must log in to answer this question.

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