创建设置其内容的最小大小的环境

创建设置其内容的最小大小的环境

我如何创建使其内容高度至少为 x 个单位的环境?它应该在其内容下添加空白。

答案1

这里有一个可能的实现(如果我理解了请求的话):环境基本上是一个minipage,但在后者开始之前,有一个具有给定深度的不可见规则。最小深度可以作为可选参数传递给环境。在这里,我将规则的宽度设置为,1pt以显示正在发生的事情(这当然会导致过满\hbox),但它当然应该是0pt

% twocolumn for smaller snapshot
\documentclass[twocolumn]{article}

\newenvironment{foo}[1][8ex]{%
   \par\noindent
   \vrule depth#1 width1pt% <-- WRITE width0pt HERE
   \minipage[t]{\linewidth}%
  }{%
   \par\xdef\next{\the\prevdepth}\endminipage
   \par
   \prevdepth\next
   \ignorespacesafterend
}

\begin{document}

See this and that,
\begin{foo}
Some text here.
\end{foo}
Some other text here.
\begin{foo}
Some longer text here which will take more than one line and in total more
than the 5ex fixed by the invisible (well, not quite) rule on the left of this
strange environment.
\end{foo}
Some other text here.
\end{document}

在此处输入图片描述

答案2

我认为您要找的是minipage,它具有设置高度的选项。问题是 minipage 会将缩进和段落跳过设置为零,但您可能希望继续使用与文档相同的格式。解决方案是保存这些设置并将它们设置在 中。minipage我还将环境高度设为可选参数。

\documentclass[12pt]{article}

% For saving the current parskip and parindent
\newlength{\saveparskip}
\newlength{\saveparindent}

% Environment with option to set height
\newenvironment{foo}[1][]
    {%
    \par\noindent%
    \setlength{\saveparskip}{\parskip}%
    \setlength{\saveparindent}{\parindent}%
    \begin{minipage}[t][#1]{\linewidth}%
    \setlength{\parskip}{\saveparskip}\setlength{\parindent}{\saveparindent}
    }
    {\strut\end{minipage}\par}

\setlength{\parskip}{2.5ex plus 0.5ex minus 0.2ex}
\setlength{\parindent}{0.0in}

\begin{document}

Outside text
\begin{foo}
    This is in the new environment
\end{foo}
outside text
\begin{foo}[1in]
    This is in the new environment with set height
\end{foo}
outside text

\end{document}

在此处输入图片描述

相关内容