TableForm
选项的 TableHeadings
是在 Mathematica FrontEnd中显示漂亮的经典表格的快捷方式。唯一的问题是,通常显示这样一个表格,其标题与左侧对齐,但表格的内容与右侧对齐。是否可以强制TableForm
以这种方式行事?或者如果没有,那么以TableForm
模拟这种行为的最佳方式是什么?
答案 0 :(得分:3)
您可以使用Grid
和Alignment
。这是一种方式:
a = Map[Mod[RandomInteger[2*^9], 10^#] &, RandomInteger[{1, 6}, {4, 7}], {2}];
b = Item[#, Alignment -> Left] & /@
{"One", "Two", "Three", "Four", "Five", "Six", "Seven"};
Grid[a~Prepend~b, Alignment -> Right]
这是另一个:
headings = {"One", "Two", "Three", "Four", "Five", "Six", "Seven"};
Grid[a ~Prepend~ headings,
Dividers -> {None, {2 -> True}},
Alignment -> {Right, Automatic, {{1, 1}, {1, -1}} -> Left}
]
答案 1 :(得分:2)
似乎有一种方法是:
RawBoxes[ToBoxes[
TableForm[RandomReal[{-10, 10}, {3, 3}],
TableHeadings -> {{"First left header", "Second left header",
"Trird left header"}, {"First top header", "Second top header",
"Third top header"}}]] /. (ColumnAlignments -> _) ->
ColumnAlignments -> {Left, Right}]
使这种行为永久化
Unprotect[TableForm];
TableForm[args___] /; ! TrueQ@$inTableForm :=
Block[{$inTableForm = True},
RawBoxes[ToBoxes[TableForm[args]] /. (ColumnAlignments -> _) ->
ColumnAlignments -> {Left, Right}]]
Protect[TableForm];
现在
TableForm[RandomReal[{-10, 10}, {3, 3}],
TableHeadings -> {{"First left header", "Second left header",
"Third left header"}, {"First top header", "Second top header",
"Third top header"}}]
给出:
另一种方法是定义替代函数myTableForm
:
myTableForm[args___] :=
RawBoxes[ToBoxes[TableForm[args]] /. (ColumnAlignments -> _) ->
ColumnAlignments -> {Left, {Right}}]
答案 2 :(得分:1)
如果TableForm不能做你喜欢的事情,你可以使用Grid或GridBox获得更多控制。