TeX - LaTeX Asked on July 24, 2021
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):
However, with titleformat{part}
, the TOC becomes:
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}{theparthspace{1em}#1}}
{addcontentsline{toc}{part}{protectnumberline{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}
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}
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}]
newcommandformattocpart[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
.
Correct answer by egreg on July 24, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP