Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

It would be useful for me if I could write:

Grid[{{a, b}, {c, d}, {e, f}}, 
 GridHeadings -> {{"r1", "r2", "r3"}, {"c1", "c2"}}]

just like

TableForm[{{a, b}, {c, d}, {e, f}}, 
 TableHeadings -> {{"r1", "r2", "r3"}, {"c1", "c2"}}]

except that I don't want the two lines TableForm produces.

How could I add this additional option to Grid?

share|improve this question
I think it is a duplicate but I can't find it. Meanwhile I'm linking great answer by Mike Honeychurch. – Kuba Jun 3 '14 at 6:08
It is worth to mention that TableForm is based on GridBox and we can see the "definition" of TableForm by evaluating: FormatValues[TableForm]. – Alexey Popkov Jun 3 '14 at 6:14
IMHO, it's probably best practice not to override the built-in functions if it's not necessary. – Myridium Jun 3 '14 at 6:22
@Kuba - thanks for the very nice link – eldo Jun 3 '14 at 6:31
this is 3 years old but may be of interest:… – Mike Honeychurch Jun 3 '14 at 7:58
up vote 2 down vote accepted
data = {{a, b}, {c, d}, {e, f}};
head = {{"r1", "r2", "r3"}, {"c1", "c2"}};

Join[Transpose[{Join[{""}, head[[1]]]}], Join[head[[{2}]], data], 2] // Grid

enter image description here

share|improve this answer
I am very pleased. "MyGrid[d_, h_] := Join[Transpose[{Join[{""}, h[[1]]]}], Join[h[[2]], d], 2] // Grid" now easily gives me what I wanted. Just flipped an acceptance coin between rasher and you. – eldo Jun 3 '14 at 7:26
@eldo: In the future, please use Mathematica's RandomInteger for such decisions: I've seen coins with Kuba's face on both sides... ;-) – ciao Jun 3 '14 at 7:32

I do not know of options that would do this to Grid. Grid is really just matrix with extra options build-in.

Can you just build-it in manually?

fillInHeader[h_, data_] := Module[{n, grid},
   n = Length[data];
   grid = Table[Null, {n + 1}, {n}];
   grid[[2 ;; -1, All]] = h[[1]];
   grid[[1, 2 ;; -1]] = h[[2]];
   grid[[2 ;; -1, 2 ;; -1]] = data;
headings = {{"r1", "r2", "r3"}, {"c1", "c2"}};
data = {{a, b}, {c, d}, {e, f}};
Grid[fillInHeader[headings, data]]

Mathematica graphics

If you want to add other Grid options, you can now do this

Grid[fillInHeader[headings, data], Frame -> All]

Mathematica graphics

You can use it with GraphicsGrid also

 GraphicsGrid[fillInHeader[headings, data], ImageSize -> 100]

Mathematica graphics


share|improve this answer
using your code above: why shouldn't it be posssible to unprotect "Grid" and add it as an option? – eldo Jun 3 '14 at 6:16
@eldo, I would not know how to do this. I also think making a simple function to fill-in the headers on the sides of the data is just simpler than what you suggest. – Nasser Jun 3 '14 at 6:19
data = {{a, b}, {c, d}, {e, f}};
headings = {{"r1", "r2", "r3"}, {"c1", "c2"}};

Grid[Prepend[Flatten /@ Transpose[{headings[[1]], data}], 
             PadLeft[headings[[2]], Length@data[[1]] + 1, ""]]]

enter image description here

I assume data sublists of same length, if not the case, more work needed as this (and other answers so far) will give goofy results.

share|improve this answer

Here's another idea that makes use of ArrayFlatten

 GridPlus[data_, headers_, opts : OptionsPattern[Grid]] := 
 Grid[ArrayFlatten[{{{{""}}, {headers[[1]]}}, \
 {Transpose[{headers[[2]]}], data}}], opts]
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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