2

I have lots of little sections and subsections in an article. some of them have a title and some don't. Is it possible to have a table of contents where only the named sections/subsections appear in it?

\chapter{My chapter}
\section{Alice}
\section{}
\subsection{}
\subsection{Ava}
\section{Bob}
\section{}
\section{}
\section{Charles}

I want only the sections and subsections named Alice, Ava, Bob, Charles to show up in the table of contents.

2
  • When you say "index" are you meaning "table of contents" ? Please be more specific, and help us to help you by providing a minimal working example (edit your post to add code).
    – Jhor
    Commented Nov 20, 2018 at 7:21
  • Yes thats what I meant...fixed the title. thanks Commented Nov 20, 2018 at 7:44

3 Answers 3

3

With a standard class you could patch \@sect:

\documentclass{report}
\usepackage{xpatch}
\let\originaladdcontentsline\addcontentsline
\makeatletter
\xpatchcmd\@sect
  {\addcontentsline {toc}{#1}{\ifnum #2>\c@secnumdepth \else \protect \numberline {\csname the#1\endcsname }\fi #7}}
  {%
    \if!#7!\else
      \originaladdcontentsline {toc}{#1}{\ifnum #2>\c@secnumdepth \else \protect \numberline {\csname the#1\endcsname }\fi #7}%
    \fi
  }{}{\PatchFailed}
\xpatchcmd\@sect
  {\addcontentsline {toc}{#1}{\ifnum #2>\c@secnumdepth \else \protect \numberline {\csname the#1\endcsname }\fi #7}}
  {%
    \if!#7!\else
    \originaladdcontentsline {toc}{#1}{\ifnum #2>\c@secnumdepth \else \protect \numberline {\csname the#1\endcsname }\fi #7}%
    \fi
  }{}{\PatchFailed}
\makeatother
\begin{document}
\tableofcontents
\chapter{My chapter}
\section{Alice}
\section{}
\subsection{}
\subsection{Ava}
\section{Bob}
\section{}
\section{}
\section{Charles}
\end{document}

Result:

enter image description here

But you could also use a KOMA-Script class. Then there would be no TOC entry for empty section titles by default:

\documentclass{scrreprt}
\begin{document}
\tableofcontents
\chapter{My chapter}
\section{Alice}
\section{}
\subsection{}
\subsection{Ava}
\section{Bob}
\section{}
\section{}
\section{Charles}
\end{document}

Result:

enter image description here

1

You need to redefine the tex macros that render the toc entry. That said, what you need to edit depends on the document class and the packages you include into your document. For the simplest latex book, no packages used, that would be something like

\makeatletter
\def\l@section#1#2{%
  \global\let\oldnumline\numberline
  \def\numberline##1{}%
  \if!#1!
    \global\let\numberline\oldnumline
  \else
    \global\let\numberline\oldnumline
    \@dottedtocline{1}{1.5em}{2.3em}{#1}{#2}%
  \fi}
\def\l@subsection#1#2{%
  \global\let\oldnumline\numberline
  \def\numberline##1{}%
  \if!#1!
    \global\let\numberline\oldnumline
  \else
    \global\let\numberline\oldnumline
    \@dottedtocline{2}{3.8em}{3.2em}{#1}{#2}%
  \fi}
\makeatother

that you need to put before \begin{document}.

2
  • This does not work, because the first argument of \l@section or \l@subscetion incudes \numberline{<number>} if the section or subsection is numbered.
    – esdd
    Commented Nov 20, 2018 at 12:31
  • @esdd good point…
    – Lupino
    Commented Nov 20, 2018 at 12:48
1

With etoc you can have both types of tables of contents at the same time (with the standard classes).

\documentclass{report}

\usepackage{etoc}

\newcommand{\trimmedtableofcontents}{%
\begingroup
\etocsetstyle{chapter}
     {}
     {\ifx\etocthename\empty
      \else
        \etocifnumbered
        {\etocsavedchaptertocline
           {\numberline{\etocnumber}\etocname}{\etocpage}}
        {\etocsavedchaptertocline{\etocname}{\etocpage}}%
      \fi
     }%
     {}
     {}
\etocsetstyle{section}
     {}
     {\ifx\etocthename\empty
      \else
        \etocifnumbered
        {\etocsavedsectiontocline
           {\numberline{\etocnumber}\etocname}{\etocpage}}
        {\etocsavedsectiontocline{\etocname}{\etocpage}}%
      \fi
     }%
     {}
     {}
\etocsetstyle{subsection}
     {}
     {\ifx\etocthename\empty
      \else
        \etocifnumbered
        {\etocsavedsubsectiontocline
           {\numberline{\etocnumber}\etocname}{\etocpage}}
        {\etocsavedsubsectiontocline{\etocname}{\etocpage}}%
      \fi
     }%
     {}
     {}
\tableofcontents
\endgroup
}

\begin{document}
\trimmedtableofcontents
\renewcommand\contentsname{Full table of contents}
\tableofcontents
\chapter{My chapter}
\section{Alice}
\section{}
\subsection{}
\subsection{Ava}
\section{Bob}
\section{}
\section{}
\section{Charles}
\end{document}

First page:

enter image description here

Second page:

enter image description here

You must log in to answer this question.

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