如何向表中添加递归行,直到它们到达 ConTeXt 中页面的末尾?

如何向表中添加递归行,直到它们到达 ConTeXt 中页面的末尾?

我在 ConTeXt 中有一张这样的表格:

\starttext
    \chapter{This is a title}

    \bTABLE
    \bTABLEhead
        \bTR \bTD a \eTD \bTD b \eTD \bTD c \eTD \eTR
    \eTABLEhead

    \bTABLEbody
        \dorecurse{100}{%
            \bTR \bTD 1 \eTD \bTD 2 \eTD \bTD 3 \eTD \eTR
        }
    \eTABLEbody
    \eTABLE
\stoptext

我如何设置递归以仅重复单元格直至填充页面的剩余部分?

答案1

一种可能的算法是循环排版表格,添加行直到超过页面上可用的剩余高度。请注意,这种方法非常慢。

\startbuffer[table]
\bTABLE
\bTABLEhead
    \bTR \bTD a \eTD \bTD b \eTD \bTD c \eTD \eTR
\eTABLEhead

\bTABLEbody
    \dorecurse{\the\scratchcounter}{%
        \bTR \bTD 1 \eTD \bTD 2 \eTD \bTD 3 \eTD \eTR
    }
\eTABLEbody
\eTABLE
\stopbuffer

\showframe

\starttext
    \chapter{This is a title}

    \scratchcounter=1
    \scratchdimen=0pt
    \loop\ifdim\scratchdimen<\dimexpr\pagegoal-\pagetotal-2\lineheight\relax % why 2\lineheight?
        \setbox\scratchbox=\vbox{\getbuffer[table]}
        \scratchdimen=\dimexpr\ht\scratchbox+\dp\scratchbox\relax
        \advance\scratchcounter by 1
    \repeat
    \advance\scratchcounter by -1
    \vbox{\getbuffer[table]}
\stoptext

答案2

这与 Henri Menke 的解决方案的想法相同,但我不是逐行添加行,而是先测量行的高度,然后使用内置命令\getrawnoflines获取剩余空间中可容纳的行数。

\define\calculatelines
    {\setbox\scratchbox\hbox{\bTABLE
        \bTR \bTD 1 \eTD \bTD 2 \eTD \bTD 3 \eTD \eTR
      \eTABLE}%
     \openlineheight=\htdp\scratchbox
     \ifdim\pagegoal=\maxdimen % we are at the beginning of the page
          \scratchdimen=\dimexpr\textheight - 2\openlineheight\relax
      \else
          \scratchdimen=\dimexpr\pagegoal - \pagetotal - 2\openlineheight \relax
      \fi
      \getrawnoflines\scratchdimen}

\define\fillTABLE
    {\blank
     \bgroup
     \calculatelines
     \bTABLE
     \bTABLEhead
         \bTR \bTD a \eTD \bTD b \eTD \bTD c \eTD \eTR
     \eTABLEhead

     \bTABLEbody
         \dorecurse{\the\noflines}
             {\bTR \bTD 1 \eTD \bTD 2 \eTD \bTD 3 \eTD \eTR}
     \eTABLEbody
     \eTABLE
     \egroup}


\setuppapersize[A5]
\showframe

\starttext

\fillTABLE

\page

\section{New section}

\fillTABLE

\page

\section{With some text}

\input knuth

\fillTABLE

\stoptext

相关内容