# Table of contents invading section title

I am using \titleformat (together with titlesec and titletoc packages) to configure the aspect of the section titles. Since I needed a solid framed title I tried:

\documentclass{article}
\usepackage[svgnames]{xcolor}
\usepackage[explicit]{titlesec}
\usepackage{titletoc}
\usepackage{multicol}
\usepackage{lipsum}

\titleformat{\section}
[block]
{
\LARGE\sffamily\color{white}
}
{}
{0pt}
{\hspace{-20pt}\colorbox{blue}{\parbox{\textwidth}{
\strut
\hspace{10pt}
#1.
\hfill
}}}

\begin{document}

%\begin{minipage}{\textwidth}
\begin{multicols}{2}
\tableofcontents
\end{multicols}
%\end{minipage}

\section{First section}
\lipsum[0]
\subsection{Subsection 1.1}
\lipsum[1]
\subsubsection{Subsubsection 1.1.1}
\lipsum[2]
\section{Second section}
The second section begins here\ldots
\subsection{Subsection 2.1}
\lipsum[4]
\subsubsection{Subsubsection 2.1.1}
\lipsum[5]
\section{Third section}
\lipsum[6]

\end{document}


which seems to work fine except for the table of contents (which I want in two columns). In this case, the second column of the table of contents invades the space of the frame (a behavior which, of course, I would like to avoid). I tried to put the two-column(ed) toc within a minipage but that doesn't seem to work either.

Any advice on how to fix this problem?

Thanks in advance!

## 1 Answer

You have to start the multicols inside the ToC (after the title) and end it at the end of the ToC. Below is a modified version of your MWE giving you what you want.

% tocprob9.tex  SE 536012 2 col ToC

\documentclass{article}
\usepackage[svgnames]{xcolor}
\usepackage[explicit]{titlesec}
\usepackage{titletoc}
\usepackage{multicol}
\usepackage{lipsum}

\titleformat{\section}
[block]
{
\LARGE\sffamily\color{white}
}
{}
{0pt}
{\hspace{-20pt}\colorbox{blue}{\parbox{\textwidth}{
\strut
\hspace{10pt}
#1.
\hfill
}}}

\begin{document}

%\begin{minipage}{\textwidth}
%\begin{multicols}{2}
\tableofcontents
\addtocontents{toc}{\begin{multicols}{2}} % PW added to go into the ToC
%\end{multicols}
%\end{minipage}

\section{First section}
\lipsum[0]
\subsection{Subsection 1.1}
\lipsum[1]
\subsubsection{Subsubsection 1.1.1}
\lipsum[2]
\section{Second section}
The second section begins here\ldots
\subsection{Subsection 2.1}
\lipsum[4]
\subsubsection{Subsubsection 2.1.1}
\lipsum[5]
\section{Third section}
\lipsum[6]

\addtocontents{toc}{\end{multicols}} % PW added to go at the end of the ToC.

\end{document}

• Thanks @Peter (+1). Unfortunately, my actual document is much longer (and much more complex too) and I get the following error: "TeX capacity exceeded, sorry [input stack size=5000]. \addtocontents{toc}{\begin{multicols}{2}}". Any idea of what can be causing it? Mar 30, 2020 at 22:59
• @suitangi I have no idea. But you could try \twocolumn \tableofcontents \onecolum instead of the code in my MWE. Mar 31, 2020 at 17:51