12

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}
3

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

  • Interesting solution -- much more elegant than what I had in mind. However, not sure I like putting everything in a tabular (or even a minipage). One problem with this current solution is that it has changed the paragraph indentation -- see how the text changes in the first example in your MWE if you comment out the \begin{tabular} and \end{tabular}. Now each of these issues can be adjusted but I don't know if there are other issues lurking that will need correction in the future. Perhaps I am just being too paranoid. – Peter Grill Jul 27 '13 at 17:22
  • @PeterGrill Well, I haven't solved the indent thing, but indeed other issues lurked. I just fixed one, in that one needed to account for \fboxsep like lengths that make the box bigger than the text itself. I now placed a \rule at the top of the page to realize I needed 18pt worth of adjustment to the MinimumHeight. – Steven B. Segletes Jul 27 '13 at 17:42
  • 18pt? That really should be computed from something as it would need changing based on the font -- probably based on \baselineskip (which is 12pt), so not sure if it is just 1.5*\baselineskip, or \baselineskip + <some_lengths> where <some_lengths> add up to 6pt. – Peter Grill Jul 27 '13 at 18:46
4

I cannot give a better answer as Steven B. Segletes for adapting mdframed. But I can show an alternative using the fitting library of tcolorbox to create such boxes. The two major drawbacks are (1) it is not mdframed which was asked for, (2) the fit boxes are not breakable but they shrink their content if its getting too large (displayed in the exampled below). I can image that an unrestricted minimum height could be a future feature for mdframed and tcolorbox. Now, here is the alternative for the current moment:

\documentclass{article}
\usepackage{lipsum}
\usepackage[most]{tcolorbox}

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

\newtcolorbox{MyEnvironment}[1][]{%
  fit height from={\MinimumHeight} to {\textheight},
  enhanced,colback=yellow!25,colframe=black,colbacktitle=brown!25,
  coltitle=black,boxrule=0.4pt,titlerule=0pt,arc=0pt,outer arc=0pt,
  fonttitle=\bfseries,left=2.5mm,right=2.5mm,
  top=1mm,bottom=1mm,toptitle=1mm,bottomtitle=1mm,#1}

\begin{document}

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

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

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

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

\begin{MyEnvironment}[title={Title of frame with too much text}]
    \lipsum[1-7]
\end{MyEnvironment}

\end{document}

enter image description here

Note the shrinked text on page two. This is an automated feature of the fit box which may or may not be of use for the intended application.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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