# How to define a function deriv[f[x]] such that its output is an unevaluated expression fx=D[f[x],x]?

I want to define a function deriv[f_[x_]] such that when I run deriv[f[x]], the output is an unevaluated expression fx=D[f[x],x].

The following does not work

deriv[f_[x_]] := ToExpression[ToString[f] <> ToString[x] <> "=" <> ToString[D[f[x], x]]]


because the output of deriv[f[x]] is just D[f[x],x], not fx=D[f[x],x].

• = is an infix alias for Set, so when you force your string with ToExpression MMA will then evaluate that expression automatically as it does with all expressions that are not held or in Unevaluated. So really you're getting the output of Set and that's why it's only the RHS of your argument. Try wrapping the downvalue in Unevaluated. Jul 29, 2015 at 15:27
• Are you looking for something like deriv[f_[x_]] := HoldForm[Set[fx, D[f[x], x]]]? Jul 29, 2015 at 15:37
• @hYPotenuser, Defer[] might actually make more sense here; on the other hand, if the input is g[z], I suppose the expected output is gz = … Jul 29, 2015 at 16:33

deriv[f_[x_]] :=
Inactivate[
ToExpression@StringJoin[ToString[f], ToString[x]] = D[f[x], x],
Set | D]


Then:

deriv[kr[x]]


outputs

Inactive[Set][kryx, Inactive[D][kr[yx], yx]]


which displays as

kryx = D[kr[yx], yx]


Maybe this:

deriv[f_[x_]] := Inactive[D][f[x], x]


Then: