Say I have a function f[x_]:=a x + b
.
Now I want to get the a x + b
part from the function to use in e.g. StringForm
.
This without using HoldForm
or Unevaluated
in the definition of f
That is, I want the function ExpandOnce
:
In[1] := f[x_]:=a x + b
StringForm["f(x)=``",ExpandOnce[f]]
Out[1]:= f(x)=a x + b
The above could work by using the Definition
command, but that also includes the left hand side of the definition. It would also be great if it's not restricted to only definitions, but also any variable that holds an expression. E.g. %
.
Any help appreciated.
I would expect this to be somewhere in this forum, but I've only found nuances of this.
Related links
- Getting unevaluated function expression This answer is very close, but not close enough, to what I would want. (I'm also fine with parsing
Definition
) - How do I evaluate only one step of an expression? Very close.
ToString[DownValues[f][[1]] /. HoldPattern[lhs_ :> rhs_] :> HoldForm[rhs]]
. $\endgroup$ToString[HoldForm[f[x]]/.DownValues[f]]
$\endgroup$