我有一张这样的桌子:
\begin{tabular}{|l|l|}
\hline
\multicolumn{2}{|l|}{\textbf{Item Name}} \\
\hline
Item Type & Item Subtype \\
\hline
\multicolumn{2}{|l|}{Item effect (long text).} \\
\hline
\multicolumn{2}{|l|}{Item description (long text).} \\
\hline
\end{tabular}
结果是:
|-------------------------------|
| Item Name |
|-------------------------------|
| Item Type | Item Subtype |
|-------------------------------|
| Item effect (long text). |
|-------------------------------|
| Item description (long text). |
|-------------------------------|
表格的宽度始终与最长的行一样宽。就我而言,底部两行通常很长,有时会包含多个句子。但由于单元格不换行,而行无法改变其高度来适应换行,因此表格会变得太宽,以至于超出页面的右侧。如何让底部两行自动换行并扩展行高以匹配行高,而不改变顶部两行的行高?
答案1
方法如下tabularx
:
\documentclass{article}
\usepackage{tabularx, lipsum}
\usepackage[showframe]{geometry}
\setlength{\extrarowheight}{2pt}
\begin{document}
\noindent
\begin{tabularx}{\linewidth}{|l|X|}
\hline
\multicolumn{2}{|l|}{\textbf{Item Name}} \\
\hline
Item Type & Item Subtype \\
\hline
\multicolumn{2}{|p{\dimexpr\linewidth-2\tabcolsep -2\arrayrulewidth}|}{\lipsum[11]} \\
\hline
\multicolumn{2}{|p{\dimexpr\linewidth-2\tabcolsep-2\arrayrulewidth}|}{Item description (long text).} \\
\hline
\end{tabularx}
\end{document}