使用多列和 tabularx 包装列文本

使用多列和 tabularx 包装列文本

我有两个表格,我正在将它们排版如下。

\documentclass[a4paper,11pt]{article}
\usepackage{tabularx}
\usepackage[left=2cm, right=2cm, bmargin=1.5cm]{geometry}

\begin{document}
\begin{tabularx}{\textwidth}{| X | X | l | l | l | l | l | l |}
\hline
   & Cost Category & Year 1 & Year 2 & Year 3 & Year 4 & Year 5 & Total (Y1-5) \\
\hline
\noalign{\vspace{0.5cm}}
\hline
\multicolumn{7}{|l|}{Some text that goes on and on and on and on and on and on and on and on and on and on} & $100\%$\\
\hline
\end{tabularx}
\end{document}

如何让底部表格中的文本自动换行,而不是将列推到页面的右边缘?基本上,我希望它采用顶部表格已为前 7 列设置的大小。

答案1

前七列的宽度\textwidth减去最后一列和\tabcolsep/ \fboxrule: \textwidth-4\tabcolsep-\widthof{Total (Y1-5)}-2\fboxrule}。没有tabularx规则不是考虑到。

\documentclass[a4paper,11pt]{article}
\usepackage{tabularx,calc}
\usepackage[left=2cm, right=2cm, bmargin=1.5cm]{geometry}

\begin{document}
\begin{tabularx}{\textwidth}{| X | X | l | l | l | l | l | l |}
\hline
   & Cost Category & Year 1 & Year 2 & Year 3 & Year 4 & Year 5 & Total (Y1-5) \\
\hline
\noalign{\vspace{0.5cm}}
\hline
\multicolumn{7}{|p{\textwidth-4\tabcolsep-\widthof{Total (Y1-5)}-2\fboxrule}|}{Some text that goes on and on and on and on and on and on and on and on and on and on} & $100\%$\\
\hline
\end{tabularx}
\end{document}

类似的事情\multicolumn{7}{|X|}{是可能的,但没有意义。它使用所有 7 个单元格的宽度,但不用于文本本身。

相关内容