2
$\begingroup$

The output of foo=Exp[x] is formatted automatically as $e^x$. For long expressions it's not as easy to read as Exp[...]. How to format the output so that $e^{something}$ shows up in function-call form as Exp[something]? I know about HoldForm[] but I want to transform the output only without touching previous code.

$\endgroup$
  • 1
    $\begingroup$ InputForm[expr] uses E^..., may be you can try InputForm? For example, screen shot: !Mathematica graphics $\endgroup$ – Nasser Dec 31 '14 at 2:40
  • $\begingroup$ The FullForm of all of these is the same, which is Power[E,argument]. So the differences are in screen presentation. To be able to do what you want, my guess is one must go to lower level and use MakeBoxes and such to force the screen display to Exp[..], which is out of my level of skill. May be someone will be able to do this. $\endgroup$ – Nasser Dec 31 '14 at 3:16
4
$\begingroup$

As Nasser already pointed out that exponential expressions are converted into powers. You can find the corresponding documentation in the Possible Issues section of Exp

When you know this, than you can circumvent the issue by holding your expression and replacing all exponential powers to Exp[..] again. Therefore, a simple solution is

FormatExp[expr_] := HoldForm[expr] /. HoldPattern[Power[E, arg_]] :> Exp[arg]

and then you go

FormatExp[Series[(1 + z/n)^n, {n, Infinity, 2}]]

Mathematica graphics

The good thing is that the displayed expressions are still functional because there is only a HoldForm wrapped around which prevents evaluation. Therefore, if you wanted to use such a displayed result, you can use ReleaseHold

Normal[ReleaseHold[%]]

There is another alternative. I wasn't sure whether to show it but let's give it a try. As you might know we have Format which can be used to set formatting rules to symbols. The problem here is, that we need to assign a special formatting rule to Power which is a protected built-in symbol. Therefore, use it with caution as I can not predict whether this breaks something.

Unprotect[Power];
Format[HoldPattern[Power[E, arg_]]] := DisplayForm[RowBox[{"Exp", "[", arg, "]"}]];
Protect[Power];

Now, every Exp is displayed per default as Exp[..] and everything should work as expected.

$\endgroup$
  • $\begingroup$ The first solution is excellent: at the last step where one needs to display some result, just use ......//FormatExp. $\endgroup$ – egwene sedai Dec 31 '14 at 5:21
  • 1
    $\begingroup$ @david You could replace HoldForm with Defer which would allow reuse without even ReleaseHold. Also an alternative to the second method that does not require unprotecting Power: MakeBoxes[E^arg_, form_] := RowBox[{"Exp", "[", ToBoxes[arg, form], "]"}] $\endgroup$ – Mr.Wizard Dec 31 '14 at 7:29
  • $\begingroup$ @Mr.Wizard Unbelievable, I had already added the MakeBoxes method (infact by up-valueing it) but I simply forgot to save the edit-edit :-( I guess it simply was too late and I was already asleep. $\endgroup$ – halirutan Dec 31 '14 at 9:52
  • $\begingroup$ @Mr.Wizard How about adding a LeafCount conditional to MakeBoxes as in: MakeBoxes[E^arg_, form_] := RowBox[{"Exp", "[", ToBoxes[arg, form], "]"}] /; LeafCount[arg] >= 10. Then E^Sqrt[x] formats differently than E^Sqrt[x^2+y^2]. Would you say this is objectionable? $\endgroup$ – QuantumDot Dec 31 '14 at 20:57
  • $\begingroup$ @QuantumDot I do not see why that would be objectionable. $\endgroup$ – Mr.Wizard Dec 31 '14 at 23:51

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.