8

The problem

Inside an mdframed environment, placing align* at the start of the environment produces extra vertical space above, whereas using \[\] instead does not create as much vertical space. This extra vertical space does not appear when align* vs. \[\] is placed after normal plain text, so the issue is specific to the align* + mdframed combination.

MWE

\documentclass{article}

\usepackage{mdframed}
\usepackage{framed}
\usepackage{amsmath}

\begin{document}

Outside \texttt{mdframed}, using \verb|\[\]|: \hrulefill
\begin{align*}
  x
\end{align*}

Outside \texttt{mdframed}, using \texttt{align*}: \hrulefill
\[
  x
\]

Inside \texttt{mdframed}, using \verb|\[\]|:
\begin{mdframed}
\[
  x
\]
\end{mdframed}

Inside \texttt{mdframed}, using \texttt{align*}:
\begin{mdframed}
  \begin{align*}
    x
  \end{align*}
\end{mdframed}

\vspace*{3em}

After setting skip lengths to 0:
\setlength{\abovedisplayskip}{0pt}
\setlength{\abovedisplayshortskip}{0pt}

Outside \texttt{mdframed}, using \verb|\[\]|: \hrulefill
\begin{align*}
  x
\end{align*}

Outside \texttt{mdframed}, using \texttt{align*}: \hrulefill
\[
  x
\]

Inside \texttt{mdframed}, using \verb|\[\]|:
\begin{mdframed}
\[
  x
\]
\end{mdframed}

Inside \texttt{mdframed}, using \texttt{align*}:
\begin{mdframed}
  \begin{align*}
    x
  \end{align*}
\end{mdframed}
\end{document}

mwe

Possibly related questions

  • mdframed package + align gives extra vertical space

    This is not a duplicate of that question, since that problem arises from a long align* environment getting broken up at a page break, which is not relevant here.

    Notice that the accepted answer for that question, which fixes that issue with \allowdisplaybreaks, the align* environment still produces an unseemly hunk of vertical space above itself when it is placed alone in the mdframed environment: unseemly hunk above <code>align*</code>

  • Align environment creates extra vertical space

    Also not a duplicate, since that issue arises also from page breaks, which are not relevant here.

  • Remove extra vertical space in amsmath's align environment

    Not a duplicate; the issue there arises from placing align* in a new paragraph after a hunk of text.

  • Remove vertical space around align

    This doesn't address the issue here. I do not want to blindly remove all vertical space before (and after) align* environments, I just want to normalize it with the \[\] environment. Nevertheless, I tried the accepted solution for that question (i.e. \setlength{\abovedisplayskip}{0pt}, \setlength{\abovedisplayshortskip}{0pt}, \setlength{\belowdisplayskip}{0pt}, \setlength{\belowdisplayshortskip}{0pt}); this seems to mitigate the issue, but not in a desirable way, since I do not want to remove all vertical spacing around my display math environments.

3

1 Answer 1

3
+50

Here's one possibility: prepending the \setlength{}{} code at the start of mdframed environments using the startcode=<code> key, so that the spaces are only modified in these environments.

\documentclass{article}

\usepackage{mdframed}
\usepackage{amsmath}

% 
\mdfdefinestyle{removespace}{%
    startcode = {%
                 \setlength{\abovedisplayskip}{0pt}%
                 \setlength{\abovedisplayshortskip}{0pt}%
                }%
}

\begin{document}

Outside \texttt{mdframed}, using \verb|\[\]|: \hrulefill
\begin{align*}
  x
\end{align*}

Outside \texttt{mdframed}, using \texttt{align*}: \hrulefill
\[
  x
\]

Inside \texttt{mdframed}, using \verb|\[\]|:
\begin{mdframed}
\[
  x
\]
\end{mdframed}

Inside \texttt{mdframed}, using \texttt{align*}:
\begin{mdframed}[style=removespace]
  \begin{align*}
    x
  \end{align*}
\end{mdframed}

\vspace*{3em}

After setting skip lengths to 0:
\setlength{\abovedisplayskip}{0pt}
\setlength{\abovedisplayshortskip}{0pt}

Outside \texttt{mdframed}, using \verb|\[\]|: \hrulefill
\begin{align*}
  x
\end{align*}

Outside \texttt{mdframed}, using \texttt{align*}: \hrulefill
\[
  x
\]

Inside \texttt{mdframed}, using \verb|\[\]|:
\begin{mdframed}
\[
  x
\]
\end{mdframed}

Inside \texttt{mdframed}, using \texttt{align*}:
\begin{mdframed}
  \begin{align*}
    x
  \end{align*}
\end{mdframed}
\end{document}

2
  • 1
    Thank you so much for providing this user who had been waiting for at least one answer for several days.
    – Sebastiano
    Commented Jan 5, 2019 at 22:57
  • Would this not unintentionally remove spaces in subsequent align* environments after the first one inside an mdframed environment? Is there a way to only affect the first?
    – Kye W Shi
    Commented Jan 24, 2019 at 18:09

You must log in to answer this question.

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