MathGroup Archive 1998

[Date Index] [Thread Index] [Author Index]

Search the Archive

Formatting Expressions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg13937] Formatting Expressions
  • From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
  • Date: Tue, 8 Sep 1998 02:52:34 -0400
  • Sender: owner-wri-mathgroup at wolfram.com

Evaluate Clear[n]; then 
1.0(5^n)  will produce an output that looks a bit like (1.5)^n.
1.0(5.2^n) will produce an output might be mistaken for 1.5(.2^n).
2(5.2^n) will produce an output that be mistake for 25(.2^n).

Well that's what I think.  I don't like to depend on the little space to
convey the meaning of expressions except for multiplication of
variables.  I don't mind using (x y) to indicate the product of (x) and
(y) because I won't use the variables (x), (y), and (xy) in the same
expression.  Anyone who does is looking for trouble.

Also I prefer to have the difference of two terms  displayed as a
difference.  For example  I prefer to have (b-a) displayed as  (b-a)
not (-a+b).  TraditionalForm does this, but you need special rules to
make StandardForm do this.

In the line below I give formatting rules that I find much better.  Of
course the saying about beauty in the eye of the beholder applies here.
Several months ago I posted similar rules, but I find these much
better.

Cheers,
Ted Ersek
  
(***********************************) (*** Use better format rules for
expressions with Head Times.  ****)

Module[{wasprotected=Unprotect[Plus,Times,MakeBoxes]},(

  MakeBoxes[c_Integer*b_Real^expon_,form_]:=
   RowBox[{MakeBoxes[c,form],"(",MakeBoxes[b^expon,form],")"}]/;
   (c=!=-1)&&(expon=!=1/2)&&(expon=!=-1/2);

  MakeBoxes[c_Integer*b_Integer^expon_,form_]:=
   RowBox[{MakeBoxes[c,form],"(",MakeBoxes[b^expon,form],")"}]/;
   Not[TrueQ[Negative at expon]]&&(c=!=-1)&&(expon=!=1/2);

  MakeBoxes[1.` *b_^expon_,form_]/;
    MachineNumberQ[Re[b]]||MachineNumberQ[Re[expon]]||
    MachineNumberQ[Im[b]]||MachineNumberQ[Im[expon]]:=
    MakeBoxes[b^expon,form];

  MakeBoxes[-1.` *b_^expon_,form_]/;
    MachineNumberQ[Re[b]]||MachineNumberQ[Re[expon]]||
    MachineNumberQ[Im[b]]||MachineNumberQ[Im[expon]]:=
    RowBox[{"-",MakeBoxes[b^expon,form]}];

  MakeBoxes[c_Real*b_Integer^expon_,form_]:=
    RowBox[{MakeBoxes[c,form],"(",MakeBoxes[b^expon,form],")"}]/;
    Not[TrueQ[Negative at expon]]&&(expon=!=1/2);

  MakeBoxes[c_Complex*b_Integer^expon_,form_]:=
    RowBox[{"(",MakeBoxes[c,form],")",MakeBoxes[b^expon,form]}]/;
    (expon=!=1/2);

  MakeBoxes[c_Real*b_Real^expon_,form_]:=
    RowBox[{MakeBoxes[c,form],"(",MakeBoxes[b^expon,form],")"}]/;
    (expon=!=1/2);

  MakeBoxes[c_Complex*b_Real^expon_,form_]:=
    RowBox[{"(",MakeBoxes[c,form],")",MakeBoxes[b^expon,form]}]/;
    (expon=!=1/2);

  (** Ensure a difference is displayed as a difference **)

  MakeBoxes[(n_Integer|n_Rational|n_Real)a_.+ b_,form_]/;
    (Head[b]=!=Plus)&&(n<0)&&(Head[b]=!=Complex)&&(Head[a]=!=Complex):=
    RowBox[{MakeBoxes[b, form],"-", MakeBoxes @@ {-n*a, form}}];

  MakeBoxes[(z_Complex)a_.+ b_,form_]/;
    (Head[b]=!=Plus)&&(Im[z]<0)&&(Re[z]===0)&&(Head[b]=!=Complex):=
    RowBox[{MakeBoxes[b, form],"-", MakeBoxes @@ {-z*a, form}}];

  (** Consolidate the above formatting rules under MakeBoxes.  **)
  (** Also return protection to Plus and Times as appropriate. **)

  FormatValues[MakeBoxes]=Join[
    FormatValues[Times],    
    FormatValues[MakeBoxes],
    FormatValues[Plus]
  ];

  FormatValues[Plus]={};
  FormatValues[Times]={};
  Protect@@wasprotected; )
]

(** Some personal preferences **)
SetAttributes[MakeBoxes,{ReadProtected, Protected}];



  • Prev by Date: Re: Simple but difficult integral ? Help ..!??
  • Next by Date: RE: Fourier transform
  • Previous by thread: Re: Integration Problems
  • Next by thread: DC/DC-Converter