[Tex/LaTex] Generating .toc file without generating a ToC

shorttoctable of contentstitletoc

Problem: I want to generate a .toc file without the table of contents actually being displayed anywhere in the text.

Background: I want a short ToC at the beginning of my text, using shorttoc. Then, using titletoc, I want a detailed in chapter ToC at the beginning of each chapter. For these detailed in chapter ToC's I need \setcounter{tocdepth}{4} in order to have the detailed information being written to the .toc. Now, shorttoc doesn't create a .toc file on its own but you have to place \tableofcontents somewhere after \shorttoc. So just leaving \tableofcontents out doesn't solve the problem.

Possible solution: Run (PDF)LaTeX first with \tableofcontents so that a .toc file is generated. Then comment it out and (PDF)LaTeX again.

That works of course but it is not at all an elegant way of solving the problem. I suppose there should be some way of creating .toc without actually creating a ToC with \tableofcontents.

Here is a MWE. It is not strictly speaking minimal, but the additional stuff shall make clear what the overall idea of the document is.

\documentclass{book}

\usepackage{titletoc,titlesec}
\usepackage{shorttoc}
\usepackage{lipsum}

\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{4}

\titlecontents{partsection}[2.3em]
{} {\contentslabel{2.3em}} {} {\titlerule*[1pc]{.}\contentspage}
\titlecontents{partsubsection}[5.5em]
{} {\contentslabel{3.2em}} {} {\titlerule*[1pc]{.}\contentspage}
\titlecontents{partsubsubsection}[9.6em]
{} {\contentslabel{4.1em}} {} {\titlerule*[1pc]{.}\contentspage}
\titlecontents{partparagraph}[8.5em]
{} {\contentslabel{0em}} {} {\titlerule*[1pc]{.}\contentspage}

\begin{document}

\frontmatter

\chapter*{Preface}
\addcontentsline{toc}{chapter}{Preface}
\lipsum[1]

\cleardoublepage

\shorttoc{Contents}{1}

\cleardoublepage

\tableofcontents

\mainmatter

\chapter{A chapter}
\startcontents[chapters]
\printcontents[chapters]{part}{1}{}
\section{Section}
\lipsum[1]
\subsection{A subsection}
\lipsum[3]
\subsection{A second subsection}
\subsubsection{SubSubsection}
\paragraph{Paragraph}
\lipsum[4]
\section{Section 2}
\lipsum
\chapter{Second chapter}
\startcontents[chapters]
\printcontents[chapters]{part}{1}{}
\section{Section}
\lipsum[2]
\section{Another section}
\lipsum
\end{document}

PS: A solution which does not require major changes of the current configuration would be preffered (I mean I would rather not like to use different packages to get the overall output I get with the above code).

Best Answer

\tableofcontents sets the chapter title and calls \@starttoc{toc}. The latter reads the contents file and starts a new one. The following redefines \tableofcontents that only calls \@starttoc{toc} and the reading is disabled by locally redefining the command for reading.

\makeatletter
\renewcommand*{\tableofcontents}{%
  \begingroup
    \let\@input\@gobble
    \@starttoc{toc}%
  \endgroup
}
\makeatother
Related Question