Saturday, October 11, 2014

Latex: Centering text in a table row when there is a image

When you are embedding images in a Latex table, often the text gets aligned to the bottom of the row, and the image decides the height of the row. To align the text in the columns to be vertically centered we have two options, as discussed in references [1][2][3][4].

Solution 1

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). As described in [2], you can defines a new column type M which does all of the above:
\usepackage{array}
\newcolumntype{M}{>{\centering\arraybackslash}m{\dimexpr.25\linewidth-2\tabcolsep}}
And use can use it in a table as:
\begin{tabular}{|M|M|M|M|}

Or as described in [4], we can also define a new column type C as follows:
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}

Solution 2

Another simpler solution is to raise the baseline of the image, as text in a row is aligned to the image's baseline. The baseline of an image is at the bottom. You could use \raisebox to shift it up. Use half of \height, which stands for the height of the box to be raised.
\raisebox{-.5\height}{\includegraphics{some_picture}}

You can use it in a table as follows:
\begin{tabular}{lll}
\raisebox{-.5\height}{\includegraphics[scale=0.25]{example-image}} & text & text\\
\end{tabular}

References:
1. http://stackoverflow.com/questions/1357798/how-to-center-cell-contents-of-a-latex-table-whose-columns-have-fixed-widths
2. http://tex.stackexchange.com/questions/46386/vertically-center-cells-of-a-table
3. http://tex.stackexchange.com/questions/19080/how-to-vertically-center-text-with-an-image-in-the-same-row-of-a-table
4. http://www.latex-community.org/forum/viewtopic.php?f=45&t=15908

No comments:

Post a Comment