6

Is there any way to make table of contents in two column without using multicol?

I added twocolumn in the document class, but the table of contents is still one column.

All solutions I found are using muitlcol.

However, using multicol in twocolumn mode gives me warning:

Package multicol Warning: May not work with the twocolumn option on input line 150.

So, I don't want to use multicol. Is there any way to make table of contents in two column without using multicol?

EDIT: I am using book class

  • But is the body of your document in two columns, or single column? – Bernard Feb 14 '17 at 18:03
  • the \tableofcontents command restores for book class to one column, – user31729 Feb 14 '17 at 18:03
  • @Bernard the body is two column – fairytale Feb 14 '17 at 18:04
  • @ChristianHupfer the problem is come from the book class? how to overcome this problem if I want to use book class? – fairytale Feb 14 '17 at 18:06
  • 4
    you can try {\let\onecolumn\relax\tableofcontents} – touhami Feb 14 '17 at 18:06
12

The \tableofcontents command from the standard book.cls checks whether twocolumn mode has been activated, uses onecolumn and sets a flag in order to restore to twocolumn after that.

\newcommand\tableofcontents{%
    \if@twocolumn
      \@restonecoltrue\onecolumn
    \else
      \@restonecolfalse
    \fi
    \chapter*{\contentsname
        \@mkboth{%
           \MakeUppercase\contentsname}{\MakeUppercase\contentsname}}%
    \@starttoc{toc}%
    \if@restonecol\twocolumn\fi
    }

I decided to use a new command, say \twocolumntoc which does not check. It is, of course, possible to put the twocolumn command into the new macro and the restoration with \onecolumn at the end of the command.

\documentclass{book}

\usepackage{pgffor}

\makeatletter
\newcommand{\twocolumntoc}{%
  \chapter*{\contentsname
    \@mkboth{%
      \MakeUppercase\contentsname}{\MakeUppercase\contentsname}}%
  \@starttoc{toc}%
}
\makeatother

\setcounter{tocdepth}{4}

\begin{document}

\twocolumn
\twocolumntoc

\onecolumn

\foreach \x in {1,...,50}{%
\chapter{Chap \x}
\section{Foo section}
\subsection{Foo subsec}
\subsubsection{Foo subsecsec}
}
\end{document}

The pgffor package is only for quick generation of content in here.

  • I vote now your post. But you vote mine post? – Sebastiano Feb 14 '17 at 18:06
  • @Sebastiano ... done for three, I can't vote for more in a short period – user31729 Feb 14 '17 at 18:11
  • problem solved,thanks a lot. I edited \setcounter{tocdepth}{4} to \setcounter{tocdepth}{2}, because the previous one includes all paragraph title in the toc, and generates 18 errors... lol – fairytale Feb 14 '17 at 18:12
  • @fairytale: Well, you did not post an example, so I used 4 instead of 2 ;-). If the problem is solve, accept the answer please – user31729 Feb 14 '17 at 18:12
  • @ChristianHupfer Thus I have undestood as I must vote. Now I have vote for seven users. Thankssssssssssssss. – Sebastiano Feb 14 '17 at 18:13

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.