# How to center ToC maintaining the section and subsection numbering?

This is a followup for this question:

How to center the TOC?

The answer to that question is great but I need to:

1. Maintain the section and subsection numbering
2. Flush left the entries

In other words, the style should be identical to the default one except for the center alignment of the entire toc and the numbers aligned on the central bullet.

Editing the answer to the previous question seems easy but I don't know how to replicate the numbering and the right spacing of the default style. Moreover, that solution touches internal LaTeX macros: isn't there a way to do it in a cleaner way using, say, tocloft (that I'm already using to customize other things)?

• I don't understand what you mean by center alignment? The whole toc on the page, or the entries vertically? – user31729 Jun 28 '15 at 11:00
• The whole toc centered, but the single entries flushleft in their own column (and correctly indented). Imagine the example picture centered in the page – gigabytes Jun 28 '15 at 12:34

You can put the entire TOC into a minipage. The \fbox and \rule are just to demonstrate the centering.

Putting the chapter and section numbers back in was a bit tricky. To get the columns to line up I had to keep the font size constant. Otherwise you might as well be using pt and adjusting by eye.

\documentclass{book}
\usepackage{xcolor}

\newlength{\tocwidth}

\def\MBox#1#2#3{%
\parbox[t]{\dimexpr \textwidth -3.5em}{\raggedright #1}%
\hfil{\color{red}#2}\hfil%
\makebox[2em][r]{#3}\linebreak}

\makeatletter
\def\WBox#1#2#3{% get width of #1
\settowidth{\@tempdima}{#1}%
\ifdim\@tempdima>\tocwidth \global\tocwidth=\@tempdima\fi
}
\renewcommand*\l@chapter[2]{\par\MBox{\bfseries\hangindent=1.5em \@tempdima=1.5em #1}{\textbullet}{#2}}
\renewcommand*\l@section[2]{\MBox{\bfseries\hangindent=3.8em \@tempdima=2.3em \hspace{1.5em}#1}{\textbullet}{#2}}
\renewcommand*\l@subsection[2]{\MBox{\bfseries\hangindent=7em \@tempdima=3.2em \hspace{3.8em}#1}{\textbullet}{#2}}
\renewcommand*\numberline[1]{\makebox[\@tempdima][l]{#1}}% left align numbers

\let\old@toc=\tableofcontents

\renewcommand{\tableofcontents}
{\bgroup% get width of largest line (excluding bullet and page number)
\let\MBox=\WBox
\tocwidth=0pt
\rlap{\@input{\jobname.toc}}% read toc file, write nothing
\egroup% restore defaults
\ifdim\tocwidth>\textwidth \tocwidth=\textwidth\fi
\begin{minipage}{\tocwidth}%
\old@toc
\end{minipage}}
\makeatother

\begin{document}
\centering\fbox{\tableofcontents}

\noindent\rule{\textwidth}{1pt}

\chapter{First Chapter}
\section{foo}
\newpage
\section{bar}
\newpage
\subsection{foobar}
foobar

\end{document}


• But how to remove dots, add the bullets and left align the numbers? Also, in this way I have to state the width of the minipage, while I would like to be set automatically depending on the length of toc entries (filling all the horizontal space if some entry is too long to fit on a single line) – gigabytes Jun 28 '15 at 14:59