# Coloring selected elements of a list created with Table [duplicate]

I have a function that returns a list of 100+ elements, using Table[]. For simplification, let's assume that it's just Table[i,{i,100}]. Some elements of this list are more important for me than others, so I would like to have them colored, with the rest in usual black. In this example I'd like to have elements form 12 to 25 in red and from 89 to 95 in blue. My first guess was to use Style, but how should I specify which elements to color? Thanks for any help.

• Try MapAt[Style[#,Red]&, list, 12;;25]. Maybe it could be marked as a duplicate: how to change the color of digits
– Kuba
Commented Feb 7, 2014 at 14:09
• Some related questions: (17550), (31449), (40754), (8191), (31152), (31535) Commented Feb 7, 2014 at 14:31
• The syntax that Kuba shows, using Span in MapAt, is almost certainly the cleanest way to do this but it will only work in v9 and later. See: How to make MapAt work with Span? Commented Feb 7, 2014 at 14:34
• @Mr.Wizard I'm on v7 Commented Feb 7, 2014 at 14:34
• @Wojciech Me too. You can use my mapAtSpan function in my answer to the last post I linked above for this. If you have any trouble please let me know. Commented Feb 7, 2014 at 14:35

You may like to replace those numbers with Style[number, Color].

To color according to number value:

Range@100 /. {i_ /; 12 <= i <= 25 :> Style[i, Red], i_ /; 89 <= i <= 95 :> Style[i, Blue]}


To color according to number position:

Range@100 // MapAt[Style[#, Red] &, #, 12;;25] & // MapAt[Style[#, Blue] &, #, 89;;95] &


As mentioned in comments, this MapAt version doesn't work on v7. On v7 we could alternatively use

Range@100 // MapAt[Style[#, Red] &, #, {#} & /@ Range[12, 25]] & //
MapAt[Style[#, Blue] &, #, {#} & /@ Range[89, 95]] &

• Thanks for the answer! However, Table[i, {i, 100}] is just a simplification, I'd like for the color to be position-dependent, not value-dependent. Commented Feb 7, 2014 at 14:14
• @Wojciech: Updated according to your comment. Commented Feb 7, 2014 at 14:28
• Thanks! What if I have a nested list {{1...50},{51..100}}? Commented Feb 7, 2014 at 15:39
• {Range@50, Range[51, 100]} // MapAt[Style[#, Red] &, #, {1, #} & /@ Range[12, 25]] & // MapAt[Style[#, Blue] &, #, {2, #} & /@ Range[39, 45]] & Commented Feb 7, 2014 at 16:47
a = Table[i, {i, 100}];
b = Position[a, _?(12 <= # <= 25 &)];
c = Position[a, _?(89 <= # <= 95 &)];
d = MapAt[Style[#, Red] &, a, b];
MapAt[Style[#, Blue] &, d, c]