如何设置两个或多个段落使用相同量的垂直空间?

如何设置两个或多个段落使用相同量的垂直空间?

给定 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是简单的vboxes,表示最后一段的高度。该height值是从两遍数据集中获取的。lastparagraph也是一个vbox,其高度是测量并保存在.tuc文件中的。\framed后面的\starttext只是为了说明而添加的。用您的内容填充它。

相关内容