Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Consider the following code:

Grid[Table[x, {4}, {7}], Background -> {4 -> LightRed}]

This highlights column 4:

enter image description here

Grid[Table[x, {4}, {7}], Background -> {None, {4 -> LightRed}}]

This highlights row 4:

enter image description here

How do you highlight an individual cell, say row 3, column 5?

share|improve this question
1  
Documentation Center: Grid[Table[x, {4}, {7}], Background -> {None, None, {{1, 1} -> Pink, {3, 3} -> Red}}] (section: Background) –  Pinguin Dirk Aug 26 '13 at 17:15
add comment

1 Answer

up vote 6 down vote accepted

As Pinguin Dirk comments there is an example of exactly this in the documentation:

Grid[Table[x, {4}, {7}],
  Background -> {None, None, {{1, 1} -> Pink, {3, 3} -> Red}}]

enter image description here

Those who value brevity may wish to note that implicit Null may be used in place of None:

Background -> {, , {{1, 1} -> Pink, {3, 3} -> Red}}

You can also use Item and MapAt:

tbl = Table[x, {4}, {7}];

MapAt[Item[#, Background -> Pink] &, tbl, {{3, 3}, {2, 6}}] // Grid

enter image description here

You could also use Style but that won't fill the cell (can be finetuned by using Pane though):

{Grid[{{1, 2}, {3, Style[4, Background -> Pink]}}, Frame -> All],
 Grid[{{1, 2}, {3, Item[4, Background -> Pink]}}, Frame -> All]}

enter image description here

share|improve this answer
    
Bleh - I'm normally really good about finding it in the documentation. I Just missed that one example when I was looking through it. Thanks for adding the alternate way using Item and MapAt. –  tjm167us Aug 26 '13 at 17:38
    
I hope you don't mind! Inferior in general, but useful sometimes. –  István Zachar Aug 26 '13 at 20:06
    
@István No I don't mind. It's a good note, but as you say usually not ideal. –  Mr.Wizard Aug 26 '13 at 20:15
3  
@Kuba Oh, that answer? I probably just assumed you were smoking crack. ;^) Seriously if that works now you should post a new answer to the question I just linked in the comment above. –  Mr.Wizard Aug 26 '13 at 20:39
2  
@Kuba Why don't you add mention of that to your answer? The poor computational complexity of MapAt is a long-known problem. This enhancement is going to significantly extend the practical applications of MapAt. –  Mr.Wizard Aug 26 '13 at 21:21
show 7 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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