In attempt to build a function that would work like MatrixForm but would wrap a matrix with brackets, I tried the following function:

matrixform[mat_] := TraditionalForm[DisplayForm[RowBox[{
StyleBox["[", SpanMaxSize -> \[Infinity]], 
 StyleBox["]", SpanMaxSize -> \[Infinity]]}]]] 

This does what I want on an ordinary matrix, say a={{1,2},{3,4}}, as well as on 2a and a+a. But if I pass in a product such as a.a it does not work properly. I have tried wrapping the product in Evaluate, and tried executing a.a separately and then passing in % and this has made no difference. Note that a better function Composition[bracketF,Grid] suggested here lacks these issues. So the question is why can we not pass in a product of matrices into matrixform? This is my first time working with functions such as StyleBox, GridBox, RowBox, etc.

  • 1
    $\begingroup$ This seems to be another strange behavior, probably a bug. I'll take a closer look tomorrow. $\endgroup$
    – Mr.Wizard
    Commented Jul 24, 2014 at 20:42
  • $\begingroup$ Not a bug after all. Please see my answer below. $\endgroup$
    – Mr.Wizard
    Commented Jul 25, 2014 at 6:57

2 Answers 2


This behavior is present in both version 7 and version 10 (Windows). Illustrated:

IdentityMatrix[2] // matrixform

enter image description here

{{1, 0}, {0, 1}} // matrixform

enter image description here

There is a difference between {{1, 0}, {0, 1}} and (the evaluated form of) IdentityMatrix[2]: the latter is a packed array.

{{1, 0}, {0, 1}}  // Developer`PackedArrayQ
IdentityMatrix[2] // Developer`PackedArrayQ


It seems that GridBox (or the Front End interpreting it) is not equipped to handle packed arrays.
Your original definition works if we unpack:

IdentityMatrix[2] // Developer`FromPackedArray // matrixform

enter image description here

You can therefore correct your definition by adding this:

matrixform[mat_?Developer`PackedArrayQ] :=
  Developer`FromPackedArray[mat] // matrixform
  • $\begingroup$ Thanks for figuring all of this out. Since it is a Duplicate, do I accept an answer still? I am guessing the reason bracketF from here "works" is because it uses ToBoxes which gives box expressions and GridBox works on box expressions. $\endgroup$ Commented Jul 25, 2014 at 14:44
  • $\begingroup$ How is this not a bug? Mathematica goes a long way towards hiding all the packing/unpacking internals hidden from the user, who, without delving into the Developer context, does not have any tools or visual hints to distinguish them. In my opinion the message of that (or at least a perfectly justified expectation) is that they should behave identically wherever a list is expected. GridBox help page does not mention anything about requiring lists to be unpacked either. $\endgroup$
    – The Vee
    Commented Sep 11, 2017 at 11:25
  • 1
    $\begingroup$ @TheVee The official position is that it is not a bug, see (4744). The quoted statement includes "Box forms interpret strings and boxes and not Mathematica expressions." This seem to suggest that GridBox[{{1, 2, 3}}] is not valid either and it should be GridBox[{{"1", "2", "3"}}]. Indeed ToBoxes@Grid[{{1, 2, 3}}] // InputForm shows that this is what Grid produces. It is interesting that GridBox[{{1, 2, 3}}] // DisplayForm is accepted however. $\endgroup$
    – Mr.Wizard
    Commented Sep 11, 2017 at 12:19

I can't explain this strange behaviour of GridBox. But replacing it with Grid I get the desired output (also with a.a // matrixform)

matrixform[mat_] := 
   RowBox[{StyleBox["[", SpanMaxSize -> \[Infinity]], Grid[mat], 
     StyleBox["]", SpanMaxSize -> \[Infinity]]}]]];

To align the numbers properly use Grid[mat, Alignment -> Right]

  • $\begingroup$ Thanks. I thought I tried Grid at one point but was somehow discouraged. I was under the impression that when working with RowBox and StyleBox that I had to use GridBox. I guess that was wrong. $\endgroup$ Commented Jul 24, 2014 at 17:25

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