7

I would like to design the table of contents aligned to a vertical line near the center of the page. The chapter and section titles need to be flushright, the numbers flushleft, and between titles and numbers, I'd like to place a separating element. It can be done easily by a tabular:

\begin{tabular}{>{\raggedleft}m{0.4\textwidth}>{\centering}m{0.3cm}>{\raggedright}m{0.4\textwidth}}
First section title & \textbullet & 27 \tabularnewline
\end{tabular}

You can see the result of the code above on this image:

But how can I build a tabular with the LaTeX \tableofcontents command? What commands do I need to redefine? After some googling I haven't found any ready solution.

Stefan Kottwitz
  • 231,401
deeenes
  • 1,269

2 Answers2

10

For the chapter insert any symbol you like, instead of the \Large\textbullet

\documentclass{book}
\usepackage{xcolor,ragged2e}
\usepackage{array}
\makeatletter
\def\MBox#1#2#3#4{%
  \parbox[t]{0.4\linewidth}{\RaggedLeft#1}%
  \makebox[0.1\linewidth]{\color{red}#2}%
  \makebox[0.4\linewidth][l]{#3}\\[#4]}
\renewcommand*\l@chapter   [2]{\par\MBox{\bfseries\Large#1}{\Large\textbullet}{#2}{5pt}}
\renewcommand*\l@section   [2]{    \MBox{\bfseries\large#1}{\textbullet}{#2}{2pt}}
\renewcommand*\l@subsection[2]{    \MBox{\bfseries      #1}{\textbullet}{#2}{1pt}}
\renewcommand\numberline[1]{}
\makeatother

\begin{document}

\tableofcontents
\chapter{First Chapter}
\section{foo}
\newpage
\section{bar}
\newpage
\subsection{foobar}
foobar
\section{An extraordinary long section title which should have a linebreak
         in the table of contents}
bar

\end{document}

enter image description here

1
  • Redefine \tableofcontents to produce the tabular environment

  • Redefine \l@chapter, \l@section etc. to produce the rows of the table. These macros take two arguments. Use makeatletter and \makeatother.

Have a look into the source of your document class, where all mentioned macros are defined. The source is on your local drive, use your search feature or kpsewhich to locate it, such as

kpsewhich book.cls

at the command prompt. On Linux, I often directly use it like

gedit `kpsewhich book.cls`

Actually, I made a shell function for that, to ease the frequent access of source code.

Stefan Kottwitz
  • 231,401
  • thank you! maybe i'll try also this way, because the vertical centering of the numbers in the case of section titles wrapped to more line. – deeenes Mar 02 '11 at 22:06