# Aligning caption of subfigure with math environmenth to the bottom

I was trying to align the subcaption letter of a subfigure that contains a math environment using temporal boxes, to align it vertically to the biggest one. However, when I do it the subcaption letter moves to the side.

What I'm doing wrong? I want to have the third expression align to the middle (vertically) and the three caption letter to appear align horizontally.

\documentclass[twocols]{article}
\usepackage{amsmath}
\usepackage{subfig}
\begin{document}

\newbox{\tmpbox}
\begin{figure}
\sbox{\tmpbox}{$% \begin{bmatrix}% 1 & 2 & 3\\ 1 & 2 & 3\\ 1 & 2 & 3 \end{bmatrix}%$}%
\subfloat[]{%
\usebox{\tmpbox}\label{fig:ex-a}%
}
\subfloat[]{%
\label{fig:ex-b}
$\begin{bmatrix} 1 & 2 & 3\\ 1 & 2 & 3\\ 1 & 2 & 3 \end{bmatrix}$
}%
\subfloat[]{%
\vbox to \ht\tmpbox{%
\vfill%
$\overbrace{123}^{x}\overbrace{45}^{y}\overbrace{67}^{z}$%
\vfill}\label{fig:ex-c}%
}
\caption{My caption.}
\label{fig:ex}
\end{figure}

\end{document}

-

There are two flaws in your approach:

1. When you typeset a paragraph in a \vbox, it gets the same width as the current line width

2. You're not taking into account the depth of \tmpbox

\subfloat[]{%
$\vcenter to \dimexpr\ht\tmpbox+\dp\tmpbox\relax{ \vfill \hbox{$\overbrace{123}^{x}\overbrace{45}^{y}\overbrace{67}^{z}$} \vfill}$\label{fig:ex-c}%
}


When doing experiments with boxes, it helps putting them in a \fbox command, so that we can figure out what TeX is really thinking they are.

Matrices are, by default, centered with respect to the line (more precisely, with respect to the axis of formulas, which runs slightly above the baseline); thus the box containing the first matrix will have some part of it (slightly more than half the total vertical size) above the baseline, which contributes to the height, and some part below the baseline (depth).

Thus, if we want that the last subfigure take exactly the same vertical space as the matrix, we need to make it as large as the sum of height and depth. But we need also to center it with respect to the axis of formulas, which is a job for \vcenter that can be issued only in math mode:

$\vcenter to\dimexpr\ht\tmpbox+\dp\tmpbox\relax{...}$


does what we need.

Now let's look at what happens inside the \vcenter. If we say

\vfill
$...$
\vfill


then TeX will start a paragraph as soon as it scans the $ and this paragraph will be assigned the current line width, no matter if it is less than one line long. Thus we need to avoid TeX starting a paragraph: it must remain in the mode when it simply stacks boxes one above the other, also known as vertical mode. Therefore we say \vfill \hbox{$...$} \vfill  that will do the job. The horizontal size of the \vcenter box will always be the maximum of the horizontal boxes it contains, in this case precisely the size of the formula. Look at the package adjustbox to get similar results in a "more LaTeX" way. Alternatively, and probably this is the simplest way, load the varwidth package and write \subfloat[]{% \begin{varwidth}[c][\dimexpr\ht\tmpbox+\dp\tmpbox][c]{\linewidth}$\overbrace{123}^{x}\overbrace{45}^{y}\overbrace{67}^{z}\$%
\end{varwidth}}

-
Thanks that worked. Can you elaborate a little bit more in the explanation about the computation of the \tmpbox dimensions. Also, why do you need the extra \hbox. And also, you have double math mode, why? –  adn Jan 4 '12 at 12:28