1
$\begingroup$

How can I change the size of a specific subset of cells in a Grid? I'm using the ItemSize parameter for the ones I want to change, but it affects others, and I can't control the sizes separately.

More detail: I want to present a large matrix (48x48) with labels for each column and row. The way I'm doing it right now is with Grid, by adding an extra row and column with the desired labels. However, since the entries are of varying lengths, I want to adjust the size of these Grid cells so that they're all the same - but I want to leave the label cells unchanged.

Here is (a MWE of) my code to begin with:

labels = {"Long property name A", "Extremely long Property name B"};
Grid[Partition[Riffle[Join[
    {Null},
    Item[Rotate[#, -90 Degree], Alignment -> Bottom] & /@ labels,
    {1, 3000, 5, 7000}],
   Item[#, Alignment -> Right] & /@ labels, {4, -1, 3}], 3], 
 Frame -> All]

This is the result:

enter image description here

Now, I would like to change the size of the cells in the bottom right 4x4 submatrix, so that they all have the same size. I fix this by changing the table entry {1,3000,5,7000} to:

Item[#, ItemSize -> 4] & /@ {1, 3000, 5, 7000}

But then this is the result:

enter image description here

I tried adding the ItemSize parameter to the Items containing the labels, but the label text appears in a single line only if the cell is extremely wide, such as ItemSize->20, which I don't want. So - how can I change the size of some of the cells without affecting the size of other cells?

$\endgroup$

2 Answers 2

1
$\begingroup$

Cheating by using Rasterize

labels = {"Long property name A", "Extremely long Property name B"};
Grid[Partition[
  Riffle[Join[{Null}, 
    Item[Rasterize[Item[Rotate[#, -90 Degree]]], 
       Alignment -> Bottom] & /@ labels, 
    Item[#, ItemSize -> 4] & /@ {1, 3000, 5, 7000}], 
   Item[#, Alignment -> Right] & /@ labels, {4, -1, 3}], 3], 
 Frame -> All]

enter image description here

No claim made that this is an acceptable answer.

$\endgroup$
1
  • 1
    $\begingroup$ Thanks for the suggestion - but when I run your code, the longer vertical name gets scaled down and is much smaller than the other one, see here: imgur.com/vK09aQJ I'm using Mathematica 12.0.0. $\endgroup$ Nov 22, 2019 at 10:19
0
$\begingroup$

I ran into the same problem, but Rasterize slowed down rendering a large grid significantly. Another workaround is to wrap your text in Row. It's maybe faster, more flexible.

labels = {"Long property name A", "Extremely long Property name B"};
Grid[Partition[
  Riffle[Join[{Null}, 
    Rotate[Row[{#}, ImageSize->{230, 20}, Alignment->{Right, Center}], -90 Degree]& /@ labels, 
    Item[#, ItemSize -> 4] & /@ {1, 3000, 5, 7000}], 
   Item[#, Alignment -> Right] & /@ labels, {4, -1, 3}], 3], 
 Frame -> All]

grid with rotated text

You'll need to specify the ImageSize for Row. Its width is the column header height and right align puts it at the bottom when rotated.

$\endgroup$

Your Answer

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

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