[Tex/LaTex] Add preliminary elements to the ToC

chapterstable of contents

Assume I have finished two sections of my book so far, yet I want to display my preliminary ToC with all parts and chapters (but with no page number) as a preview like this:

I  First Part              1

1  First Chapter           3
   1.1  First Section .... 3
   1.2  Second Section ... 5
   1.3  Third Section ....
2  Second Chapter
   2.1  First Section ....
   2.2  Second Section ...
   2.3  Third Section ....

II Second Part

3  Third Chapter
   1.1  First Section ....
   1.2  Second Section ...
   1.3  Third Section ....
4  Fourth Chapter
   2.1  First Section ....
   2.2  Second Section ...
   2.3  Third Section ....

How can I include these pseudo-parts/chapters/sections etc. without having empty parts/chapters/sections in the document.

Best Answer

Note See the GhostChapters version at the bottom

The preliminary entries can be added by faking \addtocontents and omitting the 2nd argument to the original \contentsline{} command, thus removing the page number.

In lack of a MWE this works for book, scrbook and memoir classes so far only.

\documentclass{book}

\usepackage{etoolbox}%

\newcommand{\addfakecontent}[2]{%
\refstepcounter{#1}%
\addtocontents{toc}{\protect\contentsline{#1}{\csuse{the#1}\hspace{1em}#2}{}}
}%


\AtEndDocument{%

\addfakecontent{part}{The next to final part}
\addfakecontent{chapter}{Some chapter of another part}%
\addfakecontent{chapter}{Even another chapter of another part}%


\addfakecontent{part}{The final part}
\addfakecontent{chapter}{Some chapter of the final part}
\addfakecontent{section}{A section of a final part}



}

\usepackage{blindtext}

\begin{document}

\tableofcontents

\part{First}

\chapter{Some chapter content}

\blindtext



\end{document}

enter image description here

Ghostchapter - version

This version uses the concept of ghost chapters (i.e. \parts, \chapters etc.) By using \EnableGhostChapters at 'any' arbitrary position in the document the following structure commands are disabled, but add an entry to ToC. It works for scrbook, memoir down to subparagraph, and up to subsubsection for book class -- I did not figure out why it fails for paragraph and subparagraph for the book class... (weird definition?)

The ghost chapters are displayed with darkgray color in the ToC.

Use \DisableGhostChapters to switch back to normal behaviour.

The macros \EnablePagesInToc and \DisablePagesInToc enable/disable the display of the page number in the ToC

Note: Although I used \patchcmd ... it's not a patch, it's a hack ;-)

It does not work correctly for \include{} but with \input{}

\documentclass{book}


\usepackage{xcolor}%
\usepackage{etoolbox}%


\newcommand{\preliminarycontentscolor}{darkgray}

\newbool{UsePagesInToc}  


\newbool{GhostChapters}



\newcommand{\EnablePagesInToc}{%
\clearpage
\setbool{UsePagesInToc}{true}%
}%

\newcommand{\DisablePagesInToc}{%
\setbool{UsePagesInToc}{false}%
}%


\newcommand{\EnableGhostChapters}{%
\DisablePagesInToc%
\setbool{GhostChapters}{true}%
}%


\newcommand{\DisableGhostChapters}{%
\EnablePagesInToc%
\setbool{GhostChapters}{false}%
}%




\setcounter{tocdepth}{5}

\setcounter{secnumdepth}{5}


\makeatletter





\def\addcontentsline#1#2#3{%
  \ifbool{UsePagesInToc}{%
    \addtocontents{#1}{\protect\contentsline{#2}{#3}{\thepage}}%
  }{%
    \addtocontents{#1}{\begingroup\color{\preliminarycontentscolor}{\protect\contentsline{#2}{#3}{}}\endgroup}
  }%
}%


\makeatletter

\patchcmd{\@part}{%
  \markboth{}{}%
  {\centering
    \interlinepenalty \@M
    \normalfont
    \ifnum \c@secnumdepth >-2\relax
       \huge\bfseries \partname\nobreakspace\thepart%
       \par
       \vskip 20\p@
     \fi
     \Huge \bfseries #2\par}%
   \@endpart%
 }{%
  \ifbool{GhostChapters}{\thispagestyle{empty}}{%
  \markboth{}{}%
  {\centering
    \interlinepenalty \@M
    \normalfont
    \ifnum \c@secnumdepth >-2\relax
       \huge\bfseries \partname\nobreakspace\thepart%
       \par
       \vskip 20\p@
     \fi
     \Huge \bfseries #2\par}%
   \@endpart%
  }%
}{\typeout{part structure successfully patched}}{\typeout{part structure patching failed}}


\patchcmd{\@chapter}{\chaptermark{#1}%
  \addtocontents{lof}{\protect\addvspace{10\p@}}%
  \addtocontents{lot}{\protect\addvspace{10\p@}}%
  \if@twocolumn
  \@topnewpage[\@makechapterhead{#2}]%
  \else
  \@makechapterhead{#2}%
  \@afterheading
  \fi}{% Patch
  \ifbool{GhostChapters}{\thispagestyle{empty}}{%
    \chaptermark{#1}%
    \addtocontents{lof}{\protect\addvspace{10\p@}}%
    \addtocontents{lot}{\protect\addvspace{10\p@}}%
    \if@twocolumn
    \@topnewpage[\@makechapterhead{#2}]%
    \else
    \@makechapterhead{#2}%
    \@afterheading
    \fi}
}{\typeout{chapter structure successfully patched}}{\typeout{chapter structure patching failed}}


\patchcmd{\@sect}{%  
  \ifdim \@tempskipa>\z@
    \begingroup
      #6{%
        \@hangfrom{\hskip #3\relax\@svsec}%
          \interlinepenalty \@M #8\@@par}%
    \endgroup
    \csname #1mark\endcsname{#7}%
    \addcontentsline{toc}{#1}{%
      \ifnum #2>\c@secnumdepth \else
        \protect\numberline{\csname the#1\endcsname}%
      \fi
      #7}%
  \else
    \def\@svsechd{%
      #6{\hskip #3\relax
      \@svsec #8}%
      \csname #1mark\endcsname{#7}%
      \addcontentsline{toc}{#1}{%
        \ifnum #2>\c@secnumdepth \else
          \protect\numberline{\csname the#1\endcsname}%
        \fi
        #7}}%
  \fi
  \@xsect{#5}}{%
  \ifbool{GhostChapters}{%
    \ifdim \@tempskipa>\z@
    \addcontentsline{toc}{#1}{%
      \ifnum #2>\c@secnumdepth \else
      \protect\numberline{\csname the#1\endcsname}%
      \fi
      #7}
  \else
    \def\@svsechd{%
      \addcontentsline{toc}{#1}{%
        \ifnum #2>\c@secnumdepth \else
          \protect\numberline{\csname the#1\endcsname}%
          \fi
          #7}}%
    \fi%
    \@xsect{#5}%
  }{%
    \ifdim \@tempskipa>\z@
    \begingroup
    #6{%
      \@hangfrom{\hskip #3\relax\@svsec}%
      \interlinepenalty \@M #8\@@par}%
    \endgroup
    \csname #1mark\endcsname{#7}%
    \addcontentsline{toc}{#1}{%
      \ifnum #2>\c@secnumdepth \else
      \protect\numberline{\csname the#1\endcsname}%
      \fi
      #7}%
    \else
    \def\@svsechd{%
      #6{\hskip #3\relax
        \@svsec #8}%
      \csname #1mark\endcsname{#7}%
      \addcontentsline{toc}{#1}{%
        \ifnum #2>\c@secnumdepth \else
        \protect\numberline{\csname the#1\endcsname}%
        \fi
        #7}}%
    \fi
  \@xsect{#5}
}%
}{\typeout{section structures successfully patched}}{\typeout{section structures patching failed}}


\AtBeginDocument{%
  \DisableGhostChapters%
  \EnablePagesInToc%
}%

\AtEndDocument{%
\phantom{some output}
}%

\makeatother
\usepackage{blindtext}

\begin{document}
\DisableGhostChapters
\tableofcontents



\part{First}

\chapter{First of first part}

\section{First section of first chapter}

\subsection{First subsection}

\subsubsection{First subsubsection}
\paragraph{First paragraph}

\subparagraph{First subparagraph}

\blindtext

\chapter{Second chapter}



\EnableGhostChapters

\part{OtherOne}



\chapter{New Chapter}

\section{Section of another chapter}%


\chapter{Another chapter}

\chapter{Yet another chapter of another part}

\section{First section of another chapter}

\subsection{First subsection}

\subsubsection{First subsubsection}
\paragraph{First paragraph}

\subparagraph{First subparagraph}


\EnablePagesInToc
\DisableGhostChapters

\chapter{You will see me!}

\part{And some visible part}

\EnableGhostChapters%
\DisablePagesInToc%
\chapter{With an invisible chapter}


\end{document}

enter image description here