答案1
tabularx
最适合这种情况。为了满足您提供的要求,应指定单元格 A,l
并且单元格B
应通过为其指定列类型来填充剩余空间(但不是问题中所说的固定宽度)X
。最后,单元格 C 有点棘手。它应占用等于的总长度cell A + cell B
,但您仍然必须减去两个\tabcolsep
s 加两个s。这可以通过对单元格 C \arrayrulewidth
使用来实现。p{\dimexpr\linewidth-2\tabcolsep-2\arrayrulewidth}
\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{tabularx}
\begin{document}
\begin{tabularx}{\linewidth}{|l|X|}
\hline
cell A & Cell B has a fixed width (e.g. 10 cm), and the text inside will wrap; \\ \hline
\multicolumn{2}{|p{\dimexpr\linewidth-2\tabcolsep-2\arrayrulewidth}|}{Cell C has a width of the sum of A and B, and of course allow text to wrap.} \\ \hline
\end{tabularx}
\end{document}
答案2
根据您的描述,您的表格宽度是可变的,仅取决于单元格 A 中包含的内容。为此,您可以使用以下方法测量单元格 A 的宽度zref
的savepos
模块;具体来说,将页面上的坐标(以点或s 为单位)\zsaveposx{<mark>}
保存为。然后可以在扩展课程中使用它来计算第二行的宽度:x
s
p
sp
<mark>
dim
expr
\documentclass{article}
\usepackage{zref-savepos}
\begin{document}
\begin{tabular}{ | l | p{10cm} | }
\hline
\zsaveposx{CellAl-1}Cell \texttt{A}\zsaveposx{CellAr-1} &
Cell \texttt{B} has a fixed width (\textit{e.g.}, \texttt{10cm}), and the text inside will wrap to the following line; \\
\hline
\multicolumn{2}{| p{\dimexpr10cm+\zposx{CellAr-1}sp-\zposx{CellAl-1}sp+2\tabcolsep+\arrayrulewidth} |}{%
Cell \texttt{C} has a width of the sum of \texttt{A} and \texttt{B}, and of course allow text to wrap to subsequent
lines as needed, regardless of what is presented in \texttt{A}.%
} \\
\hline
\end{tabular}
\begin{tabular}{ | l | p{10cm} | }
\hline
\zsaveposx{CellAl-2}Cell \texttt{A} plus stuff\zsaveposx{CellAr-2} &
Cell \texttt{B} has a fixed width (\textit{e.g.}, \texttt{10cm}), and the text inside will wrap to the following line; \\
\hline
\multicolumn{2}{| p{\dimexpr10cm+\zposx{CellAr-2}sp-\zposx{CellAl-2}sp+2\tabcolsep+\arrayrulewidth} |}{%
Cell \texttt{C} has a width of the sum of \texttt{A} and \texttt{B}, and of course allow text to wrap to subsequent
lines as needed, regardless of what is presented in \texttt{A}.%
} \\
\hline
\end{tabular}
\end{document}
单元格 A 位于一个l
向左对齐的列内,该列会自然扩展以适应内容。单元格 B 固定为 10cm。单元格 C 的宽度可容纳单元格 A 的宽度(\zposx{<right>}sp-\zposx{<left>}
)、在单元格 A 和 B 之间的垂直线周围插入 2 个规则\tabcolsep
、一个规则宽度和 10cm。
由于这使用了类似 a 的\label
系统\ref
,所以如果单元格 A 中的任何内容发生变化,则需要至少编译两次。