6

I am manipulating partial differential equations symbolically, and would like to get the easily readable form $\rho \frac{\partial v}{\partial t}$, leaving variables implicit.

Based on suggestions from How to make traditional output for derivatives I started out with

Derivative /: 
  MakeBoxes[Derivative[α__][f1_][vars__Symbol], TraditionalForm] := 
    Module[{bb, dd, sp},
      MakeBoxes[dd, _] ^= 
      If[Length[{α}] == 1, "\[DifferentialD]", "∂"];
      MakeBoxes[sp, _] ^= "\[ThinSpace]";
      bb /: MakeBoxes[bb[x__], _] := RowBox[Map[ToBoxes[#] &, {x}]];
      TemplateBox[{ToBoxes[bb[dd^Plus[α], f1]], 
      ToBoxes[Apply[bb, 
        Riffle[Map[bb[dd, #] &, 
        Select[({vars}^{α}), (# =!= 1 &)]], sp]]], 
        ToBoxes[Derivative[α][f1][vars]]}, "ShortFraction", 
        DisplayFunction :> (FractionBox[#1, #2] &), 
        InterpretationFunction :> (#3 &), 
        Tooltip -> Automatic]]

When functions appear outside of partial derivatives, they still appear as $v(t,x)$. Trying to fix this, I tried

supressVariable[f_Symbol] := 
  f /: MakeBoxes[f[t, x], TraditionalForm] :=
    InterpretationBox[ToBoxes[f], f[t, x]]
SetAttributes[supressVariable, Listable]
supressVariable[{v, ρ, p, f}];

This works fine for both

f[t, x] v[t, x] == 0 // TraditionalForm

and

ρ[t, x]*Derivative[0, 1][v][t, x] + 
   v[t, x]*Derivative[0, 1][ρ][t, x] + 
   Derivative[1, 0][ρ][t, x] == 0 // TraditionalForm

producing nicely readable equations. However, the simple

f[t, x] == 0 // TraditionalForm

gives error message

An unknown box name (ToBoxes) was sent as the BoxForm for the expression. Check the format rules for the expression

and I don't know what to do with this. Can anybody help me out?

Åsmund Hj
  • 388
  • 1
  • 8

2 Answers2

5

InterpretationBox holds its arguments (it has HoldAllComplete). You must evaluate ToBoxes[f] outside of this head, easily accomplished with Function as follows:

supressVariable[f_Symbol] := 
  f /: MakeBoxes[f[t, x], TraditionalForm] :=
    InterpretationBox[#, f[t, x]] & @ ToBoxes[f]
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
4

It seems to me that using MakeBoxes in this case is overkill. How about this simpler definition?

supressVariable[f_Symbol] := 
 Format[f[t, x], TraditionalForm] := Interpretation[f, f[t, x]]

SetAttributes[supressVariable, Listable]
supressVariable[{v, ρ, p, f}];

This doesn't encounter the issue you faced, because the symbol f is passed directly to Interpretation (no need to wrap it in ToBoxes at all).

Jens
  • 97,245
  • 7
  • 213
  • 499
  • Be aware that MakeBoxes can have superior behavior to Format in some ways. See this Q&A: (4112299) – Mr.Wizard Aug 25 '14 at 19:06
  • @Mr.Wizard Are you saying that there is anything wrong with Format in the context of this question? Note that the question is only about formatting for TraditionalForm. – Jens Aug 25 '14 at 20:39
  • You're right; since TraditionalForm is not for editing anyway I guess my concern is baseless. I merely got in the habit of using MakeBoxes preferentially to Format most of the time. +1 – Mr.Wizard Aug 25 '14 at 21:07