8

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?

tjm167us
  • 993
  • 5
  • 12
  • 3
    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

1 Answers1

10

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

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • 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
  • You may add example with MapAt using Span what is a big adventage of this approach. (not exactly suited to the question since it is about one element, but still good to know) – Kuba Aug 26 '13 at 20:33
  • @Kuba Wait, since when does MapAt work with Span?! I had to implement something myself for that problem. – Mr.Wizard Aug 26 '13 at 20:37
  • @Mr.Wizard I don't know since when :p http://mathematica.stackexchange.com/a/30340/5478 – Kuba Aug 26 '13 at 20:38
  • 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
  • @Kuba They don't seem to have any examples in the v9 documentation. Sneaky. How does the performance compare to using e.g. Range with MapAt? Is it faster? – Mr.Wizard Aug 26 '13 at 20:42
  • I'm working on an answer to the link you gave. There is no "Last modified in 9", again! :/ (give me the test and I will do it for you) – Kuba Aug 26 '13 at 20:45
  • @Kuba try this: big = Range[1*^5]; then First@Timing@MapAt[#^2 &, big, List /@ Range[30000, 40000]] versus First@Timing@MapAt[#^2 &, big, 30000 ;; 40000] – Mr.Wizard Aug 26 '13 at 20:49
  • Oh my, 10.2sec vs wait for it... 0.015s. – Kuba Aug 26 '13 at 20:57
  • 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