# How to get rid of space between section and equation

I'm making a cheatsheet and need to get rid of as much unneeded space as possible. However, I just can't figure out how to get rid of this space that is added between a (sub)section title and an equation:

MWE:

\documentclass[8pt]{extarticle}
\usepackage[paper=a5paper]{geometry}

\usepackage[english]{babel}

\usepackage{mathtools}
\usepackage{amssymb}
\usepackage[nodisplayskipstretch]{setspace}

\usepackage{titlesec}
\titlespacing{\section}{0pt}{0pt}{0pt}
\titlespacing{\subsection}{0pt}{0pt}{0pt}
\titlespacing{\subsubsection}{0pt}{0pt}{0pt}

\setlength{\parindent}{0pt}
\setlength{\parskip}{0pt}
\setlength{\parsep}{0pt}
\setlength{\lineskip}{0pt}
\setlength{\intextsep}{0pt}

% Display mode equation spacing
\expandafter\def\expandafter\normalsize\expandafter{%
\normalsize
\setlength\abovedisplayskip{0pt}
\setlength\belowdisplayskip{0pt}
\setlength\abovedisplayshortskip{0pt}
\setlength\belowdisplayshortskip{0pt}
}

\begin{document}
\section{Section}
\begin{equation*}
q = q_0 + q_1i + q_2j + q_3k = (q_0,q_1,q_2,q_3)
\end{equation*}
\section{Section}
Some text
\begin{equation*}
q = q_0 + q_1i + q_2j + q_3k = (q_0,q_1,q_2,q_3)
\end{equation*}
More text
\end{document}

• Welcome to TeX.SX! It's not at all recommended to start a paragraph with a math display. Jan 30, 2018 at 11:48
• Thanks! As I said, I'm creating a cheat sheet, so many sections contain nothing but equations. I guess I could redefine the section command to work around my problem but I would like a more elegant solution. Jan 30, 2018 at 12:56
• Do you really need \section, then? Jan 30, 2018 at 12:57
• Not necessarily, but I would like to keep the conveniences of \section, such as being able to configure the spacing using titlespacing. Of course this could also be done with a custom command, but that seems like a complicated solution. Jan 30, 2018 at 13:41
• Also seems like you used both ways from here : tex.stackexchange.com/questions/410946/…... but one of them would be enough I suppose. In my answer I just used your code's "\usepackage[nodisplayskipstretch]{setspace}" without really needed that Jan 30, 2018 at 13:51

## 3 Answers

Use \noindent before :

\section{Section}
\noindent\begin{equation*}
q = q_0 + q_1i + q_2j + q_3k = (q_0,q_1,q_2,q_3)
\end{equation*}


The reason: TeX operates in vertical mode after \section. If display math starts in vertical mode, the horizontal mode is opened immediately and the line with \parindent is preceded. But you can avoid this line when there is no \parindent. This can be done by \noindent.

You can insert \noindent before each : if the horizontal mode is opened already the \noindent does nothing. You can create your macro which generates \noindent or re-define .

• Thanks, that works! But I don't really understand why - I already have \setlength{\parindent}{0pt}, wouldn't that normally make \noindent unneeded? Jan 30, 2018 at 13:47
• @danmou A box with \parindent width is inserted when horizontal mode started. No matter that \parindent=0pt. The box with zero width is inserted in such case. There is only one situation when this \parindent box is not inserted: when the horizontal mode begins with \noindent. Finally: if \parindent box is inserted and display math is immediately followed then empty line is inserted before such display math. Jan 30, 2018 at 14:40

For a cheatsheet, I believe it's better to redefine \section and \subsection.

\documentclass[8pt]{extarticle}
\usepackage[paper=a5paper]{geometry}

\usepackage[english]{babel}

\usepackage{mathtools}
\usepackage{amssymb}

\renewcommand{\section}[1]{%
\par
\stepcounter{section}%
\everypar{\textbf{\thesection\ #1}\quad\everypar{}}%
}
\renewcommand{\subsection}[1]{%
\par
\stepcounter{subsection}%
\everypar{\textbf{\thesubsection\ #1}\quad\everypar{}}%
}

\setlength{\parindent}{0pt}
\setlength{\parskip}{0pt}
\setlength{\parsep}{0pt}
\setlength{\lineskip}{0pt}

\AtBeginDocument{%
\setlength\abovedisplayskip{0pt}%
\setlength\belowdisplayskip{0pt}%
\setlength\abovedisplayshortskip{0pt}%
\setlength\belowdisplayshortskip{0pt}%
}

\begin{document}

\section{Section}
\begin{equation*}
q = q_0 + q_1i + q_2j + q_3k = (q_0,q_1,q_2,q_3)
\end{equation*}

\section{Section}
Some text for explaining the following equation
\begin{equation*}
q = q_0 + q_1i + q_2j + q_3k = (q_0,q_1,q_2,q_3)
\end{equation*}
More text follows the equation

\end{document}


• Thanks! But \makeatletter shouldn't be needed right? I like the way you use \AtBeginDocument instead of the hack I was using (I got it from this question, you might want to add your solution there). Jan 30, 2018 at 14:10
• @danmou I forgot to remove it, thanks for noting. Jan 30, 2018 at 14:12

Two ways to solve your problem:

\documentclass[8pt]{extarticle}
\usepackage[paper=a5paper]{geometry}
\usepackage[english]{babel}
%\usepackage{parskip}
%\setlength{\parindent}{0pt}
\usepackage{mathtools}
\usepackage{amssymb}

\usepackage{titlesec}
\titlespacing{\section}{0pt}{0pt}{0pt}
\titlespacing{\subsection}{0pt}{0pt}{0pt}
\titlespacing{\subsubsection}{0pt}{0pt}{0pt}

\def\mycommand{\setlength{\abovedisplayskip}{0pt}%
\setlength{\belowdisplayskip}{0pt}%
\setlength{\abovedisplayshortskip}{0pt}%
\setlength{\belowdisplayshortskip}{0pt}}

\AtBeginDocument{\mycommand}

\let\oldnormalsize\selectfont
\def\selectfont{\oldnormalsize\mycommand}

\newenvironment{myeq}{\vspace{-10pt}\par\begin{equation*}}{\end{equation*}\vspace{-10pt}}

\newsavebox{\mybox}
\newlength{\mylength}
\def\myinleq#1{\savebox\mybox{$#1$}\settowidth{\mylength}{\usebox{\mybox}}\hspace{0.5\dimexpr\linewidth-\mylength-2\parindent\relax\relax}$#1$}

\begin{document}
\section{Section}
\begin{myeq}
q = q_0 + q_1i + q_2j + q_3k = (q_0,q_1,q_2,q_3)
\end{myeq}

\begin{myeq}
q_2 = q_0 + q_1i + q_2j + q_3k = (q_0,q_1,q_2,q_3)
\end{myeq}%emptyline needed here

\myinleq{q_3 = q_0 + q_1i + q_2j + q_3k = (q_0,q_1,q_2,q_3)}

\section{Section}
\noindent Some text
\begin{equation*}
q = q_0 + q_1i + q_2j + q_3k = (q_0,q_1,q_2,q_3)
\end{equation*}
More text
\end{document}