GeneralAvoiding duplicate Sections in the ToC

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
spin0za
Posts: 6
Joined: Sat Dec 10, 2011 9:33 pm

Avoiding duplicate Sections in the ToC

Post by spin0za »

Hi,

Is there a way to avoid having duplicate sections in the Table of Contents?

At the moment, I need to add a bunch of section tags in my master file. I would prefer to have the section tags in the multiple .tex files. Then simple add a bunch of include commands, and if two consecutive sections have the same name, latex would ignore the section commands.

Here's my code:

Code: Select all

\tableofcontents
\clearpage

\section*{Charles Darwin}
\addcontentsline{toc}{section}{Charles Darwin}
\input{Darwin-origine-chap4}
\input{Darwin-descendance}
\clearpage

\section*{René Descartes}
\addcontentsline{toc}{section}{René Descartes}
\input{Descartes-methode}
\input{Descartes-meditations}
\clearpage
Works fine.

Except I want to be able to just use a couple of includes. I tried putting the section tags inside the different .tex files, but then I get duplicate entries in the ToC.

Any idea how I can make the script "understand" that it's the same section title and ignore the subsequent section commands until a different section title is given?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

kaiserkarl13
Posts: 707
Joined: Tue Mar 25, 2008 5:02 pm

Avoiding duplicate Sections in the ToC

Post by kaiserkarl13 »

Since you didn't provide a document class OR the contents of your \input-ted files, I'll have to guess that you actually wrote something like this:

Code: Select all

\documentclass{article}
\tableofcontents
\clearpage

\section*{Charles Darwin}
\addcontentsline{toc}{section}{Charles Darwin}
%\input{Darwin-origine-chap4}
   \section{Charles Darwin}
   \subsection{Chapter 4}
   Here is some text.
%\input{Darwin-descendance}
   \section{Descendance}
   Here is some text.
\clearpage

\section*{René Descartes}
\addcontentsline{toc}{section}{René Descartes}
%\input{Descartes-methode}
  \section*{René Descartes}
  \subsection{Methode}
  Here is still more text.
%\input{Descartes-meditations}
  And still more.
\clearpage
The solution is then "don't repeat your section headings!" By the way, if you want a \clearpage after your \input file, just use \include instead of \input. I'd also suggest using \chapter instead of \section, since it sounds closer to what you want. If you use \section instead of \section*, it will put the sections in the ToC for you without the \addcontentsline; use \renewcommand{\secnumdepth}{0} to remove numbering if you want.

Without an example of what you're actually doing, that's the best I can do.
Post Reply