Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am making some random algebra equations, and I want to have the ordering be random too, such that if my random equation generator makes x+1, the output stays x+1, without reverting to 1+x.

I have tried ClearAttributes[Plus, Orderless], but it simply did not work - entering x+1 returns 1+x.

The following sort-of works:

Unprotect[Plus];
Format[Plus[a_, b_]] := ToString@a <> " + " <> ToString@b

But it uses Unprotect, converts everything to strings, and also doesn't work when b is a fraction (fractions p/q are represented as p\n--\nq).

Is there a nicer way to do this?

Also, I plan to convert everything to TraditionalForm at the end, so using that to control ordering won't work either.

share|improve this question
    
I found another solution, which is again not that great (as in it is super slow), but at least it gives me the results I want: Rasterize[TraditionalForm[a]]+Rasterize[TraditionalForm[b]] –  VF1 Nov 4 '12 at 19:18
    
What exactly are you trying to do? I see the problem but I don't understand the objective well enough to offer alternatives. Your suggested fix still makes x+1 turn into 1+x, right? –  Rojo Nov 4 '12 at 19:26
1  
@Rojo actually, don't worry about it. I figured it out. plus = Row[{#1, " + ", #2}] &; was what I was looking for: x~plus~1 // TraditionalForm gives exactly what I want. –  VF1 Nov 4 '12 at 19:29
    
Yeah, that would work for 2 arguments, for more you can use Riffle. Row@Riffle[{##}, " + "] &. I would prefer using that as Format to plus more than as an ownvalue. Something like Format[plus[args__]] := Interpretation[HoldForm[Plus[args]], plus[args]] –  Rojo Nov 4 '12 at 19:49
2  
Also, perhaps you would like wrapping your code in Module (or Block, depending on what you are doing) with Plus=plus, so you can use the + symbol at will. Module[{Plus = plus}, x + 1] –  Rojo Nov 4 '12 at 19:50

1 Answer 1

up vote 2 down vote accepted

With the suggested edits from Rojo in the comments above, the following is what answers my question:

plus[args__] := Row[Riffle[{args}, " + "]]

Then, Block[{Plus = plus}, x + 1 + i + 4 + z] // TraditionalForm returns:

enter image description here

share|improve this answer
    
Looks good, +1. The idea of using Interpretation however was so that the output, if used literally as input, would be interpreted as ´plus[x,1,y...]` if you wanted to further operate on that, even though it "looks like" plus. If you care about that then the second argument shouldn't be ´Row[Riffle...` but plus[args]. If you don't care about that then you can do without ´Interpretation´ and use your Row@Riffle or that HoldForm@Plus –  Rojo Nov 6 '12 at 5:53
    
@Rojo Yes - the whole point was just presentation anyway. But do you have any ideas as to why ClearAttributes didn't work? –  VF1 Nov 6 '12 at 6:54
    
sorry for the late response. I was planning on digging into it before answering, but got sidetracked and lazy. So far I hadn't thought of a good reason why, and my lack of humility makes my ignorance default to "bug" –  Rojo Nov 19 '12 at 19:05
1  
Hmmm... what about if he has terms with negative coefficients? –  JohnD Aug 16 '13 at 23:56
    
@JohnD - It's interesting you pointed that out. That issue was actually resolved in another question. A combination of that answer and maybe some tinkering should do the job. –  VF1 Aug 19 '13 at 5:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.