MathGroup Archive 1997

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

Search the Archive

Re: Use a nicer Format for differences

In article <5gqk1u$93s at> Ersek_Ted%PAX1A at 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
> ----------------------------------------------------------------------------  
> -----------------------
>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.
>             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 
>   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 / 
>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_/;

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

  • Prev by Date: coupled ODE system: won't solve [?]
  • Next by Date: Re: Lost greek letter list! Where is it?
  • Previous by thread: Use a nicer Format for differences
  • Next by thread: fonts