当某一列的内容太长而无法放在一行中时,如果该列的宽度可变(例如下面示例中的第 2 列),我该如何让 TeX 将内容分成多行?
\offinterlineskip\tabskip=0pt
\halign to.5\hsize{
\vrule#\strut&
\hfil\enskip#\enskip&
\vrule#&
\enskip#\enskip\hfil\tabskip0pt plus1fil&
\vrule#\tabskip0pt
\cr\noalign{\hrule}
&\omit\hfil\enskip\bf ID\enskip\hfil&
&\omit\span\omit\hfil\enskip\bf Description\enskip\hfil\vrule
\cr\noalign{\hrule}
&1&
&A description that should be broken up across several lines&
\cr\noalign{\hrule}
&1~000&
&A description that fits a single line&
\cr\noalign{\hrule}
&1~000~000&
&Another short description&
\cr\noalign{\hrule}}
\end
答案1
在 a 中排版表格\vbox
,获取最后一行并将其拆分以获取第一列的宽度。然后以适当的方式设置第二列的大小。
\def\myhalign#1{%
\setbox0=\vbox{
\tabskip=0pt
\halign{&##\cr#1}
\setbox0=\lastbox
\setbox0=\hbox{\unhbox0
\unskip % tabskip glue
\setbox0=\lastbox % third rule
\unskip % tabskip glue
\setbox0=\lastbox % second column
\unskip % tabskip glue
\setbox0=\lastbox % second rule
\unskip % tabskip glue
\setbox0=\lastbox % first column
\global\dimen1=\wd0
}%
}%
\dimen0=.5\hsize
\advance\dimen0 by -1.2pt % the rules
\advance\dimen0 by -2em % the padding
\advance\dimen0 by -\dimen1 % the first column
\vbox{
\offinterlineskip\tabskip=0pt
\everycr{\noalign{\hrule}}
\halign{%
\vrule##\strut&
\hfil\enskip##\enskip&
\vrule##&
\enskip\vtop{
\normalbaselines
\hsize=\dimen0
\parindent=0pt
\strut##\strut
}\enskip\hfil&
\vrule##\cr
#1\crcr
}%
}%
}
\noindent\vrule height 0.4pt width .5\hsize
\medskip
\myhalign{
&\omit\hfil\enskip\bf ID\enskip\hfil&
&\omit\span\omit\hfil\enskip\bf Description\enskip\hfil\vrule
\cr
&1&
&A description that should be broken up across several lines&
\cr
&1~000&
&A description that fits a single line&
\cr
&1~000~000&
&Another short description&
\cr
}
\bye
答案2
\offinterlineskip\tabskip=0pt
\setbox0\hbox{\enskip1~000~000\enskip}
\newdimen\foo
\foo=.5\hsize
\advance\foo-\wd0
\advance\foo-1.2pt% 3 rules
\halign to.5\hsize{
\vrule#\strut&
\hfil\enskip#\enskip&
\vrule#&
\vtop\bgroup\hsize\foo\normalbaselines
\parindent0pt
\leftskip.5em\rightskip.5em plus 3cm#\ifhmode\unskip\strut\fi\egroup&
\vrule#
\cr\noalign{\hrule}
&\omit\hfil\enskip\bf ID\enskip\hfil&
&\omit\span\omit\hfil\enskip\bf Description\enskip\hfil\vrule
\cr\noalign{\hrule}
&1&
&A description that should be broken up across several lines&
\cr\noalign{\hrule}
&1~000&
&A description that fits a single line&
\cr\noalign{\hrule}
&1~000~000&
&Another short description&
\cr\noalign{\hrule}}
\end