6

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}
Peter Grill
  • 223,288

1 Answers1

3

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

enter image description here

\documentclass{article}
\usepackage{amsmath}

\newcommand{\RemoveSpaceAtEnd}[1]{%
\begingroup
\advance\belowdisplayskip1sp
\advance\belowdisplayshortskip1sp
\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}%
    \par\addvspace{\medskipamount}%
    \noindent\RemoveSpaceAtEnd{#2}%
    \par\hrule

}

\begin{document}
\Print{\MacroA}{\MacroA}
\Print{\MacroA}{\MacroB}
\Print{\MacroB}{\MacroC}
\Print{\MacroB}{\MacroA}
\end{document}
David Carlisle
  • 757,742