我试图让lipsum
文本填充垂直标尺右侧的两个单元格,但不超过table
。
我的代码现在是:
\documentclass[a4paper,11pt]{article}
\usepackage{lipsum}
\usepackage{tabularx}
\usepackage{calc}
\newlength{\mylength}
\begin{document}
\begin{tabularx}{\textwidth}{l | X X}
\hline
Short \setlength{\mylength}{\hsize} & \textbf{This text is a little bit longer} & And here is some more and more \\
& \multicolumn{2}{p{\textwidth-2\tabcolsep-\mylength}}{\lipsum[1]} \\
Short & \textbf{This text is a little bit longer} & And here is some more and more \\
& \multicolumn{2}{p{\textwidth-2\tabcolsep-\mylength}}{\lipsum[2]} \\
\end{tabularx}
\end{document}
结果是:
答案1
这里有两种解决该问题的方法。
您的用法是正确的,但您没有正确计算 s 的数量
\tabcolsep
。您的表中有 6 个源自序言tabularx
。{l | X X}
让我们\tabcolsep
使用表示t
,那么您将有{tlt|tXttXt}
。此外, 的计算\hsize
是针对整个“tlt
-column”,因此它已经包含 2\tabcolsep
s。因此,您在计算中需要删除 8\tabcolsep
s:\noindent \begin{tabularx}{\textwidth}{l | X X} \hline Short\setlength{\mylength}{\hsize} & \textbf{This text is a little bit longer} & And here is some more and more \\ & \multicolumn{2}{p{\dimexpr\textwidth-8\tabcolsep-\mylength}}{\lipsum*[1]} \\ Short & \textbf{This text is a little bit longer} & And here is some more and more \\ & \multicolumn{2}{p{\dimexpr\textwidth-8\tabcolsep-\mylength}}{\lipsum*[2]} \\ \end{tabularx}
我已经不再使用
calc
包裹,因此可见。另请注意,我删除了和\dimexpr
之间的空格,因为这实际上会影响的大小。Short
\setlength
\hsize
捕获柱内容,将其存储在盒子中,并在过程中进行测量
tabularx
的测量阶段(...不是那么必要,但仍然如此):\newcommand{\capturemaxwidth}[1]{% % https://tex.stackexchange.com/q/227142/5764 \ifdim\hfuzz=\maxdimen\relax% trial run \setbox9=\hbox{#1}% Store content in box \ifdim\mylength>\wd9\relax% Measure box width... \setlength{\mylength}{\wd9}% ...and possibly store width \fi \fi #1% final run } ... \noindent \begin{tabularx}{\textwidth}{L | X X} \hline Short & \textbf{This text is a little bit longer} & And here is some more and more \\ & \multicolumn{2}{p{\dimexpr\textwidth-8\tabcolsep-\mylength}}{\lipsum*[1]} \\ Short & \textbf{This text is a little bit longer} & And here is some more and more \\ & \multicolumn{2}{p{\dimexpr\textwidth-8\tabcolsep-\mylength}}{\lipsum*[2]} \end{tabularx}
第二种选择会从代码中删除一些构造,将其放在序言中。这样,代码可能读起来会更好一些。你自己选择吧。这两种方法的输出都类似于:
\documentclass{article}
\usepackage{lipsum,tabularx}
\usepackage{collcell}
\newcolumntype{L}{>{\collectcell\capturemaxwidth}l<{\endcollectcell}}
\newlength{\mylength}
\newcommand{\capturemaxwidth}[1]{%
% https://tex.stackexchange.com/q/227142/5764
\ifdim\hfuzz=\maxdimen
\setbox9=\hbox{#1}% trial run
\ifdim\mylength>\wd9
\setlength{\mylength}{\wd9}%
\fi
\fi
#1% final run
}
\begin{document}
\sloppypar% Just for this example, due to lipsum
\noindent
\begin{tabularx}{\textwidth}{l | X X}
\hline
Short\setlength{\mylength}{\hsize} & \textbf{This text is a little bit longer} & And here is some more and more \\
& \multicolumn{2}{p{\dimexpr\textwidth-8\tabcolsep-\mylength}}{\lipsum*[1]} \\
Short & \textbf{This text is a little bit longer} & And here is some more and more \\
& \multicolumn{2}{p{\dimexpr\textwidth-8\tabcolsep-\mylength}}{\lipsum*[2]} \\
\end{tabularx}
\noindent
\begin{tabularx}{\textwidth}{L | X X}
\hline
Short & \textbf{This text is a little bit longer} & And here is some more and more \\
& \multicolumn{2}{p{\dimexpr\textwidth-8\tabcolsep-\mylength}}{\lipsum*[1]} \\
Short & \textbf{This text is a little bit longer} & And here is some more and more \\
& \multicolumn{2}{p{\dimexpr\textwidth-8\tabcolsep-\mylength}}{\lipsum*[2]}
\end{tabularx}
\end{document}
答案2
设置\mylength
为宽度Short
(或任何你使用的值)
\newlength{\mylength}
\settowidth{\mylength}{Short}
进而
p{\textwidth-4\tabcolsep-\arrayrulewidth-\mylength\relax}
应该这么做。
\documentclass[a4paper,11pt]{article}
\usepackage{lipsum}
\usepackage{tabularx}
\usepackage{calc}
\newlength{\mylength}
\settowidth{\mylength}{Short}
\begin{document}
\noindent
\begin{tabularx}{\textwidth}{l | X X}
\hline
Short & \textbf{This text is a little bit longer} & And here is some more and more \\
& \multicolumn{2}{p{\textwidth-4\tabcolsep-\arrayrulewidth-\mylength\relax}}{\lipsum[1]} \\
Short & \textbf{This text is a little bit longer} & And here is some more and more \\
& \multicolumn{2}{p{\textwidth-4\tabcolsep-\arrayrulewidth-\mylength\relax}}{\lipsum[2]} \\
\end{tabularx}
\end{document}
没有tabularx
(据 David 说,为了节省一些内存)你可以这样做(因为我们无论如何都会进行手动计算):
\documentclass[a4paper,11pt]{article}
\usepackage{lipsum}
\usepackage{array}
\usepackage{calc}
\newlength{\mylength}
\settowidth{\mylength}{Short}
\begin{document}
\noindent
\begin{tabular}{l | *{2}{p{(\dimexpr\textwidth-\mylength-6\tabcolsep-\arrayrulewidth\relax)/2}}}
\hline
Short & \textbf{This text is a little bit longer} & And here is some more and more \\
& \multicolumn{2}{p{\textwidth-4\tabcolsep-\arrayrulewidth-\mylength\relax}}{\lipsum[1]} \\
Short & \textbf{This text is a little bit longer} & And here is some more and more \\
& \multicolumn{2}{p{\textwidth-4\tabcolsep-\arrayrulewidth-\mylength\relax}}{\lipsum[2]} \\
\end{tabular}
\end{document}
bad boxes
你可以通过使用\raggeright
摆脱这两个
\begin{tabular}{l | *{2}{>{\raggedright\arraybackslash}p{(\dimexpr\textwidth-\mylength-6\tabcolsep-\arrayrulewidth\relax)/2}}}
\hline
答案3
您只需使用X
它来为扩展列提供基本单位宽度,但您需要将其加倍并允许2\tabcolsep
在中间。
\documentclass[a4paper,11pt]{article}
\usepackage{lipsum}
\usepackage{tabularx}
\usepackage{calc}
\begin{document}
\begin{tabularx}{\textwidth}{l | X X}
\hline
Short & \textbf{This text is a little bit longer} & And here is some more and more \\
& \multicolumn{2}{>{\setlength\hsize{2\hsize+2\tabcolsep}}X}{\lipsum[1]} \\
Short & \textbf{This text is a little bit longer} & And here is some more and more \\
& \multicolumn{2}{>{\setlength\hsize{2\hsize+2\tabcolsep}}X}{\lipsum[2]} \\
\end{tabularx}
\end{document}