Tabularx 和多列(包含一个 X 单元格和一个常规单元格)

Tabularx 和多列(包含一个 X 单元格和一个常规单元格)

tabularx关于和的问题有很多,\multicolumn但我还没有找到这个问题的答案。

使用X列有两个原因:首先是要有等宽的段落列,格式如下|X|X|X|;但还有另一个原因:要有一个全文本宽度的表格,其中包含一个段落列和其他宽度可变的非段落列,而不需要对段落列的确切宽度进行繁琐的计算(这在类型列中是必要的p)。这将是一个格式如下|l|l|X|l|

这是我正在使用的,但在某些时候我需要将此X列与相邻列合并,相邻列是l宽度未知的常规列:

\documentclass{article}

\usepackage{tabularx}

\begin{document}

\noindent\begin{tabularx}{\textwidth}{|l|l|X|}
A word&Another one&This sentence is set in the remaining space of the first line\\
Something&\multicolumn{2}{X|}{And here I would like to occupy the space of columns 2 and 3 again for a long sentence that has to be broken into a paragraph}
\end{tabularx}

\end{document}

在此处输入图片描述

在屏幕截图中,我在想要占据的空间周围放置了一个红框。

阅读其他答案时,我注意到我可以使用以下格式的代码来更改多列的宽度:>{\hsize=1.33\hsize}X。但这意味着对于每个表和列组合,我都必须摆弄才能找到因子的确切值\hsize

我需要的是一种自动获取该值的方法,而不必运行文档十几次才能获得正确的\hsize系数值。即,段落列的宽度等于该X列的宽度加上相邻列的宽度l(加上列间宽度)。

有办法得到它吗?

(请注意,与 Stack Exchange 上的其他问题相反,这不是X单元格算术,其中多列替换n X列,因此它的宽度只是n乘以列宽X。此处列宽l是可变的,与列宽没有X任何关系。

答案1

根据 David 的建议,这里有一个解决问题的方法。唯一的折衷方案是您需要在每个新定义的\multicolumn命令中提供列中最宽单元格的内容。但这比摆弄系数值(最终无法正确计算)要容易得多……。

这是我的代码:

\newlength{\yhwidth}
\def\yhmulticolumn#1#2#3#4{\multicolumn{#2}{#3}{\settowidth{\yhwidth}{#1}\hsize=\dimexpr\hsize+\yhwidth+2\tabcolsep+\arrayrulewidth\relax #4}}

\noindent\begin{tabularx}{\textwidth}{|l|l|X|}
A word&Another one&This sentence is set in the remaining space of the first line\\
Something&\yhmulticolumn{Another one}{2}{X|}{And here I would like to occupy the space of columns 2 and 3 again for a long sentence that has to be broken into a paragraph}
\end{tabularx}

结果:

在此处输入图片描述

谢谢大卫!

(现在如果有人有一个不需要提供最大单元格内容的解决方案,请不要犹豫地分享它!毕竟,longtable进行这样的最大单元格计算可以获得跨多页的表格,所以应该是可以做到的……)

相关内容