2
$\begingroup$

Is it possible to change the output of build-in functions?

I would like to change the output for IdentityMatrix[n] to "I", so I tried

Format[IdentityMatrix[n]] := "I"

For comparison I also tried

Format[Sin[x_]] := "Sine(" <> ToString[x] <> ")"

Both result into an error: "Tag IdentityMatrix in IdentityMatrix[n] is Protected."

I guess these functions are protected because they are build-in functions. Is there a way to change the output format of a build-in function?

$\endgroup$
7
$\begingroup$

You can either Unprotect the symbol, or use MakeBoxes instead

MakeBoxes[IdentityMatrix[_], StandardForm] := "I"
MakeBoxes[Sin[x_], StandardForm] := "Sine(" <> ToString[x] <> ")"

Just know that this last option provides a default way of formatting, it doesn't override anything if there already is a rule

$\endgroup$
  • $\begingroup$ The matrix identity thing doesn't work for me in version 9. Do I need to do something else? I guess it evaluates to {{1,0},{0,1},...} before it gets to format it. $\endgroup$ – masterxilo Dec 1 '14 at 13:02

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.