# Is there a way to substitute a Bracket for a Matrix Rather than a Parentheses When Using Traditional Form

I'm trying to use Mathematica to work my way through some Linear Algebra problems given by Gilbert Strang (Introduction to Linear Algebra). Consequently, I would like to use his notation as much as possible, in part, to learn better how to typeset using Mathematica and potentially to develop a complete set of answers to his problem sets using Mathematica rather than Matlab or R that mimics the look of his text.

Traditional form using Mathematica will place a parentheses () around a matrix or column vector, which is a common representation to recognize an array as a matrix (column vector). However, Strang uses brackets [], another common notation to reflect a matrix.

In a previous question regarding TraditionalForm some suggested that in the cases of functions one could use one of several user-defined functions to do this.

tF = RawBoxes[ToBoxes[TraditionalForm[#]] /. {"(" -> "[", ")" -> "]"}] &;

tF2 = Module[{f = ToString@#2}, RawBoxes[MakeBoxes[TraditionalForm[#]] //.
RowBox[{f, "(", else___, ")"}] :> RowBox[{"f", "[", else, "]"}]]] &;

makeBracketsF[f_] := (f /: MakeBoxes[f[a___], TraditionalForm] :=
"]"}])


However, the last two fail, as either the slot can not be filled with a matrix (column vector) with more than two rows, or the Traditional form is protected and can not be passed from the Module. The first answer gets close, but the bracket only surrounds the central value and does not encompass all three values, making the output look funky rather than typeset.

Is there a way to accomplish this seemingly simple substitution to permit a more flexible use of Traditional form to surround a matrix with a suitably sized pair of brackets to conform to a widely used traditional form for matrices?

• for tF2 you can use ClearAll[tF2]; tF2 = RawBoxes@MakeBoxes[TraditionalForm[#]] /. RowBox[{h_, "(", else___, ")"}] :> RowBox[{h, "[", else, "]"}] &; – kglr Oct 5 '19 at 15:10
• That doesn't seem to work, if I attempt to pass h_ as either MatrixForm[{1,2,3}] or as a vector {1,2,3}, since the first fails to substitute ] for – Stuart Poss Oct 5 '19 at 16:24
• That doesn't seem to work. If I attempt to pass h_ as either MatrixForm[{1,2,3}] or as a Column[{1,2,3}] neither work. The former seems to work in the original tF2, but only the 2 is correctly bracketed but I do get 3 rows rather than 1 in your modified tF2. – Stuart Poss Oct 5 '19 at 16:40

You can create a form for this:

BracketForm /: MakeBoxes[BracketForm[x_], form_] := RowBox[{
StyleBox["[", SpanMaxSize->Infinity],
"\[NoBreak]",
MakeBoxes[
TableForm[x,TableDepth->2,TableSpacing->{1,1},TableAlignments->{Center,Baseline}],
form
],
"\[NoBreak]",
StyleBox["]", SpanMaxSize->Infinity]
}]

Unprotect[$$OutputForms];$$OutputForms = DeleteDuplicates @ Append[$$OutputForms, BracketForm]; Protect[$$OutputForms];


The TableForm options are the same as those used by MatrixForm. Example:

BracketForm[{{1,2},{2,5}}] //TraditionalForm If you don't mind messing with internal functions, you can modify the default formatting of matrix objects by redefining:

OutputFormsDumpEncloseParens[box_]:=Which[
!OutputFormsDump$MatrixFormQ, box, TrueQ @$UseBrackets,
RowBox[{StyleBox["[",SpanMaxSize->Infinity],"\[NoBreak]", box, "\[NoBreak]", StyleBox["]",SpanMaxSize->Infinity]}],

True,
RowBox[{"(",OutputFormsDumpnoBreak,box,OutputFormsDumpnoBreak,")"}]
]


Then, setting the variable $UseBrackets = True produces: $UseBrackets = True; 