# Row and Column Headings for a Grid

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?

• 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: ibnhconsulting.blogspot.com.au/2011/06/… – Mike Honeychurch Jun 3 '14 at 7:58

data = {{a, b}, {c, d}, {e, f}};
head = {{"r1", "r2", "r3"}, {"c1", "c2"}};



• 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;
grid
];
headings = {{"r1", "r2", "r3"}, {"c1", "c2"}};
data = {{a, b}, {c, d}, {e, f}};


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

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


You can use it with GraphicsGrid also

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


etc...

• 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"}};


 GridPlus[data_, headers_, opts : OptionsPattern[Grid]] :=