# Vertically and horizontally align image inside table

I have a table with two columns. The first column contains text, the second a image. Short example:

\documentclass{article}
\begin{document}

\begin{table}[htbp]
\centering
\caption{My caption}
\label{tab:mytab}
\begin{tabular}{|p{1.0cm}|c|}
\hline
\textbf{Number}   & \textbf{Images} \\ \hline
Nr. 1 & \raisebox{.5\totalheight}{\includegraphics[width=0.8\textwidth]{foo}}       \\ \hline
Nr. 2 & \raisebox{.5\totalheight}{\includegraphics[width=0.8\textwidth]{foo}}       \\ \hline
\end{tabular}
\end{table}

\end{document}


I want to center the text and image vertically and horizontally. Does anyone know how to do this?

• You left off the minus, \raisebox{-.5\totalheight}{} Jun 15, 2016 at 9:56

With help of package adjustbox you can obtain:

In the first row the margin above and below picture is added, in the second row the image is without added margins. The code:

\documentclass{article}
\usepackage{graphicx}

\begin{document}
\begin{table}[htbp]
\centering
\caption{My caption}
\label{tab:mytab}
\begin{tabular}{|p{1.5cm}|c|}
\hline
\textbf{Number}
&   \textbf{Images} \\
\hline
Nr. 1   &   \includegraphics[width=0.8\textwidth,
margin=0pt 1ex 0pt 1ex,valign=m]{example-image}  \\
\hline
Nr. 2   &   \includegraphics[width=0.8\textwidth,valign=m]{example-image}  \\
\hline
\end{tabular}
\end{table}
\end{document}


Addendum: If you like to have horizontally centered contents also in the first table's column, than the following changes are necessary:

• in preamble to be add \usepackage{array}
• in table parameters the column type p{...} should be replaced by >{\centering}p{...}

In the provided example, a minus sign was left off the length .5\totalheight. Graphical images are set above the baseline, when using \includegraphics, whereas tabular cells are centered. Thus, one must move the graphical image down by half its height to compensate.

\documentclass{article}
\usepackage{graphicx}
\begin{document}

\begin{table}[htbp]
\centering
\caption{My caption}
\label{tab:mytab}
\begin{tabular}{|p{1.0cm}|c|}
\hline
\textbf{Number}   & \textbf{Images} \\ \hline
Nr. 1 & \raisebox{-.5\totalheight}{\includegraphics[width=0.8\textwidth]{example-image}}       \\ \hline
Nr. 2 & \raisebox{-.5\totalheight}{\includegraphics[width=0.8\textwidth]{example-grid-100x100pt}}       \\ \hline
\end{tabular}
\end{table}

\end{document}


To get even more precise centering, the downward shift of the image should be compensated by 1/2 the height of a line of text, thus, the shift should proceed as \raisebox{-.5\dimexpr\totalheight-\ht\strutbox}{...}.

Note I also widened the first column to 1.3cm, so as to allow the word Number to fit.

\documentclass{article}
\usepackage{graphicx}
\begin{document}

\begin{table}[htbp]
\centering
\caption{My caption}
\label{tab:mytab}
\begin{tabular}{|p{1.3cm}|c|}
\hline
\textbf{Number}   & \textbf{Images} \\ \hline
Nr. 1 & \raisebox{-.5\dimexpr\totalheight-\ht\strutbox}{%
\includegraphics[width=0.8\textwidth]{example-image}}       \\ \hline
Nr. 2 & \raisebox{-.5\dimexpr\totalheight-\ht\strutbox}{%
\includegraphics[width=0.8\textwidth]{example-grid-100x100pt}}\\ \hline
\end{tabular}
\end{table}

\end{document}