142

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?

5 Answers 5

105

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}
5
  • 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
    Commented Feb 21, 2011 at 19:33
  • 23
    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
    Commented Dec 2, 2017 at 8: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
    Commented Aug 1, 2019 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
    Commented May 14, 2020 at 15:05
  • agreed ruins \hyperref on the toc Commented Sep 6, 2020 at 13:00
101

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.

5
  • Non numbered chapter* header issue
    – Johannes_B
    Commented Nov 5, 2014 at 16:20
  • 1
    Do you need to include \usepackage{nameref} in the header to do this ?
    – usernumber
    Commented Jun 29, 2015 at 16:08
  • 1
    Rather \usepackage{hyperref} I think.
    – lindhe
    Commented May 23, 2016 at 13:22
  • 3
    This is a better solution for those seeking a document with both numbered and non-numbered parts/sections.
    – Avision
    Commented Dec 2, 2017 at 8:39
  • This also works for other composition units like chapters with \addcontentsline{toc}{chapter}{\nameref{<label_of_chapter>}}.
    – anaik
    Commented Feb 15 at 16:02
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.

1
  • Simple and Elegant! God bless Markus Kohm, who created the Koma Scripts. They always surprise me with very elegant solutions to awkward LaTeX problems. (See, for example solution for landscape pages) Commented Aug 5, 2022 at 8:46
5

Easy to use:

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

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

2
  • Nicely simplified, but there is not a great difference with @Vser answer. ;) Commented Aug 5, 2022 at 8:44
  • Down vote. This is exactly the code that's included in the question. Your answer adds nothing new.
    – richard
    Commented Feb 4 at 16:14
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}
}
1
  • 1
    Of course, you can do the same with \section and \section*
    – Oscar
    Commented Jun 13, 2017 at 17:51

You must log in to answer this question.

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