[Tex/LaTex] Double-spaced paragraphs, single-spaced headers

line-spacingsectioningsetspace

I am using the "setspace" package to format my document with double spacing (requirement for an assignment).
One of my section headers takes up two lines, though, and these section headers look better with single spacing.
I went ahead and fixed it with the following.

Nunc venenatis nulla eu arcu pellentesque eu molestie nunc condimentum.
Donec sodales lacinia dictum.
Sed aliquam turpis quis enim bibendum pharetra.
This is the last paragraph in section i.

\singlespace
\section{The Next Section Which Has a Fairly Long Name that Stretches Over Two Lines}
\doublespace
This is the first paragraph in section i+1.
Cras ut tortor vel dui ultricies dapibus vitae sit amet nisi.
Aliquam rhoncus leo id eros volutpat faucibus.
Integer lectus elit, varius et semper eget, tristique vel odio.

This is the only case (so far) in my paper where the heading requires two lines, so it's not a big deal to fix it with this hack.
However, I can imagine as the paper gets longer and has more sections, it could become more tedious to add this hack multiple times.

Is there any way I can indicate once that section headings should be single-spaced and paragraphs should be double-spaced, rather than adding this hack multiple times throughout my document?

Best Answer

You can use the etoolbox package to insert \singlespacing just before the sectional units, and then to append \doublespacing:

\documentclass{article}
\usepackage{setspace}
\usepackage{etoolbox}

\makeatletter
\pretocmd{\@sect}{\singlespacing}{}{}
\pretocmd{\@ssect}{\singlespacing}{}{}
\apptocmd{\@sect}{\doublespacing}{}{}
\apptocmd{\@ssect}{\doublespacing}{}{}
\makeatother

\doublespacing

\begin{document}

Nunc venenatis nulla eu arcu pellentesque eu molestie nunc condimentum.
Donec sodales lacinia dictum.
Sed aliquam turpis quis enim bibendum pharetra.
This is the last paragraph in section i.

\section{The Next Section Which Has a Fairly Long Name that Stretches Over Two Lines}

This is the first paragraph in section i+1.
Cras ut tortor vel dui ultricies dapibus vitae sit amet nisi.
Aliquam rhoncus leo id eros volutpat faucibus.
Integer lectus elit, varius et semper eget, tristique vel odio.

\end{document}

enter image description here

This will apply to \section, \subsection, \subsubsection.

Another option is to use the titlesec package:

\documentclass{article}
\usepackage{setspace}
\usepackage{titlesec}

\titleformat{\section}
{\singlespacing\normalfont\Large\bfseries}{\thesection}{1em}{}
\titleformat{\subsection}
{\singlespacing\normalfont\large\bfseries}{\thesubsection}{1em}{}
\titleformat{\subsubsection}
{\singlespacing\normalfont\normalsize\bfseries}{\thesubsubsection}{1em}{}

\doublespacing

\begin{document}

Nunc venenatis nulla eu arcu pellentesque eu molestie nunc condimentum.
Donec sodales lacinia dictum.
Sed aliquam turpis quis enim bibendum pharetra.
This is the last paragraph in section i.

\section{The Next Section Which Has a Fairly Long Name that Stretches Over Two Lines}

This is the first paragraph in section i+1.
Cras ut tortor vel dui ultricies dapibus vitae sit amet nisi.
Aliquam rhoncus leo id eros volutpat faucibus.
Integer lectus elit, varius et semper eget, tristique vel odio.

\end{document}

Or, using the reduced syntax:

\usepackage{titlesec}

\titleformat*{\section}{\normalfont\Large\bfseries\singlespacing}
\titleformat*{\subsection}{\normalfont\large\bfseries\singlespacing}
\titleformat*{\subsubsection}{\normalfont\normalsize\bfseries\singlespacing}

By the way, the setspace package provides several commands and environments; the commands (switches) end in "ing": \singlespacing, \onehalfspacing, \doublespacing, whereas the environments are singlespace, onehalfspace, doublespace.

Using \doublespace are you are doing (as a switch) is not entirely correct; the following simple document:

\documentclass{article}
\usepackage{setspace}

\doublespace

\begin{document}

test

\end{document}

when processed will show in the output console a message

(\end occurred inside a group at level 1)

### semi simple group (level 1) entered at line 4 (\begingroup)

which indicates that a group started but was never ended (in this case, the group created by the \doublespace command associated to the environment doublespace). The correct form of using the switch is

\documentclass{article}
\usepackage{setspace}

\doublespacing

\begin{document}

test

\end{document}

and, for the corresponding environment:

\documentclass{article}
\usepackage{setspace}

\begin{document}

\begin{doublespace}
test...
\end{doublespace}

\end{document}