[Tex/LaTex] mdframed box of fixed minimum height

boxesmdframedspacing

I would like my mdframed boxes from MyEnvironment to have a minimum total (title+body) height. The height can be greater (if the content warrants it), but not smaller than a certain size.

I can probably hack up a solution by

  1. Typesetting the mdframed into a \savebox,
  2. Measuring it, and
  3. Re-typsetting it with an appropriate \vspace inserted,

but wondering if there is a less hackish way to accomplish this.

The MWE produces the following, but the desired output would have blank space after the content body so that the height is the value set in \MinimumHeight.

enter image description here

Since the last box height is already greater than \MinimumHeight is should not be changed.

Notes:

  • The minimum height is a fixed constant known before compile time.

References:

Code:

\documentclass{article}
\usepackage{xcolor}
\usepackage{environ}
\usepackage{mdframed}
\usepackage{lipsum}

\newcommand*{\MinimumHeight}{5.0cm}%

\NewEnviron{MyEnvironment}[1][]{%
    \begin{mdframed}[
        frametitlebackgroundcolor=brown!25,
        frametitlerulecolor=blue,
        frametitlerulewidth=1.0pt,
        backgroundcolor=yellow!25,
        #1
    ]
    \BODY
    \end{mdframed}
}

\begin{document}
\begin{MyEnvironment}[frametitle={Short Title}]
    Some text for first paragraph.

    Some more text for other paragraphs.
\end{MyEnvironment}

\begin{MyEnvironment}[frametitle={Some much longer title that takes up more than one line in the title frame}]
    Some text for first paragraph.
\end{MyEnvironment}


\begin{MyEnvironment}[frametitle={Title of frame with much text}]
    \lipsum[1]
\end{MyEnvironment}
\end{document}

Best Answer

REVISED to account for height of title, and space from \fboxsep like things.

While not relevant to the solution presented, but only to your MWE, the use of the newly created \singlelipsum command is to avoid the \par that otherwise accompanies the end of a \lipsum.

\documentclass{article}
\usepackage{xcolor}
\usepackage{environ}
\usepackage{mdframed}
\usepackage{lipsum}
\usepackage{calc}
\makeatletter
\newcommand\singlelipsum[1]{%
  \begingroup\let\lips@par\relax\csname lipsum@\@roman{#1}\endcsname
\endgroup }
\makeatother
\newcommand*{\MinimumHeight}{3cm}%
\def\dH{18pt}

\newcommand\headerheight[1]{\heightof{\parbox[b]{\textwidth}{\strut#1\strut}}}

\NewEnviron{MyEnvironment}[1][]{%
    \begin{mdframed}[
        frametitlebackgroundcolor=brown!25,
        frametitlerulecolor=blue,
        frametitlerulewidth=1.0pt,
        backgroundcolor=yellow!25,
        #1
    ]
  \tabcolsep=0pt\relax
  \begin{tabular}{cp{\textwidth}}\mystrut{#1}&
    \BODY
  \end{tabular}
    \end{mdframed}
}
\def\mystrut#1{\rule[\heightof{\strutbox}-\MinimumHeight+\headerheight{#1}+\dH]{0ex}
  {\MinimumHeight-\headerheight{#1}-\dH}}

\begin{document}
\noindent\rule{\MinimumHeight}{.1ex} This is the MinimumHeight (sideways)
\begin{MyEnvironment}[frametitle={Short Title}]
    Some text for first paragraph.

    Some more text for other paragraphs.
\end{MyEnvironment}

\begin{MyEnvironment}[frametitle={Some much longer title that takes up more than one line in the title frame}]
    Some text for first paragraph.
\end{MyEnvironment}


\begin{MyEnvironment}[frametitle={Title of frame with much text}]
    \singlelipsum{1}
\end{MyEnvironment}
\end{document}

enter image description here