在 ConTeXt 中,表格环境显然不表现居中上下文。例如:
\startalignment[center]
\starttabular
\NC item one \NC item two \NC\NR
\NC item three \NC item four \NC\NR
\stoptabular
\stopalignment
仍然会导致表格靠在页面的左侧。这里的一个答案建议使用\midaligned {}
around 表格来使它们居中,但这种技术在应用于表格时似乎会引发错误。使表格块居中的正确方法是什么?
答案1
低级\vbox
包装之外,还有两种仅依赖用户界面实现居中表格的方法:浮点数和框架参数。浮点数更强大,但即使使用丰富的安置选择它们可能会表现得不稳定。因此,制表在某些情况下,样式表可能是首选。
%%% This is how you get a “tabular” environment in Context.
\definetabulation [tabular]
\setuptabulation [tabular] [
format={|r|i{1em}l|},
rulethickness=2pt,
]
%%% Used in solution 2. Note the *align* key.
\defineframedcontent [tabularframe] [frame=off, align=middle]
%%% The table goes into a buffer for later use.
\startbuffer [demotable]
\starttabular
\HL
\NC item one \NC item two \NC\NR
\NC item three \NC item four \NC\NR
\LL
\stoptabular
\stopbuffer
\starttext
\input knuth
%%% First solution: Floats.
%%% Use the “here” key to set the float location. Try “force” if it
%%% doesn’t behave.
\placetable[here]{Centered Table Floats}{\getbuffer[demotable]}
%%% Second solution: Frame.
%%% The second solution is even easier: As tabulations respect a
%%% (undocumented?) *frame* parameter, just hook your favorite
%%% *framedcontent* macro in there.
\setuptabulation [tabular] [frame=tabularframe]
\startalignment [middle]
\input ward
\getbuffer[demotable]
\input dawkins
\stopalignment
\stoptext
编辑正如 Marco 指出的那样,带框架的版本将抑制分页符。如果您的制表符太长,您将不得不求助于第一个版本(浮动,分裂选项)。或者你也可以创建一个更窄环境会根据表格宽度将其居中。在下面的列表中,这是在\centertabulation
以表格为参数的宏中完成的。
\newdimen\current_tabulation_offset
\def\test_hsize#1{
\setbox0\vbox{#1}%
\current_tabulation_offset\dimexpr(\hsize-\wd0)/2
}
\definenarrower [tabulate_narrower]
\setupnarrower [tabulate_narrower] [
middle=\current_tabulation_offset,
]
\def\centertabulation#1{
\test_hsize{#1}%
\starttabulate_narrower
#1%
\stoptabulate_narrower
}
\protect
\starttext
\startalignment [middle]
\input ward
\centertabulation{\getbuffer[demotable]}
\input ward
\stopalignment
\stoptext
请注意,这种方法会导致表格中的所有水平规则不对齐。
答案2
不确定这是否是强制居中的规范方法,但以下应该有效
\startalignment[middle]
\dontleavehmode
\vbox{%
\starttable[|l|l|]
\NC item one \NC item two \NC\NR
\NC item three \NC item four \NC\NR
\stoptable
}
\stopalignment
该\dontleavehmode
命令通常与一起使用\startalignment
,例如参见上下文帮助命令/开始对齐。这个\vbox
技巧是我在 Context mailing 上发现的。