2

I have a list in the following form

\begin{enumerate}
\item PARTIES 

\begin{enumerate}
\item \textbf{Party A} (Buyers) and
\item \textbf{Party B} (Sellers) 
\end{enumerate}
\end{enumerate}

I'd like to create a table of contents based on the first (outside) list items. Each outside / top level list item has a number of nested list items.

Resulting TOC would be something like

1) PARTIES ....... 2

1 Answer 1

4

Since \item does not have a mandatory argument this is not easy. But with \RenewDocumentCommand from xparse it's possible to provide an optional argument using {}` (other arguments could be done with the d-type)

The item entry which should go to the ToC as well is to be specified with {...}, otherwise the item entry is typeset only but not included in the ToC.

Please note that in case of \item[]{} there must be no space between [] and {} to make this work.

I restricted this feature to enumerate and nesting depth of 1

\documentclass{article}

\usepackage{letltxmacro}
\usepackage{xparse}

\makeatletter
\LetLtxMacro\latex@std@item\item

\RenewDocumentCommand{\item}{og}{%
  \ifnum \@enumdepth = \@ne
  \IfValueTF{#1}{%
    \IfValueTF{#2}{%
      \latex@std@item[#1] \addcontentsline{toc}{section}{#2}#2%
    }{%
      \latex@std@item[#1]
    }%
  }{%
    \IfValueTF{#2}{%
      \latex@std@item  \addcontentsline{toc}{section}{#2}#2%
    }{%
      \latex@std@item%
    }%
  }%
  \else
  \latex@std@item \IfValueTF{#2}{#2}%
  \fi
}



\makeatother

\begin{document}
\tableofcontents



\begin{enumerate}
\item {PARTIES}
\begin{enumerate}
\item \textbf{Party A} (Buyers) and
\item \textbf{Party B} (Sellers) 
\begin{enumerate}
\item \textbf{Party C} Deeper nested
\end{enumerate} 
\end{enumerate}
\item {Groups}

\item {Categories}

\item[\textbullet]{Types}
\end{enumerate}

\end{document}

enter image description here

2
  • can this solution be made to handle enumeration in the TOC and indented second-level entries in the TOC? See: this new post
    – villaa
    Jan 7, 2019 at 2:02
  • @Villaa: On first glance I would say yes.
    – user31729
    Jan 7, 2019 at 8:37

You must log in to answer this question.

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