Removing Subordinated Headline Levels From The ToC Automatically When Using *

Usually, when you use section*{}only the section will be removed from the toc. In order to remove subordinated subsections you have to add the *.

Is it possible to define a command that removes subordinated levels automatically when you remove higher level headlines from the toc? This could save some time.

In the document:

\section*{}
\subsection{}


With the desired adjustment both (the section and the subsection) would be removed -- even though only the section is provided with the *.

An ideal solution would be able to remove all subordinated levels regardless of whether they are used for parts, sections, subsections, subsubsections, paragraphs etc.

-
You can locally change the counter secnumdepth to get only unnumbered sections and subsection. With the help of the package tocvsec2 you can also change locally tocdepth. –  Ulrike Fischer Jul 30 '12 at 16:01

This works, and as far as I can tell doesn't break anything. Doing all of the conditionals by hand was a hassle, so I used the package suggested in the comments tocvsec2 to make life a lot easier.

• If a sectioning command is starred, then all subordinate sectioning levels will be unnumbered and hidden from the toc until such time as an unstarred sectioning command of equal or greater level is encountered. When this happens, toc and numbering are reset to show all subordinate levels. This will work for all sectioning commands in the article and book document classes (fingers crossed).

• Added: Macro \maxdepths[<numbering>]{<toc>} where the optional <numbering> sets greatest depth to number, e.g. subsection with default subparagraph and the mandatory second argument <toc> sets the greatest depth to display in the toc. If the command is missing, then both default to subparagraph.

Code:

\documentclass{article}
\usepackage{tocvsec2}
\usepackage{xparse}
% loc levels for sectioning commands
% part = -1
% chapter = 0
% section = 1
% subsection = 2
% etc.
\ExplSyntaxOn
% stores last hidden toc level
\tl_new:N \g_last_star_depth_tl
\tl_new:N \g_max_toc_depth_tl
\tl_new:N \g_max_numbering_depth_tl
\tl_set:Nn \g_max_numbering_depth_tl {subparagraph}
\tl_set:Nn \g_max_toc_depth_tl {subparagraph}

\NewDocumentCommand{ \maxdepths } { O {subparagraph} m }
{
\tl_gset:Nn \g_max_numbering_depth_tl {#1}
\tl_gset:Nn \g_max_toc_depth_tl {#2}
}
\makeatletter
% \@part and \@spart are the commands used by doc classes for
% parts and starred parts respectively.  Redefine them to
% make changes to toc and star depth
\cs_gset_eq:NN \old_part: \@part
\cs_gset:Npn \@part
{
\tl_gset:Nn \g_last_star_depth_tl {5}
\old_part:
}

\cs_gset_eq:NN \old_star_part: \@spart
\cs_gset:Npn \@spart
{
\tl_gset:Nn \g_last_star_depth_tl {-1} % the level of part is -1
\settocdepth{part} % don't show parts and below in toc
\setsecnumdepth{part} % don't number parts and below
\old_star_part:
}

%same for chapters
\cs_gset_eq:NN \old_chapter: \@chapter
\cs_gset:Npn \@chapter
{
\int_compare:nTF { 0 > \g_last_star_depth_tl }
{
\old_chapter:
}
{
\setsecnumdepth{\g_max_numbering_depth_tl}
\settocdepth{\g_max_toc_depth_tl}
\tl_gset:Nn \g_last_star_depth_tl {5}
\old_chapter:
}
}

\cs_gset_eq:NN  \old_star_chapter: \@schapter
\cs_gset:Npn \@schapter
{
\tl_gset:Nn \g_last_star_depth_tl {0}
\settocdepth{chapter}
\setsecnumdepth{chapter}
\old_star_chapter:
}

% this is the code from source2e responsible for making sections and below
\def\@startsection#1#2#3#4#5#6{
\if@noskipsec \leavevmode \fi
\par
\@tempskipa #4\relax
\@afterindenttrue
\ifdim \@tempskipa <\z@
\@tempskipa -\@tempskipa \@afterindentfalse
\fi
\if@nobreak
\everypar{}%
\else
\fi
\@ifstar
{
% if sectioning command is starred save toc level (#2)
% and use package to adjust toc and numbering settings
\tl_gset:Nn \g_last_star_depth_tl {#2}
\settocdepth{#1}
\setsecnumdepth{#1}
\@ssect{#3}{#4}{#5}{#6}%
}
{
% if not starred, then check if below last hidden level, ie. higher number
\int_compare:nTF {#2 > \g_last_star_depth_tl}
{
% if below then hide
\@ssect{#3}{#4}{#5}{#6}
}
{
% if not below, then set toc to show all
\setsecnumdepth{\g_max_numbering_depth_tl}
\settocdepth{\g_max_toc_depth_tl}
\tl_gset:Nn \g_last_star_depth_tl {5}
\@dblarg{\@sect{#1}{#2}{#3}{#4}{#5}{#6}}
}

}
}
\makeatother
\ExplSyntaxOff
\begin{document}

% the first optional arg sets the max numbering depth (default = subparagraph)
% the mandatory arg sets the max toc depth, if this command
% is missing, then both values default to subparagraph
%\maxdepths[subsection]{subsection}

\tableofcontents

\part*{This part should hide}
\section{This section should hide}
\part{This part should show}
\section*{This section should hide}
\subsection{This subsection should hide}
\section{This section should show}
\subsection{This subsection should show}

\end{document}


Which produces:

-
Thanks! That works perfectly for sectioning commands starting at the ´\section´ level. Would it be possible to define this solution for higher level sectioning commands like ´\part and ´chapter, too? –  Philip Jul 31 '12 at 21:15
It should work now for all of the classes and sectioning commands :) –  Scott H. Aug 1 '12 at 20:06
Thanks again!!! –  Philip Aug 1 '12 at 22:01