13

EDIT: As I see in the first answer, underline is the wrong word. I want a line under the section heading, separating the heading from the following text.

I want to separate section headings from the following text with a horizontal line. At the moment I use the following:

\newcommand{\tmpsection}[1]{}
\let\tmpsection=\section
\renewcommand{\section}[1]{\tmpsection{#1}\hrule\nobreak}

But that produces a line, that is too far away from the section-heading and to near to the following text. Has anyone a better idea?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Mnementh
  • 50,487
  • 48
  • 148
  • 202
  • Since the section command formats differently for each class, you could always copy the class you're using and redefine the `\section` command to put the `hrule` in the right place. – dreamlax Sep 08 '09 at 08:09
  • 1
    As you say, every class format it different. I would prefer a solution, that I can use in different projects and that work even if you change particular thing (switching from book to scrbook for instance). That can happen years later by someone else, who doesn't know about the redefined section. That's why I would like to use a more general solution. – Mnementh Sep 08 '09 at 08:12
  • This question appears to be off-topic because it is about LaTeX, which is more appropriate at tex.stackexchange.com. – templatetypedef Nov 03 '13 at 23:44

6 Answers6

9

I've heard that the titlesec is more powerful than sectsty package, so I've just used it. You can put the horizontal line before the section header using:

\usepackage{titlesec}
\titleformat{\section}{\large\bfseries}{\thesection}{1em}{\hrule}

or you can put it after, with the help of newcommand.

See section 4.4 of the titlesec manual.

Jonathan
  • 4,847
  • 3
  • 32
  • 37
  • 5
    I had a hard time parsing the suggestion, so for future readers: the 'with the help of newcommand' means replacing `\hrule` in the answer here with `\hruleafter`, where `\hruleafter` is defined by: `\newcommand{\hruleafter}[1]{#1\hrule}` – keflavich Jun 29 '22 at 17:52
8

Rewriting your command like this should do the trick:

\newcommand{\tmpsection}[1]{}
\let\tmpsection=\section
\renewcommand{\section}[1]{\tmpsection{\underline{#1}}}

This will evaluate to

\section { \underline {#1} }

which in turn produces underlined section headings :)

Shirkrin
  • 3,993
  • 1
  • 29
  • 35
  • 1
    It will also underline the section names in the table of contents, which is presumably *not* what you want. – Will Robertson Sep 08 '09 at 09:05
  • I see I asked my question bad. I don't want a underline in this sense, I really want a line separating the heading from the following text. – Mnementh Sep 08 '09 at 09:12
6

You can do this with the \sectionrule command in the sectsty package.

Rob Hyndman
  • 30,301
  • 7
  • 73
  • 85
  • sectsty does what I want, but I have another problem: I have a book (scrbook-documentclass) and using the command \part. That does no longer work with sectsty. Any advices? – Mnementh Sep 08 '09 at 16:46
5

You might take a look at the LaTeX \rule command:

\rule[raise-height]{width}{thickness}

However, that's an inline (horizontal) command, so you'll have to use some trickery to get it at the right position and size (\noindent and \textwidth should help a little).

Otherwise, you can use the following plain TeX

\smallskip
\hrule height 2pt
\smallskip

It doesn't really fit in the LaTeX syntax, but it works like a charm, and is guaranteed to span the entire text width.

Ruben
  • 15,217
  • 2
  • 35
  • 45
3

Another way could be:

\begin{center}
\line(1,0){400}
\end{center}
locke14
  • 1,335
  • 3
  • 15
  • 36
-1

Using the titlesec package, you can add square brackets at the very end of the \titleformat command to insert something after the section-heading (see section 3.1 of the titlesec documentation). For example:

\titleformat{\section}{\normalfont\Large\bfseries}{\thesection}{1em}{}[\hrule]