# Can't set tocdepth when including external PDF using pdfpages

### What I'm trying to do

I have a document with an appendix that consists of multiple chapters.

1. The first \chapter should not receive any special treatment.

2. For the second \chapter I'd like to exclude \section headings from the TOC. This can be achieved by placing

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


in front of the \chapter.

3. The third \chapter should include all pages of an external PDF file. This can be achieved by adding

\includepdf[pages=-]{external.pdf}


to the chapter.

### MWE

\documentclass[11pt]{scrbook}
\usepackage{graphicx}
\usepackage{pdfpages}
\begin{document}

\tableofcontents

\part{Appendix}
\appendix

\chapter{First appendix}
\section{Section}

\chapter{Second appendix}
Don't list sections in TOC, please.

\section{Section}
Should not be listed in TOC.

\chapter{Third appendix}
Should include all pages of external PDF file.

\includepdf[pages=-]{external.pdf}
\end{document}


### Problem

If I only set tocdepth (and omit the code that takes care of including external.pdf), the document compiles cleanly.

Similarly, if I only include external.pdf (and omit the code that changes tocdepth), the document also compiles cleanly.

However, when I try to compile the file as shown in the MWE above, I get the the following error message:

ERROR: Undefined control sequence.

--- TeX said ---
\@calc@post@scan ...st@scan \else \def \calc@next
{\calc@error #1}\fi \fi \f...

The package pdfpages loads calc, which makes \setcounter fragile.
\addtocontents{toc}{\protect\setcounter{tocdepth}{0}}