4

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.

2 Answers 2

1

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

1
  • Excellent, works like a charm! That was actually the first time I ever used lipsum, it seems to be the standard for these kinds of questions. For those reading this, the implication is that in your TeX you need to be careful not to have two newlines before / after your \[ equations for them to be considered in the same "paragraph" (see the code in this answer). As far as double spacing goes, I believe the main difference is best described here, but this is new to me so I could be wrong. Thanks @egreg!
    – svenevs
    Jan 19, 2018 at 0:11
5

The solution for this is to modify the next lengths:

\abovedisplayskip, \belowdisplayskip, \abovedisplayshortskip and \belowdisplayshortskip

Example that shows what happens:

\documentclass{article}

\usepackage{lipsum}

\begin{document}

{\Large Normal}

test text that has to be long enought to go over the formula
\[f(x)=3\]
test continue text that will be long enought too

test text that is short
\begin{equation}
  x=f(y)
\end{equation}
test continue text that will be long enought too

{\Large All Zero}

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


test text that has to be long enought to go over the formula
\[f(x)=3\]
test continue text that will be long enought too

test text that is short
\begin{equation}
  x=f(y)
\end{equation}
test continue text that will be long enought too

{\Large normal 10pt short 5pt}

\setlength{\abovedisplayskip}{10pt}
\setlength{\belowdisplayskip}{10pt}
\setlength{\abovedisplayshortskip}{5pt}
\setlength{\belowdisplayshortskip}{5pt}


test text that has to be long enought to go over the formula
\[f(x)=3\]
test continue text that will be long enought too


\lipsum[3]

test text that is short
\begin{equation}
  x=f(y)
\end{equation}
test continue text that will be long enought too

\end{document}

Edit after OP's (first) comment:

You can add this code to your preamble. Every font change, will include a \selectfont command (hidden or not) and so, it will survive after font changes too (size and shape I think).

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

\let\oldselectfont\selectfont
\def\selectfont{\oldselectfont\mycommand}

\mycommand

Source: http://latex.org/forum/viewtopic.php?t=4993 from Stefan Kottwitz.

Output:

enter image description here

PS: The short skip used more in cases that big is not necessary to separate clean enough the math from the text. (amsmath package uses more options for these skips.. for more about this here: abovedisplayskip vs abovedisplayshortskip)

PS2: See @dalief's comment

6
  • 1
    Note that the sizes of these lengths are normally tied to the font changing commands, such as \normalsize, so for example changes the in the preamble, does not survive for long as there are many places where \normalsize gets executed.
    – daleif
    Jan 18, 2018 at 8:39
  • I see, thank you for the information. So the global answer is: it cannot be done globally, end of story? (because as soon as font size changes / \normalsize occurs again, it will reset.)
    – svenevs
    Jan 18, 2018 at 12:47
  • You can "include it" in every change of font like \def\mycommand{<all this length changes>} \let\oldselectfont\selectfont and \def\selectfont{\oldselectfont\mycommand} and similar if needed ... Not Tested
    – koleygr
    Jan 18, 2018 at 17:22
  • I see. This definitely works, but knowing myself I won't be careful enough to remember to do it...Thank you very much for your thorough answer though!
    – svenevs
    Jan 19, 2018 at 0:05
  • 1
    @sjm324 it happens automatically when you paste in your preamble the last code after my edit... You don't have to remember antything...
    – koleygr
    Jan 19, 2018 at 0:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.