# Reducing the line spacing of math environments that support multi-lined contents

I’m trying to reduce the line spacing of “math environments that support multi-lined contents” back to singlespacing in a onehalfspacing document. I’m using the setspace and mathtools packages.

Added: By “math environments that support multi-lined contents”, I mean any math environments that allow typesetting multiple lines of contents, no matter whether they are in inline or display math mode. This includes, for instance, the matrix environment in inline math mode.

My current two-step solution is

1. creating a macro that does \linespread{1}\selectfont at the beginning of each math environment that can be fixed this way, and
2. writing special patches for those that cannot be fixed by Step 1.

This solution produces the following MWE:

\documentclass[draft]{article}
\usepackage[nodisplayskipstretch]{setspace}
\setstretch{2}% more text spacing just for exaggeration
\usepackage{mathtools}

\usepackage{etoolbox}
% Let the patches begin!
\makeatletter
\def\mathlinespread{\setspace@singlespace}% 1 by default, can be changed
\AtBeginEnvironment{#1}{%
% TeX
% amsmath
% special care
\patchcmd{\start@gather}
{\collect@body}
{}{}
\patchcmd{\start@align}
{\collect@body}
{}{}
\patchcmd{\start@multline}
{\collect@body}
{}{}
\patchcmd{\gather@split}
{}{}
% mathtools
\makeatother

\begin{document}
Subarray and small matrices are not affected by \verb|\setstretch{2}|
$\begin{psmallmatrix*}[r] -a & b \\ c & -d \end{psmallmatrix*}$ and
$\sum_{\substack{i=1\\i\neq j}}^n x_i$
With my patches, matrices and cases work fine in inline math mode:
$\begin{Vmatrix} -a & b \\ c & -d \end{Vmatrix}$,
$\begin{drcases*} -a & Words \\ c & Text \end{drcases*} = f(x)$.
If I patch \verb|\env@matrix| and \verb|\env@cases| directly, then the
patches only apply to \verb|amsmath|'s environments, not to
\verb|mathtools|.

Here are some inner multi-line math environments:
$$\begin{split} a_1, a_1, a_1, a_1, \\ a_1, a_1, a_1, \\ a_1, a_1, \\ \end{split} \quad \begin{gathered} a_1, a_1, a_1, a_1, \\ a_1, a_1, a_1, \\ a_1, a_1, \\ \end{gathered} \quad \begin{lgathered} a_1, a_1, a_1, a_1, \\ a_1, a_1, a_1, \\ a_1, a_1, \\ \end{lgathered}$$
And an outer:
\begin{align}
a & = b + c \\
& = d + e + f
\end{align}
\begin{singlespace}
Note that \verb|\RestoreMathEnvironmentLeading{split}| will not work!
Yet the method applies to \verb|aligned|, \verb|alignedat|, \verb|gathered|,
\verb|multlined|, \verb|lgathered| and \verb|rgathered|.
Also, the patch for \verb|split| happens after \verb|\collect@body|,
while those for \verb|gather|, \verb|align|, \verb|multline| happen before
\verb|\collect@body|.

Note that \verb|aligned| and \verb|alignedat| can be fixed by patching
\verb|\start@aligned| right before \verb|\spread@equation|, exactly like
\verb|split|. However, I only know how to fix \verb|gathered| by using
structurally inconsistent anyway.
\end{singlespace}

\end{document}


Even though I think I’ve covered all environments (some may suggest that the problem is solved), I’m dissatisfied with my solution for 3 reasons:

1. It turns out that align* is a pseudo-environment, hence a special patch is needed. But the three special patches for \start@gather, \start@align and \start@multline are drastically different from the one for split, with the first three happening before \collect@body while the last one after \collect@body. Why the inconsistency?
2. It turns out that matrix and friends from amsmath are also pseudo-environments, so they may be fixed by simply patching \env@matrix. The same goes for cases by patching \env@cases; for aligned and alignedat by patching \start@aligned. However, if used, the positions of these patches vary, and I fear this would cause undesired side-effect.
3. The method in Reason 2 doesn’t apply to matrix* and friends, cases* and friends from mathtools. Also, I can’t find the way to patch gathered from amsmath in that fashion (Note that \end{gathered} is actually \endaligned, again, a pseudo-environment).

## Is there a better and more consistent way to reduce the line spacing of these “math environments that support multi-lined contents”?

Added: By “better and more consistent”, I mean patches that are quantitatively minimum and structurally consistent.

You may have noticed that the subarray (includes \substack), smallmatrix and friends, and eqnarray environments are left out in my solution. For the first two environments, the line spacing is set according to \baselineskip... (p. 33 of the amsmath manual). This prevents setspace from changing their line spacing, which is desired. For eqnarray, well, I’d never, ever use eqnarray.

It seems to me that the concisest solution would be patching the code that governs \\.

Here’s an exhaustive list (I guess?) of “math environments that support multi-lined contents” provided by the mathtools package (and by the amsmath package, which is loaded by mathtools; and by TeX itself):

### TeX

• array;

### amsmath

• matrix, pmatrix, bmatrix, Bmatrix, vmatrix, Vmatrix;
• cases;
• aligned, alignedat;
• gathered;
• gather and friend;
• align and friends;
• multline and friend;
• split;

### mathtools

• matrix*, pmatrix*, bmatrix*, Bmatrix*, vmatrix*, Vmatrix*;
• cases*, dcases, dcases*, rcases, rcases*, drcases, drcases*;
• multlined;
• lgathered, rgathered;
• Have you considered setting the option nodisplayskipstretch when loading the setspace package? Separately, did you see this posting and, in particular, the answers provided by @Herbert and @egreg? – Mico Jul 14 '18 at 6:56
• @Mico Yes and yes. I didn’t include nodisplayskipstretch simply because I wanted to provide a simple MWE. The actual goal is using 1.2 leading to font size ratio for math and 1.5 ratio for CJK text. The solution by @Herbert produces warnings. The solution by @egreg is mentioned at the beginning of Reason 2. – Ruixi Zhang Jul 14 '18 at 7:20
• Which kinds of warnings do you get with @Herbert's method? – Mico Jul 14 '18 at 7:23
• @Mico The warning is mentioned by @egreg in the comments of @Herbert’s answer:LaTeX Font Warning: Command \normalsize invalid in math mode on input line .... On another note, @Herbert’s solution doesn’t cover inline matrices — I know that using matrix and friends in inline text is a typography disasters, and smallmatrix and friends should be used. But notice that the line spacing of smallmatrix and friends is not affected because of the way it is constructed. Same goes with subarray. – Ruixi Zhang Jul 14 '18 at 16:35
• Your posting contains a very broad question -- "Is there a better and more consistent way to reduce the line spacing" -- but what you might deem to be an acceptable answer is somewhat elusive. The warning related to Herbert's method is just that: a warning, and a fairly harmless one at that. AFAICT, your initial posting did not mention inline math mode matrices. To wit, the title of your posting refers to multi-line math environments; that's quite the opposite of inline math stuff, isn't it? You may want to edit your posting a bit to make it clearer what your focus is. – Mico Jul 14 '18 at 17:22