[Tex/LaTex] Table of contents [toc]: Variable tocdepth within toc when using multiple tables of contents

table of contentstitletoc

I can universally vary the table of contents depth.
This is typically handled with command:\setcounter{tocdepth}{ [depth] }.

I can partially vary the table of contents depth.
This is instead handled with package:tocvsec2.

I can create separate tables of contents, such as global and sectional.
This is handled with package:titletoc.

Currently, using tocvsec2 to vary tocdepth affects all titletoc tocs.
I would like separate [partially-varied tocdepth parameters] for separate [tocs].

MWE Explanation

In the MWE,

  • Item 1 has subsections ABC, each with subsubsections xyz.
  • Item 2 has subsections IIa and IIb Each mimics Item 1.
  • Item 3 mimics Item 1.
  • Item 4 has subsections xyz.

What is desired from the MWE,

  • The global table of contents (first) should include Item and ABC sections.
    • This occurs.
  • The global table of contents (first) should exclude xyz subsections.
    • This occurs.
  • Each sectional table of contents (all others) should exclude other top-level sections.
    • This occurs.
  • Each sectional table of contents (all others) should include other subsections.
    • This does not occur.
    • The xyz sections are blocked, like the global section.

The MWE is lengthy. Apologies.

MWE

\documentclass{scrartcl}

\usepackage{geometry}                   % margin/page layout settings
\usepackage{scrlayer-scrpage}           % improved header commands. [supercedes `fancyhdr' package].

\usepackage{titletoc}                   % sub-[tables of contents]
\usepackage[hidelinks]{hyperref}        % hyperlinks.

\usepackage{tocvsec2}                   % variable depth in table of contents


% Margin Settings:
\newlength{\xhmargin   } \setlength{\xhmargin   }{0.750in}
\newlength{\xtmargin   } \setlength{\xtmargin   }{0.750in}
\newlength{\xbmargin   } \setlength{\xbmargin   }{0.750in}
\newlength{\xheadheight} \setlength{\xheadheight}{2.700em}
\newlength{\xheadsep   } \setlength{\xheadsep   }{1.000em}
\newlength{\xfootheight} \setlength{\xfootheight}{2.700em}
\newlength{\xfootskip  } \setlength{\xfootskip  }{3.700em} % [ = Footheight + Footsep]

\KOMAoptions{fontsize   = 12pt         ,
             parskip    = half-        ,
             headheight = \xheadheight , 
             footheight = \xfootheight , 
             DIV        = current      }

\geometry{letterpaper                ,
          hmargin      = \xhmargin   ,
          tmargin      = \xtmargin   ,
          bmargin      = \xbmargin   ,
          headsep      = \xheadsep   ,
          footskip     = \xfootskip  }

%Initialize headers and footers
\chead{\normalfont Header 1 \\ Header 2}
\cfoot{\normalfont Footer 1 \\ Footer 2}
\ofoot{\normalfont Page \thepage}


%Setup section numbering: Make \paragraph like \subsubsection.
\makeatletter
\newcommand{\subsubsubsection}{\@startsection{paragraph}{4}{\z@}%
            {-2.5ex\@plus -1ex \@minus -.25ex}%
            {1.25ex \@plus .25ex}%
            {\normalfont\normalsize\bfseries}}
\makeatother


%Setup section numbering: Make \paragraph and \subparagraph numbered.
\setcounter{secnumdepth}{5}
\setcounter{tocdepth}   {5}





\begin{document}

\tableofcontents

\clearpage

\settocdepth{subsection}

\section{Item I}

\startcontents[Item I]
\printcontents[Item I]{}{1}{ \subsection*{Contents} }

\clearpage

\subsection{A}
\subsubsection{x}
\subsubsection{y}
\subsubsection{z}

\clearpage

\subsection{B}
\subsubsection{x}
\subsubsection{y}
\subsubsection{z}

\clearpage

\subsection{C}
\subsubsection{x}
\subsubsection{y}
\subsubsection{z}

\stopcontents[Item I]


\clearpage

\settocdepth{subsubsection}

\section{Item II}

\startcontents[Item II]
\printcontents[Item II]{}{2}{ \subsection*{Contents} }

\clearpage


\subsection{Item IIa}


\clearpage


\subsubsection{A}
\subsubsubsection{x}
\subsubsubsection{y}
\subsubsubsection{z}

\clearpage

\subsubsection{B}
\subsubsubsection{x}
\subsubsubsection{y}
\subsubsubsection{z}

\clearpage

\subsubsection{C}
\subsubsubsection{x}
\subsubsubsection{y}
\subsubsubsection{z}


\clearpage


\subsection{Item IIb}


\clearpage

\subsubsection{A}
\subsubsubsection{x}
\subsubsubsection{y}
\subsubsubsection{z}

\clearpage

\subsubsection{B}
\subsubsubsection{x}
\subsubsubsection{y}
\subsubsubsection{z}

\clearpage

\subsubsection{C}
\subsubsubsection{x}
\subsubsubsection{y}
\subsubsubsection{z}

\stopcontents[Item II]

\clearpage

\settocdepth{subsection}

\section{Item III}

\startcontents[Item III]
\printcontents[Item III]{}{1}{ \subsection*{Contents} }

\clearpage

\subsection{A}
\subsubsection{x}
\subsubsection{y}
\subsubsection{z}

\clearpage

\subsection{B}
\subsubsection{x}
\subsubsection{y}
\subsubsection{z}

\clearpage

\subsection{C}
\subsubsection{x}
\subsubsection{y}
\subsubsection{z}

\stopcontents[Item III]

\clearpage

\settocdepth{section}

\section{Item IV}

\startcontents[Item IV]
\printcontents[Item IV]{}{1}{ \subsection*{Contents} }

\clearpage

\subsection{x}
\subsection{y}
\subsection{z}

\stopcontents[Item IV]

\end{document}

Best Answer

We need to replace

\settocdepth{subsection}

\section{Item I}

\startcontents[Item I]
\printcontents[Item I]{}{1}{ \subsection*{Contents} }

by

\addtocontents{toc}{\string\setcounter{tocdepth}{2}}  %for first table

\startcontents
\section{Item I}

\addtocontents{ptc}{\string\setcounter{tocdepth}{3}}  % for sections tables
\printcontents{}{1}{\subsection*{Contents}}

Note: don't use \stopcontents

Complete code

\documentclass{article}

\usepackage{geometry}
\usepackage{titletoc}                   % sub-[tables of contents]
\usepackage[hidelinks]{hyperref}        % hyperlinks.

\newlength{\xhmargin   } \setlength{\xhmargin   }{0.750in}
\newlength{\xtmargin   } \setlength{\xtmargin   }{0.750in}
\newlength{\xbmargin   } \setlength{\xbmargin   }{0.750in}
\newlength{\xheadheight} \setlength{\xheadheight}{2.700em}
\newlength{\xheadsep   } \setlength{\xheadsep   }{1.000em}
\newlength{\xfootheight} \setlength{\xfootheight}{2.700em}
\newlength{\xfootskip  } \setlength{\xfootskip  }{3.700em} % [ = Footheight + Footsep]


\geometry{letterpaper                ,
          hmargin      = \xhmargin   ,
          tmargin      = \xtmargin   ,
          bmargin      = \xbmargin   ,
          headsep      = \xheadsep   ,
          footskip     = \xfootskip  }



%Setup section numbering: Make \paragraph like \subsubsection.
\makeatletter
\newcommand{\subsubsubsection}{\@startsection{paragraph}{4}{\z@}%
            {-2.5ex\@plus -1ex \@minus -.25ex}%
            {1.25ex \@plus .25ex}%
            {\normalfont\normalsize\bfseries}}
\makeatother


%Setup section numbering: Make \paragraph and \subparagraph numbered.
\setcounter{secnumdepth}{5}
\setcounter{tocdepth}   {5}

\begin{document}

\tableofcontents

\clearpage


%--------------------------------
\addtocontents{toc}{\string\setcounter{tocdepth}{2}}

\startcontents
\section{Item I}

\addtocontents{ptc}{\string\setcounter{tocdepth}{3}}
\printcontents{}{1}{\subsection*{Contents}}
%--------------------------------

\clearpage

\subsection{A}
\subsubsection{x}
\subsubsection{y}
\subsubsection{z}

\clearpage

\subsection{B}
\subsubsection{x}
\subsubsection{y}
\subsubsection{z}

\clearpage

\subsection{C}
\subsubsection{x}
\subsubsection{y}
\subsubsection{z}



\clearpage


%--------------------------------
\addtocontents{toc}{\string\setcounter{tocdepth}{3}}

\startcontents
\section{Item II}

\addtocontents{ptc}{\string\setcounter{tocdepth}{4}}
\printcontents{}{1}{\subsection*{Contents}}
%--------------------------------

\clearpage


\subsection{Item IIa}


\clearpage


\subsubsection{A}
\subsubsubsection{x}
\subsubsubsection{y}
\subsubsubsection{z}

\clearpage

\subsubsection{B}
\subsubsubsection{x}
\subsubsubsection{y}
\subsubsubsection{z}

\clearpage

\subsubsection{C}
\subsubsubsection{x}
\subsubsubsection{y}
\subsubsubsection{z}


\clearpage


\subsection{Item IIb}


\clearpage

\subsubsection{A}
\subsubsubsection{x}
\subsubsubsection{y}
\subsubsubsection{z}

\clearpage

\subsubsection{B}
\subsubsubsection{x}
\subsubsubsection{y}
\subsubsubsection{z}

\clearpage

\subsubsection{C}
\subsubsubsection{x}
\subsubsubsection{y}
\subsubsubsection{z}


\clearpage

%--------------------------------
\addtocontents{toc}{\string\setcounter{tocdepth}{2}}

\startcontents
\section{Item III}

\addtocontents{ptc}{\string\setcounter{tocdepth}{3}}
\printcontents{}{1}{\subsection*{Contents}}
%--------------------------------

\clearpage

\subsection{A}
\subsubsection{x}
\subsubsection{y}
\subsubsection{z}

\clearpage

\subsection{B}
\subsubsection{x}
\subsubsection{y}
\subsubsection{z}

\clearpage

\subsection{C}
\subsubsection{x}
\subsubsection{y}
\subsubsection{z}


\clearpage

%--------------------------------
\addtocontents{toc}{\string\setcounter{tocdepth}{1}}

\startcontents
\section{Item IV}

\addtocontents{ptc}{\string\setcounter{tocdepth}{3}}
\printcontents{}{1}{\subsection*{Contents}}
%--------------------------------

\clearpage

\subsection{x}
\subsection{y}
\subsection{z}


\end{document}
Related Question