Remove excess space at end

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}%
\noindent\RemoveSpaceAtEnd{#2}%
\hrule%
}

\begin{document}
\Print{\MacroA}{\MacroA}
\Print{\MacroA}{\MacroB}
\Print{\MacroB}{\MacroC}
\Print{\MacroB}{\MacroA}
\end{document}

-
"write a macro that can remove" One issue is that inside a box (as in the other case) you can remove items from the end of the box with \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. – David Carlisle Feb 27 '12 at 12:15
@DavidCarlisle: For my particular case the problem really is just with a display math formula. If I could detect if text ended with display math or not then adding negative vertical space would probably do. – Peter Grill Feb 27 '12 at 16:53
If a box ended with display math it will end with \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 – David Carlisle Feb 27 '12 at 17:27
answer below implements the scheme described in the comment above – David Carlisle Feb 28 '12 at 14:07

This is a working example of the algorithm sketched in the comments,hopefully it does what you meant:-)

\documentclass{article}
\usepackage{amsmath}

\newcommand{\RemoveSpaceAtEnd}[1]{%
\begingroup
\par#1\par
\ifdim\lastskip=\belowdisplayshortskip
\typeout{display here (short)}%
\nobreak
\vskip-\belowdisplayshortskip
\else
\ifdim\lastskip=\belowdisplayskip
\typeout{display here \noexpand#1}%
\nobreak
\vskip-\belowdisplayskip
\fi
\fi
\endgroup
}

\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}%
\noindent\RemoveSpaceAtEnd{#2}%
\par\hrule

}

\begin{document}
\Print{\MacroA}{\MacroA}
\Print{\MacroA}{\MacroB}
\Print{\MacroB}{\MacroC}
\Print{\MacroB}{\MacroA}
\end{document}

-
An updated version of this is at: Add \par only if last paragraph did not end with displayed math. – Peter Grill Mar 21 '13 at 22:07