# Using Needspace within paragraph provides too much space

I am trying to implement the solution provided in Equation tag in twoside-mode outer. It seems the solution provides too much space above the align environment. The following code reproduces the problem, and you can also view the attached picture. Compare the first paragraph to the second paragraph.

\documentclass{article}
\usepackage{amsmath}
\usepackage{needspace}

\begin{document}

\setbox0=\hbox{%
\begin{minipage}{\linewidth}%
\begin{align}
a^2=b^2+c^2, \\ d^2 = e^2 + f^2.
\end{align}
\end{minipage}%
}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa:
\Needspace{\ht0}
\begin{align}
a^2=b^2+c^2, \\ d^2 = e^2 + f^2.
\end{align}
Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa:
\begin{align}
a^2=b^2+c^2, \\ d^2 = e^2 + f^2.
\end{align}
Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.

\end{document}


The manual of the Needspace package does make a note that \Needspace should not be used within paragraphs; this might be causing the issue. I would like the spacing within both paragraphs to be identical. How can I achieve this?

Remark. Initially, I thought the problem was caused by Extra space above boxed multi-line equation, but the solution to add

\setlength\abovedisplayskip{0pt}


within the minipage had no effect. This suggests the issue occurs because of the \Needspace command.

• apart from the fact that Needspace itself is adding to the space \ht0 is not the length you want (I would guess) your \setbox (could have use standard latex \sbox) is measuring a minipage which by default is vertically centred so \ht0 is half its vertical extent (more or less) – David Carlisle Sep 2 '15 at 12:20

I can confirm that the problem is that \Needspace (once it parses for starred or unstarred invocation) issues a \par as its opening macro (see \makeatletter\meaning\@needsp@). SO the extra space is essentially the result of a new paragraph being inserted prior to the align environment.

Thus, here, before the \Needspace invocation, I issue a \vspace{\dimexpr-\baselineskip-\parskip\relax}. In this MWE, I alter \parskip, just to confirm its proper use in my solution.

Whether this will work for you in general, I could not say.

Per David's comment, I have also invoked the minipage with the [b] option, so that box depth does not come into play.

\documentclass{article}
\usepackage{amsmath}
\usepackage{needspace}
\parskip 3em
\begin{document}

\setbox0=\hbox{%
\begin{minipage}[b]{\linewidth}%
\begin{align}
a^2=b^2+c^2, \\ d^2 = e^2 + f^2.
\end{align}
\end{minipage}%
}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa:%
\vspace{\dimexpr-\baselineskip-\parskip\relax}\Needspace{\ht0}%
\begin{align}
a^2=b^2+c^2, \\ d^2 = e^2 + f^2.
\end{align}
Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa:
\begin{align}
a^2=b^2+c^2, \\ d^2 = e^2 + f^2.
\end{align}
Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.

\end{document}


• Removing a baselineskip and parskip indeed seems to be very close the perfect solution. It seems, however, that a little bit too much is removed (perhaps it is a glue issue?). – Stemic Sep 2 '15 at 11:43
• @Stemic I cannot say more on the issue. Perhaps it is a glue issue. But clearly, a \par is issued and must be counteracted. The vertical space associated with a \par is \baselineskip+\parskip. – Steven B. Segletes Sep 2 '15 at 11:55
• We're very close though; it looks like either an \abovedisplayskip or \abovedisplayshortskip should be added so that your solution reads \dimexpr-\baselineskip+\abovedisplay(short)skip-\parskip\relax Unfortunately, it seems which to choose depends on the length of the preceeding last line of the paragraph (and whether the equation number is left), as discussed in tex.stackexchange.com/questions/30909/… and p. 212 of TeX by Topic ctan.cs.uu.nl/info/texbytopic/TeXbyTopic.pdf, complicating a solution. – Stemic Sep 2 '15 at 12:18
• see comment about \ht0 added to question – David Carlisle Sep 2 '15 at 12:21
• @DavidCarlisle Good point, I have made the minipage with the [b] option, so that \ht0 will reflect the height. – Steven B. Segletes Sep 2 '15 at 12:32