24

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.

4
  • I have not enough reputation yet, but I would recommend changing the title of this question and adding some tags such as layout and toc... Commented Aug 19, 2010 at 11:45
  • @Bran: Did that. Not adding layout though because half the questions here could be tagged that.
    – Caramdir
    Commented Aug 19, 2010 at 12:12
  • Maybe this question can give you some hints: tex.stackexchange.com/questions/1455/…
    – Caramdir
    Commented Aug 19, 2010 at 12:15
  • @Caramdir: True, layout does not make sense. Commented Aug 19, 2010 at 12:18

4 Answers 4

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

1
  • Hi Stefan, thanks for your solution. It's exactly what I wanted. Commented Aug 19, 2010 at 17:55
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}
3
  • Good! It works. It's less complicated than my example. I just hesitate to use \centering in a moving argument, but in this case of \section* that argument isn't used for toc or header entry, so it's causing no harm.
    – Stefan Kottwitz
    Commented Aug 19, 2010 at 16:30
  • Hi Michael, thanks for your solution. The solution is short and really works fine. Commented Aug 19, 2010 at 17:57
  • Happy to help, Vahid. Welcome to the site! Commented Aug 19, 2010 at 22:06
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.

1
  • Thanks, don't you have any idea for the first part of the question? Commented Aug 19, 2010 at 11:50
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.

1
  • Thanks for your reply. This is also an issue that one should keep in mind. Commented Jun 19, 2012 at 22:01

You must log in to answer this question.

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