7
$\begingroup$

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?

$\endgroup$
  • 3
    $\begingroup$ Documentation Center: Grid[Table[x, {4}, {7}], Background -> {None, None, {{1, 1} -> Pink, {3, 3} -> Red}}] (section: Background) $\endgroup$ – Pinguin Dirk Aug 26 '13 at 17:15
10
$\begingroup$

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

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – tjm167us Aug 26 '13 at 17:38
  • $\begingroup$ I hope you don't mind! Inferior in general, but useful sometimes. $\endgroup$ – István Zachar Aug 26 '13 at 20:06
  • $\begingroup$ @István No I don't mind. It's a good note, but as you say usually not ideal. $\endgroup$ – Mr.Wizard Aug 26 '13 at 20:15
  • 3
    $\begingroup$ @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. $\endgroup$ – Mr.Wizard Aug 26 '13 at 20:39
  • 2
    $\begingroup$ @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. $\endgroup$ – Mr.Wizard Aug 26 '13 at 21:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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