[Tex/LaTex] Excess vertical space in mdframed ending with display math environment

equationsmdframedspacing

When the mdframed environment ends with an align there is excess vertical spacing at the bottom. I would like to adjust the MyBoxedEnvironment environment such that the vertical space at the bottom is uniform independent of the contents of the box ending with text or an align environment.

enter image description here

All the cases (#1 and #3 in the MWE) where I don't like the excess spacing are in red. The others (not shown in included image) in yellow seem ok to me (See Page 2 of output).

Notes:

  • To not have multiple nested boxes, the MyBoxedEnvironment applies the solution from Adjust environments's behaviour when nested.

  • To ensure that box is always the full \linwidth the contents are wrapped within a minipage. This is really only needed to fix this for the very first test case.

    Note that in the first part of the included image (where the environments are not nested) the vertical spacing at the bottom is ok.
    Before settling on the minipage solution, I attempted to use \hfill but that resulted in excess vertical space in the first box.

  • This does not seem related to Spurious space above align environment at top of page/minipage

Questions:

  1. How do I adjust this within the mdframed environment without manually adding a vertical adjustment to the contents of the box which are being \input from an external file?
  2. Also why does the vertical spacing on Case 5 change slightly if the text "Case 5" is removed? This does not bother me, but just curious as to the cause.

Update: 2012-03-12:

The MWE code has been updated to simplify comparison with the original and the current proposed solution by @DavidCarlisle. This solution works great for Case 1 and Case 3 (where we end with a display math environment). But has problems with Cases 2, 4 thru 7 where the environment does not end with a display math and eats the last line. However, after incorporating the suggestion mentioned in the comments the last line is no longer gobbled. So, now the only remaining problem is the additional vertical space at the top

Here is am image of Case 3 to show the problem. The original output (via MyBoxedEnvironmentOld) is shown on the left and the new output (via MyBoxedEnvironmentNew) is shown on the right. Note that there seems to be extra vertical space at the top of the display math environment.

enter image description here

So, I attempted to apply the solution from Spurious space above align environment at top of page/minipage by un-commenting the included code, then we get:

enter image description here

Code: (Updated 2012-02-13)

\documentclass{article}
\usepackage[paperheight=25.0in]{geometry}
\usepackage{amsmath}
\usepackage{xcolor}
\usepackage{mdframed}

%%%% This adjustment from the URL below has no effect here.
%%%% https://tex.stackexchange.com/questions/36954/spurious-space-above-align-environment-at-top-of-page-minipage
%%
%\usepackage{etoolbox}
%\makeatletter
%\pretocmd\start@align{%
%  \if@minipage\kern-\topskip\kern-\abovedisplayskip\fi
%}{}{}
%\makeatother


\colorlet{MyBackgroundColor}{yellow!10}

\newmdenv[
    backgroundcolor=MyBackgroundColor,
    linewidth=1.5pt,
    skipabove=0pt,
    skipbelow=0pt,
    innerleftmargin=1pt,
    nobreak=true,
    ]{MyBoxedEnvironmentMdframed}

% Should be no harm with trailing % here.
\newenvironment{MyBoxedEnvironmentOld}{%
    \def\MyBoxedEnvironmentOld{%
        \let\endMyBoxedEnvironmentOld\empty%
        \par\noindent\ignorespaces%
    }%
    \begin{MyBoxedEnvironmentMdframed}%
    \begin{minipage}{\linewidth}%               to fix very first test case
    \par\noindent\ignorespaces%
}{%
    %\hfill% ensure full line width of first test case {minipage version is better}.
    \end{minipage}%
    \end{MyBoxedEnvironmentMdframed}%
    \ignorespacesafterend%
}%

\makeatletter%
\newenvironment{MyBoxedEnvironmentNew}{%
    \def\MyBoxedEnvironmentNew{%
        \let\endMyBoxedEnvironmentNew\par%
        \par%\noindent\ignorespaces%
    }%
    \begin{MyBoxedEnvironmentMdframed}%
    \begin{minipage}{\linewidth}%               to fix very first test case
    \par%\noindent\ignorespaces%
}{%
    \par%
    \setbox0=\lastbox%
    \ifnum\ht\z@=\z@%
        \ifnum\dp\z@=\z@%
            \unskip\unskip\unpenalty%
    %   \else%
    %       \box\z@%
    %   \else%
    %       \box\z@%
    %   \fi%
    %\fi%
    \fi\fi\box\z@% Apply fix mentioned in comments
    \end{minipage}%
    \end{MyBoxedEnvironmentMdframed}%
    \ignorespacesafterend%
}%
\makeatother%


% Simplify repeated testing and comparrison: Run MyBoxedEnvironmentOld and MyBoxedEnvironmentNew side by side
\newcommand{\TestNestedEnvironments}[2][yellow!10]{%
    \colorlet{MyBackgroundColor}{#1}%
    \noindent%
    \begin{minipage}[t]{0.49\linewidth}
        \begin{MyBoxedEnvironmentOld}%      non-nested test
            #2%
        \end{MyBoxedEnvironmentOld}%
        %
        \begin{MyBoxedEnvironmentOld}%      nested test
            #2%
            \begin{MyBoxedEnvironmentOld}%
                \textcolor{red}{#2}%
            \end{MyBoxedEnvironmentOld}%
        \end{MyBoxedEnvironmentOld}%
    \end{minipage}%
    \hfill%
    \begin{minipage}[t]{0.49\linewidth}
        \begin{MyBoxedEnvironmentNew}%      non-nested test
            #2%
        \end{MyBoxedEnvironmentNew}%
        %
        \begin{MyBoxedEnvironmentNew}%      nested test
            #2%
        \begin{MyBoxedEnvironmentNew}%
            \textcolor{red}{#2}%
        \end{MyBoxedEnvironmentNew}%
        \end{MyBoxedEnvironmentNew}%
    \end{minipage}%
    \bigskip%
}%

\begin{document}
\noindent%
Problem only with \textbf{MyBoxedEnvironment} with display math and no text afterwards:

\TestNestedEnvironments[red!20]{
    \begin{align*}
        e = m c^2 \quad (Case~1)
    \end{align*}
}

%----------------------------------------------------------
\TestNestedEnvironments{
    \begin{align*}
        e = m c^2  \quad (Case~2)
    \end{align*}
Ok with text below (short or long).
}

%----------------------------------------------------------
\TestNestedEnvironments[red!20]{
Text only above:
    \begin{align*}
        e = m c^2  \quad (Case~3)
    \end{align*}
}

%----------------------------------------------------------
%\newpage% Don't need since paper height is now extended.
\noindent
Ok with display math and text above and below:

\TestNestedEnvironments{
Text above (and below):
    \begin{align*}
        e = m c^2  \quad (Case~4)
    \end{align*}
Text below (and above).
}

%----------------------------------------------------------
\bigskip\noindent
Ok with inline math (but slight change in spacing if the "(Case~5)" label is removed):

\TestNestedEnvironments{$e = m c^2  \quad (Case~5)$}

%----------------------------------------------------------
\bigskip\noindent
Ok with short text:

\TestNestedEnvironments{Lorem ipsum dolor sit amet.\quad (Case~6)}

%----------------------------------------------------------
\bigskip\noindent
Ok with long text:

\TestNestedEnvironments{
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
    Sed accumsan nulla ac ipsum elementum interdum. 
    Praesent ultricies faucibus turpis, non scelerisque nisi bibendum et. 
    \quad (Case~7)
}
\end{document}

Best Answer

no fair blaming this problem on align. the same thing happens with equation and no amsmath. in the provided code, comment out \usepackage{amsmath} and replace all instances of align* with equation (sorry, no * available, so you'll get display numbers), and you'll get this result:

framed display equations with bad vertical spacing

one gets the same result using \[ ... \] only (as expected) without the numbers.

it's still a problem, but deeper in the guts of (la)tex.

Related Question