MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Formatting MatrixPower in TraditionalForm

Inverse has a 2D formatting in TraditionalForm, but MatrixPower does not. 



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: 

 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]},

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: 

  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
Belo Horizonte, MG, Brasil
(31) 3283-1122

  • Prev by Date: Re: Re: List of position-specifying functions
  • Next by Date: Re: Generalized Fourier localization theorem?
  • Previous by thread: Re: Lisp Macros in Mathematica (Re: If Scheme is so good why MIT
  • Next by thread: How to get plot Exclusions for a numerical function?