Different TOC Depth for Main TOC and In-Section TOC

table of contentstitletoc

Having code like this

\documentclass{article}
\usepackage{lipsum}
\usepackage{titletoc}
\begin{document}
\tableofcontents
\section{A long section}
\startcontents[section]
\printcontents[section]{}{2}{\setcounter{tocdepth}{3}}
\lipsum[1-2]
\subsection{SubA}
\subsubsection{SSub-a}
\subsection{SubB}
\subsubsection{SSub-b}
\subsection{SubC}
\end{document}

I want to skip sub- and subsubsections for section A long section in a main TOC but have them present in a section toc. Wrapping the section with \addtocontents{toc}{\setcounter{tocdepth}{1}} and \addtocontents{toc}{\setcounter{tocdepth}{3}} hides A long section's structure from main toc but makes it's own toc disappear completely. How can I get what I want?

Best Answer

By default, titletoc writes partial ToCs to a file with extension .ptc; you can then set the tocdepth counter to the appropriate values (for the main ToC using \addtocontents{toc}{...} and for the partial ToC using using \addtocontents{ptc}{...}.)

\documentclass{article}
\usepackage{lipsum}
\usepackage{titletoc}
\begin{document}
\tableofcontents
\addtocontents{toc}{\setcounter{tocdepth}{1}}
\section{A long section}
\startcontents[section]
\printcontents[section]{}{2}{\addtocontents{ptc}{\setcounter{tocdepth}{3}}}
\lipsum[1-2]
\subsection{SubA}
\subsubsection{SSub-a}
\subsection{SubB}
\subsubsection{SSub-b}
\subsection{SubC}
\addtocontents{toc}{\setcounter{tocdepth}{3}}
\end{document}

enter image description here