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.

I'm trying to center all images vertically and horizontally within a table cell.

Therefore I'm using the adjustbox packet. This is the code I use for my table:

 \begin{table}[b]
 \begin{center}
   \begin{tabular}{ |c|c|c|c|c| }
     \hline                
      \adjustbox{valign=m}{
        \includegraphics[width=2cm,height=1cm,keepaspectratio]{./images/buttons/Windows-Buttons.png}
      }
      & 
      \adjustbox{valign=m}{
        \includegraphics[width=2cm,height=1cm,keepaspectratio]{./images/buttons/MacOSX-Buttons.png}
      }
      & 
      [...]
      & 
      \adjustbox{valign=m}{
        \includegraphics[width=2cm,height=1cm,keepaspectratio]{./images/buttons/Android-Buttons.png}
      }

      \\[.8cm] 

     \hline
     Windows 7 & Mac OS X & Gnome & iOS & Android \\
 \hline
   \end{tabular}
\end{center}

\vspace{-.5cm}
\caption{Buttons verschiedener Betriebssysteme}
\end{table}

If I attach adjustbox to every \includegraphics I don't get the desired output: http://i47.tinypic.com/apiu12.jpg

If I remove only one \adjustbox (no matter which one), with following code:

%\adjustbox{valign=m}{
  \includegraphics[width=2cm,height=1cm,keepaspectratio]{./images/buttons/MacOSX-Buttons.png}
%}

I get the desired result (well, some kind of ...): http://i49.tinypic.com/11kxlk9.jpg

I don't have to use \adjustbox. I just want the images to be vertically and horizontally centered.

Thank you for your help!

share|improve this question
1  
Welcome to TeX.SX. Pleas always post complete, compilable but minimal examples, i.e. add a small preamble etc. Some general notes: Please add a % on lines ending on { to avoid getting a space here from the line break, i.e. write \adjustbox{valign=m}{%. Note that with the export option of adjustbox you can use valign=m directly as \includegraphics option. There is also a \adjustimage macro. The center environment should not be used inside table. See Should I use center or centering for figures and tables? –  Martin Scharrer Jan 4 '13 at 20:55
    
Thank you for your advice about formatting a post and about how to use adjustbox. –  Flying Rocket Jan 4 '13 at 22:03

1 Answer 1

up vote 6 down vote accepted

This is not an issue with adjustbox. The first image is exactly what your code requests: All images vertically aligned plus 0.8cm whitespace at the bottom. Apparently you want also some whitespace on top, which is however not the job of valign. You may use the margin option of adjustbox here.

\documentclass{article}

\usepackage[export]{adjustbox}

\usepackage{mwe}% for this example only

\begin{document}

\lipsum % example text

\begin{table}[b]
\centering
\begin{tabular}{ |c|c|c|c|c| }
    \hline
    \includegraphics[width=2cm,height=1cm,keepaspectratio,valign=m,margin=0cm .8cm]{example-image}
    & 
    \includegraphics[width=2cm,height=1cm,keepaspectratio,valign=m,margin=0cm .8cm]{example-image-a}
    & 
    \includegraphics[width=2cm,height=1cm,keepaspectratio,valign=m,margin=0cm .8cm]{example-image-b}
    & 
    \includegraphics[width=2cm,height=1cm,keepaspectratio,valign=m,margin=0cm .8cm]{example-image-c}
    & 
    \includegraphics[width=2cm,height=1cm,keepaspectratio,valign=m,margin=0cm .8cm]{example-image-1x1}
    \\
    \hline
    Windows 7 & Mac OS X & Gnome & iOS & Android \\
\hline
\end{tabular}
\caption{Buttons verschiedener Betriebssysteme}
\end{table}

\lipsum % example text

\end{document}

Result

However 0.8cm seems to be a little to large here.

share|improve this answer
    
Also note that the order in which the options to includegraphics are used is important---i.e., set your margins after the width and height and the keepaspectratio. I was too slow to figure that one out to post the answer before @martin-scharrer:-) –  ArTourter Jan 4 '13 at 21:25
    
@ArTourter: The order-sensitivity is intentionally. You can e.g. first add a border and then resize if you want so. –  Martin Scharrer Jan 4 '13 at 21:28
    
I know that and it makes sense but I had not tweaked to it. I did not see it mention explicitly in the docs so I thought I'd mention it here since I encountered it. –  ArTourter Jan 4 '13 at 21:48
    
Thank you for your answer. Now I understand the usage of the export function of adjustbox. Your solution works great! I have two more questions: 1) According to your post, the \\[.8cm] don't define the row height, they define whitespace at the bottom of a row? Because it was my intention to define the row height with this code. 2) Do I still need \\[.8cm] if I write margin=.8cm .8cm? Or does margin=.8cm .8cm set the right/left margin to .8cm and not the top/bottom? –  Flying Rocket Jan 4 '13 at 22:00
1  
@FlyingRocket: just check the description of margin in the adjustbox content. As said, \\[.8cm] will add additional vertical space after the row, not set the height. –  Martin Scharrer Jan 4 '13 at 22:57

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.