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

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

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

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.

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}


• This solution works fine, thank you for your quick help, I could never have figured this out by myself. Aug 19, 2020 at 23:22