[Tex/LaTex] How to stack boxes like a vertical version of \mbox

boxeshorizontal alignmentvertical alignment

I would like a vertical equivalent to \mbox{} that somehow takes a sequence of boxes and stacks them on top of each other.

I'd like the resulting width to be the width of the widest box, and I'd like it to have the following options:

  • either no vertical space between the boxes or standard inter-line spacing
  • left, right, or center alignment
  • the baseline of the resulting box goes through one of the following:

    • top: the baseline of the first box (like \vtop)
    • middle: in the middle of the box
    • bottom: the baseline of the last box (like \vbox)

For example:

surrounding text
\fbox{\stack[align=rt]{something}{something else}{foo}}
surrounding text

would produce:

                 +----------------+
surrounding text |      something | surrounding text
                 | something else |
                 |            foo |
                 +----------------+

Does something like this already exist? If not, does anyone have any pointers about how to make it?

Edit: I'm using LaTeX

Best Answer

The v0.8 of adjustbox from 2011/11/14 has now a \stackbox[<hor>][<vert>]{<content>} macro (and a matching stackbox environment and stack key). It accepts vertical and horizontal alignment and uses the varwidth package internally. To remove the usual skip between lines, you can use \baselineskip=0pt at the begin and \par at the end. I might add an option for this later.

\documentclass{article}

\usepackage{adjustbox}

\begin{document}

surrounding text
\fbox{\stackbox[r][t]{something\\something else\\foo}}
surrounding text

surrounding text
\fbox{\stackbox[l][b]{something\\something else\\foo}}
surrounding text

surrounding text
\adjustbox{stack=cc,fbox}{something\\something else\\foo}
surrounding text

% No interline skip:
surrounding text
\fbox{\stackbox[c][b]{\baselineskip=0pt something\\something else\\foo\par}}
surrounding text

\end{document}

Result