TransWikia.com

Change background color of table of content - tocloft, titlesec and tcolorbox conflict

TeX - LaTeX Asked by Cabin on February 24, 2021

Using all three tocloft, titlesec and tcolorbox packages result in a messed up formatting in the table of contents.

Here is a MWE

% !TEX program = xelatex
documentclass{article}
usepackage{tcolorbox}
usepackage[titles]{tocloft}
usepackage{titlesec}
begin{document}
begin{tcolorbox}[colback=gray!4,arc=5pt,opacityframe=0]
tableofcontents
end{tcolorbox}
section{FIRST SEC}
subsection{First subsection}
subsection{Second subsection}
section{SECOND SEC}
subsection{Third subsection}
end{document}

which results in the loss of vertical space between subsection and section entries in the ToC as you can see below

background color is OK but spacing is not

Now commenting out usepackage{titlesec} OR removing the tcolorbox environment fix the formatting issue:

spacing is fine now...

I would like to keep using the titlesec package so I cannot comment it out.
Of course I also want to keep the colored background in my ToC.

I have no ideas how and why these packages conflict and would be grateful if someone could provide a solution or workaround.

Thanks in advance.

One Answer

I don't really know what tcolorbox does to vertical spacing. It appears that you have to »protect« your tableofcontents like so:

% !TEX program = xelatex
documentclass{article}
usepackage{tcolorbox}
usepackage[titles]{tocloft}
usepackage{titlesec}

begin{document}
begin{tcolorbox}[colback=gray!4,arc=5pt,opacityframe=0]
makeatletter
{cfttoctitlefont contentsname}{cftaftertoctitle}par@starttoc{toc}
makeatother
end{tcolorbox}
section{FIRST SEC}
subsection{First subsection}
subsection{Second subsection}
section{SECOND SEC}
subsection{Third subsection}
end{document}

tableofcontents

Correct answer by phil-elkabat on February 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP