[Tex/LaTex] Control line spacing of section headings

line-spacingsectioningsections-paragraphsspacing

I use macro and titlesec package to create custom \section heading.

\documentclass[final]{book}
\usepackage{lipsum}
\usepackage{titlesec}

\titlespacing*{\section}{0pt}{1em}{0.3em}

\def \mysection#1{
\section*{\large{#1}}
}

\begin{document}

\mysection{Lorem ipsum dolor sit amet, consectetur adipisicing elit doloribus odio aliquid aut.}

\lipsum[2]

\mysection{Another section heading with very long text within it that wraps to second line}

\lipsum[3]

\end{document}

which produces

custom section heading

I want to be able to decrease or increase line spacing between lines of that \section heading.

I have tried to do so, by changing \mysection macro to:

\def \mysection#1{
\section*{\baselineskip=2pt \large{#1}}
}

but it doesn't have any effect. (Note that I set \baselineskip to 2pt only to spot it for sure when change happens, not because it is my target line spacing).

I have also tried to change it to:

\def \mysection#1{
\section*{\linespread{0.1} \large{#1}}
}

which seems to have slight effect on line spacing, but produces nasty indent out of nowhere:

custom section heading with indent

I have also tried using setspace package with its command \singlespacing:

\documentclass[final]{book}
\usepackage{lipsum}
\usepackage{titlesec}
\usepackage{setspace}

\titlespacing*{\section}{0pt}{1em}{0.3em}

\def \mysection#1{
\section*{\singlespacing \large{#1}}
}

\begin{document}

\mysection{Lorem ipsum dolor sit amet, consectetur adipisicing elit doloribus odio aliquid aut.}

\lipsum[2]

\mysection{Another section heading with very long text within it that wraps to second line}

\lipsum[3]

\end{document}

which decreases line spacing, but adds unexpected white space above \section headings:

enter image description here

Is there any reliable way to modify line spacing of \section headings? I thought that titlesec should be able to do it, but I haven't found a clue how to do it in its documentation.

Best Answer

You can use \titleformat in the titlesec package to control the format of the section headings:

\documentclass[final]{book}
\usepackage{setspace}
\usepackage{titlesec}
\titleformat{\section}{\normalfont\large\bfseries}{\thesection}{1em}{\setstretch{0.1}}
\begin{document}
Document Content
\end{document}

From the manual, the syntax for the \titleformat command is:

\titleformat{⟨command⟩}[⟨shape⟩]{⟨format⟩}{⟨label⟩}{⟨sep⟩}{⟨before-code⟩}[⟨after-code⟩]

specifying, in order, the title command to modify, the optional "shape" of the title (display/block/...), the format of title, the horizontal length between label and title, and code to execute before and after the title body.

The example above emulates the standard section title.