我正在尝试创建一个有 2 列的表格(1 列用于文本,1 列用于带标题的图像)。现在,我得到了一个无政府主义的表格,其中第一列没问题,但第二列中的图像未居中,它们超出了框线(参见图片)。
以下是代码:
\begin{table}[h]
\centering
\begin{tabularx}{1\textwidth}{>{\centering\arraybackslash}m{3cm}|>{\centering\arraybackslash}m{3.7cm}}
Monochromatique & \includegraphics[scale=0.25]{Image/spectre_mono.png}\\
\hline
Spectre discret & \includegraphics[scale=0.55]{Image/Spectre_discret.jpg}\\[2cm]
\hline
Spectre continu & \includegraphics[scale=0.35]{Image/Spectre_lampe_halogene.jpg}\\
\hline
Spectre d'absorption & \includegraphics[scale=0.25]{Image/Abs.jpg}\\
\hline
\end{tabularx} \normalfont
\end{table}
任何想法 ?
我使用此模板创建表格:
https://stackoverflow.com/questions/25163873/horizontal-and-vertical-alignment-in-tabular-cell
答案1
我建议也使用,adjustbox
但缩放图像以尊重 X 列的宽度,仅使用width=\linewidth
。 这种方式更有吸引力,因为所有图像都有完全相同的宽度,并且避免将图像居中(请注意,我也使用height
,但仅用于模拟宽光谱图像的尺寸。 您不应该将其用于真实图像。)。
还要注意,的相对长度\linewidth
是列的宽度,但不包括的水平填充,\tabcolsep
而默认情况下没有类似的垂直填充。此外,在这种情况下,我猜你有一个左上角和下角的图像边距,但没有右边距。第二个表用解决了这个问题\fbox{}
:
\documentclass{article}
\usepackage{graphicx,tabularx,booktabs}
\usepackage[export]{adjustbox}
\begin{document}
\begin{table}
\tabcolsep0pt
\begin{tabularx}{\textwidth}{m{3cm}|X}
Monochromatique & \includegraphics[valign=c,width=\linewidth,height=1cm]{example-image}\\\hline
Spectre discret & \includegraphics[valign=c,width=\linewidth,,height=2cm]{example-image}\\\hline
Spectre continue & \includegraphics[valign=c,width=\linewidth]{example-image-1x1}\\\hline
Spectre d'absorption & \includegraphics[valign=c,width=\linewidth]{example-image-16x9}\\\hline
\end{tabularx}
\end{table}
\begin{table}
\tabcolsep0pt\fboxrule0pt\fboxsep1em
\begin{tabularx}{\textwidth}{m{3cm}|X}
Monochromatique &\fbox{\includegraphics[valign=c,width=\dimexpr\linewidth-1em,height=1cm]{example-image}}\\\hline
Spectre discret & \fbox{\includegraphics[valign=c,width=\dimexpr\linewidth-1em,height=2cm]{example-image}}\\\hline
Spectre continue & \fbox{\includegraphics[valign=c,width=\dimexpr\linewidth-1em]{example-image-1x1}}\\\hline
Spectre d'absorption & \fbox{\includegraphics[valign=c,width=\dimexpr\linewidth-1em]{example-image-16x9}}\\\hline
\end{tabularx}
\end{table}
\end{document}
答案2
您可能对以下解决方案更满意,该解决方案结合使用了 包valign=c
中的。我还用常规替换了:adjustbox
cellspace
tabularx
tabular
\documentclass{article}
\usepackage[demo]{graphicx} % remove demo option in actual document
\usepackage{array}
\usepackage{cellspace} % For added shite space above and below image
\setlength\cellspacetoplimit{\tabcolsep}
\setlength\cellspacebottomlimit{\tabcolsep}
\usepackage[export]{adjustbox} % For valign option
\begin{document}
\begin{table}[h]
\centering
\begin{tabular}{>{\centering\arraybackslash}m{3cm}|Sl}
Monochromatique & \includegraphics[scale=0.25, valign=c]{Image/spectre_mono.png}\\
\hline
Spectre discret & \includegraphics[scale=0.55, valign=c]{Image/Spectre_discret.jpg}\\
\hline
Spectre continu & \includegraphics[scale=0.35, valign=c]{Image/Spectre_lampe_halogene.jpg}\\
\hline
Spectre d'absorption & \includegraphics[scale=0.25, valign=c]{Image/Abs.jpg}\\
\hline
\end{tabular}
\end{table}
\end{document}