# How to vertically-center the text of the cells?

I have a simple table as follows:

\begin{table*}
\centering
\begin{tabular}{|l|c|c|c|c|p{2in}|}
...
...
\end{tabular}
\caption{The factors the camera solver depends on to evaluate the rules.}
\label{table:factors}
\end{table*}


How is it possible to vertically-center the text of the cells?

-
This earlier question might be of help to you. – morbusg Dec 16 '10 at 12:34
Looking closer at your example, I realize you obviously have the array package loaded. p{...} aligns the content toward the top, m{...} aligns the content toward the center, while b{...} aligns it toward the bottom. – Jimi Oke Dec 17 '10 at 23:19
@Jimi: the example works even without array. The p specifier is standard. – Stefan Kottwitz Dec 18 '10 at 15:35
@Stefan: Oh, I didn't know that. Thanks! – Jimi Oke Dec 18 '10 at 16:18
Question, actually. How in the world would a person who knows nothing about code go about this? I'm drowning in information, here. – user44066 Jan 12 '14 at 23:33

\documentclass{article}
\usepackage[a4paper,vmargin=2cm,hmargin=1cm,showframe]{geometry}
\usepackage[demo]{graphicx}
\usepackage[table]{xcolor}
\usepackage{array}
\usepackage{longtable}

\parindent=0pt

\def\correction#1{%
\abovedisplayshortskip=#1\baselineskip\relax\belowdisplayshortskip=#1\baselineskip\relax%
\abovedisplayskip=#1\baselineskip\relax\belowdisplayskip=#1\baselineskip\relax}

\arrayrulewidth=1pt\relax
\tabcolsep=5pt\relax
\arrayrulecolor{red}
\fboxsep=\tabcolsep\relax
\fboxrule=\arrayrulewidth\relax

\newcolumntype{A}[2]{%
>{\minipage{\dimexpr#1\linewidth-2\tabcolsep-#2\arrayrulewidth\relax}\vspace\tabcolsep}%
c<{\vspace\tabcolsep\endminipage}}

\newenvironment{Table}[4]{%
\longtable{%
|A{#1}{1.5}% for figure
|>{\centering$\displaystyle}A{#2}{1}<{$}% for inline equation
|>{\correction{-1}\strut$}A{#3}{1}<{$\strut}% for displayed equation
|>{\centering}A{#4}{1.5}% for text
|}\hline\ignorespaces}{%
\endlongtable\ignorespacesafterend}

\newcommand{\dummy}{%
It is practically a big lie that \LaTeX\
makes you focus on the content without

\newcommand{\Row}{%
\includegraphics[width=\linewidth]{newton}&
\frac{a+b}{a-b}=0&
\int_a^b f(x)\, \textrm{d}x=\frac{b-a}{b+a}&
\fcolorbox{cyan}{yellow}{\parbox{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}{\dummy}}
\tabularnewline\hline}

\begin{document}
\begin{Table}{0.25}{0.25}{0.25}{0.25}
\Row
\Row
\end{Table}

\def\x{\centering$\displaystyle\int_a^bf(x)\,\textrm{d}x=\frac{a-b}{a+b}$}

\longtable{|A{0.2}{1.5}*2{|A{0.25}{1}}|A{0.3}{1.5}|}\hline
\x & \x & \multicolumn{2}{A{0.55}{1.5}|}{\x} \tabularnewline\hline
\multicolumn{2}{|A{0.45}{1.5}|}{\x} & \x & \x\tabularnewline\hline
\x & \multicolumn{2}{A{0.5}{1}|}{\x} & \x\tabularnewline\hline
\multicolumn{4}{|A{1}{2}|}{\x}\tabularnewline\hline
\endlongtable
\end{document}

-
Your solution is working absolutely fine, but isn't there a simpler solution? Aligning contents vertically feels time like simpler than the proposed solution. – Rafid Dec 16 '10 at 19:42
Thanks, but what about the other solution below? – Rafid Dec 26 '10 at 20:48
@xport: Your edits have nothing to do with Rafid's question. For his question, the widths of the columns just don't matter. I think it's not good to include stuff that's really unrelated. – Hendrik Vogt Dec 28 '10 at 22:25

One easy way to this would be to use the array package, specifying your column width with m{...}. For example:

\begin{tabular}{ m{4cm} m{1cm} }
... & ... \\
\end{tabular}


will give you a four centimeter-long column and a one centimeter-long column. In each cell, the contents will be vertically aligned to the center. Note, however, that the cell contents will be horizontally aligned left. If you also want to align all the cell contents toward the center in a horizontal sense, then you could do something like this:

\begin{tabular}{ >{\centering\arraybackslash} m{4cm} >{\centering\arraybackslash} m{4cm} }
... & ... \\
\end{tabular}


The point of \arraybackslash is to return \\ to its original meaning because the \centering command alters this and could possibly give you a noalign error during compilation.

If you have several columns and do not want your source to look cluttered, you could define new columns before your tabular environment, for example:

\newcolumntype{C}{ >{\centering\arraybackslash} m{4cm} }
\newcolumntype{D}{ >{\centering\arraybackslash} m{1cm} }
\begin{tabular}{ C D }
... & ... \\
\end{tabular}


There is a lot of useful information on tables in the wiki LaTeX guide, if you want to explore this further.

-
Are you sure that an image inclusion will be EXATCLY vertically centered using your method above? – xport Dec 19 '10 at 22:42
@xport: It might be relative to the first and last baselines of the cells, not the exact totalheight. – Martin Scharrer Jul 10 '11 at 15:53
When using this method, people should be cautious NOT to mix other column types such as p. The height of a row AND vertical-alignment follows that of the cell with the maximum height in that row. It is fine if an m column cell has the maximum height, but otherwise the vertical-align would not work. – Achimnol Dec 27 '12 at 15:22

There is a command \vcenter which vertically centers its content in horizontal mode. It can only be used in mathmode.

Here is an example with Plain XeTeX (compile with xetex yourfilename.tex)

{ \offinterlineskip
\def\trule{\noalign{\hrule}}
\def\hcenter#1{\hfil#1\hfil}
\halign{\vrule#&&\hcenter{$\vcenter{\hbox{#}}$}\vrule\cr\trule
&Lorem ipsum dolor sit amet&\XeTeXpicfile "test-pattern.jpg" &
\TeX&$E=mc^2$&$\displaystyle{a^2-b^2\over c^2}$\cr\trule
&Etiam quam lacus&\vrule width 4em height 5ex depth 2ex&\eTeX &
$E\ne mc^2$&{\it \&} cetera\cr\trule}
}
\bye


-