For some complex expressions, I want to display the Conjugate[x]
as $x^*$ (keyboard input as esc-conj-esc) to make the expression cleaner. I can get the desired display with TraditionalForm[...]
but I would like to make the expressions easy to copy-paste for part of the calculation later and TraditionalForm[...]
transforms function foo[x]
to foo(x)
which may be problematic. /. {Conjugate[x] -> x\[Conjugate]}
leave the expression untouched. I guess I need to hold the star-superscript so mathematica does not treat it as Conjugate
when displaying and then unhold the pattern. How can this be done? The documenation for HoldPattern
is not too clear for me.
1 Answer
You could modify the formatting for Conjugate
:
Unprotect[Conjugate];
Conjugate /: MakeBoxes[Conjugate[x_], StandardForm] := TemplateBox[
{Parenthesize[x, StandardForm, Power]},
"Conjugate",
DisplayFunction->(SuperscriptBox[#1,"*"]&)
]
Protect[Conjugate];
I used a TemplateBox
so that copy/paste works as expected. Examples:
Conjugate[x]
Conjugate[a+x]
-
$\begingroup$ thanks! on my mathematica ver 11.0,
Parenthesize
is missing from the documentation, and the frontend thinksParenthesize
is missing the fourth argument "group" and shows a highlight with red "^" for the Parenthesize fucntion, but the code runs without problem. $\endgroup$ Oct 31, 2017 at 23:27 -
$\begingroup$ I've definitely seen more
TemplateBoxes
in your answers than I'd ever seen before. I like it. My mind does not immediately run toTemplateBox
, but of course it's super convenient. $\endgroup$– b3m2a1Oct 31, 2017 at 23:31