6

In my report I'd like to have a short table of contents, thus I printed it with multicol in two columns to safe space. But the alignment on the top is like in the picture.enter image description here

I'd more prefer it if the first row of the second column is aligned to the first entry of my toc of the first column. All in all Section 3 should move to the left column below Section 2 and Section 4 shall stay in one line with Section 1.

I found some solutions, but they were somehow very manual where you have to write yourself which section appears in which column. Here, so far, everything happens automatically :)

My code so far:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
%%%%%% additional packages
\usepackage{multicol}
\usepackage{etoolbox}
%%%%%%

\setcounter{tocdepth}{1} %set depth of printed table of contets.

\makeatletter
\patchcmd{\l@section}
  {\hfil}
  {\leaders\hbox{\normalfont$\m@th\mkern \@dotsep mu\hbox{.}\mkern \@dotsep     mu$}\hfill}
  {}{}
\makeatother %print dots in sections in toc.

\begin{document}


\begin{multicols}{2}
\tableofcontents
\end{multicols}


\section{Introduction}%%%%%%%%%%Introduction
\subsection{bla}
\subsubsection{blabla}

\section{Approach}%%%%%%%%%%Approach
\subsection{bla}
\subsubsection{blabla}

\section{Experimental results}%%%%%%%%%%Experimental results
\subsection{bla}
\subsubsection{blabla}

\section{Discussion}%%%%%%%%%%Discussion
\subsection{bla}
\subsubsection{blabla}

\section{Summary}%%%%%%%%%%Summary
\subsection{bla}
\subsubsection{blabla}


\end{document}

Thank you very much, SemtexB

1
  • 1
    Could you fix your code so that it (1) compiles, and (2) produces the output you show in the image?
    – cfr
    Apr 4, 2015 at 20:01

3 Answers 3

9

enter image description here

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
%%%%%% additional packages
\usepackage{multicol,etoolbox}
%%%%%%

\setcounter{tocdepth}{2} %set depth of printed table of contets.

\makeatletter


\patchcmd{\l@section}
  {\hfil}
  {\leaders\hbox{\normalfont$\m@th\mkern \@dotsep mu\hbox{.}\mkern \@dotsep     mu$}\hfill}
  {}{}

\renewcommand\tableofcontents{%
    \begin{multicols}{2}[\section*{\contentsname
        \@mkboth{%
           \MakeUppercase\contentsname}{\MakeUppercase\contentsname}}]%
    \@starttoc{toc}%
    \end{multicols}%
    }

\makeatother %print dots in sections in toc.

\begin{document}


\tableofcontents


\section{zz}
aa
\section{bbbb}
\subsection{xgxgxgxg}
aaa
\subsection{xgxgxgxg}
aaa
\subsection{xgxgxgxg}
aaa
\subsection{xgxgxgxg}


\end{document}
2
  • Works perfectly fine, also with other tocdepth's. Thank you!
    – SemtexB
    Apr 4, 2015 at 20:12
  • A little addition: The \multicolsep length sets the space before the multicols environment. By default this behavior diminishes the space between the section title (\contentsname) and the TOC, which looks odd in comparison with the rest of the sections in the document. To keep some consistency, add: \setlength{\multicolsep}{2\multicolsep}. (There might be better solutions/explanations, though.)
    – undostres
    Sep 6, 2018 at 19:19
5

A shorter, if sneakier, solution:

\documentclass{article}
\usepackage{multicol}

\AtBeginDocument{\addtocontents{toc}{\protect\begin{multicols}{2}}}
\AtEndDocument{\addtocontents{toc}{\protect\end{multicols}}}

\begin{document}
\tableofcontents
\section{fee}
\subsection{foo}
\subsection{bar}
\section{fie}
\section{fo}
\section{fum}
\end{document}
0

This is an old thread but i had the same problem and found a solution that i judge simpler, so... just remove the table of contents title, add it manually before \tableofcontents. Your code would then look like this:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
%%%%%% additional packages
\usepackage{multicol}
\usepackage{etoolbox}
%%%%%%

\setcounter{tocdepth}{1} %set depth of printed table of contets.

\makeatletter
\patchcmd{\l@section}
  {\hfil}
  {\leaders\hbox{\normalfont$\m@th\mkern \@dotsep mu\hbox{.}\mkern \@dotsep     mu$}\hfill}
  {}{}
\makeatother %print dots in sections in toc.

\begin{document}

\section*{Contents} % As suggested by TeXnician
\renewcommand{\contentsname}{}

\begin{multicols}{2}
\tableofcontents
\end{multicols}


\section{Introduction}%%%%%%%%%%Introduction
\subsection{bla}
\subsubsection{blabla}

\section{Approach}%%%%%%%%%%Approach
\subsection{bla}
\subsubsection{blabla}

\section{Experimental results}%%%%%%%%%%Experimental results
\subsection{bla}
\subsubsection{blabla}

\section{Discussion}%%%%%%%%%%Discussion
\subsection{bla}
\subsubsection{blabla}

\section{Summary}%%%%%%%%%%Summary
\subsection{bla}
\subsubsection{blabla}


\end{document}
2
  • Welcome to TeX.SX! This is not a good idea. Maybe you could do a \section*{Contents} instead to avoid manual formatting. Otherwise it is fine of course.
    – TeXnician
    May 1, 2021 at 16:39
  • You're right, thats much better, thanks.
    – DJ9
    May 2, 2021 at 18:14

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.