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.

What is the easiest way to center align each row of table? I have a table column that contains an image and other cells in the same row contain text. I would like to know how to vertically center align the image and text cells. Currently each row is vertically bottom aligned. Here is my code:

\documentclass[a4paper]{article}
\usepackage[margin=1.0in]{geometry}
\usepackage{booktabs, caption, underscore, url, graphicx}
\captionsetup{font=bf}
\date{2012-03-01}
\begin{document}
\begin{table}[ht]
\centering

\begin{tabular}{cccc} \bottomrule[2pt]
A & B & V & D & E \\ \bottomrule
\includegraphics[scale=0.35]{img1.eps} & 2 & 3 & 4 \\ 
\includegraphics[scale=0.35]{img2.eps} & 2 & 3 & 4  \\ 
\bottomrule[2pt]
\end{tabular}

\end{table}
\end{document}

Here is a quick sketch of the table I am trying to achieve:

enter image description here

The first column contains an image while the other columns contain a single line of text. How can the text be center aligned with the middle of the image?

share|improve this question
2  
Do you mean vertical alignment? –  Count Zero Mar 1 '12 at 16:13
    
Hmmm, what I am trying to say is: How can I align the contents of each cell in a row, such that they are centered horizontally. I've added a drawing to the question to provide clarity. –  dr.bunsen Mar 1 '12 at 16:35
    
I think you mean that in each row the vertical centers of each cell lie in the same (horizontal) line. That's usually called vertical alignment. –  Matthew Leingang Mar 1 '12 at 16:37

2 Answers 2

up vote 21 down vote accepted

Vertically centering cell entries is possible via the m{<width>} column type from the array package. Horizontal centering is obtained by prepending the column entries with \centering\arraybackslash (also supported by array). For completeness and brevity, the MWE below defines the new column type M which does all of the above:

enter image description here

\documentclass[a4paper]{article}
%\usepackage{showframe}% http://ctan.org/pkg/showframe
\usepackage[margin=1.0in]{geometry}% http://ctan.org/pkg/margin
\usepackage{booktabs}% http://ctan.org/pkg/booktabs
\usepackage{array}% http://ctan.org/pkg/array
\newcolumntype{M}{>{\centering\arraybackslash}m{\dimexpr.25\linewidth-2\tabcolsep}}
\begin{document}
\begin{table}[ht]
  \centering
  \begin{tabular}{MMMM}
    \toprule
    A & B & D & E \\
    \midrule
    \rule{15pt}{10pt} & One & Two & Three \\ 
    \rule{15pt}{10pt} & Three & One & Two \\ 
    \rule{15pt}{10pt} & Two & Three & One \\ 
    \bottomrule
  \end{tabular}
\end{table}
\end{document}​

For images, I've used \rule{15pt}{10pt}, although the above solution works for any image size.

The table width is also chosen to fit exactly within the text block, making each of the four columns the same width (.25\linewidth-2\tabcolsep). To see this, uncomment the showframe package which highlights the text block boundary.

share|improve this answer
    
Fantastic! Thank you for the help. This did precisely what I was looking for. I previously tried to find a solution with the array package, but I was unsuccessful. –  dr.bunsen Mar 1 '12 at 16:58
    
This is great! I get an error and my table doesn't compile whenever I replace the M in the \columntype{}{} command with anything longer than 1 character. –  John Mark Aug 1 '12 at 10:10
    
@JohnMark: You can only use a single character for a column type. Otherwise is would be difficult to assess whether the column specification MR (say) refers to a single MR column or a two-column M and R arrangement. –  Werner Aug 1 '12 at 14:04

Images are always set with their base on the baseline (if they aren't rotated). So what you need is to lower the graphics; the easiest way is to say

\raisebox{-.5\height}{\includegraphics[scale=0.35]{img1.eps}}

If the height of the row is to be taken into consideration, then a slightly more complex calculation is necessary:

\raisebox{\dimexpr-.5\height+.5\ht\strutbox\relax}
  {\includegraphics[scale=0.35]{img1.eps}}

The adjustbox package has many features that ease this kind of job; after \usepackage{adjustbox} you can say

\adjustbox{valign=m}{\includegraphics[scale=0.35]{img1.eps}}
share|improve this answer
    
Thanks for the help. For some reason I could get your solution to work. –  dr.bunsen Mar 1 '12 at 16:58

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.