1

In this answer, I designed a simple TOC style. However, I find the TOC setting for part doesn't work normally if the title format for \part has been modified by titlesec.

Without \titleformat{\part}, the TOC looks like (which is the desired result):

enter image description here

However, with \titleformat{\part}, the TOC becomes:

enter image description here

How can I get the desired effect back?

Below is a MWE.

\documentclass{book}

% \usepackage{titlesec}
% \titleclass{\part}{top} % make part like a chapter
% \titleformat{\part}[display]
%     {\filleft}
%     {\MakeUppercase{\partname}~\thepart}
%     {1em}
%     {\MakeUppercase}

\usepackage{etoolbox}
\makeatletter
\patchcmd\@part% change the part style
  {\addcontentsline{toc}{part}{\thepart\hspace{1em}#1}}
  {\addcontentsline{toc}{part}{\protect\numberline{\texorpdfstring{-~\thepart~-}{\thepart~}}\texorpdfstring{\\\MakeUppercase{#1}}{#1}}}
  {}{\FAIL}
\makeatother

\usepackage{titletoc}
\titlecontents{part}
    [0em]
    {\addvspace{1.5pc}\filcenter}
    {\thecontentslabel}
    {}
    {} % without page number
    [\addvspace{.5pc}]

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

\begin{document}
\ttfamily

\tableofcontents

\part{First part}

\end{document}

1 Answer 1

3

You need the newparttoc option and to change the rendering of \thecontentslabel.

\documentclass{book}

\usepackage[newparttoc]{titlesec}

\titleclass{\part}{top} % make part like a chapter
\titleformat{\part}[display]
    {\filleft}
    {\MakeUppercase{\partname}~\thepart}
    {1em}
    {\MakeUppercase}

\usepackage{titletoc}
\titlecontents{part}
    [0em]
    {\addvspace{1.5pc}\filcenter}
    {- \thecontentslabel\ - \\}
    {}
    {} % without page number
    [\addvspace{.5pc}]

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

\begin{document}

\ttfamily

\tableofcontents

\part{First part}

\end{document}

enter image description here

Suppose you want to process the contents in the toc. You can do something like

\titlecontents{part}
    [0em]
    {\addvspace{1.5pc}\filcenter}
    {\formattocpart} % numbered
    {} % unnumbered
    {} % filler
    [\addvspace{.5pc}]

\newcommand\formattocpart[1]{%
  \colorbox{red!60!green}{%
    \begin{tabular}{@{}c@{}}
    - \thecontentslabel\ - \\
    #1
    \end{tabular}%
  }%
}

The second and third mandatory arguments to \titlecontents are similar to the last mandatory argument to \titleformat.

enter image description here

7
  • With this method one seems to lose access to the title (First part). Can I somehow put it into a color box which, as I understand, only may be done by access to #1?
    – Jinwen
    Commented Feb 8, 2021 at 0:38
  • @Jinwen what precisely do you want to color?
    – egreg
    Commented Feb 8, 2021 at 8:23
  • Actually I want to put the title into a box to control its width.
    – Jinwen
    Commented Feb 8, 2021 at 8:25
  • @Jinwen In the toc or in the actual part title?
    – egreg
    Commented Feb 8, 2021 at 8:28
  • Just in the toc. To be precise, I wish to mimic the look of a typewriter, making sure all the characters are vertically aligned, thus sometimes the text needs to shift a half width of one character.
    – Jinwen
    Commented Feb 8, 2021 at 8:31

You must log in to answer this question.

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