4
$\begingroup$

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] := 
RowBox[{ToString@f, "[", MakeBoxes[Row[{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?

$\endgroup$
  • $\begingroup$ for tF2 you can use ClearAll[tF2]; tF2 = RawBoxes@MakeBoxes[TraditionalForm[#]] /. RowBox[{h_, "(", else___, ")"}] :> RowBox[{h, "[", else, "]"}] &; $\endgroup$ – kglr Oct 5 '19 at 15:10
  • $\begingroup$ 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 $\endgroup$ – Stuart Poss Oct 5 '19 at 16:24
  • $\begingroup$ 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. $\endgroup$ – Stuart Poss Oct 5 '19 at 16:40
4
$\begingroup$

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

enter image description here

If you don't mind messing with internal functions, you can modify the default formatting of matrix objects by redefining:

OutputFormsDump`EncloseParens[box_]:=Which[
    !OutputFormsDump`$MatrixFormQ,
    box,

    TrueQ @ $UseBrackets,
    RowBox[{StyleBox["[",SpanMaxSize->Infinity],"\[NoBreak]", box, "\[NoBreak]", StyleBox["]",SpanMaxSize->Infinity]}],

    True,
    RowBox[{"(",OutputFormsDump`noBreak,box,OutputFormsDump`noBreak,")"}]
]

Then, setting the variable $UseBrackets = True produces:

$UseBrackets = True;
{{1, 3}, {2, 5}} //TraditionalForm

enter image description here

As always when using or modifying internal, undocumented functions, the behavior is subject to change in future versions of Mathematica.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Rather complicated, but yes it does work. The first seems preferable since it's unlikely to be deprecated or break in future releases. Thanks for the insight. $\endgroup$ – Stuart Poss Oct 5 '19 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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