TableForming的TableHeadings对齐到Left,但表格的内容对齐到Right [英] TableForm with TableHeadings aligned to Left but the content of table aligned to Right

查看:113
本文介绍了TableForming的TableHeadings对齐到Left,但表格的内容对齐到Right的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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?

推荐答案

看来,实现此目的的一种方法是:

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

可以使用 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"}}]

给予:

另一种方法是定义替代功能myTableForm:

Another way is to define alternative function myTableForm:

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

这篇关于TableForming的TableHeadings对齐到Left,但表格的内容对齐到Right的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆