TableForm with TableHeadings aligned left, but table contents aligned right

TableForm with the TableForm option is a quick and easy way to display a beautiful classic table in Mathematica FrontEnd. The only problem is that usually such a table is displayed with headings aligned to the left, and the contents of the table aligned to the right. Is it possible to make TableForm behave this way? Or, if not, what is the way to make an analogue of TableForm that behaves this way?

+4
source share
3 answers

It seems like one way to do this:

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

You can make this behavior permanent using the Villagas-Galey 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 an alternative function myTableForm :

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

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 one:

 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

+2
source

You can get a lot more control with a Grid or GridBox if TableForm doesn't do what you like.

+1
source

All Articles