3

I'm using the kaobook class, which in turn uses the etoc package to create local TOCs in my document. Because the local TOCs should be in the margin of the page where where they are called (typically at the beginning of a chapter), they are essentially included like this:

\documentclass{scrbook}
\usepackage{etoc}
\usepackage{marginnote}
\usepackage[hidelinks]{hyperref}

\newcounter{margintocdepth} % Set the depth of the margintoc
\setcounter{margintocdepth}{\sectiontocdepth}

\newlength{\mtocshift} % Length of the vertical offset used for margin tocs
\setlength{\mtocshift}{-52mm}

% Command to print a table of contents in the margin
\newcommand{\margintoc}[1][\mtocshift]{ % The first parameter is the vertical offset; by default it is \mtocshift
    \begingroup
        % Set the style for chapter entries
        \etocsetstyle{chapter}
        {\parindent -10pt \parskip 0pt}
        {\leftskip 0pt}
        {\makebox[.5cm]{}
        \hangindent=0.5cm\etoclink{\etocthename}\nobreak\hbox{\hbox to 1.5ex {\hss\hss}}\hfill\makebox[5mm][l]{\etocpage}\nobreak
        \par}
        {}
        % Set the style for section entries
        \etocsetstyle{section}
        {\parindent 0pt \parskip 0pt}
        {\leftskip 0pt}
        {\makebox[.5cm]{}
        \hangindent=0.5cm\etoclink{\etocthename}\nobreak\hbox{\hbox to 1.5ex {\hss\hss}}\hfill\makebox[5mm][l]{\etocpage}\nobreak
        \par}
        {}
        % Set the global style of the toc
        \etocsettocstyle{\usekomafont{section}\small\raggedright}{}
        \etocsetnexttocdepth{\themargintocdepth}
        % Print the table of contents in the margin
        \marginnote[#1]{
            \parbox[b]{\marginparwidth}{Contents\vspace{-0.65\baselineskip}\\\vspace{0.1\baselineskip}\rule{\marginparwidth}{0.5pt}}
            \localtableofcontents
        }
    \endgroup
}

\begin{document}

\tableofcontents

\part{First Part}
\setchapterpreamble[u]{\margintoc}
\chapter{Introduction}

    \section{Section 1}

    \section{Section 2}

\chapter{Body}

    \section{Section 1}

    \section{Section 2}

\chapter{Conclusion}

    \section{Section 1}

    \section{Section 2}

\part{Second Part}

\chapter{Introduction}

\chapter{Body}

\chapter{Conclusion}



\end{document}

This works well, but the local TOC only includes the elements from within the chapter (which I guess is usually exactly what you want). But instead, I would like the local TOC to include everyting from the parts level. Is there any way to do that?

Using \etocsetnexttocdepth unfortunately doesn't work and calling \localtableofcontents without \setchapterpreamble will create a local TOC with the corect scope, but it will be printed on its own page.

2
  • 1
    Please help people help you by providing a compilable sample document that people can play with. This should include enough code to reproduce the problem you're describing.
    – Alan Munn
    Mar 25, 2023 at 15:20
  • @AlanMunn sure, I've added an MWE. I just thought this might be a simple case where that's not necessary.
    – mapf
    Mar 25, 2023 at 15:57

1 Answer 1

2

According to the package documentation section 15. Labeling and reusing elsewhere

etoc allows to typeset at some location a local table of contents which is defined elsewhere. For this, two simple steps:

  1. insert \localtableofcontentsat the distant place, and follow it by some \label{foo}.

  2. insert \tableofcontents\ref{foo} at the place where you want this distant table of contents to appear.

  3. in step 1, if you use \invisiblelocaltableofcontents in place of \localtableofcontents, there will be no typesetting at its place of definition.

You need some way to generate automatically this mark-up with some suitable lables, for example \label{part:1}, but then you will need your chapter to know in which part it is. Ok I tested with \the\value{part} both to generate the label and use as reference for chapters in that part, and it appears to work indeed.

first chapter in first part

margin toc in first chapter of first part

first chapter in second part

margin toc in first chapter of second part

\documentclass{scrbook}
\usepackage{etoc}
\usepackage{marginnote}
\usepackage[hidelinks]{hyperref}

\newcounter{margintocdepth} % Set the depth of the margintoc
\setcounter{margintocdepth}{\sectiontocdepth}

\newlength{\mtocshift} % Length of the vertical offset used for margin tocs
\setlength{\mtocshift}{-52mm}

% Command to print a table of contents in the margin
\newcommand{\margintoc}[1][\mtocshift]{ % The first parameter is the vertical offset; by default it is \mtocshift
    \begingroup
        % Set the style for chapter entries
        \etocsetstyle{chapter}
        {\parindent -10pt \parskip 0pt}
        {\leftskip 0pt}
        {\makebox[.5cm]{}
        \hangindent=0.5cm\etoclink{\etocthename}\nobreak\hbox{\hbox to 1.5ex {\hss\hss}}\hfill\makebox[5mm][l]{\etocpage}\nobreak
        \par}
        {}
        % Set the style for section entries
        \etocsetstyle{section}
        {\parindent 0pt \parskip 0pt}
        {\leftskip 0pt}
        {\makebox[.5cm]{}
        \hangindent=0.5cm\etoclink{\etocthename}\nobreak\hbox{\hbox to 1.5ex {\hss\hss}}\hfill\makebox[5mm][l]{\etocpage}\nobreak
        \par}
        {}
        % Set the global style of the toc
        \etocsettocstyle{\usekomafont{section}\small\raggedright}{}
        \etocsetnexttocdepth{\themargintocdepth}
        % Print the table of contents in the margin
        \marginnote[#1]{
            \parbox[b]{\marginparwidth}{Contents\vspace{-0.65\baselineskip}\\\vspace{0.1\baselineskip}\rule{\marginparwidth}{0.5pt}}
            \localtableofcontents\ref{part:\the\value{part}}
        }
    \endgroup
}

\begin{document}

\tableofcontents

\part{First Part}
\invisiblelocaltableofcontents\label{part:\the\value{part}}
\setchapterpreamble[u]{\margintoc}
\chapter{Introduction}

    \section{Section 1}

    \section{Section 2}

\chapter{Body}

    \section{Section 1}

    \section{Section 2}

\chapter{Conclusion}

    \section{Section 1}

    \section{Section 2}

\part{Second Part}
\invisiblelocaltableofcontents\label{part:\the\value{part}}

\setchapterpreamble[u]{\margintoc}
\chapter{Introduction}

\chapter{Body}

\chapter{Conclusion}



\end{document}

Update: the above is for using \part. It will fail with \part*. Then use rather than \the\value{part} something such as \the\value{mycounter} having done \newcounter{mycounter} and issuing \stepcounter{mycounter} or \refstepcounter{mycounter} at each \part*. You can also do \refstepcounter{part} as you mention in a comment but this appears to be reasonable only if you include only unnumbered parts in the document, else the numbering of the numbered ones will get shifted. Beware that \part* may have another issue of not correctly delimiting the local table of contents according to the etoc documentation, section 21. The \etocsetlocaltop.toc and \etocimmediatesetlocaltop.toc commands. I have not tested and your comment (I don't know how to link to it) seemingly indicates you had no issues, but you may have to do things such as that for unnumbered parts, which I found looking for part* in the etoc documentation.

\part*{Extra unnumbered part}
\etocsetlocaltop.toc{part}
\invisiblelocaltableofcontents

In retrospect as your comment seemed to indicate all ok, maybe this is not needed and possibly even detrimental.

5
  • Amazing. This works. Thanks a lot! Somehow I didn't find that crucial section in the docs.
    – mapf
    Mar 25, 2023 at 20:16
  • Do you by any chance also know why the in the local TOC, the only the name of the first chapter (Introduction) is propperly indented, while the others (Body and Conclusion) are not?
    – mapf
    Mar 25, 2023 at 20:18
  • I made a separate post for that issue.
    – mapf
    Mar 25, 2023 at 20:56
  • Hi, I just realized that this unfortunately doesn't work if you use \addpart (which allows you to add unnumbered parts) instead of just part. part* also doesn't work. Any idea to fix this? It says Label "part:0" multiply defined. Maybe it is possible to set this manually?
    – mapf
    Mar 29, 2023 at 12:35
  • Ah, easy fix. Just do \refstepcounter{part} in front of any additional ``\invisiblelocaltableofcontents` calls. Maybe worth adding to the solution.
    – mapf
    Mar 29, 2023 at 12:43

You must log in to answer this question.

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