4
$\begingroup$

Evaluating

TableForm[Table[{x + y, x - y}, {x, 10}, {y, 10}], TableHeadings -> {Table[x, {x, 10}], Table[y, {y, 10}]}]

gives me

enter image description here

What I would like to have is something like

enter image description here

It doesn't have to be exactly like this but I would like to make clear which variable, $x$ or $y$, changes along rows and which along columns. Is this possible in Mathematica?

Also, within each cell, I would like to indicate what the upper and what the lower element represents, i.e. $x+y$ or $x-y$. My first idea was to insert a second header column (perhaps using Prepend but that got quite clunky) as shown in the second picture, but if someone can think of a better way, I'm open for suggestions.

$\endgroup$
5
$\begingroup$

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

Blockquote

$\endgroup$
5
$\begingroup$

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 :

enter image description here

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]  

enter image description here

Edit

Or better , thanks to Alexey Popkov's comment :

ToString@Format[TableForm[
Table[{x+y,x-y},{x,5},{y,5}],
Table‌​Headings->{Table[x,{‌​x,5}],Table[y,{y,5}]‌​,{x+y,x-y}},
TableAli‌​gnments->{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}}]  

enter image description here

$\endgroup$
  • 1
    $\begingroup$ You can convert TableForm into the formatted plain text form using ToString: ToString[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]. $\endgroup$ – Alexey Popkov May 30 '17 at 8:14
  • 1
    $\begingroup$ Alternative method: 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]. $\endgroup$ – Alexey Popkov May 30 '17 at 8:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.