你好,我有一段代码,在表格中如下所示:
\usepackage{mwe}
\begin{tabular}
(1) & \includegraphics[width=0.2\textwidth]{images/some_image.png} & He thinks:
$``$Maybe, the food is in the yellow pot.$"$
\newline
But, the yellow pot is empty.\\
\end{tabular}
我使用 \newline 将句子移至下一行,但不起作用。我尝试过 p{some cm},但问题是当我添加它时,出现了两条我不想要的水平线,并且文本移动到了图像下方。
我希望以这样一种方式保留它:左边有一个图像,右边有相应的文本,但如果文本很长,则可以将其移动到类似于图像的新行:左侧为图片,右侧为对应文字
答案1
tabularx
为了确保表格与文本宽度一样宽,valign=c
并使adjustbox
元素垂直居中,请执行以下操作:
\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{tabularx}
\renewcommand{\tabularxcolumn}[1]{m{#1}}
\usepackage[export]{adjustbox}
\begin{document}
\noindent
\begin{tabularx}{\textwidth}{llX}
(1) & \includegraphics[width=0.2\textwidth, valign=c]{images/some_image.png} & He thinks:
``Maybe, the food is in the yellow pot.'' But, the yellow pot is empty.\\
\end{tabularx}
\end{document}
如果您的表格长度超过一页,您可以使用包xltabular
。为此,只需将上面的 MWE 中的替换tabularx
为即可。xltabular
为了好玩,这里还有另外两个版本,其中每组图像和文本都自动编号。版本 1 利用magicrownumber
了自动表格行号minipages
而版本 2 在环境内部并行使用enumerate
:
\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{tabularx}
\renewcommand{\tabularxcolumn}[1]{m{#1}}
\usepackage[export]{adjustbox}
%%% used in example 1 %%%%%
\usepackage{etoolbox}
\preto\tabular{\setcounter{magicrownumbers}{0}}
\newcounter{magicrownumbers}
\newcommand\rownumber{\stepcounter{magicrownumbers}\arabic{magicrownumbers}}
%%% used in example 2 %%%%%
\usepackage{enumitem}
\begin{document}
\noindent
\begin{tabularx}{\textwidth}{@{\makebox[3em][r]{(\rownumber)\quad}} lX}
\includegraphics[width=0.2\textwidth, valign=c]{images/some_image.png} & He thinks:
``Maybe, the food is in the yellow pot.'' But, the yellow pot is empty.\\
\end{tabularx}
\begin{enumerate}[label={(\arabic*)}]
\item \begin{minipage}{0.2\textwidth}
\includegraphics[width=\textwidth, valign=c]{images/some_image.png}
\end{minipage}
\hfill
\begin{minipage}{0.7\textwidth}
He thinks: ``Maybe, the food is in the yellow pot.'' But, the yellow pot is empty.
\end{minipage}
\end{enumerate}
\end{document}
答案2
出于简单起见,我删除了开头的序列号 1——图像没有问题,可以通过包装进行调整\usepackage[export]{adjustbox}
,也可以通过添加valign=c
@leandriis 的回答进行调整
可以通过取最长的长度并将其设置为列宽来设置右侧的长度,以便任何较长的内容都会自动流到第二行
所以
\settowidth\colwidth{He thinks: $``$Maybe, the food is in the yellow pot.$"$}
成为第二列的列宽
取自 -
https://tex.stackexchange.com/a/512217/197451
\newlength\colwidth
\settowidth\colwidth{He thinks: $``$Maybe, the food is in the yellow pot.$"$}
\begin{tabular}{lm{\colwidth}}
\includegraphics[width=0.2\textwidth, valign=c]{example-image} &
He thinks: $``$Maybe, the food is in the yellow pot.$"$
But, the yellow pot is empty.\\
\end{tabular}