# Printing Block Matrices

I have a 2D matrix, such as:

d=2;
M=Array[a, {d^2,d^2}];


and I would like to print it in a way which emphasizes its dxd block matrix structure. If MatrixForm had a Dividers option like Grid, then this is what I would use. But it doesn't. The first thing I tried was:

With[
{div={False, Join[Table[False, {n, d - 1}], {Dashed}], False}},
{{Grid[M,Dividers->{div,div}]}}//MatrixForm
]


where the {{stuff}}//MatrixForm is simply to get the parentheses.

The problem with this is that if your matrix is big (d>2) and certain entries of your matrix are big expressions, then they do an ugly line wrap thing. To counter this, I call {{TableForm[#]}}& on each element of the matrix first:

With[
{div = {False,Join[Table[False, {n, d - 1}], {Dashed}],False}},
{{
Grid[Map[TableForm[{{#}}] &, M, {2}],Dividers -> {div, div}]
}} // MatrixForm
]


but this is a hack. So I suppose my questions are: What Form do TableForm and MatrixForm use on each of their elements, and can I use this here? I've tried StandardForm but that's not it. Next, is there in general some better way to do what I am trying to do?

Edit: As requested, here is an example that illustrates the "ugly line wrap thing":

d = 3;
M = Table[Expand[(a + b)^RandomInteger[4]], {i, d^2}, {j, d^2}];

-
– Artes Oct 10 '12 at 21:30

In the absence of a better example I am going to suppose that you just need ItemSize -> Full:

With[{div = {False, Append[Table[False, {d - 1}], Dashed], False}},
{{Grid[M, Dividers -> {div, div}, ItemSize -> Full]}} // MatrixForm
]


Code for convenience that shows wrapping without that option:

ClearAll[a, x]

d = 3;
a[7, 2] = Expand[(1 + x)^10];
M = Array[a, {d^2, d^2}]

-
Awesome, thanks. I put my edit in before I saw your example, and I see we came up with similar examples. The matrix I actually want to print is too unwieldly to copy here. – Ian Hincks Oct 12 '12 at 13:49
@Ian Glad I could help. If you want the items to all the be same size to make a nice regular table you can use ItemSize -> All. – Mr.Wizard Oct 12 '12 at 14:51