将不同大小的图像垂直和水平地置于表格一列的每个单元格中?

将不同大小的图像垂直和水平地置于表格一列的每个单元格中?

表格由多行文本单元格和一列图像单元格组成。每行中的图像位于一列内。但图像大小不同。希望将每幅图像在其单元格中水平和垂直居中。如何编写这样的代码?

代码如下:

\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*}

答案1

你的问题有点不清楚。你应该提供一个完全的 最小工作示例(MWE),因为,即使\footnotesize您的表格大于普通文章的文本宽度。

如果您希望所有单元格垂直和水平对齐,可以使用前面m{...}带有 的列类型。为了方便起见,我创建了一种新的列类型。>{\centering\arraybackslash}M

不要center在环境内部使用环境figure\centering而是使用,参见此处: 我应该对图形和表格使用 center 还是 centering ?

tabularxX如果你的表定义中没有列,那么它是无用的。

只是为了帮助改善您的问题,而不是真正回答,我试图纠正您的代码,并建议使用booktabs和的替代代码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}

使用您的代码并进行一些更正:

在此处输入图片描述

我的建议:

在此处输入图片描述

答案2

具有不同宽度的列\ tabularx的解决方案:X

\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} 

在此处输入图片描述

相关内容