Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
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... –  Bran the Blessed Aug 19 '10 at 11:45
    
@Bran: Did that. Not adding layout though because half the questions here could be tagged that. –  Caramdir Aug 19 '10 at 12:12
    
Maybe this question can give you some hints: tex.stackexchange.com/questions/1455/… –  Caramdir Aug 19 '10 at 12:15
    
@Caramdir: True, layout does not make sense. –  Bran the Blessed Aug 19 '10 at 12:18
add comment

4 Answers

up vote 21 down vote accepted

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

share|improve this answer
    
Hi Stefan, thanks for your solution. It's exactly what I wanted. –  Vahid Damanafshan Aug 19 '10 at 17:55
add comment

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.

share|improve this answer
    
Thanks, don't you have any idea for the first part of the question? –  Vahid Damanafshan Aug 19 '10 at 11:50
add comment

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}
share|improve this answer
    
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 Aug 19 '10 at 16:30
    
Hi Michael, thanks for your solution. The solution is short and really works fine. –  Vahid Damanafshan Aug 19 '10 at 17:57
    
Happy to help, Vahid. Welcome to the site! –  Michael Underwood Aug 19 '10 at 22:06
add comment

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.

share|improve this answer
    
Thanks for your reply. This is also an issue that one should keep in mind. –  Vahid Damanafshan Jun 19 '12 at 22:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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