我想将文本和图像在长表中垂直和水平居中。但不幸的是,我似乎无法让它工作。下面是一个例子:
\documentclass[11pt,a4paper,english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{array,longtable,ragged2e}
\usepackage{graphicx}
\begin{document}
\begin{longtable}{| l | c | c |}
\hline
& Here is a Text & Here is a Text \\\hline
Here is a Text & \includegraphics[height=2cm]{img/image.png} & \includegraphics[height=2cm]{img/image.png} \\\hline
Here is a Text & \raisebox{-\height}{\includegraphics[height=2cm]{img/image.png}} & \raisebox{-\height}{\includegraphics[height=2cm]{img/image.png}} \\\hline
\caption{My Caption}
\label{tab:FirstTab}
\end{longtable}
\mbox{}\\
\end{document}
我也尝试了下面的更改,但没有帮助:
Here is a Text & \raisebox{-\height}{\includegraphics[height=2cm]{img/image.png}} & \raisebox{-\height}{\includegraphics[height=2cm]{img/image.png}} \\\hline
答案1
一种方法是使用包valign
中的宏adjusbox
:
\documentclass[11pt,a4paper,english]{article}
\usepackage[T1]{fontenc}
\usepackage{ragged2e}
\usepackage{babel}
\usepackage{array,longtable,makecell}
\usepackage[demo, % in real document delete 'demo'
export]{adjustbox} % it load graphicx too
\begin{document}
\begingroup
\setcellgapes{3pt}
\makegapedcells % add vertical space/above/below cell's content
\begin{longtable}{| l | c | c |}
\hline
& Here is a Text & Here is a Text \\\hline
Here is a Text & \includegraphics[height=2cm,valign=c]{img/image.png} & \includegraphics[height=2cm,valign=c]{img/image.png} \\\hline
Here is a Text & \includegraphics[height=2cm,valign=c]{img/image.png} & \includegraphics[height=2cm,valign=c]{img/image.png} \\\hline
\caption{My Caption}
\label{tab:FirstTab}
\end{longtable}
\endgroup
\end{document}
在使用时adjustbox
,我们可以includegraphics
用\adjustimage
以下设置来替换添加确定单元格中图像的位置\adjustboxset
:
\documentclass[11pt,a4paper,english]{article}
\usepackage[T1]{fontenc}
\usepackage{ragged2e}
\usepackage{babel}
\usepackage{array,longtable}
\usepackage[demo, % in real document delete 'demo'
export]{adjustbox} % it load `graphicx` too
\begin{document}
\begingroup
\adjustboxset{height=20mm,valign=c, margin=0pt 3pt 0pt 3pt} % <---
\begin{longtable}{| l | c | c |}
\hline
& Here is a Text & Here is a Text \\
\hline
Here is a Text & \adjustimage{}{img/image} & \adjustimage{}{img/image} \\
\hline
Here is a Text & \adjustimage{}{img/image} & \adjustimage{}{img/image} \\
\hline
\caption{My Caption}
\label{tab:FirstTab}
\end{longtable}
\endgroup
\end{document}
其结果与第一个提出的解决方案相同。笔记,上面的 MWE 省略了图形文件扩展名png
。如果存在具有不同扩展名的相同图像文件,并且您只想使用其中选定的一个,则它们是必需的。