ConTeXt 中自定义制表开始-停止的问题

ConTeXt 中自定义制表开始-停止的问题

在我的文档中,我有几个制表环境的实例,它们都看起来像这样:

\startplacetable[location=force,number=no]
  \starttabulate[|l|r|rw(2cm)|]
    \HL \NC {\bf Process} \NC {\bf\em T} \NC {\bf\em C} \NC \NR \HL
    % --- varying content here, example line: ---
    \NC A \NC 200ms \NC 20ms \NC \NR
    % -------------------------------------------
    \HL
  \stoptabulate
\stopplacetable

因此我尝试创建一个自定义的启停环境:

\definestartstop[ProcessTable][
  before={%
    \startplacetable[location=force,number=no]
      \starttabulate[|l|r|rw(2cm)|]
        \HL \NC {\bf Process} \NC {\bf\em T} \NC {\bf\em C} \NC \NR \HL
    },%
  after={%
        \HL
      \stoptabulate
    \stopplacetable
  }%
]

这在文档中使用时永远不会结束,例如:

\starttext
  \startProcessTable
    \NC A \NC 200ms \NC 20ms \NC \NR
  \stopProcessTable
\stoptext

ConTeXt 输出的最后两行:

close source    > level 2, order 3, name '/.../test.tex'
close source    > level 1, order 3, name '/usr/local/context/tex/texmf-context/tex/context/base/mkiv/cont-yes.mkiv'

当尝试将其与原始代码一起使用时:

\starttext
  \startProcessTable
    \NC A \NC 200ms \NC 20ms \NC \NR
  \stopProcessTable

  \startplacetable[location=force,number=no]
    \starttabulate[|l|r|rw(2cm)|]
      \HL \NC {\bf Process} \NC {\bf\em T} \NC {\bf\em C} \NC \NR \HL
    \stoptabulate
  \stopplacetable
\stoptext

我得到这个输出,暗示某些事情严重错误:

tex error       > tex error on line 34 in file /.../test.tex: ! Missing } inserted

<inserted text>
}
<to be read again>
\endtemplate
<template> \endtemplate

\tabl_tabulate_column_normal ...\unskip \aligntab
                                                  \ifconditional \c_tabl_tab...
\tabl_tabulate_insert_body ...\dotagconstruct \NC
                                                  A \NC 200ms \NC 20ms \NC \...
\tabl_tabulate_insert_content ...late_insert_body
                                                  \tabl_tabulate_insert_foot...
...
l.34     \stoptabulate


24     %]
25
26     \starttext
27       \startProcessTable
28         \NC A \NC 200ms \NC 20ms \NC \NR
29       \stopProcessTable
30
31       \startplacetable[location=force,number=no]
32         \starttabulate[|l|r|rw(2cm)|]
33           \HL \NC {\bf Process} \NC {\bf\em T} \NC {\bf\em C} \NC \NR \HL
34 >>      \stoptabulate
35       \stopplacetable
36     \stoptext

在尝试解决这个问题时,我发现有\definetabulate,所以我尝试使用它:

\definetabulate[ProcessTable][|l|r|rw(2cm)|]
\setuptabulate[ProcessTable][
  before={%
    \startplacetable[location=force,number=no]
  },
  inner={%
    \HL \NC {\bf Process} \NC {\bf\em T} \NC {\bf\em C} \NC \NR \HL
  },
  after={%
    \stopplacetable
  }%
]

这可以编译,但不会呈现表格(即,呈现了标题和正文中的文本,但没有线条和对齐方式)。我假设我使用了inner错误的参数,但我没有找到相关文档或示例。当我删除该参数时inner,表格会呈现,但没有标题。

我该如何正确设置这样的环境?

答案1

看起来\starttabulate是在寻找\stoptabulate,所以把它放进去\definestartstop是行不通的。你可以解决这个问题,只需收集表内容作为参数即可。

\unexpanded\long\def\startProcessTable#1\stopProcessTable
  {\startplacetable[location=force,number=no]
    \starttabulate[|l|r|rw(2cm)|]
      \HL \NC {\bf Process} \NC {\bf\em T} \NC {\bf\em C} \NC \NR \HL
      #1%
      \HL
    \stoptabulate
  \stopplacetable}

\starttext
  \startProcessTable
    \NC A \NC 200ms \NC 20ms \NC \NR
  \stopProcessTable
\stoptext

在此处输入图片描述

如果您稍后需要重用表内容,或者以任何方式处理它,我建议使用缓冲区:

\unprotect

\unexpanded\def\startProcessTable
  {\buff_pickup{ProcessTable}{startProcessTable}{stopProcessTable}{\relax}{\process_table}{\zerocount}}

\unexpanded\def\process_table
  {\startplacetable[location=force,number=no]
    \starttabulate[|l|r|rw(2cm)|]
      \HL \NC {\bf Process} \NC {\bf\em T} \NC {\bf\em C} \NC \NR \HL
      \rawbuffer{ProcessTable}%
      \HL
    \stoptabulate
  \stopplacetable}

\protect

\starttext
  \startProcessTable
    \NC A \NC 200ms \NC 20ms \NC \NR
  \stopProcessTable
\stoptext

答案2

为了使表格具有特定的第一行和最后一行,您可以使用\starttablehead\starttabletail(中有一个错误tabl-tbl.mkiv;我会将错误修复发送到上下文列表,但同时,我也在下面包含了修复)。

\starttabulatehead
  \HL \NC {\bf Process} \NC {\bf\em T} \NC {\bf\em C} \NC \NR \HL
\stoptabulatehead

\starttabulatetail
  \HL
\stoptabulatetail

\starttext
\starttabulate[|l|r|rw(2cm)|]
  \NC A \NC 200ms \NC 20ms \NC \NR
  \NC B \NC 400ms \NC 30ms \NC \NR
\stoptabulate
\stoptext

但是,这会给每个表添加一个头和尾。要将它们应用于特定表,我们可以简单地定义一个新的制表符:

\definetabulate[process][|l|r|rw(2cm)|]

\starttabulatehead[process]
  \HL \NC {\bf Process} \NC {\bf\em T} \NC {\bf\em C} \NC \NR \HL
\stoptabulatehead

\starttabulatetail[process]
  \HL
\stoptabulatetail

\starttext
\startprocess
  \NC A \NC 200ms \NC 20ms \NC \NR
  \NC B \NC 400ms \NC 30ms \NC \NR
\stopprocess
\stoptext

现在,如果您使用浮点数使表格居中,则可以添加:

\setuptabulate
  [process]
  [
    before={\startplacetable[location={force,none}]\insidefloatfalse},
    after={\stopplacetable},
  ]

需要\insidefloatfalse确保after应用了键;否则,ConTeXt 以外部模式启动,应用之前键,并且当应用之后键时,它会检查表是否位于浮点数内并且不应用之后键!

以下是完整的代码(修复了错误tabl-tab.mkiv

\unprotect
\def\tabl_tabulate_check_full_content % - needed, else confusion with \c!header
  {\ifcsname\??tabulatehead\currenttabulation\endcsname
    %\expandafter\ifx\csname\??tabulatehead\currenttabulation\endcsname\empty
     \expandafter\ifx\lastnamedcs\empty
       \let\tabl_tabulate_insert_head\empty
     \else
       \let\tabl_tabulate_insert_head\tabl_tabulate_insert_head_content
     \fi
   \else
     \let\tabl_tabulate_insert_head\empty
   \fi
   \ifcsname\??tabulatefoot\currenttabulation\endcsname
     \expandafter\ifx\csname\??tabulatefoot\currenttabulation\endcsname\empty
    %\expandafter\ifx\lastnamedcs\empty
       \let\tabl_tabulate_insert_foot\empty
     \else
       \let\tabl_tabulate_insert_foot\tabl_tabulate_insert_foot_content
     \fi
   \else
     \let\tabl_tabulate_insert_foot\empty
   \fi}
\protect

\definetabulate[process][|l|r|rw(2cm)|]

\setuptabulate
  [process]
  [
    before={\startplacetable[location={force,none}]\insidefloatfalse},
    after={\stopplacetable},
  ]

\starttabulatehead[process]
  \HL \NC {\bf Process} \NC {\bf\em T} \NC {\bf\em C} \NC \NR \HL
\stoptabulatehead

\starttabulatetail[process]
  \HL
\stoptabulatetail

\starttext

\input knuth

\startprocess
  \NC A \NC 200ms \NC 20ms \NC \NR
  \NC B \NC 400ms \NC 30ms \NC \NR
\stopprocess

\input knuth

\startprocess
  \NC A \NC 200ms \NC 20ms \NC \NR
  \NC B \NC 400ms \NC 30ms \NC \NR
\stopprocess

\input knuth

\stoptext

这使

在此处输入图片描述

相关内容