2
$\begingroup$

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?

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

3 Answers 3

3
$\begingroup$

One way could be using a Style.

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

enter image description here

$\endgroup$
3
$\begingroup$

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

enter image description here

or

expr /. rule[x^2]

giving

enter image description here

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!

$\endgroup$
1
$\begingroup$

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]}

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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