0
$\begingroup$

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.

$\endgroup$
1
  • 1
    $\begingroup$ Please include a hand drawn image of what you want as output. $\endgroup$
    – Syed
    Commented Oct 30, 2023 at 2:46

1 Answer 1

1
$\begingroup$
$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

enter image description here

SeedRandom[1234];

grid = Array[RandomChoice[{"Blue", "Red"}] &, {n, n}] // Grid

enter image description here

grid /. str_String :> ToExpression@str

enter image description here

grid /. {"Blue" :> Blue, "Red" :> Red}

enter image description here

grid /. str_String :> Style[str, ToExpression@str]

enter image description here

$\endgroup$
1
  • $\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$
    – matrixbud
    Commented Oct 30, 2023 at 22:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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