Extract unevaluated definition of function

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 := f[x_]:=a x + b
StringForm["f(x)=",ExpandOnce[f]]
Out:= 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.

• Try ToString[DownValues[f][] /. HoldPattern[lhs_ :> rhs_] :> HoldForm[rhs]]. – J. M. will be back soon Feb 25 at 8:24
• That seem to work beautifully! Want to make it an answer? – Andreas Storvik Strauman Feb 25 at 8:30
• Alternatively: ToString[HoldForm[f[x]]/.DownValues[f]] – Lukas Lang Feb 25 at 10:35

More general than going through the DownValues would be to Trace the evaluation and look only at the first step,

f[x_] := a x + b
StringForm["f(x)=", Trace[f[x]][]]

f(x)=a x + b

Of course if Trace[f[x]] is a huge affair, then this becomes very inefficient. Maybe the option TraceDepth -> 1 will help here:

f[x_] := a x + b
StringForm["f(x)=", Trace[f[x], TraceDepth -> 1][]]

f(x)=a x + b

For the % symbol the trace is longer, involving the lookup in the Out database. We must therefore go to the fourth evaluation step,

2 + 91
(* 93 *)
StringForm["%=", Trace[%][]]

%=93

Assuming that f has only 1 downvalue, then you can use:

ExpandOnce[f] := Replace[
DownValues[f][],
_[_, rhs_] :> HoldForm[rhs]
]

Clear[f]
f[x_] := a x + b
StringForm["f(x)=", ExpandOnce[f]]

f(x)=a x+b

Note that contrary to the answers in the comments, I don't use ToString in the definition of ExpandOnce. I think using ToString is usually a bad idea, as something like the following can happen:

Clear[f]
f[x_] := 10^-7 x + 1/2
StringForm["f(x)=", ToString @ ExpandOnce[f]]
f(x)= x    1
--- + -
7   2
10