I found it's tricky to colorize a sub-expression of an expression that changes to a different expression when it is evaluated.

For example, if I replace s with Style[s,Red] in $$ \sum_{k=1}^{n}k^{-s} $$, I get HarmonicNumber[n,s] with s colored red.

The solution I found is to evaluate Style[s,Red] in another line and use copy-paste to replace the s in the original expression.

Is there any better way?


2 Answers 2


Use an Inactive Sum

expr = Inactive[Sum][k^-s, {k, 1, n}] /. k -> Style[k, Red]

enter image description here

However, this form cannot be directly activated

expr // Activate

enter image description here

Convert back prior to activating

expr /. Style[k, Red] -> k // Activate

(* HarmonicNumber[n, s] *)

This produces an input cell that can be evaluated:

    Style[k, StripOnInput -> True, FontColor -> Red]^-s, {Style[k, 
      StripOnInput -> True, FontColor -> Red], 1, n}],

Mathematica graphics

[It basically constructs input equivalent to selecting each k in the sum and using the menu Format > TextColor > Red to change the color, which is what I did in the comment I left under the question.]


Your Answer

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

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