12

Having code like this

\documentclass{article}
\usepackage{lipsum}
\usepackage{titletoc}
\begin{document}
\tableofcontents
\section{A long section}
\startcontents[section]
\printcontents[section]{}{2}{\setcounter{tocdepth}{3}}
\lipsum[1-2]
\subsection{SubA}
\subsubsection{SSub-a}
\subsection{SubB}
\subsubsection{SSub-b}
\subsection{SubC}
\end{document}

I want to skip sub- and subsubsections for section A long section in a main TOC but have them present in a section toc. Wrapping the section with \addtocontents{toc}{\setcounter{tocdepth}{1}} and \addtocontents{toc}{\setcounter{tocdepth}{3}} hides A long section's structure from main toc but makes it's own toc disappear completely. How can I get what I want?

12

By default, titletoc writes partial ToCs to a file with extension .ptc; you can then set the tocdepth counter to the appropriate values (for the main ToC using \addtocontents{toc}{...} and for the partial ToC using using \addtocontents{ptc}{...}.)

\documentclass{article}
\usepackage{lipsum}
\usepackage{titletoc}
\begin{document}
\tableofcontents
\addtocontents{toc}{\setcounter{tocdepth}{1}}
\section{A long section}
\startcontents[section]
\printcontents[section]{}{2}{\addtocontents{ptc}{\setcounter{tocdepth}{3}}}
\lipsum[1-2]
\subsection{SubA}
\subsubsection{SSub-a}
\subsection{SubB}
\subsubsection{SSub-b}
\subsection{SubC}
\addtocontents{toc}{\setcounter{tocdepth}{3}}
\end{document}

enter image description here

  • great! that's exactly what i wanted – qdiesel Jun 16 '12 at 20:29
  • And would it be possible to have one global tableofcontents for the sections only, then one partial tableofcontents for the subsections only, and finally one partial tableofcontents for subsubsections only? – pluton Aug 15 '13 at 3:52
  • @pluton yes; it's possible using \startcontents[...], \resumecontents[...]. Perhaps you could ask this in a new question? – Gonzalo Medina Aug 15 '13 at 14:02
1

In response to my own comment on Gonzalo's post, a slight modification to his solution works with the mathtools and mhchem package:

\startcontents[section]
\printcontents[section]{}{2}{\setcounter{tocdepth}{3}}

Note the only change from the MWE above is that I have removed \addcontents{ptc} and the extra set of curly braces from setcounter in the \printcontents command.

I have used this solution both in an article and a book documentclass with no problems.

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.