I have a 2D matrix, such as:

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:

    {div={False, Join[Table[False, {n, d - 1}], {Dashed}], False}},

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:

    {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}];

1 Answer 1


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}]
  • $\begingroup$ 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. $\endgroup$
    – Ian Hincks
    Oct 12, 2012 at 13:49
  • $\begingroup$ @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. $\endgroup$
    – Mr.Wizard
    Oct 12, 2012 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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