如何設定兩個或多個段落使用相同的垂直空間?

如何設定兩個或多個段落使用相同的垂直空間?

給定 ConTeXt 或 plain 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的解決方案,基於與Davids解決方案相同的想法。它融合了現在兩遍數據機制。

\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只是為了說明而添加的。用您的內容填充它。

相關內容