1
$\begingroup$

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.

$\endgroup$
2
$\begingroup$

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]

enter image description here

$\endgroup$
  • $\begingroup$ thanks! on my mathematica ver 11.0, Parenthesize is missing from the documentation, and the frontend thinks Parenthesize is missing the fourth argument "group" and shows a highlight with red "^" for the Parenthesize fucntion, but the code runs without problem. $\endgroup$ – egwene sedai Oct 31 '17 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 to TemplateBox, but of course it's super convenient. $\endgroup$ – b3m2a1 Oct 31 '17 at 23:31

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.