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 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.

share|improve this question
1  
note that \baselineskip=2pt indicates the total distance between baselines, so with any type of readable size, it's quite impossible. \baselineskip=2cm should have an effect, but if the expansion of \section doesn't end with \par (i didn't check), then it wouldn't have any effect anyway. –  barbara beeton Jun 3 at 13:16

3 Answers 3

up vote 3 down vote accepted

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.

share|improve this answer
    
What length/measure is set to {1em}? –  Rafal Jun 3 at 11:20
1  
@Rafal It's the separation between the label and the title. Have added more details above. –  Andrew Jun 3 at 13:56
    
Thanks for details. I tried to modify it earlier to check effect, but since I was using \section*{} I didn't see any change as there was no label :) –  Rafal Jun 3 at 14:39

You could do like this:

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

\titlespacing*{\section}{0pt}{3ex plus 2ex}{1ex}
\titleformat*{\section}{\fontsize{12}{12}\bfseries}

\setcounter{secnumdepth}{0}
\pagestyle{plain}

\begin{document}

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

\lipsum[2]

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

\lipsum[3]

\end{document}

With a 10pt main font size, \large chooses 12pt size.

However, this will give uneven spacing in case characters clash with each other: one with a descender in the first line, one with an ascender in the second line. The example shows it.

I've added also some refinements; the space above a section title should have some flexibility, for instance.

enter image description here

If you want to be independent of the main font size selected as a class option, change the \titleformat* line above into

\makeatletter
\titleformat*{\section}{\large\fontsize{\f@size}{\f@size}\bfseries}
\makeatother

With \large we select the size specified by the class option; this stores the size in \f@size and we use it for both arguments.


The final version, with barbara beeton's recommended \raggedright:

\makeatletter
\titleformat*{\section}{\raggedright\large\fontsize{\f@size}{\f@size}\bfseries}
\makeatother

Complete example

\documentclass[final,12pt]{book}
\usepackage{lipsum}
\usepackage{titlesec}

\titlespacing*{\section}{0pt}{3ex plus 2ex}{1ex}
\makeatletter
\titleformat*{\section}{\raggedright\large\fontsize{\f@size}{\f@size}\bfseries}
\makeatother

\setcounter{secnumdepth}{0}
\pagestyle{plain}

\begin{document}

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

\lipsum[2]

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

\lipsum[3]

\end{document}

enter image description here

share|improve this answer
    
From my testing it seems that arguments of \fontsize command ({12}{12}) are responsible for font size and line spacing respectively. Am I right? –  Rafal Jun 3 at 11:35
    
@Rafal Yes, you're right. –  egreg Jun 3 at 11:37
    
gee, this would really look nicer -- and be more readable -- without the overly wide inter-word spacing. (most obvious in the "lorem ipsum" example, and it can get lots worse.) ragged right, please. –  barbara beeton Jun 3 at 13:19
    
@barbarabeeton Your wish is my command. ;-) –  egreg Jun 3 at 13:36
\def \mysection#1{%  <-----
  \section*{\linespread{0.1}\large#1}% <-----
}

Otherwise you'll get a trailing space in front of the title

share|improve this answer
1  
It is important to note that space between \linespread and \large commands also needs to be removed. It is that way in your example, but just wanted to note that in case it isn't obvious for someone else that this space can create trailing space in document as well. –  Rafal Jun 3 at 11:30

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.