How to add space before and after sections and subsections in LaTeX?

Imagine you’re writing a document in LaTeX with many subheadings. But suddenly, you notice that the space before or after the subheadings isn’t right, making the document look messy. You’re wondering, “How can I fix this?” Don’t worry! Managing space in LaTeX is easy.

Now, I’ll show you how to neatly fix the space before and after the subheadings so that your document becomes well organized!

Use the vspace command to adjust spacing

The easiest way is to use the \vspace command. With this, you can add a specific amount of vertical space (space above or below). For example, you can use this command if you need some extra space before or after a subheading.

\documentclass[12pt]{article}
\usepackage{lipsum,xcolor}
\usepackage[top=1.5cm]{geometry} % for margin
\definecolor{simu}{HTML}{F6F8FA}
\begin{document}
\pagecolor{olive!15} % for page color
 \lipsum[1][1-5]
\section*{Introduction}
 \lipsum[2][1-4]
 \vspace{7pt}
\section*{Background}
 \vspace{5pt}
 \lipsum[3][2-8]
 \vspace{17pt}
\section*{Templates}
 \vspace{15pt}
 \lipsum[5][1-7]
\end{document}

Output:

The \vspace command adds extra space before or after subheadings or any text. In other words, it adds more space than the default space that LaTeX already provides.

For example, if there’s already 9pt of space after a section by default, and you add \vspace{5pt}, the total space will be 14pt (default 12pt + 5pt).

Using titlespacing command from titlesec package

Sometimes, you may want to keep the same spacing for all subheadings. Instead of using \vspace every time, you can set specific spacing for the entire document with the help of the titlesec package.

\titlespacing{command}{left}{before}{after}

Here, command is the subheading command like \section or \subsection, and left, before, and after refer to the left indent, space before, and space after the subheading, respectively.

The \titlespacing command gives you full control over the spacing, meaning it completely changes the default spacing and sets new spacing.

\documentclass[12pt]{article}
\usepackage{titlesec,lipsum,xcolor}
\usepackage[top=1.5cm,left=2cm, right=2cm]{geometry}
\titlespacing*{\section}{0pt}{17pt}{20pt}
\titlespacing*{\subsection}{0pt}{15pt}{8pt}
\begin{document}
\pagecolor{olive!10}
 \lipsum[1][1-6]
\section*{Introduction}
 \lipsum[2][1-5]
\section*{Background}
\lipsum[3][2-7]
 \subsection{Yellow}
   \lipsum[4][2-5]
 \subsection{Orange}
   \lipsum[4][1-4]
\section*{Templates}
 \lipsum[5][1-7]
\end{document}

Output:

For example, if you use \titlespacing{\section}{0pt}{10pt}{5pt}, you’re setting the exact space before and after the section. The default spacing has no effect here because you’re applying custom spacing.

Conclusion

No matter which method you choose, both are simple and effective. But always remember, the important thing is how you want to maintain the structure of your document.

Do you want more space before the subheading? Or more space after it? Set the spacing based on that.

Share tutorial

Leave a Comment

Your email address will not be published. Required fields are marked *