Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.
%%%% http://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}
share|improve this question
1  
i'd change the title to "Excess vertical space in mdframed ending with a display math environment" except that it would have to be accompanied by too many other changes, and would make my answer somewhat less comprehensible. how best to handle this? –  barbara beeton Feb 10 '12 at 20:01
    
@barbarabeeton: I think changing it to "displaymath" instead makes sense. You comment about this not being just a problem with align illustrates that. –  Peter Grill Feb 10 '12 at 20:43

2 Answers 2

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.

share|improve this answer

I think this is a "colo[u]r sometimes messes up spacing issue. I wrote in the original grfguide a lifetime ago:

There is a different type of problem that will occur for all drivers. Due to certain technical difficulties, it is possible that at points where the colour changes, the spacing is affected.

The extra white space is

........\penalty 10000
........\glue 0.0
........\penalty 0
........\glue(\belowdisplayskip) 10.0 plus 2.0 minus 5.0
........\glue(\baselineskip) 8.39996
........\hbox(0.0+0.0)x331.0, glue set 331.0fil
.........\pdfcolorstack 0 pop
.........\penalty 10000
.........\glue(\parfillskip) 0.0 plus 1.0fil
.........\glue(\rightskip) 0.0

that is, it's a paragraph with a blank line that just consists of a color whatsit and some filling glue.

Rather than worry too much which of the packages is doing that, you could just detect a zero height box and remove stuff, producing:

enter image description here

I also removed the \noindent from the start of the environments as, as noted in the other thread, they are likely to cause similar blank lines at the start (\noindent starts a paragraph so if it is followed by vertical material it makes a blank line with an empty para).

\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor}
\usepackage{mdframed}

\showboxdepth30
\showboxbreadth10000
%%% This adjustment from the URL below has no effect here.
%%% http://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.
\makeatletter
\newenvironment{MyBoxedEnvironment}{%
    \def\MyBoxedEnvironment{%
        \let\endMyBoxedEnvironment\par%
        \par%\noindent\ignorespaces%
    }%
    \begin{MyBoxedEnvironmentMdframed}%
    \begin{minipage}{\linewidth}%               to fix very first test case
    %\setlength{\belowdisplayskip}{0pt}%        to see effects of manual adjustments
    %\setlength{\belowdisplayshortskip}{0pt}%
    \par%\noindent\ignorespaces%
}{%
    %\hfill% ensure full line width of first test case {minipage version is better}.
    \par
\setbox0=\lastbox
\ifnum\ht\z@=\z@
\ifnum\dp\z@=\z@
\unskip\unskip\unpenalty
\else
\box\z@
\else
\box\z@
\fi
\fi
\end{minipage}%
    \end{MyBoxedEnvironmentMdframed}%
    \ignorespacesafterend%
}%

% Simplify repeated testing
\newcommand{\TestNestedEnvironments}[2][yellow!10]{%
    \colorlet{MyBackgroundColor}{#1}%
    \begin{MyBoxedEnvironment}%      non-nested test
        #2%
    \end{MyBoxedEnvironment}%
    %
    \begin{MyBoxedEnvironment}%      nested test
        #2%
    \begin{MyBoxedEnvironment}%
        \textcolor{red}{#2}%
    \end{MyBoxedEnvironment}%
    \end{MyBoxedEnvironment}%
}%



\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\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}
share|improve this answer
    
I hade such a problem because I used \color@setgroup instead of \color@begingroup for the package mdframed. –  Marco Daniel Feb 10 '12 at 22:11
    
hmm it may be best to always put the box back (after taking it off to remove the glue in the 0pt case) as otherwise you'd remove the whatsit that pops the pdf color stack, which probably leaves it in an incorrect state. so you might want to change \else \box\z@ \else \box\z@ \fi \fi to \fi\fi\box\z@ –  David Carlisle Feb 10 '12 at 22:58
    
@DavidCarlisle: This seemed great, but only appears to work if the environment ends with display math (Case 1 and 3), but eats the last line in the other cases 2, 4 thru 7. I have updated the MWE to show the output of both version side by side. –  Peter Grill Feb 14 '12 at 1:14
    
@DavidCarlisle: OK, I had not applied the fix mentioned in the comments. After applying that, the last line is no longer gobbled. Yeah!! However, there is vertical space at the beginning which is not making any sense to me. MWE updated to incorporate fix mentioned in comments. –  Peter Grill Feb 14 '12 at 1:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.