How to style a Grid with ItemStyle?

I have one question: “How to combine style definitions within ItemStyle?” and one explanation: “How to define cell regions within ItemStyle”.

See the following notebook:

Unfortunately this embedded notebook does not show colors. No idea why. That is why I attach the same notebook to this post.

220519 Styling a Grid with ItemStyle.nb (28.6 KB)

As far as i know, this is how it works:

  • first come column specs which can be a list of a spec per column, then come rows, then come specific rules for elements

  • Column styles go before row styles, so a row style will not override a column style.

  • specific style rules override column and row specs. Also the last rule spec overrides previous ones.

     Grid[tbl, ItemStyle -> {
     {Directive[{Darker@Yellow}], Green, Directive[{Bold, Red}]},(*list of rules for each columns*)
     Directive[{"Text", Gray}],(*one general spec for all rows*)
     {{4, 4} -> Directive[Blue], {{2, 3}, {2, 4}} -> Directive[{"Text", Purple}], {4, 4} -> Directive[ Bold]}}
     (*specific style rules for elements*)
     ]
    

Great explanation, Martijn, great example! Thanks so much.

Hence my little problem (each item as “Text” and column 2 as Red) is solved by:

Grid[tbl, ItemStyle -> {{2 -> Red}, "Text"}]

I was not aware of that strong sequence columns, rows, items. In the former example the “Text”-style is specified as the style for each rows. This is rather arbitrary. But I found no way to specify an overall ItemStyle.

To bring it together with some region specifications one would write:

Grid[tbl, 
 ItemStyle -> {{2 -> Red}, 
   "Text", {{{2, 3}, {2, 4}} -> Green, {-1, -1} -> Magenta}}]

See attached new notebook version “220519a Styling a Grid with ItemStyle.nb”.

220519a Styling a Grid with ItemStyle.nb (41.3 KB)

One can use the option BaseStyle for whatever should apply to all items

 Grid[tbl,
 	ItemStyle -> {2 -> Red, Automatic, {{{2, 3}, {2, 4}} -> Green, {-1, -1} -> Magenta}},
 	BaseStyle -> "Text"
 ]

Yes, of course. I forgot about that.

Thanks