Skip to main content
added 921 characters in body
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k
formula[{a_, b_, c_, d_}] := Defer[(a - b) + (c - d)] formula[{1, 2, 3, 4}] 
(1 - 2) + (3 - 4) 

Addressing your comment below the question, Defer (also HoldForm) does work, but the output formatting engine is changing the appearance of your result. You can see this by using InputForm:

Defer[1 * 2 * 1 * 31] // InputForm 
Defer[1*2*1*31] 

The hold functions, even HoldComplete, do not prevent the formatting engine form going to work:

HoldComplete[1 * 2 * 1 * 31] 

Mathematica graphics

You need to attack the problem at its source, by Blocking Times during Box creation:

SetAttributes[defer, HoldAll] MakeBoxes[defer[args__], fmt_] := Block[{Times}, MakeBoxes[Defer[args], fmt] ] 

You can now use defer as you would Defer but Times will not be formatted:

formula[{a_, b_, c_, d_}] := defer[a*b*c*d] formula[{1, 2, 1, 31}] 
1 * 2 * 1 * 31 
formula[{a_, b_, c_, d_}] := Defer[(a - b) + (c - d)] formula[{1, 2, 3, 4}] 
(1 - 2) + (3 - 4) 
formula[{a_, b_, c_, d_}] := Defer[(a - b) + (c - d)] formula[{1, 2, 3, 4}] 
(1 - 2) + (3 - 4) 

Addressing your comment below the question, Defer (also HoldForm) does work, but the output formatting engine is changing the appearance of your result. You can see this by using InputForm:

Defer[1 * 2 * 1 * 31] // InputForm 
Defer[1*2*1*31] 

The hold functions, even HoldComplete, do not prevent the formatting engine form going to work:

HoldComplete[1 * 2 * 1 * 31] 

Mathematica graphics

You need to attack the problem at its source, by Blocking Times during Box creation:

SetAttributes[defer, HoldAll] MakeBoxes[defer[args__], fmt_] := Block[{Times}, MakeBoxes[Defer[args], fmt] ] 

You can now use defer as you would Defer but Times will not be formatted:

formula[{a_, b_, c_, d_}] := defer[a*b*c*d] formula[{1, 2, 1, 31}] 
1 * 2 * 1 * 31 
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k

formula[{a_, b_, c_, d_}] := Defer[(a - b) + (c - d)] formula[{1, 2, 3, 4}] 
(1 - 2) + (3 - 4)