# Change ToC depth mid document

How do I change the ToC depth mid document?

so in this example

\documentclass[a4paper,10pt,oneside]{book}

\begin{document}

\chapter{hello}

\frontmatter

\chapter{hello}

\setcounter{tocdepth}{3}
\tableofcontents

\mainmatter

\chapter{hello}
\section{hello}
\subsection{hello}
\subsubsection{hello}

\chapter{hello}

\appendix

\setcounter{tocdepth}{1}

\chapter{hello}
\section{hello}
\subsection{hello}
\subsubsection{hello}

\end{document}

But I want the ToC depth in the appendix to only be of depth 1 (while the rest of the document stays in tocdepth 3). I tried the \setcounter{tocdepth}{1} right after \appendix but that didn't do anything as you can see...

(my guess would be that the ToC is build right after I call \tableofcontents thus I can't change it afterwards this way)

Write the change in tocdepth into the toc like shown in this example:

\documentclass[a4paper,10pt,oneside]{book}

\begin{document}

\chapter{hello}

\frontmatter

\chapter{hello}

\setcounter{tocdepth}{3}
\tableofcontents

\mainmatter

\chapter{hello}
\section{hello}
\subsection{hello}
\subsubsection{hello}

\chapter{hello}

\appendix