[Tex/LaTex] Exclude appendix sections, subsections and subsubsection from TOC

appendicestable of contents

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}

Best Answer

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

Related Question