Table with headings across multiple rows/columns
In case you are not aware, you can add a third level of headings :
TableForm[Table[
{x + y, x - y}, {x, 5}, {y, 5}],
TableHeadings -> {Table[x, {x, 10}], Table[y, {y, 10}],{x+y,x-y}}]
Unfortunatly, the result is not satisfactory :
you may be interested by this trick :
fileName=CreateTemporary[];
st=OpenWrite[fileName,FormatType-> OutputForm];
Write[st,TableForm[
Table[{x + y, x - y}, {x, 5}, {y, 5}],
TableHeadings -> {Table[x, {x, 5}], Table[y, {y, 5}],{x+y,x-y}},
TableAlignments-> {Right,Top,Right}],
];
Close[st];
Import[fileName]
Edit
Or better , thanks to Alexey Popkov's comment :
ToString@Format[TableForm[
Table[{x+y,x-y},{x,5},{y,5}],
TableHeadings->{Table[x,{x,5}],Table[y,{y,5}],{x+y,x-y}},
TableAlignments->{Right,Top,Right}],OutputForm]
Edit (19 August 2019)
I have put in this chatroom , at the very beginning, the code for a function flatTableForm[]
that does what you need (among other things).
flatTableForm[Table[{x + y, x - y}, {x, 10}, {y, 10}],
TableHeadings -> {Table[x, {x, 10}],
Table[y, {y, 10}], {x + y, x - y}}]
It is easy to add a horizontal row of headers. You can use Prepend
to add vertical headers to the actual data, and then use Grid
with Dividers
:
Grid[{
{" ", "x y", Sequence @@ Table[x, {x, 10}]},
Sequence @@ MapThread[Prepend /* Flatten,
{
Table[Column[{x + y, x - y}], {x, 10}, {y, 10}],
Table[{Column[{"x+y", "x-y"}], x}, {x, 10}]
}]
},
Dividers -> {{2 -> True, 3 -> True}, 2 -> True}]