95

I know this subject has been mentioned before, but I'm still having a problem in creating a space under a section title. I used the titlesec package and then tried using the command \titlespacing{\section}...

This is my code. Can anybody help?

\documentclass[10pt]{book}
\usepackage[utf8]{inputenc}
\usepackage{fullpage}
\usepackage[compact]{titlesec}
\usepackage{setspace}
\usepackage{sectsty}
\chapterfont{\huge}
\sectionfont{\Huge}
\begin{document}
\chapter*{Kapitel 1}
\section*{Einleitung}
\begin{large}
\subsection*{1.1 Motivation}
abdu
  • 1,849
  • 1
    Welcome to TeX.SX. A tip: If you indent lines by 4 spaces, then they're marked as a code sample. You can also highlight the code and click the "code" button ({}) or hit Ctrl+K. – Claudio Fiandrino Apr 14 '13 at 13:05
  • Thanks! But sorry I didn't get what you mean. So leave the titlespacing and just use the \ to make the spaces? – abdu Apr 14 '13 at 13:56
  • @user29019 but exactly what kind of space are you trying to get? Exactly how much space do you want after \chapter and \section? – Gonzalo Medina Apr 14 '13 at 14:55
  • @GonzaloMedina yes I tried it, it works. The \begin{large} is for the font size of the text. So everything is working till now. But I need create a titlespacing under the 'Einleitung' so between the section title und the text beneath it. I'm a new LaTeX user, I'm writing a scientific thesis. so there would be chapters, section and subsections. I searched in the internet for packages and commands I found things like \vspace, \titlespacing{\section}... but non of them worked, I compile the .tex file through a terminal (ubuntu) but I get no changes. – abdu Apr 14 '13 at 16:16
  • So I need like about 3 empty lines between section title and subsection title! – abdu Apr 14 '13 at 16:20
  • @abdu please compile the following test document exactly as it is: `\documentclass[10pt]{book} \usepackage{titlesec}

    \titlespacing*{\section} {0pt}{3.5ex plus 1ex minus .2ex}{8.3ex plus .2ex}

    \begin{document}

    \chapter{Kapitel 1} \section{Einleitung} \subsection{Motivation}

    \end{document}`. Is it something like that what you need?

    – Gonzalo Medina Apr 14 '13 at 19:21
  • @GonzaloMedina, yes! That's what I need! So how can I change the spacings? What are these numbers? 3.5ex? Can I also do the same for the Chapter? And does this also work with the document class 'report'? Thanks a lot! – abdu Apr 14 '13 at 19:49
  • @GonzaloMedina, by the way, do you think that the packages conflict with each other? your code worked. I then added the spacing command to my code, and it didn't work. And one more question, I always get the error 'no line here to end', shall I just ignore it? Or can I avoid it somehow? Thanks!

    Ok I think sectsty and titlesec don't like each other. I just tried both together in a simple code, and the titlespacing didn't work again, so this is the problem I think.

    – abdu Apr 14 '13 at 20:07
  • @abdu please see my answer. The error "there's no line here to end" appears because perhaps you are using \\ incorrectly. – Gonzalo Medina Apr 14 '13 at 21:28

4 Answers4

130

Using the titlesec package you can use \titlespacing* you can change the spacing before and after the title; the syntax of the command is:

\titlespacing*{<command>}{<left>}{<before-sep>}{<after-sep>}

(there's an additional optional argument, but it's not important here). <left> increases the left margin; <before-sep> controls the vertical space before the title; <after-sep> controls the vertical space after the title. (Please refer to the package documentation for further information). A complete example:

\documentclass[10pt]{book}
\usepackage{titlesec}
\usepackage{lipsum}% just to generate text for the example

\titlespacing*{\section}
{0pt}{5.5ex plus 1ex minus .2ex}{4.3ex plus .2ex}
\titlespacing*{\subsection}
{0pt}{5.5ex plus 1ex minus .2ex}{4.3ex plus .2ex}

\begin{document}

\chapter{Kapitel 1}
\lipsum[4]
\section{Einleitung}
\lipsum[4]
\subsection{Motivation}
\lipsum[4]

\end{document}

enter image description here

I used ex (approximately the height of an "x" in the current font) as the unit for the lengths used, but you can use instead any other valid LaTeX unit (cm, in, mm, pt, among others); you can also use multiples of predefined lengths such as \baselineskip:

\titlespacing*{\subsection}
  {0pt}{2\baselineskip}{3\baselineskip}

I used ex since in this way the space is font-dependent. If you want to change the formatting of the titles, you can also use the same package and its powerful \titleformat command.

Gonzalo Medina
  • 505,128
  • thanks very much! Everything worked, so now I know that I only have to use the titlesec package. I used the cm unit to change the spacing before the chapter and it worked! However I still have a problem, the one with the error. I can ignore it but it bothers while compiling through the terminal. I usually seperate two paragraphs with \ \ and this makes the error I think. Is there a way I can seperate paragraphs? – abdu Apr 15 '13 at 11:08
  • @abdu Yes, using two consecutive \ commands will result in errors. You can use \par\bigskip instead at the end of each paragraph. Another option would be to load the parskip package which automatically suppresses indentation and increases separation between paragraphs. Yet another option would be to switch to the document class scrreprt and use its parskip features. – Gonzalo Medina Apr 15 '13 at 12:49
  • \pas\bigskip worked. I tried the parskip package by defining the skip length: \setlength{\parskip}{0pt} and then typing \parskip between the paragraphs, but I got errors while compiling. – abdu Apr 15 '13 at 13:16
  • @abdu The idea with the parskip package is just to load it; no need to set \parskip nor to explicitly call \parskip. You don't need the line \setlength{\parskip}{0pt} (you can delete it) and you don't need to use \parskip in your document. – Gonzalo Medina Apr 15 '13 at 13:34
  • alright! I'll just use \par\bigskip. Thanks one more time for all the infos, a big help! – abdu Apr 15 '13 at 13:39
  • 2
    Why do you use parameters such as 5.5ex plus 1ex minus .2ex? Why not just 6.3ex? – Adam_G Nov 04 '15 at 15:26
  • 1
    @Adam_G The first option allows for stretching or shrinking; the second one, doesn't. – Gonzalo Medina Nov 04 '15 at 17:46
  • @GonzaloMedina - I'm not sure I understand this. – Adam_G Nov 04 '15 at 21:29
  • 2
    @Adam_G texdoc texbytopic (or The TeXbook) and search for glue. The answer to http://tex.stackexchange.com/q/64756/3954 can also give you an explanation. – Gonzalo Medina Nov 04 '15 at 23:50
  • How do you keep one of the non-optional arguments the same (default)? I only want to change the <after sep>. – Fr4nc3sc0NL May 27 '20 at 15:52
  • To answer my own question: you can look up the default values in section 9.2 of the package documentation (linked in above answer) – Fr4nc3sc0NL May 27 '20 at 18:21
  • instead of 5.5ex plus 1ex minus .2ex can I just use -0.5mm ? – alper Oct 04 '22 at 16:50
31

If you don't need all the "baggage" of an additional package like titlesec, you can simply modify these aspects of your headings yourself. See this example. I use a few sections and save the original section definition, so we can see a comparison.

I then define and use a section "prelude" prior to the invocation of the original section command. Then I add a "postlude" to the sectioning command. The prelude and the postlude here both add an extra em of vertical space, but you could use it to put in other sectioning highlights like rules, etc.

\documentclass{article}
\begin{document}
\section{First Section}

This is the first line of text.  Note the vertical spacing.  

\section{Second Section}

Observe the spacing prior to and following the sectioning command. Now let me
redefine a few things.

\makeatletter
\let\origsection\section
\renewcommand\section{\@ifstar{\starsection}{\nostarsection}}

\newcommand\nostarsection[1]
{\sectionprelude\origsection{#1}\sectionpostlude}

\newcommand\starsection[1]
{\sectionprelude\origsection*{#1}\sectionpostlude}

\newcommand\sectionprelude{%
  \vspace{1em}
}

\newcommand\sectionpostlude{%
  \vspace{1em}
}
\makeatother

\section{Next Section}
Did this text drop 1em lower relative to the heading than the prior
section?  If so, we have succeeded.

\section{Final Section}

And the result is permananent, as you can see.

\end{document}

enter image description here

  • 1
    Hey thanks for your answer Steven. But I think I'll use the titlesec package just because I know how it works now. And since I'm using the report class I need chapters sections and subsections it's makes more sense to use the titlesec package. But thanks a lot, I'll save your code and try to use it in the future! =) – abdu Apr 15 '13 at 11:01
  • 1
    @abdu. I understand completely. It's just one more option, depending on the particular need. – Steven B. Segletes Apr 15 '13 at 11:03
  • Nice! How would this work if I wanted to do the same for \subsection*'s? – texfan Aug 09 '16 at 13:52
  • 3
    @texfan If you take my source and do a global search and replace from section to subsection, recompile and see the result. – Steven B. Segletes Aug 09 '16 at 13:55
  • @StevenB.Segletes Ah ok, I thought maybe there was a way to do both at once, but this works, thanks :)! – texfan Aug 10 '16 at 09:49
  • @texfan The only duplicative code would occur if you wanted the \sectionprelude to be identical to the \subsectionprelude (also for postlude). Then, you could have a common prelude/postlude, without duplication. – Steven B. Segletes Aug 10 '16 at 11:30
12

In case anybody wants to adjust the spaces while using KOMAscript -- @gonzalo's answer is not recommended since KOMA and titlesec are incompatible. However, KOMA provides commands to modify the section commands. The command

\RedeclareSectionCommand[beforeskip=-5.5ex plus -1ex minus -.2ex,afterskip=4.3ex plus -.2ex]{section}

modifies the space before and after a section title to similar values.

Twonky
  • 750
  • 2
    This is a very important "detail" indeed! Note that the negative vale for beforeskip doesn't work too well, but the command itself is exactly what is needed when using e.g. \documentclass{scrartcl} – Dalker May 01 '21 at 13:56
7

To control each space individually you can simply use:

\vspace{5mm} or \vspace{1em} or \vspace{1cm} to increase (to add space) and;

\vspace{-5mm} or \vspace{-1em} or \vspace{-1cm} to decrease (take from the standard space).

It is most suitable to control few sections of the standard space.

Follow a working example:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
 \geometry{a4paper,
 total={170mm,257mm},
 left=20mm, top=20mm,}

\title{Spacing before and after section titles, \LaTeX} \author{} \date{}

\begin{document}

\maketitle

\section{\huge Increase \Large space between headings and text}

\subsection{With (\texttt{\textbackslash vspace{4mm}})} \vspace{4mm} Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium.

\subsection{With (\texttt{\textbackslash vspace{0.5em}})} \vspace{0.5em} Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium.

\subsection{With (\texttt{\textbackslash vspace{0.6cm}})} \vspace{0.6cm} Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium.

\vspace{1cm} \hrulefill \vspace{1cm}

\section{\huge Decrease \Large space between headings and text}

\subsection{With (\texttt{\textbackslash vspace{-3.5mm}})} \vspace{-3.5mm} Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium.

\subsection{With (\texttt{\textbackslash vspace{-1em}})} \vspace{-1em} Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium.

\subsection{With (\texttt{\textbackslash vspace{-0.44cm}})} \vspace{-0.44cm} Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium.

\end{document}

enter image description here

jared
  • 231