固定最小高度的 mdframed 框

固定最小高度的 mdframed 框

我希望我的mdframedMyEnvironment具有最小总高度(标题+正文)。高度可以更大(如果内容允许),但不能小于某个尺寸。

我可能可以通过以下方法解决问题

  1. 将 排版mdframed\savebox
  2. 测量它,并且
  3. 重新排版并\vspace插入适当的内容,

但想知道是否有一种不那么黑客的方法来实现这一点。

MWE 产生以下内容,但所需的输出在内容主体后会有空白,因此高度是中设置的值\MinimumHeight

在此处输入图片描述

由于最后一个盒子的高度已经大于\MinimumHeight不应改变的高度。

笔记:

  • 最小高度是编译前已知的固定常数。

参考:

代码:

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

答案1

已修改,以考虑标题的高度以及\fboxsep类似事物的空间。

虽然与所提出的解决方案无关,而仅与您的 MWE 相关,但使用新创建的\singlelipsum命令是为了避免\par伴随 的结束\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}

在此处输入图片描述

答案2

对于 的改编,我无法给出像 Steven B. Segletes 更好的答案mdframed。但我可以展示一种使用fitting库来tcolorbox创建此类框的替代方案。两个主要缺点是 (1) 它不是mdframed所要求的,(2)框不可破坏,但如果内容变得太大,它们会缩小(如下面的示例中所示)。我可以想象,不受限制的最小高度可能是和fit的未来功能。现在,这是当前的替代方案:mdframedtcolorbox

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

在此处输入图片描述

注意第二页的缩小文本。这是盒子的自动功能fit,对于预期的应用可能有用,也可能没用。

在此处输入图片描述

相关内容