Suppose I have a simple dot product function DP that I want to display nicely, I might use e.g.

Format[DP[x,y],StandardForm]:= (x\[CenterDot]y)

If x == y, then I'll have a different rule:

Format[DP[x,x],StandardForm]:= x^2

How can I have a rule that sets DP[x,x]^n = x^(2n)? I've tried

Format[DP[x,x]^(n_.),StandardForm]:= x^(2n) but I get hit with an error:

SetDelayed::write: Tag Power in MakeBoxes[(x^2)^n_.,StandardForm] is Protected.



1 Answer 1

Format[DP[x_, y_], StandardForm] := (x·y)
Format[DP[x_, x_], StandardForm] := x^2

Format[DP[x_, x_]^(n_), StandardForm] := x^(2 n)

DP[a, b]
(* a·b *)

DP[a, a]
(* a^2 *)

DP[b, b]^6
(* b^12 *)
  • 2
    $\begingroup$ Your second example produces (a\[CenterDot]a) on my system (v13.2.1 on a Mac). Format[DP[x_, x_], StandardForm] := x^2 is also needed. $\endgroup$
    – Bob Hanlon
    Mar 23, 2023 at 14:20
  • 1
    $\begingroup$ @BobHanlon, you're right, thanks! I had not properly cleaned the definitions from testing :) $\endgroup$
    – Domen
    Mar 23, 2023 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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