You raise a very good question. One way to do that is as follows. Let us hold for a moment a part of your expression:
expr1 = Hold[x + 2 x*y] - x*y + y;
Now one may apply Collect to it to extract y out of parentheses in the part that is not held, and after that release hold:
expr2 = Collect[expr1, y] // ReleaseHold
The result is here:
(* x + (1 - x) y + 2 x y *)
Now we need to protect the expression (1 - x) y from simplification with the rest of expression. This term is second:
expr2[[2]]
(* (1 - x) y *)
We can map Hold onto it, Collect terms ~x and finally release hold:
expr3 = Collect[MapAt[Hold, expr2, {2}], x] // ReleaseHold
That is the result:
(* (1 - x) y + x (1 + 2 y) *)
However, there is a subpackage "Manipulations" of the package "Presentations" to be used as the Mathematica add-on of David Park that does such things easier. Check here: http://home.comcast.net/~djmpark/index.html
To address your question placed in the comment.
These expressions I will use several times:
Clear[expr1, expr2, expr3, expr4, expr3A, expr3B, expr4A, expr4B,
expr5, expr5A, expr5B];
Assume we have the expression you are asking about:
expr1 = x + x*y + y;
and we know that we want to represent it in the form: (1-x)y+x(1+2 y). All this only has sense, if this expression is a part of some more complex expression, and we want to operate it completely on-screen without going to the paper. If it is a self-standing expression, as simple as expr1, it is, of course, more easy to retype it in the final form.
So, let us start. There are several ways.
- Using Hold-Collect approach
This makes the expression like the one you want, but prevents Mathematica to simplify it:
expr2 = expr1 + x*y - Hold[x*y]
The results is:
(* x + y + 2 x y - Hold[x y] *)
Then you can factor the part that is not held by application Collect to the whole expression:
expr3 = Collect[expr2, x] // ReleaseHold
the result is:
(* y - x y + x (1 + 2 y) *)
Like that we factored one part of the expression, and released the held part. Now we can hold the already factored part and then factor the rest (as in the previous example):
expr4 = MapAt[Hold, expr3, {3}]
expr5 = Collect[expr4, y] // ReleaseHold
The result is
(* y - x y + Hold[x (1 + 2 y)] )
( (1 - x) y + x (1 + 2 y) *)
Using rules
Clear[expr1, expr2, expr3, expr4, expr3A, expr3B, expr4A, expr4B,
expr5, expr5A, expr5B];
expr1 = x + x*y + y;
expr2 = expr1 /. x + x*y + z__ -> Hold[x + 2*x*y] - x*y + z
(I ma sorry, I tried hard to transform few lines above into a code, but it does not work)
Like this we effectively add and subtract the term x*y to the expression. The result is:
(* y - x y + Hold[x + 2 x y] *)
expr3 = Collect[expr2, y] // ReleaseHold
(* x + (1 - x) y + 2 x y *)
Now we have two possible ways.
The way a) is to follow the rule-based approach:
expr4A = expr3 /. x + 2 x*y + z__ -> x (1 + 2 y) + z
The result is:
(* (1 - x) y + x (1 + 2 y) *)
Done.
The way b) is to further follow the Hold-Collect approach:
expr3 = Collect[expr2, y] // ReleaseHold
(* x + (1 - x) y + 2 x y *)
and
expr4A = Collect[MapAt[Hold, expr3, {2}], x] // ReleaseHold
(* (1 - x) y + x (1 + 2 y) *)
Done.
3) The list-based approach. The idea is to first transform your expression into list. The list terms will not be simplified without your permission. You operate this list, and later transform it back to a sum.
Clear[expr1, expr2, expr3, expr4, expr3A, expr3B, expr4A, expr4B,
expr5, expr5A, expr5B];
expr1 = x + x*y + y;
The following transforms the expression into a list:
expr2 = List @@ expr1
(* {x, y, x y} *)
Now we need to effectively add and subtract the term x*y. Let us insert -x*y close to the first term, x, and x*y as a last term, x*y, so that later it is more convenient to group terms x with -x*y and y with x*y and another x*y:
expr3 = Insert[Insert[expr2, -x*y, {2}], x*y, {5}]
(* {x, -x y, y, x y, x y} *)
Now we can take the terms which will become the sub-expressions in question and can operate them separately:
expr4A = Take[expr3, 2]
expr4B = Take[expr3, -3]
(* {x, -x y} *)
(* {y, x y, x y} *)
Now we can transform each of them into sums, and then factorize them. In this case one may equally factorize either with Collect or with Factor. Let us do it with Factor:
expr5A = Plus @@ expr4A // Factor
expr5B = Plus @@ expr4B // Factor
(* -x (-1 + y) *)
(* (1 + 2 x) y *)
Now we already almost have it. The problem is now to add one to another and to avoid simplification. Here again Hold may be used:
expr6 = (Hold@expr5A + Hold@expr5B) // ReleaseHold
(* x (1 - y) + (1 + 2 x) y *)
Done.
Just to conclude, there are probably other approaches, I just gave those that immediately came into head. These operations seem a bit cumbersome at the first glance, but I know by experience, that when doing complex and long analytical calculations it pays off, since you avoid small but malignant errors, like forgotten minuses, or extra factors 2 and alike.
Have fun!