Derivatives Pretty Printing

I looked for a solution to print derivatives using Traditional form that lok the same as the standard Matematical notation. Despite some
good resources, in general they were based on old Mathematica versions and do not explored fully the new Wolfram languages features. Since this might be useful to others struggling with the same subject. I am posting a solution, of course it will be nice if someone could improve it, or present a more
general solution.

The idea was to define a set of substitution rules that could transform a TraditionalForm derivative
in something easier to read.

generateD[f_, vars_, order_] :=
 
 Module[{iterators, args, args2, funcvar},
  funcvar[lis_] := Select[lis, #[[2]] != 0 &] /. {x_, 1} -> x;
  iterators = 
   Sequence @@ 
    Table[{ToExpression["n" <> ToString[i]], 0, order}, {i, 1, 
      Length[vars]}];
  
  args = Table[ToExpression["n" <> ToString[i]], {i, 1, Length[vars]}];
  args2 = Table[{vars[[i]], args[[i]]}, {i, 1, Length[vars]}];
  Rest[Flatten[
    Table[Rule[Derivative[Sequence @@ args][f][Sequence @@ vars], 
      Inactivate[D[f[Sequence @@ vars], Sequence @@ funcvar[args2]], 
       D]], Evaluate[iterators]]]]]

Usage:

eq = D[f[r,q,z],{r,2},z,q];

lissubs = generateD[f, {r, q, z}, 2];

TraditionalForm[eq /. lissubs]

It will be nice if that could be done in a more general way, detecting the derivative expressions and applying the transformative rules.

fyi, there is also a blog post by Vitaliy Kaurov which has a function that does the same:

mathematica-qa-series-converting-to-conventional-mathematical-typesetting/

Here is the function pdConv applied to your equation, which gives similar output as your function lissubs

pdConv[f_] := 
 TraditionalForm[f /. Derivative[inds__][g_][vars__] :> 
    Apply[Defer[D[g[vars], ##]] &, Transpose[{{vars}, {inds}}] /. {{var_, 0} 
        :> Sequence[], {var_, 1} :> {var}}]]

1 Like

Hi Nasser,

I rediscover basically the same solution:

DerivativeForm[expr_] := Module[{funcvar, lisubs},
  funcvar[n_][var_] := 
   Select[Flatten[Transpose[{var, n}], {1}], #[[2]] != 0 &] /. {x_, 
      1} -> x;
  lissubs = 
   Derivative[n__][f_][var__] :> 
    Inactivate[D[f[var], Sequence @@ funcvar[{n}][{var}]], D];
  TraditionalForm[expr /. lissubs]
  ]

The only diffference now is that I am using Inactivate instead of Defer. I am also defining a Function that transform the expression to Traditional Form.