3

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
                            \expandafter\Etoc@set@tocdepth@
                       \else\expandafter\Etoc@set@toctocdepth
                       \fi }
\def\Etoc@set@tocdepth@ #1{\@ifundefined {Etoc@#1@@}
      {\PackageWarning{etoc}
          {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

\documentclass{article}
\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
\def\contentsname{}
\etocsettocstyle{\par\medskip}{}%\etocsettocstyle{beforetoc}{aftertoc}, use for heading
\etocsetstyle{subsection}
{\begin{itemize}}
{\scshape\item}
{\etocname{}}
{\end{itemize}}

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

\begin{document}
% Main Stuff
\section{Main Stuff}
\itemizedlocaltoc

% Sub stuff for demo
\newcount\step
\step=1
\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
\ifnum\step<6
\repeat

\end{document}

Output

enter image description here

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

Touhami mentioned \etocsetnexttocdepth, which solves the issue

Definition of \etocsetnexttocdepth

\def\etocsetnexttocdepth #1{%
    \@ifundefined{Etoc@#1@@}
     {\PackageWarning{etoc}
       {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 '17 at 20:48
  • Good note about hyperref. I don't want any disturbances. – Jonathan Komar Jun 12 '17 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.