# Centering different sizes of images in one column of a table vertically and horizontally in each cell?

The table consists of lines of cells of text and a cell of image. The image in each line is within one column. But the images are of different sizes. Intends to center each image horizontally and vertically in its cell. How to write such code?

The code is as follows:

\begin{table*}[ht]
\footnotesize
\caption{Some table}

\begin{center}
\label{sometable}
\begin{tabularx}{7.4in}{ p{2cm} | p{1.8cm} | p{1.8cm} | p{3.5cm} | p{3.5cm} | p{3.5cm} }

\hline
\textbf{Image}  &  \textbf{Detected}  &  \textbf{Indirectly Related}  & \textbf{Sentences Generated by Model with Indirectly Related}  & \textbf{Sentences Generated by Model without Indirectly Related}  & \textbf{Standard Model}  \\
\raisebox{-1.0\height}{\includegraphics[width=0.8in]{some image}}  &  Some text  &  Some text  &  Some text  &  Some text  \\  \hline

\end{tabularx}
\end{center}
\end{table*}

• Welcome to TeX SX! Could you post a minimal example code of what you've tried. How are defined the cells in text columns? Jun 11, 2018 at 0:42
• To vertically center an image, use \raisebox{-0.5\height}{...}. In a tabular, use c for the image and p{...} for the text. Jun 11, 2018 at 0:53
• @Bernard I have added my code. Jun 11, 2018 at 2:39
• Why do you use tabularx if you do specify each column width?
– NBur
Jun 11, 2018 at 6:53

Your question is a bit unclear. You should provide a complete minimal working example (MWE), because, even with \footnotesize your table is larger than an ordinary article text width.

If you want all your cell vertically and horizontally aligned, you could use m{...} column type with a >{\centering\arraybackslash} before it. I created a new column type M for convenience.

Don't use center environment inside a figure one, use \centering instead, see here: Should I use center or centering for figures and tables?

tabularx is useless if you don't have an X column in your table definition.

Just to help to improve your question, rather than really answer, I tried to correct your code and I suggest an alternative one with booktabs and threeparttable.

\documentclass{article}
\usepackage{array}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
\renewcommand{\arraystretch}{1.2}
\usepackage{booktabs}
\usepackage{threeparttable}
\usepackage{graphicx}
\usepackage{caption}

\begin{document}
\begin{table*}[ht]
\centering\footnotesize
\caption{Some table\label{sometable}}
\begin{tabular}{M{2.1cm}|M{1.4cm}|M{1.4cm}|M{1.4cm}|M{1.4cm}|M{1.4cm}}
\hline
\textbf{Image}  &  \textbf{Detected}  &  \textbf{Indirectly Related}  & \textbf{Sentences Generated by Model with Indirectly Related}  & \textbf{Sentences Generated by Model without Indirectly Related}  & \textbf{Standard Model}  \\
\includegraphics[width=0.8in]{example-image}  &  Some text  &  Some text  &  Some text  &  Some text  \\
\includegraphics[width=0.5in]{example-image-a}  & Another image  &  with another dimension  &  Some text  &  Some text  \\
\includegraphics[width=0.7in]{example-image-b}  & Another image again &  with another dimension  &  Some text  &  Some text  \\
\hline
\end{tabular}
\end{table*}

\begin{table*}[ht]
\centering\footnotesize
\caption{Some table with attempt at improvement\label{mytable}}
\begin{threeparttable}
\begin{tabular}{M{2.1cm}M{1.4cm}M{1.4cm}M{1.4cm}M{1.4cm}M{1.4cm}}
\toprule
\textbf{Image}  &  \textbf{Detected}  &  \textbf{Indirectly Related}  & \textbf{With IR}\tnote{a}  & \textbf{Without IR}\tnote{b}  & \textbf{Standard Model}  \\
\midrule
\includegraphics[width=0.8in]{example-image}  &  Some text  &  Some text  &  Some text  &  Some text  \\
\includegraphics[width=0.5in]{example-image-a}  & Another image  &  with another dimension  &  Some text  &  Some text  \\
\includegraphics[width=0.7in]{example-image-b}  & Another image again &  with another dimension  &  Some text  &  Some text  \\
\bottomrule
\end{tabular}
\begin{tablenotes}
\item[a] Sentences Generated by Model \emph{with} Indirectly Related.
\item[b] Sentences Generated by Model \emph{without} Indirectly Related.
\end{tablenotes}
\end{threeparttable}
\end{table*}
\end{document}


With your code, with some corrections:

My suggestion:

• Thank you so much! I have used 'c' for the first column and use \raisebox to manually adjust it vertically. Jun 11, 2018 at 20:49

A solution with \ tabularx and X columns of different widths:

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{array}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{caption, makecell}
\usepackage{ragged2e}

\begin{document}

\begin{table*}[ht]
\centering
\renewcommand{\tabularxcolumn}[1]{>{\footnotesize\RaggedRight\arraybackslash}m{#1}}
\setlength{\tabcolsep}{3pt}
\caption{Some table}\label{mytable}
\begin{tabularx}{\linewidth}{c*{2}{>{\hsize=0.625\hsize}X}*{3}{>{\hsize=1.25\hsize}X}}
\toprule
& & & \multicolumn{2}{c}{\thead{Sentences Generated by Model}} \\
\cmidrule{4-5}
\midrule
\adjincludegraphics[width=0.8in, valign=c]{example-image} & Some text & Some text & Some text & Some text \\

• You can, but valign=c and redefining the X column type is simpler – unless you want the text columns to be top-aligned. Jun 11, 2018 at 21:04