Infix[] works only at first level:
Infix[(c a^b)^d]
(*
-> (a^b c) ~Power~ d
*)
As I want to (don’t ask why) get the full expression switched to infix notation, I tried something like:
SetAttributes[toInfx, HoldAll];
toInfx[expr_] := Module[{prfx, infx},
prfx = Level[expr, {0, Infinity}];
infx = Infix /@ prfx /. {Infix[a_Symbol] -> a, Infix[a_?NumericQ] -> a};
Fold[ReplaceAll[#1, #2] &, expr, Reverse@Thread[Rule[prfx, infx]]]
]
k = toInfx[(c a^b)^d]
(*
-> (c ~Times~ (a ~Power~ b)) ~Power~ d
*)
But this has two evident problems, because
(c a^b)^d == a~Power~b~Times~c~Power~d
So what I get is not an efficient use of infix.- It is not robust, and fails for easy expressions such as
k = toInfx[a/b + ArcTan[a/b]]
Is there an easy way to get Infix[] working for All (leaves)?
Here is one way:
I used
HoldFormsince you may want the code to remain unevaluated. Here is an example:EDIT
and,
End EDIT
As to the superfluous parentheses, it is harder to remove them since often they are indeed needed due to precedence of various operators, but should be possible.
EDIT 2
To take care of precedence, here is an attempt:
Now, I get: