[Tex/LaTex] Set a minimum space above and below displayed math

equationsline-spacingmath-mode

NOTE: This question is specifically about adding vertical space that increases to compensate for tall symbols. Thus, it is not a duplicate of How can I change the whitespace above and below math displays?.

I'm trying to revise my university's thesis class to produce spacing with displayed math that works a little bit better with double spacing (which is required). What I have in mind is this:

  1. The distance above and below a displayed math environment should be the same as the distance between the top of a typical line of text and the bottom of the preceeding line. In particular, symbols that go even slightly above the "usual" height should lower the baseline.
  2. Within a multiline displayed math environment such as align or eqnarray, the distance between baselines should be the same as \baselineskip unless this would cause the distance between symbols to go below (say) 6pt, in which case the baseline should be lowered so that the distance between symbols is exactly 8pt.

For instance, in the document below, the space between the two Z= lines needs to be increased, and the space below the bottom math line needs to be increased even more.

enter image description here

How can this be accomplished? (By changing settings in the preamble; I'm writing a document class, not playing with specific pieces of math.)

Here's what I've got so far:

\documentclass[10pt]{article}

%Code that belongs in the class file:
\makeatletter
    \RequirePackage[doublespacing]{setspace}
    \AtBeginDocument{%
%        \lineskiplimit=0pt%
%        \lineskip=\dimexpr\baselineskip-\f@size pt\relax%
        \abovedisplayshortskip=0pt%
        \belowdisplayshortskip=0pt%
        \abovedisplayskip=0pt%
        \belowdisplayskip=0pt%
        \jot=0pt%
    }
\makeatother

%Code that belongs in the preamble:
\usepackage{amsmath}
\usepackage{lipsum}

\begin{document}
\lipsum*[2]
\begin{align*}
f \colon X &\to Y \\
x &\mapsto y \\
Z &= \left\lfloor\frac{\mathcal E \hat{} \otimes \mathcal E}{g\mathcal E \hat{} \otimes \mathcal E} \right\rfloor \\
Z &= \left\lfloor\frac{\mathcal E \hat{} \otimes \mathcal E}{g\mathcal E \hat{} \otimes \mathcal E} \right\rfloor
\end{align*}
\lipsum*[2]
\end{document}

Best Answer

I agree with @egreg (especially) that typesetting 10/20pt is not a card of the good typography. I have a special and extensive notice about this bad practise in theses mentioned in my templates for theses. But the problem from TeXnical point of view is very interesting and it seems to be non-trivial. This is the reason why I tried something...

We need to switch between two modes:

  • normal paragraph, where \lineskiplimit=0pt is good choice, because the in-text sum or integral can be occur here, but the main preference is to keep the equidistant baselineskip here. May be the negative \lineskiplimit can be tried here too because the rare in-text tall objects would be probably not be exactly above one another.

  • display math, where \lineskiplimit=10pt, \lineskip=10pt is good choice to solve the desired task.

The most difficult problems are at the border between these two modes, especially below display formula: the first line of the next paragraph is set by zero \lineskiplimit which needs to insert the strut into this first line. That is solved by \aftergroup and \belowdislayABCD macros. Moreover, we test if the paragraph ends here or not.

Additionally, I solved the following problems:

  • I destruct the strut named \strut@ locally in display because it interferes badly with positive \lineskiplimit setting.

  • I set right value of the \strutbox in order for the array environment to work properly. Notice that this is a bug of the setspace/doublespacing package. If we do none other changes (only use this package), the matrices generated by \begin/\end{array} are bad.

  • I set \above/below*skip to zero. This yelds the grid for lines in paragraph and for the display lines (if there aren't tall). You can set a positive value here if you need to add additional space.

My experiment is without any warranty. This is not a definitive solution but only the material for further study. It is very probable that some problems occur with normal documents and that these changes interfere with plenty of LaTeX packages.

\documentclass[10pt]{article}

%Code that belongs in the class file:
\makeatletter
    \RequirePackage[doublespacing]{setspace}
    \AtBeginDocument{%
        \abovedisplayshortskip=0pt
        \belowdisplayshortskip=0pt
        \abovedisplayskip=0pt
        \belowdisplayskip=0pt
        \jot=0pt
        \everydisplay={\lineskiplimit=10pt \lineskip=10pt \normallineskiplimit=10pt
           \let\strut@=\relax
           \setbox\strutbox=\hbox{\vrule height8pt depth4pt width0pt}
           \aftergroup\belowdisplayA}
    }
\makeatother

\def\belowdisplayA {\afterassignment\belowdisplayB \let\tmp= }
\def\belowdisplayB {\ifx\tmp\spacetoken \expandafter \belowdisplayC
                    \else \expandafter\belowdisplayD \fi}
\def\belowdisplayC {\afterassignment\belowdisplayD \let\tmp= }
\def\belowdisplayD {\raise20pt\vbox{}\ifx\tmp\par \vskip-\baselineskip \fi \tmp}
\edef\tmp{\let\noexpand\spacetoken= \space}\tmp

%Code that belongs in the preamble:
\usepackage{amsmath}
\usepackage{lipsum}

\begin{document}

\lipsum*[2]
\begin{align*}
%Z &= \left\lfloor\frac{\mathcal E \hat{} \otimes \mathcal E}{g\mathcal E
%\hat{} \otimes \mathcal E} \right\rfloor \\
f \colon X &\to Y \\
x &\mapsto y \\
Z &= \left\lfloor\frac{\mathcal E \hat{} \otimes \mathcal E}{g\mathcal E
\hat{} \otimes \mathcal E} \right\rfloor \\
Z &= \left\lfloor\frac{\mathcal E \hat{} \otimes \mathcal E}{g\mathcal E
\hat{} \otimes \mathcal E} \right\rfloor
\end{align*}
This is a display formula with matrix. This is a display formula with matrix.
$$
  a=b, \quad
  {1\over2}
  \left(\begin{array}{cc|c}
    a & b & c \\
    d & e & f
  \end{array}\right)
$$
This is a simple display formula. This is a simple display formula.
$$
  a=b
$$
\lipsum*[2]
\end{document}

10/20 math typesetting

Related Question