6

I've tried searching here and in the WRI documentation, but I haven't found what I'm looking for in either place.

I'm defining a dx operator to act as a shorthand for D[#,x], based on this question. In my case, I want Mathematica to produce an output form with the text d/dx expression = [...], where by [...] I mean the actual symbolic meaning of my dx operator (i.e., D[#, x]).

I have tried this:

Format[dx[a_] := "\!\(\*FractionBox[\(d\), \(dx\)]\)"  a  "="  D[a, x]]

I've also tried

Format[dx[#], TraditionalForm] := 
  DisplayForm[
    RowBox[{FractionBox["\DifferentialD]", "\[DifferentialD]x"],
    #, "=", D[#, x]}]];

and various other similar things, none of which produce the correct expression.

I would appreciate help with getting this right.

Trevor
  • 237
  • 1
  • 4

1 Answers1

9

You can use something like this

dx /: MakeBoxes[dx[a_], fmt_] := 
  RowBox[{FractionBox["\[PartialD]", "\[PartialD]x"], 
    MakeBoxes[a, fmt], "=", MakeBoxes[#, fmt] &@D[a, x]}];

dx[Sin[x]]

enter image description here

dx[Sin[x]] // TraditionalForm

enter image description here

I prefer MakeBoxes but it also can be implemented with Format

ClearAll[dx]

Format[dx[a_]] := 
  DisplayForm@RowBox[{FractionBox["\[PartialD]", "\[PartialD]x"], MakeBoxes[a], 
     "=", MakeBoxes[#] &@D[a, x]}];

dx[Sin[x]]

enter image description here

ybeltukov
  • 43,673
  • 5
  • 108
  • 212
  • What does the MakeBoxes[#] refer to? Does it refer to the result supplied by the later @D function? – Trevor Oct 07 '13 at 13:33
  • 1
    @Trevor MakeBoxes[#]&@D[a, x] inserts evaluated D[a, x] into MakeBoxes. Evaluate doesn't work here because MakeBoxes has HoldAllComplete attribute. – ybeltukov Oct 07 '13 at 13:40
  • Thanks a lot. I will learn your answer. It has great value for me. – Trevor Oct 07 '13 at 14:10