In[]:=
ClearAll["Global`*"];
How to style a Grid with ItemStyle?
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”.
I use a table:
In[]:=
tbl=Table[j^(1/i),{i,4},{j,5}];Grid[tbl]
Out[]=
1 | 2 | 3 | 4 | 5 |
1 | 2 | 3 | 2 | 5 |
1 | 1/3 2 | 1/3 3 | 2/3 2 | 1/3 5 |
1 | 1/4 2 | 1/4 3 | 2 | 1/4 5 |
Combined FormatStyle?
The following work. I can style the grid with a style or color a column:
In[]:=
Grid[tbl,ItemStyle"Text"]
Out[]=
1 | 2 | 3 | 4 | 5 |
1 | 2 | 3 | 2 | 5 |
1 | 1/3 2 | 1/3 3 | 2/3 2 | 1/3 5 |
1 | 1/4 2 | 1/4 3 | 2 | 1/4 5 |
In[]:=
Grid[tbl,ItemStyle{2->Red}]
Out[]=
1 | 2 | 3 | 4 | 5 |
1 | 2 | 3 | 2 | 5 |
1 | 1/3 2 | 1/3 3 | 2/3 2 | 1/3 5 |
1 | 1/4 2 | 1/4 3 | 2 | 1/4 5 |
But how to achieve both of them? This does not work:
In[]:=
Grid[tbl,ItemStyleDirective["Text",{2->Red}]]
Out[]=
1 | 2 | 3 | 4 | 5 |
1 | 2 | 3 | 2 | 5 |
1 | 1/3 2 | 1/3 3 | 2/3 2 | 1/3 5 |
1 | 1/4 2 | 1/4 3 | 2 | 1/4 5 |
This doesn’t work either:
In[]:=
Grid[tbl,ItemStyleDirective["Text",{Automatic,Automatic,{2->Red}}]]
Out[]=
1 | 2 | 3 | 4 | 5 |
1 | 2 | 3 | 2 | 5 |
1 | 1/3 2 | 1/3 3 | 2/3 2 | 1/3 5 |
1 | 1/4 2 | 1/4 3 | 2 | 1/4 5 |
How can I get all items as “Text” and the 3rd column Red?
Region ItemStyle?
Styling one or more items is done by a list. An item is specified with its {row, column}-coordinates, where the standard notation by negative numbers holds. Note that you cannot omit the two Automatic styles for columns and rows.
In[]:=
Grid[tbl,ItemStyle{Automatic,Automatic,{{2,3}->Red,{4,-1}->Green}}]
Out[]=
1 | 2 | 3 | 4 | 5 |
1 | 2 | 3 | 2 | 5 |
1 | 1/3 2 | 1/3 3 | 2/3 2 | 1/3 5 |
1 | 1/4 2 | 1/4 3 | 2 | 1/4 5 |
Styling a region. I want to color cells {2,2} to {3,4}:
This does not work. At first I had no idea how the region is specified . It is not specified by {row, column}-coordinates of their edges:
In[]:=
Grid[tbl,ItemStyle{Automatic,Automatic,{{{2,2},{3,4}}->Red,{-1,-1}->Green}}]
Out[]=
1 | 2 | 3 | 4 | 5 |
1 | 2 | 3 | 2 | 5 |
1 | 1/3 2 | 1/3 3 | 2/3 2 | 1/3 5 |
1 | 1/4 2 | 1/4 3 | 2 | 1/4 5 |
Then I assumed that the first list is a list of rows, the second list a list of columns and the region is their intersection, but this is wrong.
In[]:=
Grid[tbl,ItemStyle{Automatic,Automatic,{{{2,3},{2,3,4}}->Red,{-1,-1}->Green}}]
Out[]=
1 | 2 | 3 | 4 | 5 |
1 | 2 | 3 | 2 | 5 |
1 | 1/3 2 | 1/3 3 | 2/3 2 | 1/3 5 |
1 | 1/4 2 | 1/4 3 | 2 | 1/4 5 |
Then I found that the first list denotes a range of rows, the second list a range of columns and the region is their intersection. That’s it!:
In[]:=
Grid[tbl,ItemStyle{Automatic,Automatic,{{2,3},{2,4}}->Red}]
Out[]=
1 | 2 | 3 | 4 | 5 |
1 | 2 | 3 | 2 | 5 |
1 | 1/3 2 | 1/3 3 | 2/3 2 | 1/3 5 |
1 | 1/4 2 | 1/4 3 | 2 | 1/4 5 |