![表格环境中的垂直对齐问题](https://linux22.com/image/295365/%E8%A1%A8%E6%A0%BC%E7%8E%AF%E5%A2%83%E4%B8%AD%E7%9A%84%E5%9E%82%E7%9B%B4%E5%AF%B9%E9%BD%90%E9%97%AE%E9%A2%98.png)
我在垂直对齐表格元素时遇到了一些问题。文本或图像上方或下方似乎有太多空间。这与环境有关吗fbox
?我希望图像和文本在每个表格单元格中垂直居中。
\documentclass[a4paper,11pt]{article}
\usepackage[english]{babel}
\usepackage{graphicx}
\usepackage[top=1in,bottom=1in,left=1in,right=1in]{geometry}
\usepackage{array}
\newcommand{\mybox}[1]{
\begin{center}
\fbox{
\parbox{0.8\linewidth}{
\begin{center}
\begin{tabular}{ c m{4.5in} }
\includegraphics[height=0.3in]{./myfig} & {#1}
\end{tabular}
\end{center}
}
}
\end{center}
}
\begin{document}
\mybox{hello}
\mybox{hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello}
\end{document}
编辑:按照 Gonzalo Medina 的建议,我得到: 并且和
之间没有区别。
center
centering
答案1
您可以m{length}
对第一列使用某种列类型:
\documentclass[a4paper,11pt]{article}
\usepackage[english]{babel}
\usepackage{graphicx}
\usepackage[top=1in,bottom=1in,left=1in,right=1in]{geometry}
\usepackage{array}
\newcommand{\mybox}[1]{
\begin{center}
\fbox{%
\parbox{0.8\linewidth}{%
\begin{center}
\begin{tabular}{m{0.3in}m{4.5in} }
\includegraphics[height=0.3in]{example-image-a} & #1
\end{tabular}
\end{center}
}
}
\end{center}%
}
\begin{document}
\mybox{hello}
\mybox{hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello}
\end{document}
我不确定行尾的一些空格是否是故意的;我将它们注释掉了。还考虑到这center
会增加一些垂直间距,因此也许您可以考虑使用它\centering
。
作为替代方案,我想建议tcolorbox
包装以轻松生产您的盒子;您甚至可以进一步定制它:
\documentclass[a4paper,11pt]{article}
\usepackage[english]{babel}
\usepackage{graphicx}
\usepackage[top=1in,bottom=1in,left=1in,right=1in]{geometry}
\usepackage[many]{tcolorbox}
\newcommand\mybox[1]{%
\begin{tcolorbox}[
sidebyside,
lefthand width=.3in,
colback=white,
outer arc=0pt,
arc=0pt.
colframe=black,
boxrule=0.4pt,
segmentation engine=empty
]
\includegraphics[width=.3in]{example-image-a}
\tcblower
#1
\end{tcolorbox}%
}
\begin{document}
\mybox{hello}
\mybox{hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello}
\end{document}
答案2
您可以使用包中valign
的宏adjustbox
\includegraphics[valign=c,height=0.3in]{example-image-a}
代码:
\documentclass[a4paper,11pt]{article}
\usepackage[english]{babel}
\usepackage[export]{adjustbox} %% export is needed
\usepackage[top=1in,bottom=1in,left=1in,right=1in]{geometry}
\usepackage{array}
\newcommand{\mybox}[1]{%
\begin{center}
\fbox{%
\parbox{0.8\linewidth}{%
\begin{center}
\begin{tabular}{ c m{0.75\linewidth} }
\includegraphics[valign=c,height=0.3in]{example-image-a} & #1
\end{tabular}
\end{center}
}%
}%
\end{center}
}
\begin{document}
\mybox{hello}
\mybox{hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello}
\end{document}