我在表格中对齐(顶部对齐)图像和文本时遇到一些问题。
\documentclass[11pt,table,a4paper]{article}
\usepackage{array,ragged2e}
\usepackage{graphicx}
\usepackage[top=2cm, bottom=3cm,right=1cm, left=1cm, headsep=26pt]{geometry}
\usepackage[T1]{fontenc}
\usepackage{fancyhdr}
\usepackage[table]{xcolor}
\usepackage{sidecap}
\usepackage{longtable}
\usepackage{helvet}
\renewcommand{\familydefault}{\sfdefault}
\begin{document} % End of preamble and beginning of text.
\arrayrulecolor{gray}
\begin{center}
\newcolumntype{M}[1]{>{\arraybackslash}p{#1}}
\renewcommand{\arraystretch}{2.0}\begin{longtable}{M{5cm}|M{6cm}|M{6cm}}
\hline
\rowcolor{gray}
\textbf{ Col1} & \textbf{Col2} & \textbf{Col3} \\ \hline
\vspace{0pt} \large{ABC} & \includegraphics[width=0.15\textheight, width=0.15\textwidth]{Europe_regions.png} \hspace{.1cm} \includegraphics[width=0.15\textwidth]{male-female.png} & \vspace{0pt} XYZ.
\\ \hline
\end{longtable}
\end{center}
\end{document} % End of document.
如何使图像和文本顶部对齐?
答案1
valign
提供的选项正是adjustbox
您正在寻找的:
\documentclass[11pt,a4paper]{article}
\usepackage[margin=1cm]{geometry}
\usepackage[demo]{graphicx} % the demo option is just for the example
\usepackage{array}
\usepackage[table]{xcolor}
\usepackage[export]{adjustbox}
\begin{document}
\begin{center}
\arrayrulecolor{gray}
\begin{tabular}{p{5cm}|p{6cm}|p{6cm}}
\hline
\rowcolor{gray}
\textbf{ Col1} & \textbf{Col2} & \textbf{Col3} \\ \hline
\vspace{0pt}\large ABC
& \large
\includegraphics[width=2.5cm,valign=T]{Europe_regions.png}%
\hspace{.1cm}%
\includegraphics[width=2.5cm,valign=T]{male-female.png}
&\vspace{0pt} XYZ.
\\
\hline
\end{tabular}
\end{center}
\end{document}
我已经将输入简化到最低限度(longtable
不需要显示效果,但可以使用;M
列类型没用)。
答案2
\belowbaseline
该包的命令会stackengine
更改内容的基线,包括图像。在本例中,将可选长度参数设置为负数[0pt-\heightof{X}]
会将主要参数(图像)的顶部边缘移到基线上方(在本例中为字母“X”的高度)。回想一下,在其他列中,基线位于“ABC”和“XYZ”的底部边缘。因此,如果我将图片设置为基线下方 0pt,则图像的顶部将与“ABC”和“XYZ”的底部对齐。
请注意,我的上述评论适用于所谓的“短”堆栈,这是的默认堆栈类型stackengine
。“长”堆栈的行为会有所不同(有关详细信息,请参阅包文档)。
\documentclass[11pt,table,a4paper]{article}
\usepackage{array,ragged2e}
\usepackage[demo]{graphicx}
\usepackage[top=2cm, bottom=3cm,right=1cm, left=1cm, headsep=26pt]{geometry}
\usepackage[T1]{fontenc}
\usepackage{fancyhdr}
\usepackage[table]{xcolor}
\usepackage{sidecap}
\usepackage{longtable}
\usepackage{helvet}
\usepackage{stackengine}
\renewcommand{\familydefault}{\sfdefault}
\begin{document} % End of preamble and beginning of text.
\arrayrulecolor{gray}
\begin{center}
\newcolumntype{M}[1]{>{\arraybackslash}p{#1}}
\renewcommand{\arraystretch}{2.0}\begin{longtable}{M{5cm}|M{6cm}|M{6cm}}
\hline
\rowcolor{gray}
\textbf{ Col1} & \textbf{Col2} & \textbf{Col3} \\ \hline
\vspace{0pt} \large{ABC} &
\belowbaseline[0pt-\heightof{X}]{%
\includegraphics[width=0.15\textwidth]{Europe_regions.png}%
}
\hspace{.1cm}%
\belowbaseline[0pt-\heightof{X}]{%
\includegraphics[width=0.15\textwidth]{male-female.png}
} &
\vspace{0pt} XYZ.
\\ \hline
\end{longtable}
\end{center}
\end{document} % End of document.
答案3
或者,您可以使用 cals-tables:
\documentclass[11pt,table,a4paper]{article}
\usepackage{array,ragged2e}
\usepackage[draft]{graphicx}
\usepackage[top=2cm, bottom=3cm,right=1cm, left=1cm, headsep=26pt]{geometry}
\usepackage[T1]{fontenc}
\usepackage{fancyhdr}
\usepackage[table]{xcolor}
\usepackage{sidecap}
\usepackage{cals}
\usepackage{helvet}
\renewcommand{\familydefault}{\sfdefault}
\begin{document}
\begin{calstable}
\makeatletter
\colwidths{{5cm}{65mm}{6cm}}
\brow
\def\cals@bgcolor{gray}\bfseries
\cell{Col1}
\cell{Col2}
\cell{Col3}
\def\cals@bgcolor{}\mdseries
\erow
\brow
\cell{\large{ABC}}
\cell{\includegraphics[width=0.15\textheight, width=0.15\textwidth]{Europe_regions.png} \hspace{.1cm} \includegraphics[width=0.15\textwidth]{male-female.png}}
\cell{XYZ.}
\erow
\end{calstable}
\end{document}