6

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?

David Carlisle
  • 757,742
kloop
  • 9,684
  • 1
    duplicate: http://tex.stackexchange.com/questions/7208/how-to-vertically-center-the-text-of-the-cells/7227#7227 – Display Name 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
  • 1
    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
  • 1
    {lllllllllllll} represents there are 13 columns. But the first and second rows has 6 columns each. – Display Name Jul 10 '11 at 17:59
  • 1
    You are creating a poster? 7.3 inch / column x 6 columns x 2.54 cm = about 1.1 meter. – Display Name Jul 10 '11 at 18:01
  • @kloop: I would recommend you to accept egreg's answer instead. Even if my very similar solution is the one you prefer for your specific document (a poster?), egreg's answer is the more correct one for the general case and would help others with the same issue more. – Martin Scharrer Jul 11 '11 at 06:32

3 Answers3

6

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?.

Martin Scharrer
  • 262,582
  • 1
    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
5

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.

egreg
  • 1,121,712
  • +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
5
\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}

enter image description here

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}

enter image description here

Leo Liu
  • 77,365
  • 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
  • @Martin: Indeed, I was answering vertically-center alignment question. After that I realized the origin question is asking for top alignment. I'll try lrbox or other method. And I want to get a general column type instead of egreg's \topincludegraphics. – Leo Liu Jul 10 '11 at 17:02
  • 1
    \newcolumntype{T}{>{\vtop\bgroup\vspace*{-\ht\strutbox}\hbox\bgroup}c<{\egroup\egroup}} – egreg Jul 10 '11 at 17:03
  • @Leo: I'm looking forward to see your solution. It's just about were to put it. This question should be either merged with the other one or renamed to something like "How to top align images in a table?". – Martin Scharrer Jul 10 '11 at 17:05
  • @Martin: Now updated solution uses lrbox. It works fine. – Leo Liu Jul 10 '11 at 17:13
  • @greg: Good. I'll still add a \rule{0pt}{\ht\strutbox} in the \hbox for plain text. – Leo Liu Jul 10 '11 at 17:17
  • @Leo: Nice. A \strut should also do it instead of the \rule. I coded a \collectbox{<code>}{<box content>} macro in my adjustbox package which accepts the second "argument" also as \bgroup .. \egroup box. Once the new version is released you could use >{\collectbox{\raisebox{..}{\BOXCONTENT}}\bgroup} .. <{\egroup} or smaller using plainTeX macros: >{\collectbox{\raise\dimexpr ..\relax\BOXCONTENT}\bgroup} .. <{\egroup} – Martin Scharrer Jul 10 '11 at 17:18
  • @Martin: I use \rule{0pt}{\ht\strutbox} instead of \strut to avoid extra depth. I didn't use your ajustbox but I tried environ package for this question, that isn't better than lrbox here. – Leo Liu Jul 10 '11 at 17:25
  • @Leo: The extra depth should be an issue when its set together with the image in horizontal mode and the image is not shorter than that depth. Also environ doesn't work in tabular cells. You need to use collcell for that. It uses a very similar technique but specially for tabular cells. However, lrbox is much better here anyway! (and the new version of adjustbox which brings that feature isn't released yet) – Martin Scharrer Jul 10 '11 at 17:29