5

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).

lpdbw
  • 8,330

4 Answers4

3

\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
Heiko Oberdiek
  • 271,626
2
\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.

David Carlisle
  • 757,742
2

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}
lockstep
  • 250,273
  • Your solution (as well as mine) seems to be incompatible with hyperref. Could please try and load hyperref and give a feedback if you also get an error!? Thanks. – lpdbw Aug 09 '12 at 16:41
  • 1
    @lpdbw When adding the hyperref package (at the end of the preamble), you have to delete all auxiliary files before compiling again -- then it seems to work. – lockstep Aug 09 '12 at 16:46
0

I found another answer to my question: I actually do not need the shorttoc since by putting \addtocontents{ptc}{\setcounter{tocdepth}{4}} into the last argument of \printcontents I can specify the depths for the partial ToC. So I can choose \setcounter{tocdepth}{1} for the normal ToC and nvertheless have deeper partial in chapter ToC's:

\documentclass{book}

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

\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{1}

\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

\tableofcontents

\mainmatter

\chapter{A chapter}
\startcontents[chapters]
\printcontents[chapters]{part}{1}{\setcounter{tocdepth}{4}}
\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}{\setcounter{tocdepth}{4}}
\section{Section}
\lipsum[2]
\section{Another section}
\lipsum
\end{document}
lpdbw
  • 8,330
  • OK, now there are 4 more or less different solution ... difficult to say which is the most elegant one!? ;-) – lpdbw Aug 09 '12 at 15:37
  • 1
    Your solution is similiar to my one -- \addtocontents doesn't seem necessary, though. – lockstep Aug 09 '12 at 15:40
  • David's and Heiko's answesr are great for situations where you want only partial ToCs. In case of a full ToC plus partial ToCs with higher depth, I'd prefer simply not to use the shorttoc package. – lockstep Aug 09 '12 at 15:43