0

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*}
4
  • 1
    Welcome to TeX SX! Could you post a minimal example code of what you've tried. How are defined the cells in text columns?
    – Bernard
    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

2 Answers 2

1

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:

enter image description here

My suggestion:

enter image description here

1
  • 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
0

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{graphicx, adjustbox}
\usepackage{caption, makecell}
\usepackage{ragged2e}

\begin{document}

\begin{table*}[ht]
\centering
\renewcommand{\tabularxcolumn}[1]{>{\footnotesize\RaggedRight\arraybackslash}m{#1}}
\renewcommand{\theadfont}{\footnotesize\bfseries}
\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}
\thead{Image} & \thead{Detected} & \thead{Indirectly\\ Related} & \thead{with Indirectly\\Related} & \thead{without Indirectly\\Related}& \thead{Standard Model} \\
\midrule
\adjincludegraphics[width=0.8in, valign=c]{example-image} & Some text & Some text & Some text & Some text \\
\addlinespace
\adjincludegraphics[width=0.5in, valign=c]{example-image-a} & Another image & with another dimension & Some text & Some text \\
\addlinespace
\adjincludegraphics[width=0.7in, valign=c]{example-image-b} & Another image again & with another dimension & Some text & Some text \\
\bottomrule
\end{tabularx}
\end{table*}

\end{document} 

enter image description here

3
  • 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:50
  • You can, but valign=c and redefining the X column type is simpler – unless you want the text columns to be top-aligned.
    – Bernard
    Jun 11, 2018 at 21:04
  • How to write such code then? Just only use X column and valign=c according to you. Jun 11, 2018 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.