Custom formatting of your output

In many instances it is desirable to have your output in a particular format to make it easier for your audience to read. Getting the "textbook standard form" for partial derivatives provides an example. This is what Mathematica returns by default.

D[U[a, b], a]

Reformatting the output

Using the Format  command we can change the appearance of our output without changing the internal form. This is important if we want to use our output in subsequent calculations. Here we give the steps for formatting first and second partial derivatives.

We begin by Unprotecting D, the differential operator.

Unprotect[D];

Next we assign the desired formats. Note that we need to do this for each individual form we want to reformat. We use StringForm where the order of the variables is important.

Format[D[f_[x_, y_], x_]] :=  (Subscript[f, x])[x, y];
Format[D[f_[x_, y_], y_]] :=  (Subscript[f, y])[x, y];
Format[D[f_[x_, y_], x_, y_]] :=  
        (Subscript[f, StringForm["`` ``", x, y]])[x, y];
Format[D[f_[x_, y_], y_, x_]] :=  
        (Subscript[f, StringForm["`` ``", y, x]])[x, 
          y];
Format[D[f_[x_, y_], y_, y_]] :=  
      (Subscript[f, StringForm["``", (y^2)]])[x, y]

When we have made all the formatting assignments we want we Protect D to keep it from being inadvertently altered.

Protect[D];

Some examples of our formatting.

D[f[x, y], x]

fprime[x_, y_] = D[f[x, y], x]

D[fprime[x, y], y]

D[D[f[x, y], x], y]

D[g[r, s], s, r]

D[g[s, t], {t, 2}]

Clear[x, y, r, s, f, g]