# Pavel's Boring Notes

Here I provide a minimal example of how to create a custom table of contents (ToC) in LaTeX which appears as a table. #latex

While experimenting with different ways of presenting some parts of LaTeX document I created a table of contents which looks as a table. See the illustration below:

This was possible with a package called etoc.

A preamble, nothing special here:

\documentclass{article}

\usepackage{hyperref}

\usepackage{booktabs}
\usepackage{colortbl}
\usepackage{nicematrix}

\usepackage{etoc}


etoc allows to fully control how to typeset a ToC and even allows to specify which elements to display in the ToC. Let's say we introduce a custom sectioning level which we want to display as ToC. Somewhere in preamble we write:

% dummy sectioning level
\etocsetlevel{specialsection}{90}


Then we define a command which draws the ToC for us. We have to put it inside “group”:

\newcommand\mylistofsomething{%
\begingroup

% body follows below

\endgroup
}


The following calls instruct etoc to handle only the section level we want:

  \etocglobaldefs% mandatory
\etocsetnexttocdepth{4}

% mimics subsubsection
\etocsetlevel{specialsection}{4}

\etocsetlevel{chapter}{99} % no chapters
\etocsetlevel{section}{99} % no sections
\etocsetlevel{subsection}{99} % no subsections
\etocsetlevel{subsubsection}{99} % no subsections
\etocsetlevel{part}{99} % no parts


For our custom section we set a style which in fact creates a table row for each entry:

  \etocsetstyle{specialsection}
{}
{\\}
{
\rowcolor{gray}
\etocnumber&\etocname&just text%
}


What remains is to set style for the whole ToC and write it. In ToC style definition we tell etoc to create a table:

  \etocsettocstyle{%
\begin{NiceTabularX}{\textwidth}{X[1,c]X[3,l]X[1,c]}[hvlines]
\rowcolor{black}
{\textcolor{white}{\textbf{number}}} &
{\textcolor{white}{\textbf{name}}} &
{\textcolor{white}{\textbf{text}}}
}{
\end{NiceTabularX}
}

\tableofcontents%


The defined command can be places as usual in the document body:

\mylistofsomething%


Here is how we can fill our custom ToC:

\section{section 1}

\subsection{subsection 1}

\subsubsection{subsubsection 1}

\section{section 2}

\subsection{subsection 2}

\subsubsection{subsubsection 2}


\etoctoccontentsline operator ads an entry to the ToC.
Special quirk \protect\numberline{somenumber2} can be used (this is optionally) to set an entity which can later be referenced as \etocnumer when filling the table.
name 1 or name 2 strings can be referenced as \etocname.