23

I'm using LaTeX and the book class and have one problem:

I want that my starred sections (only starred sections) appear in the center of the pages instead of the left side of the pages and also these sections to be added to the table of contents automatically.

Martin Scharrer
  • 262,582

4 Answers4

25

I recommend to create a new macro with a different name instead of changing the behaviour of \section* and thus redefining \section. A separate macro would be a cleaner solution.

The main LaTeX command for sectioning is \@startsection. It is documented in source2e.pdf, just type texdoc source2e at the command prompt. You would find it in 61.2 Sectioning.

So, let's create a macro, use \@startsection with a \centering in the formatting argument:

\documentclass{book}
\makeatletter
\newcommand\@csection{\@startsection {section}{1}{\z@}%
  {-3.5ex \@plus -1ex \@minus -.2ex}%
  {2.3ex \@plus.2ex}%
  {\normalfont\Large\bfseries\centering}}
\newcommand{\csection}[1]{%
  \@csection*{#1}%
  \addcontentsline{toc}{section}{#1}%
}
\makeatother
\begin{document}
\tableofcontents
\chapter{First chapter}
\section{First section}
some text
\csection{A centered section}
more filler text.
\end{document}

Since \@startsection contains the @ character, I had to use \makeatletter ... \makeatother. I defined an internal macro \@csection that does the sectioning. The final user command \csection calls this but additionally adds the entry to the table of contents.

\csection does what you asked for: it creates an unnumbered section-level heading with toc entry. Note, it doesn't support an optional argument, since this hadn't been a requirement.

If you need more customization, also regarding other sectioning commands, I recommend to use the titlesec package. It offers a very good interface for sectioning customization.

Output:

output

Caramdir
  • 89,023
  • 26
  • 255
  • 291
Stefan Kottwitz
  • 231,401
13

You can define a new command to use in place of \section*, combining Bran's answer and the \centering command:

\newcommand{\secstar}[1]{\addcontentsline{toc}{section}{#1}\section*{\centering #1}}

The new \secstar command takes the section name as an argument, then adds it to the the table of contents and creates a new \section*, centering the section title. A complete example of its use is:

\documentclass{book}
\newcommand{\secstar}[1]{\addcontentsline{toc}{section}{#1}\section*{\centering #1}}
\begin{document}
\tableofcontents
\section{Test 1}
First numbered section
\secstar{Test 2}
An unnumbered section
\end{document}
Michael Underwood
  • 21,356
  • 13
  • 52
  • 41
7

For the second part of your question (adding these sections to the table of contents), you can use \addcontentsline{toc}{chapter}{My chapter} before specifying the section command.

4

It would appear that freshmen are neither allowed to comment, nor did a proposed edit of the above pass peer review. So unfortunately I am forced adding a separate answer.

The above proposals of using \addcontentsline are faulty since \section* encourages page breaks before the section start, detaching the addcontentsline and leading to a page entry that is one page too early.

One way to fix that would be rewriting the proposed definition as

\newcommand{\secstar}[1]{\section*{\addcontentsline{toc}{section}{#1}\centering #1}}

Of course, it would be easier to use the KomaScript classes and their \addsec command. This would also affect the running page header otherwise left unchanged.