[Tex/LaTex] changing vertical spacing before and after math display mode

math-modepdftexxetex

I am writing a double space document, but I do not want excessive spacing between math display environments. For things like code listings I can use etoolbox and say \AtBeginEnvironment{minted}{\setstretch{1}} etc, is there a way to modify the stretch before / after math display?

\documentclass{article}
\usepackage{lipsum}
\usepackage[margin=0.5in]{geometry}

\usepackage{setspace}
\setstretch{2}

% kind of does the right thing, but only at the bottom...
% \everydisplay{\vspace*{-1em}}

\usepackage{amsmath}

% i think this is only for align?
% \belowdisplayskip=0pt

% this also doesn't work, just for array?
% \setlength{\jot}{-1ex}

\usepackage{etoolbox}
% is math / amsmath an environment?
\AtBeginEnvironment{math}{\setstretch{1}}
\AfterEndEnvironment{math}{\setstretch{2}}

\begin{document}
    \lipsum[1]

    % desired effect without manual every time
    % \setstretch{1}
    $$
        ax + by + cz + d = 0
    $$
    % not really even working
    % \setstretch{2}\vspace*{-2em}

    \lipsum[2]
\end{document}

I want to do this globally, getting rid of the spacing denoted by the red rectangles

enter image description here

I feel like this has to be a duplicate, but I can't get anything I find to work :/ If it matters, the above code was pdftex but I'm writing with xetex. I don't think that makes a difference in this question, but it may.

Best Answer

There are two errors in your input:

You can use the nodisplayskipstretch option to setspace, which will reduce the spacing to acceptable ones.

\documentclass{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{amsmath}
\usepackage[nodisplayskipstretch]{setspace}

\usepackage{lipsum}

\setstretch{2}

\begin{document}

\lipsum*[1]% no \par or blank line before a math display
\[% not $$
ax + by + cz + d = 0
\]% and no blank line after a math display, generally.
\lipsum[2]

\end{document}

enter image description here

For comparison, here is the output without the option

enter image description here

and also what you get with blank lines and $$

enter image description here

I also add the output with \doublespacing (it's commonly believed that double spacing means \setstretch{2}, which is incorrect).

enter image description here

Related Question