4

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?

Alexey Popkov
  • 9,355
  • 4
  • 42
  • 93

3 Answers3

3

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

Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
  • 1
    The main problem is with left table headings, not the top. It is just always takes too much time to think about `Transpose` and `Prepend`. So I need a easy-to use short function that will take (optionally) left and top table headings and construct classical table as `TableForm` does: `TableForm[{{5, 7}, {4, 26}, {100, 3}}, TableHeadings -> {{"Group A", "Group B", "Group C"}, {"y1", "y2"}}]`. But with another alignment, of course. – Alexey Popkov Jun 05 '11 at 11:20
  • 1
    It is worth to mention that `TableForm` is based on `GridBox` and we can see the "definition" of `TableForm` by evaluating: `FormatValues[TableForm] // InputForm`. – Alexey Popkov Jun 05 '11 at 11:26
2

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}}]
Community
  • 1
  • 1
Alexey Popkov
  • 9,355
  • 4
  • 42
  • 93
1

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

Codie CodeMonkey
  • 7,669
  • 2
  • 29
  • 45