I'd like to create local table of contents that list the subsections within a section as an itemized list. To do this, I used etoc and defined a style \etocsetstyle accordingly.

The problem is that by calling my local toc \itemizedlocaltoc, I also set the ToC depth globally by calling \etocsettocdepth{subsection}. This changes the depth of the bookmarks for the rest of the document: scope leak.

Is there a way to limit the scope of \etocsettocdepth{subsection}?

Relevant part of etoc.sty mentions \global, which would seem to imply "no".

\def\etocsettocdepth   {\futurelet\Etoc@nexttoken\Etoc@set@tocdepth }
\def\Etoc@set@tocdepth {\ifx\Etoc@nexttoken\bgroup
                       \fi }
\def\Etoc@set@tocdepth@ #1{\@ifundefined {Etoc@#1@@}
          {Unknown sectioning unit #1, \protect\etocsettocdepth\space ignored}}
{\global\c@tocdepth\csname Etoc@#1@@\endcsname}%

Bookmarks without \listsubseclocaltoc

enter image description here

Bookmarks with \listsubseclocaltoc

enter image description here

\usepackage{fontspec}% xelatex
\usepackage{etoc}% local toc mechanism
\usepackage{lipsum}% dummy text
\usepackage{hyperref}% get pdf bookmarks

\def\itemizedlocaltoc{% All settings, particularly \etocsettocdepth, should be local to this scope
% Available vars provided by etoc: \etocname \etocnumber \etocpage
\etocsettocstyle{\par\medskip}{}%\etocsettocstyle{beforetoc}{aftertoc}, use for heading

%\etocsettocmargins[0mm]{0mm}{0mm}% custom margins for local toc https://tex.stackexchange.com/questions/87709/minitoc-of-a-subsection/87716
\etocsettocdepth{subsection} % set depth of this toc

% Main Stuff
\section{Main Stuff}

% Sub stuff for demo
\loop% TeX loop for fun
\subsection{Substuff \the\step}
Here is a nice sentence about cool stuff \#\the\step.
\subsubsection{Subsubstuff \the\step}
\advance \step by 1



enter image description here

  • 2
    there is \etocsetnexttocdepth, you can try it.
    – touhami
    Jun 9, 2017 at 8:45
  • @touhami That seemed to do the trick. Thank you. Jun 9, 2017 at 8:52

1 Answer 1


Touhami mentioned \etocsetnexttocdepth, which solves the issue

Definition of \etocsetnexttocdepth

\def\etocsetnexttocdepth #1{%
       {Unknown sectioning unit #1, \protect\etocsetnextocdepth\space ignored}}
     {\edef\Etoc@aftertochook {\global\c@tocdepth\the\c@tocdepth\space
                              \let\noexpand\Etoc@aftertochook\noexpand\@empty }%
      \global\c@tocdepth\csname Etoc@#1@@\endcsname}%

This definition differs from \etocsettocdepth by adding a hook that will be executed after each etoc table of contents. As shown in the code above, \edef\Etoc@aftertochook is set to the register containing the current ToC depth \c@tocdepth before the depth \c@tocdepth is changed.

  • Indeed, a \global is always done by LaTeX's \setcounter{...}, and etoc dutifully manipulates the LaTeX counter tocdepth similarly, although not using \setcounter. Thus the method is to store the current value and reset the tocdepth to this value afterwards. This is very useful when hyperref is present, because hyperref checks value of tocdepth for bookmarks. Thus setting tocdepth only temporarily in-between sectioning commands avoids any disturbance to hyperref.
    – user4686
    Jun 9, 2017 at 20:48
  • Good note about hyperref. I don't want any disturbances. Jun 12, 2017 at 12:29

You must log in to answer this question.

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