2

When I use this code some space above appears.

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

\begin{document}

\begin{mdframed}    
\begin{gather*}
    \lambda^2-2\lambda = 0\\
    \lambda_1 = 0, \enspace \lambda_2 = 2
\end{gather*}
\end{mdframed}

\end{document}

enter image description here

I tried to implement this answer by egreg and it works, however I am getting these errors.

enter image description here

\documentclass{article}
\usepackage{amsmath}
\usepackage{mdframed}
\usepackage{etoolbox}
\makeatletter
\pretocmd\start@gather{%
    \if@mdframed\kern-\topskip\kern-\abovedisplayskip\fi
}{}{}
\makeatother

\begin{document}

\begin{mdframed}    
\begin{gather*}
    \lambda^2-2\lambda = 0\\
    \lambda_1 = 0, \enspace \lambda_2 = 2
\end{gather*}
\end{mdframed}

\end{document}

enter image description here


Also I would like to know how to make it work for all the amsmath envinronments. Now only one with \start@gather is affected.

antshar
  • 4,238
  • 9
  • 30

1 Answers1

3

As @UlrikeFischer pointed in the question comment, there is no boolean \if@mdframed in mdframed package. The following workaround

  • uses the value of TeX count \mdf@envdepth to determine if the code is executed inside mdframed environtment, and
  • patches each of \start@gather, \start@align and \start@multline.

Hope that would work for every amsmath multiline displayed equation environment.

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

\makeatletter
\pretocmd\start@gather{%
  \ifnum\mdf@envdepth>0
    \kern\glueexpr-\topskip-\abovedisplayskip\relax
  \fi
}{}{\fail}
\pretocmd\start@align{%
  \ifnum\mdf@envdepth>0
    \kern\glueexpr-\topskip-\abovedisplayskip\relax
  \fi
}{}{\fail}
\pretocmd\start@multline{%
  \ifnum\mdf@envdepth>0
    \kern\glueexpr-\topskip-\abovedisplayskip\relax
  \fi
}{}{\fail}
\makeatother

\begin{document}

gather:
\begin{mdframed}    
\begin{gather*}
    \lambda^2-2\lambda = 0\\
    \lambda_1 = 0, \enspace \lambda_2 = 2
\end{gather*}
\end{mdframed}

align:
\begin{mdframed}    
\begin{align*}
    \lambda^2-2\lambda = 0\\
    \lambda_1 = 0, \enspace \lambda_2 = 2
\end{align*}
\end{mdframed}

multline:
\begin{mdframed}    
\begin{multline*}
    \lambda^2-2\lambda = 0\\
    \lambda_1 = 0, \enspace \lambda_2 = 2
\end{multline*}
\end{mdframed}

\end{document}

enter image description here


A looping version in case you prefer a more compact patching code:

\makeatletter
\@for\@tempa:=gather,align,multline\do{%
  \expandafter\pretocmd\csname start@\@tempa\endcsname{%
    \ifnum\mdf@envdepth>0
      \kern\glueexpr-\topskip-\abovedisplayskip\relax
    \fi
  }{}{\fail}
}
\makeatother

Here \@for is a LaTeX2e internal macro which is documented in, for example macros2e documentation.

muzimuzhi Z
  • 26,474
  • Perfect! Special thanks for the compact version.. – antshar May 27 '20 at 13:51
  • @antshar My original answer was wrong. See my updated answer for an improved solution. – muzimuzhi Z May 31 '20 at 21:21
  • What was wrong with the previous version? – antshar May 31 '20 at 21:24
  • @antshar Hmmm, that's strange. I thought the old solution will cause problems but it did not. Rollbacked to the old solution. – muzimuzhi Z May 31 '20 at 21:49
  • @antshar It seems the \kern added before envs like align which is not at the very beginning of mdframed inserts a horizontal skip (10pt), not vertical, but that skip is not shown. Maybe because of the centering alignment of equation? Not sure. – muzimuzhi Z May 31 '20 at 22:20