134

I've searched many sites but I couldn't get any answer. Here is this mystical question:

How can i reduce the spacing after headings?

Here is my code.

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[turkish]{babel}
\usepackage[left=3.50cm, right=2.50cm, top=3.00cm, bottom=3.00cm,nohead,nofoot]{geometry}
 \usepackage[overload]{textcase} 
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{pslatex}
\usepackage[explicit]{titlesec}
\linespread{1.5}

\usepackage{setspace}
\setlength{\parindent}{0pt}
\setlength{\parskip}{\baselineskip}

\addto\extrasturkish{\uccode`i=\string"9D \lccode`I=\string"19 }
\addto\noextrasturkish{\uccode`i=`I \lccode`I=`i }

\titleformat{\section}
  {\normalfont\normalsize\bfseries}{\thesection}{1em}{\MakeTextUppercase{#1}}
\titleformat{\subsection}
  {\normalfont\normalsize\bfseries}{\thesubsection}{1em}{#1}
\titleformat{\subsubsection}
  {\normalfont\normalsize\bfseries\itshape}{\thesubsubsection}{1em}{#1}

\usepackage{chngcntr}

My actual purpose is to set line spacing 1.5 lines. This is a MS Word template that I must use for my project report. When I use \setlength{\parskip}{\baselineskip} with \linespread{1.5} the gap between section headings and paragraphs are huge, so huge it is funny. If I don't use \linespread{1.5} the lines are not spreading as expected. If I don't use \setlength{\parskip}{\baselineskip} this time there is no spacing between paragraphs.

lockstep
  • 250,273
interlude
  • 2,316

5 Answers5

176

You've loaded the titlesec package- it provides the command \titlespacing which has the format

\titlespacing{command}{left spacing}{before spacing}{after spacing}[right]

From the titlesec package

% Spacing: how to read {12pt plus 4pt minus 2pt}
% 12pt is what we would like the spacing to be.
% plus 4pt means that TeX can stretch it by at most 4pt.
% minus 2pt means that TeX can shrink it by at most 2pt.
% This is one example of the concept of, 'glue', in TeX.

A complete MWE follows

\documentclass{article}
\usepackage{lipsum}
\usepackage{titlesec}

\titlespacing\section{0pt}{12pt plus 4pt minus 2pt}{0pt plus 2pt minus 2pt} \titlespacing\subsection{0pt}{12pt plus 4pt minus 2pt}{0pt plus 2pt minus 2pt} \titlespacing\subsubsection{0pt}{12pt plus 4pt minus 2pt}{0pt plus 2pt minus 2pt}

\begin{document} \section{Section} \lipsum[1]

\end{document}

rainman
  • 583
cmhughes
  • 100,947
  • Thank you. Good explanation and good answer. I've changed 0pt to -5 pt and this really looks good. – interlude Apr 25 '12 at 20:22
  • Do you know how the gap between "1" and "Section" is set? The \titlespacing parameters don't seem to adjust that. – Steven B. Segletes Jan 13 '15 at 15:56
  • @steven: I believe that titlespacing is vertical space only; not sure of the official way to tweak the gap you describe, but I usually use an ugly hack involving titleformat and hspace – cmhughes Jan 13 '15 at 16:39
  • Found it. In the definition of \@sect, one will find \begingroup #6{\@hangfrom {\hskip #3\relax\@svsec}\interlinepenalty\@M #8\@@par}\endgroup. A kern can be added after \@svsec... Actually, that's close, but it's more complicated than that, to account for * sections. But I'll get there. – Steven B. Segletes Jan 13 '15 at 17:59
  • But even better, \@seccntformat is defined as {\csname the#1\endcsname\quad} The \quad represents the gap of interest and can be changed. – Steven B. Segletes Jan 13 '15 at 18:27
  • Nice one. Works fine. But it removes the numbering of the section headings. Is there any way to bring back the section numbers ? – Debashish Nov 24 '16 at 13:57
  • I never understood what is the use of plus and minus in such commands could you kindly give a link where to understand their purpose? – Ébe Isaac May 13 '17 at 06:06
  • That is great but it does not change the space between section title and the starting point when I change first number (12) – Turkan Jul 08 '21 at 05:35
  • it might be worth noting that the titlesec package must be loaded after the sectsty package (the latter can be used to change the color of section titles) otherwise it has no effect. – Alain Aug 15 '21 at 19:23
34

You can use the \titlespacing command from the titlesec package. For example:

\titlespacing{\section}{0pt}{\parskip}{-\parskip}
\titlespacing{\subsection}{0pt}{\parskip}{-\parskip}
\titlespacing{\subsubsection}{0pt}{\parskip}{-\parskip}

See titlesec.pdf for more options.

yori
  • 5,681
  • 29
  • 59
  • I found this suggestion work the best for markdown with YMAL header rendered by pandoc – Yu Shen Mar 21 '17 at 00:20
  • 2
    Does \parskip include the "glue" that the author of titlesec describes? There's no mention of ± anything in the documentation. Why would you use \parskip instead of the more specific lengths that @cmhughes advocates? – Merchako Apr 27 '20 at 23:46
11

If you are using setspace, then mixing it with your own \linespread{1.5} is probably not a good idea.

Try to delete the line about \linespread and use instead

\usepackage{setspace}
\onehalfspacing

The package documentation says that it does more than just change line spread: it also take care of skips before and after headings, displays, etc. - the things you say you need.

Boris
  • 38,129
3

To control each space individually, it is possible to use \vspace{-5mm} or \vspace{-1em} or \vspace{-1cm}.

Follow a working example:

    \documentclass{article}
    \usepackage[utf8]{inputenc}
    \usepackage{geometry}
     \geometry{a4paper,
     total={170mm,257mm},
     left=20mm, top=20mm, }
\title{Space handling in \LaTeX}
\author{Yuri Gelsleichter}
\date{December 2020}

\begin{document}

\maketitle

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

\subsection{Using (\texttt{\textbackslash vspace\{5mm\}})}
\vspace{5mm}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.

\subsection{Using (\texttt{\textbackslash vspace\{1em\}})}
\vspace{1em}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.

\subsection{Using (\texttt{\textbackslash vspace\{0.7cm\}})}
\vspace{0.7cm}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.

\vspace{2cm}
\hrulefill
\vspace{2cm}

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

\subsection{Using (\texttt{\textbackslash vspace\{-4mm\}})}
\vspace{-4mm}

Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

\subsection{Using (\texttt{\textbackslash vspace\{-1em\}})}
\vspace{-1em} 

Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

\subsection{Using (\texttt{\textbackslash vspace\{-0.4cm\}})}
\vspace{-0.4cm}

Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

\end{document}

enter image description here

  • Hi, and welcome to TeX.SE. Can you provide to a complete full code starting from \documentclass ..(packages)...\begin{document} ....\end{document}. – Sebastiano Sep 29 '20 at 14:46
  • 1
    @YuriGelsleichter I added an image of the output. For next time, it can easily be done when editing the post by clicking on the image icon. If you're not sure how to do it when you write or edit a post, you can click on the question mark on the upper right corner of the box, and more details will appear. – Vincent Dec 02 '20 at 17:59
  • 1
    Hi @Sebastiano, thanks very much! I was not aware that I could add image yet – Yuri Gelsleichter Dec 03 '20 at 01:31
  • @YuriGelsleichter Welcome into the TeX.SE. community again and all the best. – Sebastiano Dec 05 '20 at 09:16
1

In case you are using a KOMA-Script based document class (e.g. scrartcl), you can't (or rather: shouldn't) use the titlesec package as it is incompatible with KOMA-Script.

Instead, you can follow the approach given here, which suggests to use the \RedeclareSectionCommand macro. A minimal example of this could look like this (copied from the linked answer):

\documentclass[
  parskip=half% inserts additional space after \section
]{scrartcl}

\usepackage{lipsum} \usepackage{multicol}

\RedeclareSectionCommand[afterskip=1sp]{section}

\begin{document} \rule{\textwidth}{15cm}

\begin{multicols}{2}
    \section*{Section 1}
    \lipsum[1-2]
\end{multicols}

\end{document}

Raven
  • 3,023