4

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

expression
  • 5,642
  • 1
  • 19
  • 46

1 Answers1

5
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

kglr
  • 394,356
  • 18
  • 477
  • 896