81

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?

78

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}
  • 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
  • 7
    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 2 '17 at 8:40
46

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:

\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.

  • 1
    Do you need to include \usepackage{nameref} in the header to do this ? – usernumber Jun 29 '15 at 16:08
  • Rather \usepackage{hyperref} I think. – lindhe May 23 '16 at 13:22
  • This is a better solution for those seeking a document with both numbered and non-numbered parts/sections. – Avision Dec 2 '17 at 8:39
3

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.

0

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
    Of course, you can do the same with \section and \section* – Oscar Jun 13 '17 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.