14

I want to exclude ALL sections, subsections, and subsubsection in the Appendix from the table of contents. But I need to have the "Chapter"(Appendix) to be shown in the TOC.

\documentclass[a4paper,12pt,icelandic]{report}
\usepackage[toc,page]{appendix}
\begin{document}
\tableofcontents

\chapter{Chapter 1}
\section{Section 1.1}
\subsection{Section 1.1.1}
\chapter{Chapter 2}
\section{Section 2.1}
\subsection{Section 2.1.1}
\appendix
\chapter{Appendix A}
\section{Appendix A.1}        % Do not show in table of content.
\subsection{Appendix A.1.1}   % Do not show in table of content.
\chapter{Appendix B}
\section{Appendix B.1}        % Do not show in table of content.
\section{Appendix B.2}        % Do not show in table of content.
\end{document}
  • 1
    Maybe this answer could help you. – albert Nov 26 '14 at 18:18
  • Thank you very much Albert. :) Exactly what I've been looking for. - (I accidentally double-clicked the "up-vote" for your comment. So I can't give it another vote.) – Bjartmar Nov 26 '14 at 18:32
16

You have to write \setcounter{tocdepth}{0} in the .toc file when \appendix is issued. You might do it as

\appendix
\addtocontents{toc}{\protect\setcounter{tocdepth}{0}}

but it's better programming style doing this in the preamble:

\documentclass[a4paper,12pt]{report}
\usepackage[toc,page]{appendix}

\usepackage{etoolbox}
\appto\appendix{\addtocontents{toc}{\protect\setcounter{tocdepth}{0}}}

% reinstate the correct level for list of tables and figures
\appto\listoffigures{\addtocontents{lof}{\protect\setcounter{tocdepth}{1}}}
\appto\listoftables{\addtocontents{lot}{\protect\setcounter{tocdepth}{1}}}


\begin{document}

\tableofcontents

\chapter{Chapter 1}
\section{Section 1.1}
\subsection{Section 1.1.1}
\chapter{Chapter 2}
\section{Section 2.1}
\subsection{Section 2.1.1}

\appendix
\chapter{Appendix A}
\section{Appendix A.1}        % Do not show in table of content.
\subsection{Appendix A.1.1}   % Do not show in table of content.
\chapter{Appendix B}
\section{Appendix B.1}        % Do not show in table of content.
\section{Appendix B.2}        % Do not show in table of content.
\end{document}

enter image description here

  • Since I added: \usepackage{etoolbox} \appto\appendix{\addtocontents{toc}{\protect\setcounter{tocdepth}{0}}} I've been experiencing problems with list of figures and list of tables. They disappear.. When % the lines out they come back. After few runs they disappear again. Any ideas @egreg ? – Bjartmar Nov 27 '14 at 11:44
  • 1
    @bjartmar I added the fix – egreg Nov 27 '14 at 11:52
  • Nice one! Thank you.. Thank you.. This seems to do the trick. :D – Bjartmar Nov 27 '14 at 12:03
  • @bjartmar LaTeX should really have independent counters for toc, lof and lot. – egreg Nov 27 '14 at 12:05
3

Here is the solution for future problems. Thanks to @albert

\documentclass[a4paper,12pt,icelandic]{report}
\usepackage[toc,page]{appendix}

\newcommand{\nocontentsline}[3]{}
\newcommand{\tocless}[2]{\bgroup\let\addcontentsline=\nocontentsline#1{#2}\egroup}

\begin{document}
\tableofcontents

\chapter{Chapter 1}
\section{1}
\subsection{2}
\chapter{Chapter 2}
\section{1}
\subsection{2}
\subsubsection{3}
\appendix
\chapter{Appendix A}
\tocless\section{Appendix A.1}        % Do not show in table of content.
\tocless\subsection{Appendix A.1.1}   % Do not show in table of content.
\chapter{Appendix B}
\tocless\section{Appendix B.1}        % Do not show in table of content.
\tocless\section{Appendix B.2}        % Do not show in table of content.
\end{document}
0

One could insert content into the ToC mid-document that would influence the way the ToC is printed. For that I've created two macros:

  • \stoptocwriting

    Set the counter tocdepth to -5, where no sectional heading level exists above. As such, entries following this macro will be dropped with the ToC altogether;

  • \resumetocwriting

    Resets the counter of tocdepth to whatever it was set at in the preamble. This restores the ToC entries style of display.

Here is a minimal example showing the use of the above macros:

enter image description here

\documentclass{report}
\usepackage[toc,page]{appendix}
\newcommand{\stoptocwriting}{%
  \addtocontents{toc}{\protect\setcounter{tocdepth}{-5}}}
\newcommand{\resumetocwriting}{%
  \addtocontents{toc}{\protect\setcounter{tocdepth}{\arabic{tocdepth}}}}
\begin{document}
\tableofcontents

\chapter{Chapter 1}
\section{Section 1.1}
\subsection{Section 1.1.1}
\chapter{Chapter 2}
\section{Section 2.1}
\subsection{Section 2.1.1}
\appendix
\chapter{Appendix A}
\stoptocwriting
\section{Appendix A.1}        % Do not show in table of content.
\subsection{Appendix A.1.1}   % Do not show in table of content.
\resumetocwriting
\chapter{Appendix B}
\stoptocwriting
\section{Appendix B.1}        % Do not show in table of content.
\section{Appendix B.2}        % Do not show in table of content.
\resumetocwriting
\end{document}

The .toc contains all the original entries, together with the setting of tocdepth:

\contentsline {chapter}{\numberline {1}Chapter 1}{2}
\contentsline {section}{\numberline {1.1}Section 1.1}{2}
\contentsline {subsection}{\numberline {1.1.1}Section 1.1.1}{2}
\contentsline {chapter}{\numberline {2}Chapter 2}{3}
\contentsline {section}{\numberline {2.1}Section 2.1}{3}
\contentsline {subsection}{\numberline {2.1.1}Section 2.1.1}{3}
\contentsline {chapter}{\numberline {A}Appendix A}{4}
\setcounter {tocdepth}{-5}
\contentsline {section}{\numberline {A.1}Appendix A.1}{4}
\contentsline {subsection}{\numberline {A.1.1}Appendix A.1.1}{4}
\setcounter {tocdepth}{0}
\contentsline {chapter}{\numberline {B}Appendix B}{5}
\setcounter {tocdepth}{-5}
\contentsline {section}{\numberline {B.1}Appendix B.1}{5}
\contentsline {section}{\numberline {B.2}Appendix B.2}{5}
\setcounter {tocdepth}{0}

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.