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}
5
  • 1
    "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. Commented Feb 27, 2012 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. Commented Feb 27, 2012 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 Commented Feb 27, 2012 at 17:27
  • answer below implements the scheme described in the comment above Commented Feb 28, 2012 at 14:07
  • Related Questions: Excess vertical space in mdframed ending with display math environment, Add \par only if last paragraph did not end with displayed math and [How to check if last displayed content was displaymath / minipage ends on displaymath? ](tex.stackexchange.com/q/448856/4301). Commented Oct 4, 2018 at 20:16

1 Answer 1

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}
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .