Here is (another way) to do it in a longtable, here using package etoc
.
\documentclass[a4paper]{article}
\usepackage[hscale=0.8]{geometry}
\usepackage{etoc}
\usepackage{array}
\usepackage{longtable}
% It is not at all obvious to display a TOC in a (long)-table,
% using etoc, but it is possible. See in its doc
% "Typesetting the TOC as a table".
% Let's illustrate here in a case which has only sections.
% We will use the *standard* way of displaying the sections in the TOC, except
% it will all take places in p cells of a longtable.
\usepackage[linktoc=all]{hyperref}% let's increase the challenge !
\newcounter{column}
\newtoks\toctabletok
\newcommand*\appendtotok[2]
{% #1=toks variable, #2=macro,
% #2 will be expanded once.
#1\expandafter\expandafter\expandafter{\expandafter\the\expandafter #1#2}%
}
\makeatletter % for \l@section
\newcommand*{\PrepareSectionCell}{%
\toks0 \expandafter{\etocthelinkednumber}% don't worry, etoc always in a group
\toks2 \expandafter{\etocthelinkedname}%
\toks4 \expandafter{\etocthelinkedpage}%
% or we could use \unexpanded from e-TeX, rather than the \toks registers
\edef\toctablecell {\relax % mandatory to stop TeX's future too
% early expansion of cell
\noexpand\par % \l@section MUST start in vertical mode
\noexpand\vspace*{-\baselineskip}% compensate for the \par (I think)
\noexpand\l@section % call the hotline if you have unnumbered sections
{\noexpand\numberline{\the\toks0 }\noexpand\strut\the\toks2 }{\the\toks4 }%
}%
}
%
\newcommand*{\TableTableOfContents}{\begingroup
%
\etocsetstyle{section}{}{}
{\PrepareSectionCell
\appendtotok\toctabletok\toctablecell
\stepcounter{column}%
\ifnum\value{column}=3
\toctabletok \expandafter{\the\toctabletok \\\hline}%
\setcounter{column}{0}%
\else
\toctabletok \expandafter{\the\toctabletok &}%
\fi}
{}
%
\etocsettocstyle
{\toctabletok{%\hypersetup{hidelinks}%
\begin{longtable}{*{3}{|p{.3\linewidth}}|}
\hline
\multicolumn{3}{|c|}{\Large\bfseries\strut TABLE OF CONTENTS}%
\\
\hline\hline}}
{\ifcase\value{column}%
\or\toctabletok\expandafter{\the\toctabletok &\\\hline}%
\or\toctabletok\expandafter{\the\toctabletok \\\hline}%
\fi
\global\toctabletok\expandafter{\the\toctabletok \end{longtable}}%
}
%
\etocsetnexttocdepth {section}%
\tableofcontents
\endgroup
\the\toctabletok % the only thing which survived the closing of the group
}
\makeatother
\begin{document}
\TableTableOfContents
\section{the first section}
some text
\section{the second section}
some text
\section{the third section}
some text
\section{the fourth section}
some text
\newcount\loopcount
\loopcount 50
\loop
\section {and another section}
hello
\clearpage
\advance\loopcount-1
\ifnum\loopcount>0
\repeat
\section {and the final section which happens to have quite a long name. By
the way you can click on it, its number or the page number}
\end{document}

and then continued on page 2:

I add some extra info: there is a difference between a p
column in a longtable
and a p
column in a standard tabular. The latter starts in vertical mode. But the former's p
cell contents typesetting does not start in vertical mode. As a result, the \l@section
which could have been used directly in a tabular
at the start of a cell (after \relax
) needed here in the longtable
an extra \par
. I then needed to compensate the resulting \baselineskip
. There is a remnant vertical space which is due to the \addvspace{1em plus 1pt}
done by \l@section
.
As for the vertical space below the section names it is due to the fact that when a p
column finishes, an (invisible) \vrule
is added by macro \@finalstrut
. Here this is the same in a tabular
as in a longtable
. In both cases this provokes a switch to horizontal mode hence again some vertical space.
In brief, using \l@section
inside longtable
was fun, but I should presumably rather have used the \etocthename
etc... things directly and not wrap them up in \l@section
(see the documentation of etoc for an example in section "Typesetting the TOC as a table").
etoc
package provides some means for this:-)
. But anyhow the accepted answer isn't atable
...longtable
.