6
$\begingroup$

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.

$\endgroup$
9
$\begingroup$

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.