141

I have many unnumbered sections in my report (more than 100), all created with:

\section*{section name}

I want to find a way to easily add them all to the TOC (specifically to a minitoc). Since I use the * to remove the numbering, they won't appear in TOC. I found out in minitoc documentation that I should use:

\addcontentsline{toc}{section}{repeat here section title}

However, I don't want to have a duplicated title in every section, or even to change all the involved files to add those already written sections to the TOC. How to add those sections' names to the minitoc without using the addcontentsline command? Is it possible?

Geremia
  • 2,201
Weslei
  • 4,015
  • 8
  • 32
  • 37

5 Answers5

104

The easiest way to do this is to use \section but to change secnumdepth so that they don't get numbered.

\documentclass{article}
\setcounter{secnumdepth}{0}
\begin{document}
\tableofcontents
\section{ADSF}
\section{Foo}
\end{document}
TH.
  • 62,639
  • 3
    Thanks, it worked like a charm! It's easier to remove the * from each section than duplicate all their names! And I could even use the \setcounter{secnumdepth}{0} after some chapters, keeping the numbering on the previous ones! – Weslei Feb 21 '11 at 19:33
  • 22
    A convenient solution, but unsuitable for documents with a mix of numbered and non-numbered parts. For these scenarios, look at @Vser 's answer below – Avision Dec 02 '17 at 08:40
  • 1
    For a scoped version, enclose the relevant part in {} and use \makeatletter\c@secnumdepth=0\makeatother at the beginning.Also, to apply either to chapters, use -1. – golvok Aug 01 '19 at 17:00
  • 2
    This messes up either toc page numbers and/or bookmark page numbers and/or clickable links (wrong pages), unfortunately. You have to randomly spread \phantomsection additions and hope that nothing breaks. – Emit Taste May 14 '20 at 15:05
  • agreed ruins \hyperref on the toc – tedioustortoise Sep 06 '20 at 13:00
99

Another solution using \addcontentsline{toc}{section}{repeat here section title} relies on nameref. In fact I'm numbering every section (chapters and so on) with a label. So you could use:

% Package 'hyperref' needed for command '\nameref'
\section*{Introduction}
\label{sec:intro}
\addcontentsline{toc}{section}{\nameref{sec:intro}}

It's only a local change and is persistent to names' change of your section.

Vser
  • 1,911
16

If you are using KOMA-Script the easiest way is to use these built in commands:

\addpart \addchap \addsec

Which work just like \part* \chapter* \section* while also being added to the toc.

4

Easy to use:

\section*{Preface}
\addcontentsline{toc}{section}{Preface}

got this answer here : https://latex.org/forum/viewtopic.php?t=335

1

What you can do is to redefine your chapters (or sections). Here I copy what I did for my report. I redefined \chapter* and defined a \chapter**

The latter is the old \chapter*, while the former is an unnembered chapter, but added to the toc.

 \let\@originalchapter\chapter
     \def\chapter{
                 \@ifstar\chapterstar\@originalchapter
                 }
     \def\chapterstar{
                 \@ifstar\chapter@nonum@notoc\chapter@nonum
                 }
 \newcommand\chapter@nonum[2][]{
         \ifthenelse{\isempty{#1}}{
          \cleardoublepage
          \phantomsection
          \addcontentsline{toc}{chapter}{\protect\numberline{}#2}
          \@originalchapter*{#2}
          \chaptermark{#2}
          }{
          \cleardoublepage
          \phantomsection
          \addcontentsline{toc}{chapter}{\protect\numberline{}#1}
          \@originalchapter*[#1]{#2}
          \chaptermark{#1}
       }
  }
 \newcommand\chapter@nonum@notoc[2][]{
    \ifthenelse{\isempty{#1}}{
        \@originalchapter*{#2}
        \chaptermark{#2}
     }{
     \@originalchapter*[#1]{#2}
     \chaptermark{#1}
   }
}

\let\@originaltableofcontents\tableofcontents
  \def\tableofcontents{
   \let\@toclocaloriginalchapter\chapter
   \def\chapter{\@toclocaloriginalchapter*}
   \@originaltableofcontents
   \def\chapter{\@toclocaloriginalchapter}
}
Oscar
  • 531
  • 3
  • 13