40

Is there any chance to hide the "Table of contents" heading that \tableofcontents generates?

If figured that \renewcommand\contentsname{} works (it sets the title to "", an empty string), but it does not remove the heading completely - the space is still blocked.

doncherry
  • 54,637
Niko
  • 825

3 Answers3

43

If you are using the article class you can find the definition of \tableofcontents in the article.cls file:

\newcommand\tableofcontents{%
    \section*{\contentsname
        \@mkboth{%
           \MakeUppercase\contentsname}{\MakeUppercase\contentsname}}%
    \@starttoc{toc}%
    }

Now you can just remove the part where \section* is defined:

\documentclass{article}
\makeatletter
\renewcommand\tableofcontents{%
    \@starttoc{toc}%
}
\makeatother
\begin{document}
\tableofcontents
\section{test}
\section{test2}
\end{document}
Thorsten
  • 12,872
  • 1
    How and why does this work? (Feel free to edit the answer into your answer) – doncherry Jun 22 '12 at 22:04
  • 1
    @doncherry Just take the original definition of \tableofcontents from article.cls and remove the code that creates the heading. – Thorsten Jun 23 '12 at 06:08
  • 3
    I applied current validated answer (see above) and it didn't work at first. I had a LaTeX Error: Something's wrong--perhaps a missing \item error message. All I had to do was to add spaces before \tableofcontents. – kogexo May 28 '18 at 08:45
4

You say that using an empty name does not remove the space before the TOC. You can change that using the extra code below:

\renewcommand\contentsname{} % the empty name

\begingroup
\let\clearpage\relax
\vspace{-5cm} % the removed space. Set as appropriate
\tableofcontents
\endgroup
luchonacho
  • 4,161
  • 1
    A simple one-liner does it better (your solution didn't work for me) : \renewcommand\contentsname{\vspace*{-20pt}}. – Anthony Apr 28 '21 at 15:20
4

If you want no TOC, but still want the "sidebar" created by hyperref you may do this:

\documentclass{...}
...
\usepackage{hyperref}

\begin{document}
\newsavebox{\hidden}\sbox{\hidden}{\vbox{\tablefocontents}}

<code>
\end{document}