Generating .toc file without generating a ToC
\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
Instead of using the shorttoc
package, you could simply set the tocdepth
counter to 1 at the start of the front matter (hereby affecting \tableofcontents
), and to 4 at the start of the main matter.
\documentclass{book}
\usepackage{titletoc,titlesec}
\usepackage{lipsum}
\setcounter{secnumdepth}{3}
\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
\setcounter{tocdepth}{1}
\chapter*{Preface}
\addcontentsline{toc}{chapter}{Preface}
\lipsum[1]
\tableofcontents
\mainmatter
\setcounter{tocdepth}{4}
\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}
\makeatletter
\newwrite\tf@toc
\immediate\openout\tf@toc\jobname.toc\relax
\makeatother
is the bit of \tableofcontents
that does not start a section and input the toc file.