1

The question is on the title - How to hide (exclude) all sections of the certain chapter in book class document (pdflatex)? Thanks in advance

2
  • 1
    Of a certain chapter? Shall this be done automatically? – user31729 Apr 14 '16 at 14:29
  • @ChristianHupfer really I'd like to hide all sections from 2 chapters from the toc. – Olga K Apr 14 '16 at 14:31
4

Improved version, with automatic removal from ToC using a user-defined \HideFromTOC{2,4,8} command, the numbers being the chapter numbers:

\documentclass{book}


\usepackage{xparse}
\usepackage{xpatch}

\newcommand{\hidelevel}{0}  % 0 means chapter and part only
\newcounter{backuptocdepth}

\AtBeginDocument{%
  \setcounter{backuptocdepth}{\value{tocdepth}}%
}


\ExplSyntaxOn%

\clist_new:N \g_chapterhide_clist

\newcommand{\HideFromTOC}[1]{%
  \clist_gclear:N \g_chapterhide_clist%
  \clist_gset:Nx \g_chapterhide_clist {#1}
}

\newcommand{\checkiftohide}[1]{%
  \tl_set:Nx \l_tmpa_tl {#1}
  \clist_if_in:NVT \g_chapterhide_clist {\l_tmpa_tl}{%
    \addtocontents{toc}{\protect\setcounter{tocdepth}{\hidelevel}}
  }
}

\ExplSyntaxOff
\makeatletter

\xpatchcmd{\@chapter}{%
  \ifnum \c@secnumdepth >\m@ne
}{%
  \checkiftohide{\the\numexpr\value{chapter}+1}%
  \ifnum \c@secnumdepth >\m@ne
}{\typeout{Success}}{\typeout{Patch failure}}

\xpretocmd{\chapter}{%
  \setcounter{tocdepth}{\value{backuptocdepth}}%
  \addtocontents{toc}{\protect\setcounter{tocdepth}{\number\value{tocdepth}}}% Write the old value back to ToC
}{}{} % Restore the old tocdepth counter value


\makeatother

\HideFromTOC{2,4,8}


\begin{document}


\tableofcontents


\chapter{First}

\section{Foo}
\section{Bar}

\chapter{Second}

\section{Foo}
\section{Bar}


\chapter{Third}
\section{Foo}
\section{Bar}



\chapter{Fourth}

\section{Foo}
\section{Bar}

\chapter{Fifth}

\section{Foo}
\section{Bar}


\chapter{Sixth}

\section{Foo}
\section{Bar}


\chapter{Seventh}

\section{Foo}
\section{Bar}

\chapter{Eighth}

\section{Foo}
\section{Bar}


\end{document}

The output:

enter image description here

Old-Version

A first version with tocdepth set to 0 temporarily and restored later on.

\documentclass{book}



\begin{document}
\tableofcontents
\chapter{First}

\section{Foo}
\section{Bar}

\chapter{Second}

\section{Foo}
\section{Bar}


\chapter{Third}
\addtocontents{toc}{\setcounter{tocdepth}{0}}
\section{Foo}
\section{Bar}


\addtocontents{toc}{\setcounter{tocdepth}{2}}

\chapter{Fourth}

\section{Foo}
\section{Bar}

\chapter{Fifth}

\section{Foo}
\section{Bar}


\end{document}
9
  • My book has parts also. I've tried this \addtocontents{toc}{\setcounter{tocdepth}{0}}, but it gives an error with my books compiliation. But your MWE works fine – Olga K Apr 14 '16 at 14:39
  • @OlgaK: You did not provide a compilable example, unfortunately – user31729 Apr 14 '16 at 14:45
  • I know. Just working with pre-defined complicated publisher's conditions. They gave Cls file, probably my fail follows from cls-settings of TOC. Thank you, Christian, so kind! – Olga K Apr 14 '16 at 14:56
  • @OlgaK: It's no book.cls then. – user31729 Apr 14 '16 at 15:08
  • it's specialbook.cls, you're right – Olga K Apr 14 '16 at 15:16
1

The simplest solution is

\newcommand{\emptysection}[1]{%
    \begingroup\renewcommand\addcontentsline[3]{}%
    \section{#1}\endgroup%
}
2
  • Yes, but this won't work for \subsection etc. (if needed!) – user31729 Apr 15 '16 at 10:45
  • Ok, then \newcommand{\emptysubsection}[1]{% \begingroup\renewcommand\addcontentsline[3]{}% \subsection{#1}\endgroup% } – Olga K Apr 15 '16 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.