根据参数定义新环境

根据参数定义新环境

我想创建一个新的环境,该环境取决于参数是否存在。基本定义(效果很好)是:

\newrgbcolor{bcolor}{1. 1. 1.}%
\newenvironment{pbox}
{\begin{lrbox}{\dummybox}%
\begin{minipage}{0.95\linewidth}
}
{\end{minipage}\end{lrbox}%
{\setlength{\fboxsep}{0.02\linewidth}%
\colorbox{bcolor}{\usebox{\dummybox}}}\vskip1.2em}

在某些情况下,我需要指定小页面的高度,因此我尝试了

\usepackage{ifthen}
\newrgbcolor{bcolor}{1. 1. 1.}%
\newenvironment{pbox}[1][0]
{\begin{lrbox}{\dummybox}%
\ifthenelse{\value{#1}=0}
 {\begin{minipage}{0.95\linewidth}}
 {\begin{minipage}[c][#1][c]{0.95\linewidth}}
}
{\end{minipage}\end{lrbox}%
{\setlength{\fboxsep}{0.02\linewidth}%
\colorbox{bcolor}{\usebox{\dummybox}}}\vskip1.2em}

但事实并非如此。

在大多数情况下,我希望使用不带额外参数的 pbox 来自动检测正确的大小。只有当我指定附加参数时,框才会固定为此大小。

例子:

\begin{pbox}
 This is a normal pbox which height is depending on the content. 
\end{pbox}
\begin{pbox}[10cm]
 This is a pbox which is at least 10cm height
\end{pbox}

如何正确完成此操作?

答案1

这有效(etoolbox可以做的更多ifthen

\documentclass[a4paper]{memoir}
\usepackage{xcolor}
\usepackage{etoolbox}
\newsavebox\dummybox
\newenvironment{pbox}[1][]{
  \begin{lrbox}{\dummybox}%
    \ifblank{#1}{%
      \begin{minipage}{0.95\linewidth}%
    }{%
      \begin{minipage}[c][#1][c]{0.95\linewidth}%
    }%
  }{
    \end{minipage}%
  \end{lrbox}%
  \setlength{\fboxsep}{0.02\linewidth}%
  \colorbox{red}{\usebox{\dummybox}}%
  \vskip1.2em
}
\begin{document}
\begin{pbox}
   sdfsdfsd asdfasdf
as

 asdfasdf
\end{pbox}

\begin{pbox}[3cm]
  sdfsdfsd
 asdfasdf
 as

 asdfasdf
\end{pbox}
\end{document}

相关内容