给定 ConTeXt 或纯 TeX 中的几个段落,如何通过在末尾添加空格来确保所有段落使用相同数量的垂直空间,作为组中最长的段落?例如:
This is a paragraph
with only two lines.
This paragraph is a
little bit longer and it
has three lines.
This is the longest
paragraph, as it has
a second, third, and
fourth line.
上面的段落随后会添加空白,以便它们都占据与最后一段相同的垂直空间,因为它是最长的,例如:
This is a paragraph
with only two lines.
This paragraph is a
little bit longer and it
has three lines.
This is the longest
paragraph, as it has
a second, third, and
fourth line.
答案1
另一个基于 ConTeXt 的解决方案,但这个方案只需一次即可完成。它要求您将内容存储在缓冲区中,然后测量所有缓冲区并将高度设置为最大高度(不一定是最后一段的高度)。
\newdimen\maxbufferheight
\def\placebuffertomaximumheight[#1]%
{\maxbufferheight\zeropoint
\processcommalist[#1]\domeasurebuffermaxheight
\processcommalist[#1]\doplacebuffertomaxheight}
\def\domeasurebuffermaxheight#1%
{\setbox\scratchbox\vbox{\getbuffer[#1]}%
\scratchdimen\ht\scratchbox
\ifdim\scratchdimen>\maxbufferheight
\maxbufferheight=\scratchdimen
\fi}
\def\doplacebuffertomaxheight#1%
{\ruledvbox to \maxbufferheight
{\getbuffer[#1]}%
\blank[none]} %change \blank[..] to \par to get regular inter-para space
\starttext
\startbuffer[one]
Single line
\stopbuffer
\startbuffer[two]
\input tufte
\stopbuffer
\startbuffer[three]
\input ward
\stopbuffer
\placebuffertomaximumheight[one,two,three]
\stoptext
一旦掌握了基本机制,就可以直接将其包装到宏中。
\newcount\nofmeasuredparagraphs
\def\startparagraph
{\increment\nofmeasuredparagraphs
\grabbufferdata[measuredparagraph-\nofmeasuredparagraphs][startparagraph][stopparagraph]}
\def\stopparagraph{}
\def\startmeasuredparagraph
{\nofmeasuredparagraphs\zeropoint}
\def\stopmeasuredparagraph
{\maxbufferheight\zeropoint
\dorecurse\nofmeasuredparagraphs
{\domeasurebuffermaxheight{measuredparagraph-\recurselevel}}%
\dorecurse\nofmeasuredparagraphs
{\doplacebuffertomaxheight{measuredparagraph-\recurselevel}}}
\starttext
\startmeasuredparagraph
\startparagraph
Single line
\stopparagraph
\startparagraph
\input tufte
\stopparagraph
\startparagraph
\input ward
\stopparagraph
\stopmeasuredparagraph
\stoptext
这给出
我使用\ruledvbox
以便您可以看到该框。\vbox
如果您不想看到该框,请将其更改为。
答案2
您可以使用
\vbox to 5cm{stuff.......\vfill}
强制使用正确垂直尺寸的盒子,并在末尾填充空间。
你可以先测量其中一个盒子,而不是 5 厘米,这样
\setbox0\vbox{longest stuff....}
然后对于每个段落你可以做
\vbox to \ht0{stuff.......\vfill}
答案3
这里我提供了一个更符合 ConTeXtish 的解决方案,它基于与 David 的解决方案相同的想法。它结合了现在两次传递数据机制。
\defineframedtext [normalparagraph]
[
frame=off,
offset=overlay,
width=\textwidth,
height=\datasetvariable{lastparagraph}{last}{height},
]
\definedataset [lastparagraph]
\newbox\mylastbox
\definestartstop [lastparagraph]
[
before=\setups{last:before},
after=\setups{last:after},
]
\startsetups last:before
\setbox\mylastbox\vbox\bgroup
\stopsetups
\startsetups last:after
\egroup
\setdataset [lastparagraph] [last] [height=\the\ht\mylastbox]
\box\mylastbox
\stopsetups
\starttext
\startnormalparagraph
\framed[align=normal]{\input knuth\par}
\stopnormalparagraph
\startnormalparagraph
\framed[align=normal]{\input ward\par}
\stopnormalparagraph
\startlastparagraph
\input knuth
\stoplastparagraph
\stoptext
结果:
snormalparagraph
是简单的vbox
es,表示最后一段的高度。该height
值是从两遍数据集中获取的。lastparagraph
也是一个vbox
,其高度是测量并保存在.tuc
文件中的。\framed
后面的\starttext
只是为了说明而添加的。用您的内容填充它。