这是我在这里的第一篇帖子,所以如果我做错了,请谅解。我想在 LaTeX 中创建一个表格,其中图标应显示在第一列(居中),文本(垂直居中)显示在第二行。
我想将整个内容插入为\newcommand
。我的问题如下图所示。如果只有一行文本,所有内容看起来都居中。但如果有多行文本,图标将停留在其位置。图标水平居中,但不垂直居中。有人能帮帮我吗?
此致
迈克尔
\documentclass{article}
\usepackage[]{graphicx}
\newcommand{\icon}[2][1cm]{\begin{minipage}{#1}\includegraphics[width=#1]{#2}\end{minipage}}
\begin{document}
\begin{tabular}{|c|p{13cm}|}\hline
Symbol & Description \\\hline
& \\ [-0.5em] \icon{icon_settings.pdf} & Text in only one line. \\ [-0.5em] & \\\hline
& \\ [-0.5em] \icon{icon_administrator.pdf} & Here is a longer text that is displayed over more than one line. The symbol is not vertically centered, but has the same distance to the top as the icon in the first line. Here is a longer text that is displayed over more than one line. The symbol is not vertically centered, but has the same distance to the top as the icon in the first line. \\ [-0.5em] & \\\hline
\end{tabular}
\end{document}
答案1
我会做以下事情:
- 用于
tabularx
表格环境 - 对于第二列使用
\renewcommand\tabularxcolumn[1]{m{#1}}
,垂直居中单元格内容 \adjustimage
使用来自包中的宏将图像包含在表格中{adjustbox}
,将图像基线移动到其垂直中心- 定义
\adjustboxset
图像大小、垂直位置及其边距: 使用包
cellspace
在带有文本的单元格上方和下方添加一些垂直空间\documentclass{article} \usepackage{adjustbox}% [demo] \usepackage{cellspace, tabularx} \setlength\cellspacetoplimit{3pt} \setlength\cellspacebottomlimit{3pt} \addparagraphcolumntypes{X} %---------------- show page layout. don't use in a real document! \usepackage{showframe} \renewcommand\ShowFrameLinethickness{0.15pt} \renewcommand*\ShowFrameColor{\color{red}} %---------------------------------------------------------------% \begin{document} \begin{table} \renewcommand\tabularxcolumn[1]{m{#1}} \adjustboxset{width=.1\textwidth,valign=c, margin=0pt 3pt 0pt 3pt} \begin{tabularx}{\linewidth}{|c|X|} \hline Symbol & Description \\ \hline \adjustimage{}{example-image-duck} & Text in only one line. \\ \hline \adjustimage{}{example-image-duck} & Here is a longer text that is displayed over more than one line. The symbol is not vertically centered, but has the same distance to the top as the icon in the first line. Here is a longer text that is displayed over more than one line. The symbol is not vertically centered, but has the same distance to the top as the icon in the first line. \\ \hline \end{tabularx} \end{table} \end{document}
(红线表示文本边框=
如果您想更改特定图像的大小,则包括该图像的宽度,例如如下:
\adjustimage{width=2cm}{example-image-duck}
编辑:
您也可以坚持您的解决方案。在序言中只需添加array
包并使用以下类型代替p{...}
列类型m{...}
:
\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{array}
\newcommand{\icon}[2][1cm]{\begin{minipage}{#1}\includegraphics[width=#1]{#2}\end{minipage}}
\begin{document}
\begin{tabular}{|c|m{13cm}|}\hline
Symbol & Description \\\hline
& \\ [-0.5em] \icon{icon_settings.pdf} & Text in only one line. \\ [-0.5em] & \\\hline
& \\ [-0.5em] \icon{icon_administrator.pdf} & Here is a longer text that is displayed over more than one line. The symbol is not vertically centered, but has the same distance to the top as the icon in the first line. Here is a longer text that is displayed over more than one line. The symbol is not vertically centered, but has the same distance to the top as the icon in the first line. \\ [-0.5em] & \\\hline
\end{tabular}
\end{document}