Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Recently, I fiddled with placing a table and a figure next to each other such that their bottom parts aligned with each other. I managed to do by using \vbox and \hbox, but there is one thing that bothers me: In order for it to work, I had to put a \vspace{0pt} after the table inside the \vbox{}. Why?

To illustrate, here is an example. Notice that I don't need the vspace{0pt} for the \rules, so why is it only necessary for the table?

enter image description here

\documentclass{article}

\begin{document}
\newsavebox{\firstfig}
\newsavebox{\secondfig}
\newsavebox{\thirdfig}
\savebox{\firstfig}{%
  \begin{tabular}{ l c r }
    \hline
    1 & 2 & 3 \\
    4 & 5 & 6 \\
    7 & 8 & 9 \\
    \hline
  \end{tabular}
}
\savebox{\secondfig}{%
  \rule{5em}{5em}
}
\savebox{\thirdfig}{%
  \rule{2em}{2em}
}

\noindent
Without \textsf{\textbackslash vspace\{0pt\}}: \\
\mbox{}%
\vbox{\vbox{\hbox{\usebox{\firstfig}}}}
\vbox{\vbox{\hbox{\usebox{\secondfig}}}}

\vspace{3\baselineskip}

\noindent
With \textsf{\textbackslash vspace\{0pt\}}: \\
\mbox{}%
\vbox{\vbox{\hbox{\usebox{\firstfig}}\vspace{0pt}}}
\vbox{\vbox{\hbox{\usebox{\secondfig}}}}

\vspace{3\baselineskip}

\noindent
Without \textsf{\textbackslash vspace\{0pt\}}: \\
\mbox{}%
\vbox{\vbox{\hbox{\usebox{\thirdfig}}}}
\vbox{\vbox{\hbox{\usebox{\secondfig}}}}

\end{document}
share|improve this question
add comment

2 Answers

up vote 10 down vote accepted

A fundamental concept for understanding box placement is that of reference point. Each box has one, and also other items define one.

What TeX processes when typesetting are lists of items. You're mainly interested in vertical lists, where TeX just piles up horizontal boxes and vertical space items (plus other things that are not so relevant here). Between hboxes some glue (vertical spacing) can be inserted automatically by TeX to ensure equidistance between the reference points of these.

What's the reference point of an hbox? It's just the same: every box (or character inside) has one and TeX usually puts together these boxes so that their reference points are on the same horizontal line. However some of them may be shifted vertically: this is the case of the box that results from

\begin{tabular}{<arg>}
<tabular content>
\end{tabular}

whose reference point lies midway from the top to the bottom of the typeset tabular.

If a rule (or an image) is not shifted (which requires manual intervention by the user) its reference point is the lower left corner.

The reference point of a \vbox is the one of the last item inside it. It follows that saying \vbox{\vbox{...}} is just the same as \vbox{...}.

Your first example
The reference points of the two boxes are on the same horizontal line (remember that the "tabular box" has its reference point in the middle).

Your second example
The reference points of the two boxes are on the same horizontal line:

\mbox{}% to ensure horizontal mode
\vbox{\vbox{\hbox{\usebox{\firstfig}}\vspace{0pt}}}
\vbox{\vbox{\hbox{\usebox{\secondfig}}}}

can be reduced to

\mbox{}% to ensure horizontal mode
\vbox{\usebox{\firstfig}\vspace{0pt}}
\usebox{\secondfig}

and in the \vbox the last item is a spacing item, that defines as reference point its bottom.

Your third example
Again the boxes are aligned with respect to their reference points:

\mbox{}% to ensure horizontal mode
\vbox{\vbox{\hbox{\usebox{\thirdfig}}}}
\vbox{\vbox{\hbox{\usebox{\secondfig}}}}

is equivalent to

\mbox{}% to ensure horizontal mode
\usebox{\thirdfig}
\usebox{\secondfig}

because the enclosing boxes do not do anything that changes the reference point position. The boxes contain rules, whose reference point is the lower left corner.

How to get automatically bottom alignment

If you say

\savebox{\firstfig}{%
  \begin{tabular}[b]{ l c r }
    \hline
    1 & 2 & 3 \\
    4 & 5 & 6 \\
    7 & 8 & 9 \\
    \hline
  \end{tabular}
}

(notice the optional argument [b] to \begin{tabular}) the tabular will have its reference point at the bottom item inside it, that is, the \hline. Thus

\usebox{\firstfig} \usebox{\secondfig}

will do what you're looking for without the need to enclose things in an outer \vbox.

Top alignment

Top alignment is slightly harder, but not impossible. With

\savebox{\firstfig}{%
  \begin{tabular}[t]{ l c r }
    \hline
    1 & 2 & 3 \\
    4 & 5 & 6 \\
    7 & 8 & 9 \\
    \hline
  \end{tabular}
}

the reference point of \firstfig will be the top \hline; so what you need is to shift down \secondfig:

\usebox{\firstfig}
\raisebox{-\height}{\usebox{\secondfig}}

but the second box will be slightly lower than the first one, because the reference point is the bottom of the \hline, which has a thickness. With the calc package one can adjust for it:

\usepackage{calc} % in the preamble

\usebox{\firstfig}
\raisebox{-\height+\arrayrulewidth}{\usebox{\secondfig}}

Final remark

The package adjustbox by Martin Scharrer lifts many of these problems, providing very easy methods for modifying the reference point and the alignment of the objects.

share|improve this answer
add comment

The reason is that the tabular has some depth, i.e. it propagetes under the baseline. If you change it to

  \begin{tabular}[b]{ l c r }

you get what you want, because with the [b] flag, the tabular is aligned above baseline (b for baseline at the Bottom).

The command \vspace{0pt} makes it work because it makes the baseline of the whole box to be below the table, at the place of this \vspace.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.