# Formatting derivatives in traditional output

This is related to this question, but goes a bit further. I have also seen Vitalyi's Blog Post on the topic. I, on the other hand, have used a modified version of Pekka Janhunen's DFormat package to provide a more "traditional" appearance for derivatives. I think the package has advantages over both of the solutions provided at the first two links above, but perhaps the advantages/disadvantages can be combined to get the best of all worlds. One advantage of the code shown in Jens' post is that it provides a different symbol for ordinary derivatives (of functions of a single variable) versus partial ones.

The DFormat code I am using right now is as follows:

BeginPackage["PJDFormat"]

DFormat::usage =
"DFormat lets you control the appearance of partial derivatives
within expressions. DFormat[On] turns formatting on, DFormat[NoVars]
turns formatting on but with no variables explicitly displayed,
DFormat[Off] restores the original Mathematica behavior.

NoVars::usage ="DFormat[NoVars] sets derivative formatting on
but with no variables."

Begin["Private"]

dsign = If[\$System === "Macintosh", "6", "\[PartialD]", "\[PartialD]"]

denomList[nn_List,vars_List]:=
Block[{i,result = {}},
Do[Which[
TrueQ[nn[[i]]==0], Null,
TrueQ[nn[[i]]==1], result = Join[result,{dsign,vars[[i]]}],
True, result = Join[result,{dsign,Superscript[vars[[i]],nn[[i]]]}] ],
{i,1,Length[nn]} ];
result]

DFormat[Off]:=
(Unprotect[Derivative];
Format[Literal[Derivative[nn__][ff_][vars:(_Symbol ..)]]]=.;
Protect[Derivative])

DFormat[On]:=
(Unprotect[Derivative];
Format[Literal[Derivative[nn__][ff_][vars:(_Symbol ..)]]]:=
If[TrueQ[Plus@@{nn} == 1],
SequenceForm[dsign,ff[vars]],
SequenceForm[Superscript[dsign,Plus@@{nn}],ff[vars]]]/
SequenceForm @@ denomList[{nn},{vars}];
Protect[Derivative])

DFormat[NoVars]:=
(Unprotect[Derivative];
Format[Literal[Derivative[nn__][ff_][vars:(_Symbol ..)]]]:=
If[TrueQ[Plus@@{nn} == 1],
SequenceForm[dsign,ff(*[vars]*)],
SequenceForm[Superscript[dsign,Plus@@{nn}],ff(*[vars]*)]]/
SequenceForm @@ denomList[{nn},{vars}];
Protect[Derivative])

End[]

EndPackage[]

DFormat[On]

Null


This works fine for what it does, with one exception: If I have a derivative of a function of a single variable, I would like to see the DifferentialD operator instead of the PartialD one that is used with this package. I realize that this may be possible with a near-trivial change of the code below, but I just haven't been able to get this to work. There's also some minor quibbles with the typesetting of expressions such as

D[f[x, y], {x, 2}, {y, 3}]


which gives a TraditionalForm output that looks like so:

If you are fussy about things like that you will notice that the second PartialD in the denominator is a bit too close to the x^2, and a bit too far from the y^3. One might also want the PartialD^5 in the numerator a bit closer to the f there.