# Stacking two hboxes

I am trying to write the macro \foo{...}{...} that creates an hbox made up of both \hbox{#1} and \hbox{#2}, such that both hboxes are stacked one on top of the other. The width of the resulting hbox should be the maximum of the two hboxes. The arguments to the macro are in plain mode (i.e., not in math mode).

-
Note that it is unusual around here to sign your questions (as there is already a box with your username below it). –  lockstep Mar 5 '11 at 17:22

While this is an old question, it looked like a good place to highlight the various capabilities of my new stackengine package in one place, rather than (as I have been doing) on a piecemeal basis. All the gap/shift distances are specifiable, as well. [EDIT: This MWE revised to use V2.0 syntax, which was implemented to allow stackgap lengths to be scalable with fontsize. The V2.0 package was submitted to CTAN 7/11/13]

\documentclass{article}
\usepackage{stackengine}
\def\x{\rule[-.2pt]{2ex}{.4pt}}
\parindent 0pt\parskip 1em
\def\blmark{\x\stackengine{-.9ex}{B}{\rule{.35ex}{0pt}L}{U}{l}{F}{T}{S}\x}
\begin{document}

The \textsf{stackengine} package:

SHORT STACKS (constant inter-item gap):
\def\stacktype{S}\setstackgap{S}{4pt}
\\with center alignment

\blmark
\stackon{macro}{stackon}\x
\stackunder{stackunder}{macro}\x
\Shortstack{this is the Shortstack macro}\x
\Shortunderstack{the Shortunderstack macro}\x
\blmark

LONG STACKS (constant inter-baseline distance):
\def\stacktype{L}\setstackgap{L}{1.1\baselineskip}
\\with left alignment\def\stackalignment{l}

\blmark
\stackon{macro}{stackon}\x
\stackunder{stackunder}{macro}\x
\Longstack{this is the Longstack macro}\x
\Longunderstack{the Longunderstack macro}\x
\blmark

\def\useanchorwidth{T}

use anchorwidth for
{\Longunderstack{this is the Longunderstack macro}}
stack rather than the full stackwidth

\def\stackalignment{c}
\def\stacktype{L}
In addition, the \textsf{stackengine} package does baseline
\belowbaseline[6pt]{shifts}, as well as
\tllap{tl}\bllap{bl}lapp\tclap{tc}\bclap{bc}ing\trlap{tr}\brlap{br},
and insetting (\bottominset{*}{O}{-2pt}{}verlapping) text and
\def\stackalignment{r}
\topinset{\rule{1cm}{1cm}}
{\fbox{\rule[-1.5cm]{0pt}{3cm}Figures~~~~~}}{2pt}{4pt}

\end{document}


-

In general you can stack \hboxes inside a \vbox (which should be placed inside a \hbox as well if you want to use the result in horizontal mode), like Gonzalo Medina shows in his nice answer.

There are also some macros and packages for this:

• \shortstack{foo\\bar} will stack foo over bar (adding \struts might be a good idea to get correct baseline skip)
• The small minibox package provides \minibox:
\minibox{foo\\bar}
It also accepts an optional argument [l], [c] or [r] to align the lines.

Both macros allow for more than two hbox'es/lines.

-
If you try \foo{some}{test text} \foo{some}{test text} \foo{some}{test text} then Gonzalo's solution does not work -- the stacks are placed one under the other, rather then to their side. Now the minibox package works just great, and I created a wrapper macro that does just what I need. Thanks! –  Mayer Goldberg Mar 5 '11 at 17:51
@Mayer Goldberg: Note that \minibox itself is just a wrapper around tabular: \long macro:[#1]#2->\begin {tabular}{@{}#1@{}} #2 \end {tabular} –  Martin Scharrer Mar 5 '11 at 18:00

Perhaps something like this?

\documentclass{article}
\newcommand*\foo[2]{%
\hbox{\vbox{\hbox{#1}\hbox{#2}}}
}

\begin{document}

\foo{some}{test text}

\end{document}


or (more in TeX's spirit):

\documentclass{article}
\def\foo#1#2{%
\hbox{\vbox{\hbox{#1}\hbox{#2}}}
}

\begin{document}

\foo{some}{test text}

\end{document}

-
Not quite: The problem with this is that I need these foo-stacks to be printed one after the other on the same line. So I might use it as \foo{in}{in} \foo{principio}{the beginning} \foo{creavit}{created (3rd, sg)} \foo{Deus}{God} \word{caelum}{heaven} \word{et}{and} \word{terram.}{earth.} I'm trying to use this for inter-linear texts. –  Mayer Goldberg Mar 5 '11 at 17:40
@Meyer That's because you're using it before entering horizontal mode (i.e., before properly beginning a paragraph). You can take care of this case by changing it to \def\foo#1#2{\leavevmode\hbox{\vbox{\hbox{#1}\hbox{#2}}}}, i.e., just insert \leavevmode. –  Phil Hirschhorn Mar 5 '11 at 18:40