4

Bug introduced in 6.0 and persisting through 11.0.1 or later

Grid ang ItemStyle are new in 6.0


I would expect the following to output the same grids

{Grid[{{1, 2}, {3, 4}}, ItemStyle -> {, , {{1, 1} -> Directive[30]}}, 
  Background -> {, , {{1, 1} -> Red}}, Frame -> All],
 Grid[{{1, 2}, {3, 4}}, 
  ItemStyle -> {, , {{1, 1} -> Directive[Background -> Red, 30]}}, 
  Frame -> All], 
 Grid[{{NumberForm@1, 2}, {3, 4}}, 
  ItemStyle -> {, , {{1, 1} -> Directive[Background -> Red, 30]}}, 
  Frame -> All]}

instead of

enter image description here

Update:

The following should all produce identical grids and in fact they (almost) do - provided you mix and match between different Mathematica versions:

 {Grid[{{1, 2}, {3, 4}},ItemStyle -> {None, None,{{1, 1} -> Directive[30]}},Background -> {, , {{1, 1} -> Red}}, Frame -> All], 
  Grid[{{1, 2}, {3, 4}},ItemStyle -> {{} , {},{{1, 1} -> Directive[Background -> Red, 30]}}, Frame -> All], 
  Grid[{{NumberForm@1, 2}, {3, 4}},ItemStyle -> {Automatic,Automatic, {{1, 1} -> Directive[Background -> Red, 30]}}, Frame -> All],
  Grid[{{Item[1, BaseStyle -> {Background -> Red, FontSize -> 30}],2}, {3, 4}},Frame -> All],
  Grid[{{Item[Style[1, 30], Background -> Red], 2}, {3, 4}},Frame -> All]}

V6:

enter image description here

V7

enter image description here

V8

enter image description here

V9

enter image description here

The "almost" is that the second Grid - perhaps the cleanest implementation - is yet to fire but perhaps in V10 ...? (As a side note, n.b. the alternative padding (None,Automatic,{},,}) to reach the cell specification in the ItemStyle option - maybe one day argument positions might more seamlessly combine with their structure? )

Upshot: There seems to be a bug in combining values for the Background option with other styling within a single Directive but this can be easily overcome by using both specifications as per the first Grid above.

Ronald Monson
  • 6,076
  • 26
  • 46
  • 1
    fyi, your code gives syntax error on V9.01 on windows. An improperly formatted directive with head Symbol was encountered – Nasser Jan 29 '14 at 15:38
  • no syntax error on V9.0 on OS X. Does Grid[{{1, 2}, {3, 4}}, ItemStyle -> {None, None, {{1, 1} -> Directive[30]}}, Background -> {None,None, {{1, 1} -> Red}}, Frame -> All] go through? – Ronald Monson Jan 29 '14 at 15:43
  • @Nasser Not here ! – Dr. belisarius Jan 29 '14 at 15:46
  • 1
    Screen shot. Need to do it with a fresh Mathematica session (restart M itself) Mathematica graphics Next time, evaluated, the error goes away. Only first time it shows up. – Nasser Jan 29 '14 at 15:50
  • Grid[{{1, 2}, {3, 4}}, ItemStyle -> {None, None, {{1, 1} -> Directive[30]}}, Background -> {None,None, {{1, 1} -> Red}}, Frame -> All] is OK. No error. – Nasser Jan 29 '14 at 15:58
  • The same as Nasser's observations. Well I'd use None instead of Null, why Null anyway? – Kuba Jan 29 '14 at 15:59
  • @Kuba brevity as per accepted answer in http://mathematica.stackexchange.com/questions/31152/changing-the-background-color-of-one-element-in-a-grid?rq=1 but yes on OS X the syntax coloring prior to evaluation indicates non-standard usage so yes "None" is probably better -otoh maybe a latent wish that without either (i.e. single argument) would syntactically suffice since the pattern shouldn't interfere with other option usages? – Ronald Monson Jan 29 '14 at 16:22

1 Answers1

3

The documentation for ItemStyle says

Item style specifications can include all options to Item, as well as options to Style. Directive[opt1, opt2, ...] can be used to specify multiple options.

Therefore, as I see it

Grid[{{1, 2}, {3, 4}}, 
 ItemStyle -> {{}, {}, {{1, 1} -> Directive[30, Background -> Red]}}, 
 Frame -> All]

grid1

should be equivalent to

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

grid2

but it clearly isn't, so this looks like a bug to me.

Update

I reported this to Wolfram Research tech support and receives the following reply:

Thank you for bringing this issue about ItemStyle to our attention.

I simplified the code to

Grid[{{1,2},{3,4}},ItemStyle->Background->Red]

and found similar results. I have sent a note about this to the appropriate members of our development team.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257