5

My objective is to print a mini ToC under a section but this mini ToC should only print the subsection* automatically and not appear in the main ToC. As a reference, I am using the following: sample and another sample.

What I have gathered so far is that I can use, say:

\startcontents[sections]
\printcontents[sections]{}{3}{} % allows printing of subsection* only

and

\subsection*{Test subsection not in ToC}
\addcontentsline{ptc}{subsection}{Test subsection in ToC now} % must be added manually unless a command is created that takes as argument the subsection.

but all of this seems a bit cumbersome.

Can anyone suggest an automatic minitoc creation of subsection* level under a section?

For the sake of completion, here is mwe:

\documentclass{article}
\usepackage{titletoc}
\begin{document}

\section*{Contents}
\startcontents[mytoc]
\printcontents[mytoc]{}{1}{}
\section{Test section in ToC}
\subsection*{Test subsection not in ToC}
\subsection{Test subsection in ToC}

\end{document}
azetina
  • 28,884

1 Answers1

4

You can redefine \subsection to automate this via xparse:

enter image description here

\documentclass{article}

\usepackage{titletoc,lipsum}
\usepackage{xparse}
\let\oldsubsection\subsection
\RenewDocumentCommand{\subsection}{s o m}{%
  \IfBooleanTF{#1}
    {\oldsubsection*{#3}%
     \addcontentsline{ptc}{subsection}{#3}}
    {\IfValueTF{#2}
       {\oldsubsection[#2]{#3}}
       {\oldsubsection{#3}}}
}

\begin{document}

\section*{Contents}
\startcontents[mytoc]
\printcontents[mytoc]{}{2}{} % allows printing of subsection only

\clearpage

\section{Test section in ToC}\lipsum[1]
\subsection*{Test subsection now in ToC}\lipsum[2]
\subsection{Test subsection in ToC}\lipsum[3]

\end{document}
Werner
  • 603,163
  • Will this interfere with the main ToC? I just want to print locally? That is, mytoc should only appear under the corresponding section and not under the main ToC. – azetina Jun 23 '16 at 19:25
  • I updated the question. Would you suggest to create a new title class similar to subsection? If so one would need to somehow declare commands so that it prints a mini ToC containing it locally. – azetina Jun 23 '16 at 19:30
  • @azetina: When I take my answer and add \tableofcontents at the start of the document, I get the \subsection* only in the section ToC, not the \tableofcontents. That's because the \subsection* ToC-entry is written to .ptc, not .toc. – Werner Jun 23 '16 at 19:35