# Can highlight an expression in mathematica in an expansion?

I ask mathematica to make an expansion of some expression. Is there a way to ask mathematica to highlight all terms that, say, goes by x^3 since it is really hard to find them? E.g.

Expand[(x + y) (x + z) (x + w)]


gives

w x^2 + x^3 + w x y + x^2 y + w x z + x^2 z + w y z + x y z


and the question is how can I highlight or isolate all terms that go as x^3?

• Does Collect[...,x^3] do what you're looking for? I will at least group all the x^3 terms together Apr 28, 2016 at 11:58
• Seems to help, indeed. But, it would be great if there was a way to highlight stuff not look line by line.. Apr 28, 2016 at 12:21

One way could be using a Style.

Expand[(x + y) (x + z) (x + w)] /. {x^2 ->
Style[x^2, Red, Background -> LightBlue]} Try this:

    expr = Expand[(x + y) (x + z) (x + w)];
rule[x_] := x -> Style[x, Background -> Yellow];


Then you may apply the rule to the expression as follows

expr /. rule[x^3]


with the effect or

expr /. rule[x^2]


giving but the most simple substitution

expr /. rule[x]


does not work, since it highlights all terms containing x. But this might be no problem for your aims.

Have fun!

Just an extension to Alexei Boulbitch's answer: if you want to highlight x, you can change the function to:

Clear[rule];
expr = Expand[(x + y) (x + z) (x + w)];
rule[x_] := {Power[x, y_] :> Power[x, y],
x :> Style[x, Background -> Yellow]} 