Re: Use a nicer Format for differences
- To: mathgroup at smc.vnet.net
- Subject: [mg6496] Re: Use a nicer Format for differences
- From: soiffer (Neil Soiffer)
- Date: Thu, 27 Mar 1997 02:42:24 -0500 (EST)
- Organization: Wolfram Research, Inc.
- Sender: owner-wri-mathgroup at wolfram.com
In article <5gqk1u$93s at smc.vnet.net> Ersek_Ted%PAX1A at mr.nawcad.navy.mil writes: > >If you have a difference of two terms Mma presents them so they are in >standard order. Consider the following: > >In[1]:= y-d > >Out[1]= -d+y > >It puts "-d" before "y", and most would find this unapealing. >You can get the nicer result by using TraditionalForm. > >In[2]:= y-d//TraditionalForm > >Out[2]=y-d > > ---------------------------------------------------------------------------- > ----------------------- >You can also get the nicer result by writing your own Format. >Dave Withoff at WRI helped me get this working. >However, he hasn't seen my latest version. > >In[3]:=Unprotect[Plus]; > > Format[(n_Real | n_Integer | n_Rational)*a_.+b_/; > (Head[b]=!=Plus)&&(n<0)&&OrderedQ[{n*a,b}]]:= > SequenceForm[b,"-",-n*a] > > Protect[Plus]; > > ---------------------------------------------------------------------------- > ---------------------- >After entering In[3] any of the following are displayed as a difference in >Out[n]. > y-d > y-3 d > y-2/3 d > y-1.25 d > y-2/3 > y-a b > >Also this format is used when the difference is inside another function. >As in: 1/Log[y-3 d] > >However, this format is not used when more that two terms are added / >subtracted. >This is the way I wanted it. Dave Withoff's solution is simple and correct. However, it has the drawback in V3.0 of using an InterpretationBox for the output. This is not a good thing in general because it means what is displayed is not necessarily what the meaning is when evaluated. In V3.0, it usually better to use MakeBoxes[]. Here is Dave's suggestion rewritten using MakeBoxes[]: MakeBoxes[(n_Real | n_Integer | n_Rational)*a_.+b_/; (Head[b]=!=Plus)&&(n<0)&&OrderedQ[{n*a,b}],form_]:= RowBox[{ MakeBoxes[b,form], "-", Apply[MakeBoxes,{-n*a,form}] }]; MakeBoxes[] will produce an output that is editable and can be used as input without hidden meaning. There are two important things to notice: 1. You need to apply MakeBoxes recursively to its arguments 2. MakeBoxes is HoldAllComplete. This means you need to use Apply instead of the simpler MakeBoxes[-n*a,form] so that the '-n' evaluates. >Eventually I will have the above Format used automatically as soon as Mma is >opened. However, I have yet to figure out that part. Put this in your init.m file and this function will take effect whenever you run your kernel. Neil Soiffer Wolfram Research