表标题左对齐但表内容右对齐的表格表单

发布于 2024-11-14 10:53:25 字数 225 浏览 4 评论 0原文

带有 TableHeadings 选项的 TableForm 是在 Mathematica FrontEnd 中显示美观的经典表格的快速、简单的方法。唯一的问题是,显示这样的表格时,标题左对齐但表格内容右对齐是很常见的。是否可以强制 TableForm 以这种方式运行?或者如果不是,那么模拟 TableForm 的最佳方法是什么?

TableForm with TableHeadings option is a quick and easy way to display good-looking classical table in Mathematica FrontEnd. The only problem is that it is common to display such a table with headings aligned to the left but the content of the table aligned to the right. Is it possible to force TableForm to behave in this way? Or if not, what is the best way to make an analog of TableForm that behaves in this way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

堇色安年 2024-11-21 10:53:25

您可以使用网格对齐。这是一种方法:

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}
]

在此处输入图像描述

You can use Grid and Alignment. Here is one way:

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]

Here is another:

headings = {"One", "Two", "Three", "Four", "Five", "Six", "Seven"};

Grid[a ~Prepend~ headings,
     Dividers -> {None, {2 -> True}}, 
     Alignment -> {Right, Automatic, {{1, 1}, {1, -1}} -> Left}
]

enter image description here

踏月而来 2024-11-21 10:53:25

看来做到这一点的一种方法是:

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}]

可以使用 Villegas-Gayley 技巧

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"}}]

给出:

Modified TableForm

另一种方法是定义替代函数 myTableForm

myTableForm[args___] := 
 RawBoxes[ToBoxes[TableForm[args]] /. (ColumnAlignments -> _) -> 
    ColumnAlignments -> {Left, {Right}}]

It appears that one way to do this is:

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}]

One can make such behavior permanent using Villegas-Gayley trick:

Unprotect[TableForm];
TableForm[args___] /; ! TrueQ@$inTableForm := 
 Block[{$inTableForm = True}, 
  RawBoxes[ToBoxes[TableForm[args]] /. (ColumnAlignments -> _) -> 
     ColumnAlignments -> {Left, Right}]]
Protect[TableForm];

Now

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"}}]

gives:

Modified TableForm

Another way is to define alternative function myTableForm:

myTableForm[args___] := 
 RawBoxes[ToBoxes[TableForm[args]] /. (ColumnAlignments -> _) -> 
    ColumnAlignments -> {Left, {Right}}]
终遇你 2024-11-21 10:53:25

如果 TableForm 不符合您的要求,您可以使用 Grid 或 GridBox 获得更多控制。

You can get far more control using Grid or GridBox if TableForm doesn't do what you like.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文