Graphics, Figures & Tables\section* and \tableofcontents

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
sombrancelha
Posts: 22
Joined: Fri May 18, 2007 6:48 pm

\section* and \tableofcontents

Post by sombrancelha »

Hello,

I'm trying to make all \section*{} (and \subsection*{}) to appear in the \tableofcontents.

I tried using

Code: Select all

\addcontentsline{toc}{section}{\rightmark}
but that only shows the page number in the Table of Contents.

Here's a MWE:

Code: Select all

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}

\begin{document}

\tableofcontents

\newpage

\section*{First Section}
some text
\addcontentsline{toc}{section}{\rightmark}

\section*{Second Section}
\subsection*{First Subsection}
more text
\end{document}
Thanks.
Last edited by sombrancelha on Tue Jun 22, 2010 10:43 pm, edited 1 time in total.

Recommended reading 2024:

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

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

meho_r
Posts: 823
Joined: Tue Aug 07, 2007 5:28 pm

\section* and \tableofcontents

Post by meho_r »

Type the actual name of a section instead of \rightmark.

Code: Select all

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}

\begin{document}

\tableofcontents

\newpage

\section*{First Section}
some text
\addcontentsline{toc}{section}{\rightmark}

\section*{Second Section}
\addcontentsline{toc}{section}{Second Section}

\subsection*{First Subsection}
\addcontentsline{toc}{subsection}{First Subection}

more text
\end{document}
bkarpuz
Posts: 124
Joined: Thu Dec 18, 2008 4:53 pm

\section* and \tableofcontents

Post by bkarpuz »

Dear sombrancelha,

The following link can also be useful:
http://www.latex-community.org/forum/vi ... =44&t=8151

bkarpuz
sombrancelha
Posts: 22
Joined: Fri May 18, 2007 6:48 pm

\section* and \tableofcontents

Post by sombrancelha »

Writing the name of the section in the \addcontentsline command would be exhaustive and boring in a long document as mine. What I've done is define \Section (I've also formatted to suit my needs) and use it instead:

Code: Select all

\newcommand{\Section}[1]{\clearpage % put new chapters on a new page
 \refstepcounter{section} % manually increment the chapter number
% manually add the chapter to the table of contents
 \addcontentsline{toc}{section}{\protect\numberline{\thesection}#1}
% and now format the header according to spec.
 \begin{center}
 \LARGE\scshape{#1}
 \vspace*{12pt}
 \end{center}
}
* Based on this website.

The only problem of this is that in the contents table, the sections are still numbered - ideas on how to remove it?
User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

\section* and \tableofcontents

Post by frabjous »

In the definition of \Section, just remove the part that says \protect\numberline{\thesection}, i.e.:

Code: Select all

\newcommand{\Section}[1]{\clearpage % put new chapters on a new page
\refstepcounter{section} % manually increment the chapter number
% manually add the chapter to the table of contents
\addcontentsline{toc}{section}{#1}
% and now format the header according to spec.
\begin{center}
\LARGE\scshape{#1}
\vspace*{12pt}
\end{center}
}
That should remove the section numbers from the ToC.
sombrancelha
Posts: 22
Joined: Fri May 18, 2007 6:48 pm

Re: \section* and \tableofcontents

Post by sombrancelha »

Thanks! This solved it!
Post Reply