# how to vertically align a cell in a table?

I have a table that I define

\begin{tabular}{lllllllllllll}
\includegraphics[width=7.3in]{img1.pdf} &
\includegraphics[width=7.3in]{img2.pdf} &
\includegraphics[width=7.3in]{img3.pdf} &
\includegraphics[width=7.3in]{img4.pdf} &
\includegraphics[width=7.3in]{img5.pdf} &
\includegraphics[width=7.3in]{img6.pdf} \\
\includegraphics[width=7.3in]{img7.pdf} &
\includegraphics[width=7.3in]{img8.pdf} &
\includegraphics[width=7.3in]{img9.pdf} &
\includegraphics[width=7.3in]{img10.pdf} &
\includegraphics[width=7.3in]{img11.pdf} &
\includegraphics[width=7.3in]{img12.pdf} \\
\includegraphics[width=7.3in]{img13.pdf} & \multicolumn{12}{|c}{
\hspace{3cm} \includegraphics[width=92cm]{i3} }
\end{tabular}


Not all images are of the same height. I want all of the images to be aligned to the top of the tabular cell, instead of the bottom, which seems to be the case now. How would I do that?

-
duplicate: tex.stackexchange.com/questions/7208/… –  xport Jul 10 '11 at 15:38
@xport: Thanks for adding the link. However, IMHO this question can be taken as a special case of the more general question you linked and can be solved with much less code. –  Martin Scharrer Jul 10 '11 at 16:06
Is it really just images or do you also have text? Especially multi-line text requires more work than when you have multiple images. It would be nice if you could rephrase your title if it's just about images. –  Martin Scharrer Jul 10 '11 at 17:07
{lllllllllllll} represents there are 13 columns. But the first and second rows has 6 columns each. –  xport Jul 10 '11 at 17:59
You are creating a poster? 7.3 inch / column x 6 columns x 2.54 cm = about 1.1 meter. –  xport Jul 10 '11 at 18:01

If you only have images in the cells you can use \raisebox to vertical shift all images below the baseline, so that they all get top aligned

\raisebox{-\height}{\includegraphics[width=7.3in]{imgX.pdf}}


However, if you have text in the same row it will be placed just above the images (baseline is on the top corner). In that more complicated case see How to vertically-center the text of the cells?.

-
PS: In the next release of adjustbox a new option raise for \includegraphics will be included, then you can use \includegraphics[width=7.3in,raise=-\height]{imgX.pdf} for short. I might also add a align=t or similar option which does the details for you. –  Martin Scharrer Jul 10 '11 at 15:51

Use \topincludegraphics defined as follows

\newcommand{\topincludegraphics}[2][]{%
\raisebox{\dimexpr-\height+\ht\strutbox\relax}{\includegraphics[#1]{#2}}}


instead of \includegraphics; without \ht\strutbox there would be gaps, because each table row is always at least as high as a strut.

-
+1 I didn't thought about the automatically added strut. (I did however plan to have an align=t and align=T (see my comment above) for exactly this two different cases; so actually I should have known better) –  Martin Scharrer Jul 10 '11 at 16:04
\documentclass{article}
\usepackage{array}

\newsavebox\topalignbox
\newcolumntype{T}{%
>{\begin{lrbox}\topalignbox
\rule{0pt}{\ht\strutbox}}
c
<{\end{lrbox}%
\raisebox{\dimexpr-\height+\ht\strutbox\relax}%
{\usebox\topalignbox}}}

%%% or
%\newcolumntype{T}{%
%  >{\vtop\bgroup\vspace*{-\ht\strutbox}%
%    \hbox\bgroup\rule{0pt}{\ht\strutbox}}
%  c
%  <{\egroup\egroup}}

\begin{document}

\begin{tabular}{|T|T|} \hline
\rule{2cm}{3cm} & \rule{3cm}{4cm} \\ \hline
\rule{4cm}{5cm} & \rule{2cm}{2cm} \\ \hline
aabb & ccdd \\ \hline
\end{tabular}

\end{document}


This is somewhat complex. It is different with xport's solution since it ease the width calculation. The core code is the same as egreg's.

For vertical centering, it is better to use primitive TeX's \vcenter:

\documentclass{article}
\usepackage{array}
\newcolumntype{M}{>{$\vcenter\bgroup\hbox\bgroup}c<{\egroup\egroup$}}
\begin{document}
\begin{tabular}{|M|M|} \hline
\rule{2cm}{3cm} & \rule{3cm}{4cm} \\ \hline
\rule{4cm}{5cm} & \rule{2cm}{2cm} \\ \hline
\end{tabular}
\end{document}


-
Package varwidth Warning: Failed to reprocess entire contents on input line 15. –  egreg Jul 10 '11 at 16:47
IMHO this answer would be better suited for How to vertically-center the text of the cells?. The idea was to have this similar question for the specific case of images with simpler answer and the linked question for the more complicated general case. (maybe I should merge the two question after all?) –  Martin Scharrer Jul 10 '11 at 16:52
BTW: you could use the lrbox environment to box the whole cell and then use \raisebox on it. But the trick is to aligned in right. The geometric center or top seems to be not "the correct" (TM) one. –  Martin Scharrer Jul 10 '11 at 16:54
@egreg: Yes, I noticed that. But the result seems OK, I didn't pry varwidth's code. varwidth here is certainly overused. I'm thinking about a new implementation. –  Leo Liu Jul 10 '11 at 16:58
\newcolumntype{T}{>{\vtop\bgroup\vspace*{-\ht\strutbox}\hbox\bgroup}c<{\egroup\‌​egroup}} –  egreg Jul 10 '11 at 17:03