2

I'm frequently using the following group code to reduce vertical space between some equations (with the equation, align, gather and other maths environment) and the text above it:

\begingroup\abovedisplayskip=0pt
    some equations in there
\endgroup

But instead of typing this group code each time by hand, how could I create a simple macro in the preamble to do the same? I'm guessing that the macro should be used like the following, but can it be simpler?

\eqlift
     some equations in there
\eqdown

Here's a MWE code that shows the problem, which I solve with the code above (only after compilation):

\documentclass[11pt,letterpaper,twoside]{book}
\usepackage{lmodern}
\usepackage{amsmath}

\begin{document}

Blabla bla :
\begin{equation}
    y(x) = a \, x^2.
\end{equation}
Blabla bla blabla bla bla :
\begin{equation}
    y(x) = a \, x^2.
\end{equation}
Blabla bla blabla bla blabla :
\begin{equation}
    y(x) = a \, x^2.
\end{equation}
Blabla bla blabla bla blabla blabla bla bla blabla :
\begin{equation}
    y(x) = a \, x^2.
\end{equation}

\end{document}

And here's a preview of what it does: enter image description here

I often get some too large vertical space for any class, any font size or other configuration I've tried in the past (see equation (3) in the preview picture). The only way I know to solve this, is to insert the group code I've shown above.

Cham
  • 2,304
  • 1
    \newenvironment{raiseequations}[1][0pt]{\setlength{\abovedisplayskip}{#1}}}{}? And then \begin{raiseequations}[<optional length>] ... equations ... \end{raiseequations}. – Henri Menke Dec 06 '19 at 01:55
  • 2
    Well, the spacing depends on the class you're using, the packages you load \dots, so, yes. If the spacing looks consistently 'goofy', that sounds like something is wrong and would be better fixed rather than asking for a sticking plaster to cover it up. – cfr Dec 06 '19 at 01:57
  • @cfr, I'm not sure of this. For any class and settings I used before, I often get this vertical blank space between some text and the equation below it. I believe that it's a well known problem with LaTeX, that cannot be solved in a consistent way without having to interfere from time to time. – Cham Dec 06 '19 at 02:01
  • Yes, but if you're doing it so often you want a special macro, that sounds like something askew. It might not be. Perhaps you just have clumpy content (I don't mean that to be offensive - some content is naturally clumpy, as in the case of images, diagrams, tables etc. and some disciplines tend to clumpier content than others). But it is worth making sure that's the case, I would think. Also, it is just easier for people to understand and play with - including users less familiar with LaTeX who might have the same problem. – cfr Dec 06 '19 at 02:11
  • 1
    @cfr, agreed. But I don't like the extra vertical space after the text (see equation (3) in the preview I've posted). I know how to reduce it (but only after compilation, so I can see the extra space that I cannot predict), and I would like to make it easier with a macro. The suggestion from Henri Menke above works well, but could it be simpler (without having to type \begin{...} \end{...})? – Cham Dec 06 '19 at 02:14
  • Does https://tex.stackexchange.com/questions/375170/vertical-spacing-of-equations-using-align-environment-following-short-texts?rq=1 help at all? (Note: I'm not saying it does. It just looked somewhat possibly relevant.) – cfr Dec 06 '19 at 02:18
  • From the answers to the question shown by @cfr, it appears that the solution is to load the nccmath package: \usepackage{nccmath}, then use the command \useshortskip just before the math environment. But then again, this can be used only after a first compilation to see the offending spaces. – Cham Dec 06 '19 at 02:38
  • @Cham: FYI, I used to use the nccmath package package but ran into numerous issues (mostly related to minipages) that I decided to drop it. See Is there a replacement for nccmath? and the problems listed in the question. – Peter Grill Dec 06 '19 at 03:07
  • @PeterGrill, you're making me very nervous now! I was adding the nccmath package to my huge preamble, just to use the medium fractions (\mfrac{}{}) and the vertical spacing option, but I don't want to run into big troubles with my cards castle! – Cham Dec 06 '19 at 03:46
  • @Cham: Sorry, just thought I'd let you know my experience. It was quite some time ago that I gave up on it, but perhaps you won't run into such problems, or they won't bother you as much as they botherd me. – Peter Grill Dec 06 '19 at 04:07
  • it isn't clear why you just highlight (3), the first two two use \abovedisplayshortskip and the second two use \abovedisplayskip which is typically larger, but does not need to be, but why do you only mark (3) and not (4) ? – David Carlisle Dec 06 '19 at 08:07
  • @DavidCarlisle, I don't understand the \abovedisplayshortskip. What does it do? In my preview picture, I don't see any problem with equation (1(, (2) and (4). Only (3) has a vertical space problem with the text above it. – Cham Dec 06 '19 at 11:12
  • You get abovedisplayskip unless the last line of the paragraph is short and doesn't overlap the math in which case you get abovedisplayshortskip short here means at least 2em shorter than the leftmost math so in case 3 you just switch to getting the longer length – David Carlisle Dec 06 '19 at 12:30
  • The space in 3 and 4 is the same – David Carlisle Dec 06 '19 at 12:31
  • I'm not in a position to experiment right now, but a possible approach is to make the equation appear to be narrower, by applying \hspace*(-1em) to both ends. This would be appropriate when the actual length of the equation is very close to the 2em limit (as described in @David Carlisle's answer), and still requires a visual review of the output. But it wouldn't suffer from the bad effects demonstrated in the cited answer. – barbara beeton Dec 06 '19 at 15:27
  • @barbarabeeton what bad effect do you see from putting the hspace at the end of the paragraph? – David Carlisle Dec 06 '19 at 15:52
  • Not at the end of the paragraph -- inside the equation. It just makes the equation look narrower. Unless I'm missing something critical? – barbara beeton Dec 06 '19 at 16:24
  • I've finally had a chance to test (although I can't insert the output here). This is what I meant using \hspace*: \begin{equation} \hspace*{-1em} y(x) = a \, x^2. \hspace*{-1em} \end{equation} – barbara beeton Dec 10 '19 at 04:02

1 Answers1

3

enter image description here

Normally TeX uses \abovedisplayskip above an equation, but if the last line of the previous paragraph is short then it uses \abovedisplayshortskip by default most classes set this to a shorter length and so the equation "tucks in" to the space left by the short line above.

The shortdisplayskip is used if the last line finishes 2em to the left of the equation, to make sure there is still a visual gap.

Your case (3) just happens to be very close to the cutoff, TeX is using the longer length the same as used in (4) but subjectively you want it to work like (1) and (2).

I offer two variants here.

Equations 5-8 show the setting when both lengths have the same value you then avoid the discontinuity as tex switches from one style to the other, but (8) ends up looking rather tight.

Equations 9-12 are set with the default settings as in your original, but the length of the last line of the third paragraph is made to look (to TeX) artificially shorter by inserting an arbitrary negative space. That causes TeX to insert the same space as for the first two paragraphs.

\documentclass[11pt,letterpaper,twoside]{book}
\usepackage{lmodern}
\usepackage{amsmath}

\begin{document}

\section{Original}

Blabla bla :
\begin{equation}
    y(x) = a \, x^2.
\end{equation}
Blabla bla blabla bla bla :
\begin{equation}
    y(x) = a \, x^2.
\end{equation}
Blabla bla blabla bla blabla :
\begin{equation}
    y(x) = a \, x^2.
\end{equation}
Blabla bla blabla bla blabla blabla bla bla blabla :
\begin{equation}
    y(x) = a \, x^2.
\end{equation}

\section{use abovedisplayshortskip everywhere}

{
\setlength\abovedisplayskip{\abovedisplayshortskip}


Blabla bla :
\begin{equation}
    y(x) = a \, x^2.
\end{equation}
Blabla bla blabla bla bla :
\begin{equation}
    y(x) = a \, x^2.
\end{equation}
Blabla bla blabla bla blabla :
\begin{equation}
    y(x) = a \, x^2.
\end{equation}
Blabla bla blabla bla blabla blabla bla bla blabla :
\begin{equation}
    y(x) = a \, x^2.
\end{equation}

}

\section{abovedisplayshortskip in (3=11)}

Blabla bla :
\begin{equation}
    y(x) = a \, x^2.
\end{equation}
Blabla bla blabla bla bla :
\begin{equation}
    y(x) = a \, x^2.
\end{equation}
Blabla bla blabla bla blabla :\hspace{-2cm}\mbox{}
\begin{equation}
    y(x) = a \, x^2.
\end{equation}
Blabla bla blabla bla blabla blabla bla bla blabla :
\begin{equation}
    y(x) = a \, x^2.
\end{equation}


\end{document}
David Carlisle
  • 757,742
  • See my comment above. Maybe you can do something with it. – barbara beeton Dec 06 '19 at 15:28
  • So, if I understand your answer, you suggest to add \hspace{-2cm}\mbox{} before an equation to solve the issue? The spacing under equation (11) appears to be too short. – Cham Dec 06 '19 at 23:09
  • @Cham the space after 11 is identical to the space after 10 an 9 – David Carlisle Dec 07 '19 at 01:02
  • 1
    @Cham actually I would take the default spacing, I don't think there is an issue to solve, but if, as you ask here you want an equation near the point where tex switches layout to use the shortdisplayskip setting, then the negative hspace is the most natural way to do that, – David Carlisle Dec 07 '19 at 01:04
  • @DavidCarlisle (negative \hspace ?), ok, I think I got it, but I need a confirmation it is a proper way of doing this. Instead of using a group code to reduce the vertical space by changing the \ abovedisplayskip value to 0pt, is it a good idea to simply put \vspace{-8pt} just before the offending math environment? And what negative size should I use? I tried 8pt = 11pt + 3pt - 6pt, and the result appears to be pretty. Is this right? – Cham Dec 07 '19 at 19:34
  • @Cham changing \abovedisplayskip to 0pt doesn't make a space consistent with any of the other examples. more or less any negative space will do as you only want to do this when you are right on the cusp of the switch in styles to persuade tex to use the shortdisplayskip version/ – David Carlisle Dec 07 '19 at 20:36
  • 1
    @Cham if for some reason you want to use a negative vspace instead then the amount you want is \vspace{\glueexpr\abovedisplayshortskip-\abovedisplayskip} – David Carlisle Dec 07 '19 at 20:41