Formatting MatrixPower in TraditionalForm
- To: mathgroup at smc.vnet.net
- Subject: [mg102530] Formatting MatrixPower in TraditionalForm
- From: cca at gregosetroianos.mat.br
- Date: Wed, 12 Aug 2009 04:36:15 -0400 (EDT)
Inverse has a 2D formatting in TraditionalForm, but MatrixPower does not. FormatValues@Inverse FormatValues@MatrixPower Two methods (and there are others!) are given bellow to do this. I do not know the situation with $VersionNumber>7. Anyway, I hope my short exposition might be useful to reveal some of the new progresses made in Mathematica with respect to notation programming. ------------------------------------------ Method 1. The following rule formats MatrixPower as a power in TraditionalForm. MakeBoxes[MatrixPower[m_, n_], TraditionalForm] := With[{fm = TraditionalForm}, TemplateBox[{Parenthesize[m, fm, Power, Left], MakeBoxes[n, fm]}, "MatrixPower", DisplayFunction -> (SuperscriptBox[#1, #2] &), SyntaxForm -> SuperscriptBox, Tooltip -> Automatic]] (If desired, one can attach this rule to MatrixPower using Unprotect and TagSetDelayed. In this case, FormatValues@MatrixPower will show the rule.) Some tests: MatrixPower[m, n] // TraditionalForm % // FullForm Another way to see the formatting: (1) paste the expression MatrixPower[m, n] in a StandardForm or TraditionalForm cell; (2) select the expression; (3) go to menu Cell|Convert to|TraditionalForm. The converted expression can be pasted in a StandardForm cell -- and will retain its meaning even if you click on the "Paste Literally" button from the "Pasting TraditionalForm" dialog box. There is no need of a FormBox wrapper here because the semantics resides entirely in TemplateBox. For example, the following two expressions evaluate to the same result: ToExpression@ FormBox[TemplateBox[{"m", "n"}, "MatrixPower"], "TraditionalForm"] ToExpression@TemplateBox[{"m", "n"}, "MatrixPower"] Consequently, we can alter the MakeBoxes rule above to include StandardForm. Bellow I give a button to use with Method 1. Button[Tooltip[#, "MatrixPower"] &@ "\!\(\*SuperscriptBox[\"\[SelectionPlaceholder]\", \"\[Placeholder]\"]\)", NotebookApply[InputNotebook[], #] &@ TemplateBox[{"\[SelectionPlaceholder]", "\[Placeholder]"}, "MatrixPower", DisplayFunction -> (SuperscriptBox[#1, #2] &), Tooltip -> Automatic]] Note. For $VersionNumber<6, very similar results can be achieved by using InterpretTemplate (an old parent of TemplateBox). ------------------------------------------ Method 2. We can use a simpler rule for MakeBoxes: MakeBoxes[MatrixPower[m_, n_], TraditionalForm] := With[{fm = TraditionalForm}, TemplateBox[{Parenthesize[m, fm, Power, Left], MakeBoxes[n, fm]}, "MatrixPower"]] Notice that this time we gave no formatting rule to TemplateBox. As an example, try this: RawBoxes@TemplateBox[{"m", "n"}, "MatrixPower"] The pink rectangle indicates that a display function is missing. (The default value of DisplayFunction is None) However, a TemplateBox is still present, so the returned expression is interpreted as expected. Now, paste the following cell on the stylesheet of your notebook and see what happens: Cell[StyleData["MatrixPower"], TemplateBoxOptions -> {DisplayFunction -> (SuperscriptBox[#1, #2] &), Tooltip -> Automatic}] // CellPrint With the appearance defined in the stylesheet, one can use the following simpler button: Button[Tooltip[#, "MatrixPower"] &@ "\!\(\*SuperscriptBox[\"\[SelectionPlaceholder]\", \"\[Placeholder]\"]\)", NotebookApply[InputNotebook[], #] &@ TemplateBox[{"\[SelectionPlaceholder]", "\[Placeholder]"}, "MatrixPower"]] ------------------------------------------ Carlos Cesar de Araujo Gregos & Troianos Educacional www.gregosetroianos.mat.br Belo Horizonte, MG, Brasil (31) 3283-1122