TransWikia.com

Highlight part of the expression like this

Mathematica Asked on January 26, 2021

I want to highlight the coefficient of the polynomial is not ±1, I have tried

Factor[x^105 - 1] /. k_ x_^n_ /; Abs[k] > 1 -> Style[k x^n, Red]

HoldForm @@ {Factor[x^105 - 1]} /.  k_ x_^n_ /; Abs[k] > 1 -> Style[k , Red] x^n

enter image description here
These are not the results I want, I hope to get results like this

enter image description here

It would be better if it could be displayed in traditional format in programmatically
enter image description here

One Answer

HoldForm @@ {Factor[x^105 - 1]} /. k_ x_^n_ :> 
  RuleCondition[With[{b = Style[Abs[k], Red]}, 
    If[Internal`SyntacticNegativeQ[k], -1, 1] HoldForm[b x^n]], Abs[k] > 1]

enter image description here

Use Highlighted[Abs[k]] instead of Style[Abs[k], Red] to get

enter image description here

See also: Replacement inside held expression

Update: Displaying in TraditionalForm:

HoldForm @@ (TraditionalForm /@ {Factor[x^105 - 1]}) /. k_ x_^n_ :>
     RuleCondition[With[{b = Style[Abs[k], 14, Red]}, 
    If[Internal`SyntacticNegativeQ[k], -1, 1] HoldForm[b x^n]], Abs[k] > 1]

enter image description here

We can get Highlighted looks using

HoldForm @@ (TraditionalForm /@ {Factor[x^105 - 1]}) /. k_ x_^n_ :>
    RuleCondition[With[{b = Framed[Style[Abs[k], 14], Background -> Yellow, 
       FrameStyle -> None]}, 
    If[Internal`SyntacticNegativeQ[k], -1, 1] HoldForm[b x^n]], Abs[k] > 1]

enter image description here

Correct answer by kglr on January 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP