2

I want to create a new environment which depend if a parameter exists or not. The basic definition, which works fine, is:

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

In some cases I need to specify the height of the minipage, there for I tried

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

which does not.

In most cases I want to use the pbox without the extra argument to detect the right size automatilly. Only when I specifiy the additional parameter the box should be fixed to this size.

Example:

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

How can this be done in a correct way?

  • 1
    First of all, start by posting a full MWE (minimal example), e.g. where does \newrgbcolor come from? Also I do not think it is a good idea to declare a new color each time the environment is used. – daleif Feb 1 '12 at 15:10
  • The color comes from the original version which I try to modify. The pbox is used on a poster with non-white background. That's why I fixed the color which was originally an optional parameter. – M.K. aka Grisu Feb 1 '12 at 15:17
  • That still does not help the rest of us, why should we debug irrelevant parts of your code? The color part is irrelevant for the problem at hand. – daleif Feb 1 '12 at 15:19
3

This works (etoolbox can do much more than 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}

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.