0

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.

1 Answer 1

0

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.