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:


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. Jul 30, 2012 at 16:01

1 Answer 1


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.


% loc levels for sectioning commands
% part = -1
% chapter = 0
% section = 1
% subsection = 2
% etc.
% 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}
% \@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}

\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

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

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

% this is the code from source2e responsible for making sections and below  
\if@noskipsec \leavevmode \fi
\@tempskipa #4\relax
\ifdim \@tempskipa <\z@
\@tempskipa -\@tempskipa \@afterindentfalse
      % 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}
    % 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
          % if not below, then set toc to show all
          \tl_gset:Nn \g_last_star_depth_tl {5}


% 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


\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}


Which produces:

enter image description here

  • 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, 2012 at 21:15
  • It should work now for all of the classes and sectioning commands :)
    – Scott H.
    Aug 1, 2012 at 20:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .