I realize that one should not have two consecutive display math equations as mentioned in Uneven vertical spacing displaymath. However, in my case, I have a macro that typesets two given macros in a specific manner and each may contain display math. This sometimes results in excess space.
If I understand it correctly, in this answer to excess vertical space in mdframed ending with display math environment, David Carlisle's mention of "you could just detect a zero height box and remove stuff" makes me think that it should be possible to write a macro that can remove any excess space at the end of text as passed to it. Unfortunately, I don't understand that code so can't easily adapt it.
My attempt to write such a macro below intuitively seems wrong, but sort of works. But, in case #3 and #4 below the spacing is not correct. What I would like is that \RemoveSpaceAtEnd
removes any space at the end. So if the parameter that is passed to it ends in a display math environment, that the \belowdispalyskip
is 0pt
only for the very last display math, not for every the display math in that macro
\documentclass{article}
\usepackage{amsmath}
\newcommand{\RemoveSpaceAtEnd}[1]{% Need to figure out code here. Hack here almost works
\setlength{\abovedisplayskip}{0pt}%
\setlength{\belowdisplayskip}{0pt}%
#1%
}
\newcommand*{\MacroA}{%
Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.
Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.
}
\newcommand{\MacroB}{%
\begin{align*}
e &= m c^2
\end{align*}
}
\newcommand{\MacroC}{%
\begin{align*}
F &= ma
\end{align*}
}
\newcommand{\Print}[2]{%
\noindent\RemoveSpaceAtEnd{#1}%
\par\addvspace{\medskipamount}%
\noindent\RemoveSpaceAtEnd{#2}%
\hrule%
}
\begin{document}
\Print{\MacroA}{\MacroA}
\Print{\MacroA}{\MacroB}
\Print{\MacroB}{\MacroC}
\Print{\MacroB}{\MacroA}
\end{document}
\lastbox
,\unskip
and friends, however on the main vertical list you can't, so the techniques have to be different. If you know how much space was added you can of course add a negative space to visually correct for it, but that's not really the same as actually removing things.\belowdisplayshortskip
or\belowdisplayskip
so if you set those to a magic detectable value (by for example changing their value by something invisible like 1sp and seeing if the glue at the end has that value) then you can look at\lastskip
and if it has either of those magic values act accordingly