I am trying to colorize rows in a table defined with the Grid command using ItemStyle. I have simplified my problem down to the following. Table["Blue",2] evaluates as the list, {Blue,Blue}. I observe that {Blue,Blue} evaluates as a list composed of two colored blue squares, not as the simple text list {Blue,Blue}. When I use Table["Blue",2] in my grid definition, the table remains black and white. However, if I use {Blue,Blue}, then the table becomes colorized the way I want. Is there a way to change Table["Blue",2] so that it evaluates with the colored blue boxes? My actual expression is more complex than just this, but I think my problem would be solved if I could find something like Evaluate[Table["Blue",2]] or List[Table["Blue",2]] to generate a list with blue boxes.
1 Answer
$\begingroup$
$\endgroup$
1
$Version
(* "13.3.1 for Mac OS X ARM (64-bit) (July 24, 2023)" *)
Clear["Global`*"]
n = 7;
SeedRandom[1234];
Array[RandomChoice[{Blue, Red}] &, {n, n}] // Grid
SeedRandom[1234];
grid = Array[RandomChoice[{"Blue", "Red"}] &, {n, n}] // Grid
grid /. str_String :> ToExpression@str
grid /. {"Blue" :> Blue, "Red" :> Red}
grid /. str_String :> Style[str, ToExpression@str]
-
$\begingroup$ Thank you, Bob. You showed me that I just had to drop the quotes around Blue and Magenta. My application actually makes tables with rows of blue and magenta text, not boxes, but this solves it for me. $\endgroup$ Commented Oct 30, 2023 at 22:54