忽略 mdframed 环境顶部的 \addvspace

忽略 mdframed 环境顶部的 \addvspace

在下面的 MWE 中,我在\addvspace{2.0ex}图表前面添加了一个。这看起来不错,除了如果图形位于顶部mdframe。有没有办法让\MyFigure宏检测出我是否位于框架顶部,这样\addvspace{2.0ex}就不会添加?

因此,第一种和第三种情况工作正常。中间的情况在顶部(图之前)有额外的间距,我想消除它。

在此处输入图片描述

梅威瑟:

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

\mdfdefinestyle{MyStyle}{%
    frametitlebackgroundcolor=red!40,
    frametitlerule=true, 
    skipabove=0pt,
    skipbelow=\baselineskip plus 2pt minus 1pt,
}

\newcommand*{\MyFigure}[1]{%
    \par
    \addvspace{2.0ex}% <--- This is the issue
    {\centering
    \begin{tikzpicture}
    \draw [fill=yellow!50] (0,0) rectangle (5,1) node [midway] {#1};
    \end{tikzpicture}%
    \par\addvspace{2.0ex}}
}%

\begin{document}

\begin{mdframed}[
    style=MyStyle, 
    frametitle={Imagine in middle},
    ]
    Some text before.
    \MyFigure{Spacing above and below is fine}
    Some text following.
\end{mdframed}

\begin{mdframed}[
    style=MyStyle, 
    frametitle={Image on Top},
    ]
    \MyFigure{Spacing above is a problem}
    \noindent
    Some text following.
\end{mdframed}

\begin{mdframed}[
    style=MyStyle, 
    frametitle={Image at End},
    ]
    Some text before.
    \MyFigure{Spacing above and below is fine}
\end{mdframed}
\end{document}

相关内容