[Tex/LaTex] How to create a multiple-column table of contents

table of contentstables

Given some latex code like

    \section{the first section}
    some text

    \section{the second section}
    some text

    \section{the third section}
    some text

    \section{the fourth section}
    some text

I want to create a TOC as a three-column table, like so:

    -------------------------------------------------
    | 1. the first  | 2. the second | 3. the third  |
    |    section    |    section    |    section    |
    -------------------------------------------------
    | 4. the fourth |               |               |
    |    section    |               |               |
    -------------------------------------------------

It is okay if I use a completely custom command instead of section, as long as the numbering in the document body matches the numbering in the table.

How can I approach this problem?

Best Answer

enter image description here

\documentclass{article}
\makeatletter
\def\l@section#1#2{%
\noindent\begin{minipage}[t]{\dimexpr(.333\linewidth)-2em\relax}%
\def\numberline##1{##1: }\parfillskip0pt\relax
#1 \mbox{}\dotfill #2\end{minipage}\linebreak[0]\hspace{2em plus 2em}}
\makeatletter

\begin{document}

\tableofcontents

    \section{the first section}
    some text

    \section{the second section}
    some text

    \section{the third section}
    some text

    \section{the fourth section}
    some text

\end{document}