2

I was wondering if there is a more versatile version of minipage. Currently, I need a minipage with a specific vertical alignment characteristics and with a background color. So, I nest a minibox in a colorbox in an adjustbox. (See the sample below.) That's not neat. It's cumbersome and the syntax isn't uniform.

Of course, one can always make one's own macro or environment, but I wonder if somebody has already made a package that provides such a versatile box, such that

\begin{omnibox}[width=0.3\textwidth, bgcolor=red, valign=c]

Syntax is important: currently I can't do anything without looking at each package's documentation. I'm not able to remember whether colorbox has an environment version or not; whether colorbox uses the "bgcolor=red" syntax or not; whether it allows for changing a foreground color or not; whether adjustbox uses parentheses or square brackets for its arguments; etc.

[Edit: Please don't focus on the example below too much. For the particular example, you don't need adjustbox because minipage has some vertical alignment options, but minipage's capability doesn't include all the options of adjustbox. On the other hand, I think minipage is necessary to have a fixed-width textbox. I'm not 100% sure if adjustbox doesn't have the capability, though.]

Here is the sample:

\documentclass{article}
\usepackage[export]{adjustbox}
\usepackage{xcolor}
\usepackage{lipsum}
\begin{document}
\begin{adjustbox}{valign=c}
  \colorbox{red}{%
    \begin{minipage}{0.3\textwidth}
      \lipsum[1]
    \end{minipage}%
  }
\end{adjustbox}
\begin{adjustbox}{valign=c}
  \colorbox{gray}{%
    \begin{minipage}{0.5\textwidth}
      \lipsum[2]
    \end{minipage}%
  }
\end{adjustbox}
\end{document}

[Edit: I've added some %'s to remove spurious spacing. ]

5
  • 3
    You could have a look at tcolorbox. – thewaywewalk Aug 22 '16 at 7:53
  • 2
    tcolorbox is probably what you want. although in the example you give the markup is wrong, adjustbox isn't needed as minipage are vertically centred by default and you are missing a % after {red}{ and after \end{minipage} so you have a spurious space either side of the minipage in the colorbox. – David Carlisle Aug 22 '16 at 8:26
  • Thanks for the tcolorbox tips! . . . "in the example you give the markup is wrong" . . . I know! I thought I didn't have to bother because the point I'm making stands with or without correct spacing. – Ryo Aug 22 '16 at 13:28
  • Just a quick note: I've tried tcolorbox and failed so far. \begin{tcolorbox}[box align=center,width=0.3\textwidth,colback=red] does create a desired box, but two of them refuse to be set on a single line. I'll continue to study the tcolorbox package. – Ryo Aug 22 '16 at 15:18
  • Finally I'm able to use tcolorbox. You need the nobeforeafter option to prevent a tcolorbox from occupying an entire line. This is just a result of trial-and-error and I don't know details. How should I "close" this thread, now that exactly what I requested has been found? – Ryo Aug 26 '16 at 17:11
2

You don't seem to need adjustbox; here's an implementation with expl3.

\documentclass{article}
\usepackage{xparse}
\usepackage{xcolor}
\usepackage{lipsum}

\ExplSyntaxOn

\NewDocumentEnvironment{omnibox}{m}
 {
  \keys_set:nn { ryo/omnibox } { #1 }
  \__ryo_omnibox_start:
 }
 {
  \__ryo_omnibox_end:
 }

\keys_define:nn { ryo/omnibox }
 {
  width .dim_set:N = \l__ryo_omnibox_width_dim,
  width .value_required:n = true,
  bgcolor .tl_set:N = \l__ryo_omnibox_bgcolor_tl,
  valign .tl_set:N = \l__ryo_omnibox_valign_tl,
  valign .initial:n = c,
 }
\box_new:N \l__ryo_omnibox_wrapper_box

\cs_new_protected:Nn \__ryo_omnibox_start:
 {
  \tl_if_empty:NF \l__ryo_omnibox_bgcolor_tl
   {
    \begin{lrbox}{\l__ryo_omnibox_wrapper_box}
   }
  \begin{minipage}[\l__ryo_omnibox_valign_tl]{\l__ryo_omnibox_width_dim}
 }
\cs_new_protected:Nn \__ryo_omnibox_end:
 {
  \end{minipage}
  \tl_if_empty:NF \l__ryo_omnibox_bgcolor_tl
   {
    \end{lrbox}
    \colorbox{\l__ryo_omnibox_bgcolor_tl}{\usebox{\l__ryo_omnibox_wrapper_box}}
   }
 }
\ExplSyntaxOff

\begin{document}

\begin{omnibox}{width=4cm,bgcolor=green}
\lipsum[2]
\end{omnibox}\quad
\begin{omnibox}{width=4cm,valign=t}
\lipsum[2]
\end{omnibox}\quad
\begin{omnibox}{width=4cm,valign=b,bgcolor=red}
\lipsum[2]
\end{omnibox}

\end{document}

enter image description here

In order to also implement a foreground color, the lrbox should be used in all cases.

\documentclass{article}
\usepackage{xparse}
\usepackage{xcolor}
\usepackage{lipsum}

\ExplSyntaxOn

\NewDocumentEnvironment{omnibox}{m}
 {
  \keys_set:nn { ryo/omnibox } { #1 }
  \__ryo_omnibox_start:
 }
 {
  \__ryo_omnibox_end:
 }

\keys_define:nn { ryo/omnibox }
 {
  width .dim_set:N = \l__ryo_omnibox_width_dim,
  width .value_required:n = true,
  bgcolor .tl_set:N = \l__ryo_omnibox_bgcolor_tl,
  fgcolor .tl_set:N = \l__ryo_omnibox_fgcolor_tl,
  valign .tl_set:N = \l__ryo_omnibox_valign_tl,
  valign .initial:n = c,
 }
\box_new:N \l__ryo_omnibox_wrapper_box

\cs_new_protected:Nn \__ryo_omnibox_start:
 {
  \begin{lrbox}{\l__ryo_omnibox_wrapper_box}
  \tl_if_empty:NF \l__ryo_omnibox_fgcolor_tl
   {
    \color{\l__ryo_omnibox_fgcolor_tl}
   }
  \begin{minipage}[\l__ryo_omnibox_valign_tl]{\l__ryo_omnibox_width_dim}
 }
\cs_new_protected:Nn \__ryo_omnibox_end:
 {
  \end{minipage}
  \end{lrbox}
  \tl_if_empty:NTF \l__ryo_omnibox_bgcolor_tl
   {
    \usebox{\l__ryo_omnibox_wrapper_box}
   }
   {
    \colorbox{\l__ryo_omnibox_bgcolor_tl}{\usebox{\l__ryo_omnibox_wrapper_box}}
   }
 }
\ExplSyntaxOff

\begin{document}

\begin{omnibox}{width=4cm,bgcolor=green,fgcolor=blue}
\lipsum[2]
\end{omnibox}\quad
\begin{omnibox}{width=4cm,valign=t}
\lipsum[2]
\end{omnibox}\quad
\begin{omnibox}{width=4cm,valign=b,bgcolor=red}
\lipsum[2]
\end{omnibox}

\end{document}

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.