2
$\begingroup$

How to control the spacing of each cell in a grid?

In the example below, Title and Subtitle rows waste too much space above and below.

Also the rectangle in cell {4,1} is rather small, how to change its size?

Also the SpanfromBoth does not seem to work well since a vertical line shows up between cells {6,1} and {6,2}, and also between {6,4} and {6,5}.

title = Text[Style["Title", 26, Black], {0, 0}, {0, 0}];
subtitle1 = Text[Style["Subtitle 1", 26, Black], {0, 0}, {0, 0}];
subtitle2 = Text[Style["Subtitle 2", 26, Black], {0, 0}, {0, 0}];
Grid[{
  {Graphics[{title}], SpanFromLeft, SpanFromLeft, SpanFromLeft, 
   SpanFromLeft, SpanFromLeft},
  {Graphics[subtitle1], SpanFromLeft, SpanFromLeft, 
   Graphics[subtitle2], SpanFromLeft, SpanFromLeft},
  {Graphics[Rectangle[]], Graphics[Rectangle[]], 
   Graphics[Rectangle[]], Graphics[Rectangle[]], 
   Graphics[Rectangle[]], Graphics[Rectangle[]]},
  {Graphics[Rectangle[{0, 0}, {6, 1}]], SpanFromLeft, SpanFromLeft, 
   Graphics[Rectangle[{0, 0}, {6, 1}]], SpanFromLeft, SpanFromLeft},
  {Graphics[Rectangle[]], SpanFromLeft, Graphics[Rectangle[]], 
   Graphics[Rectangle[]], SpanFromLeft, Graphics[Rectangle[]]},
  {SpanFromAbove, SpanFromBoth, Graphics[Rectangle[]], SpanFromAbove, 
   SpanFromBoth, Graphics[Rectangle[]]},
  {Graphics[Rectangle[]], SpanFromLeft, SpanFromLeft, SpanFromLeft, 
   SpanFromLeft, SpanFromLeft}}, Frame -> All]

enter image description here

$\endgroup$

2 Answers 2

2
$\begingroup$

I discovered I could play with individual AspectRatios and Imagesizes:

title = Text[Style["Title", 26, Black], {0, 0}, {0, 0}];
subtitle1 = Text[Style["Subtitle 1", 26, Black], {0, 0}, {0, 0}];
subtitle2 = Text[Style["Subtitle 2", 26, Black], {0, 0}, {0, 0}];
Grid[{
  {Graphics[{title}, AspectRatio -> 0.1], SpanFromLeft, SpanFromLeft, 
   SpanFromLeft, SpanFromLeft, SpanFromLeft},
  {Graphics[subtitle1, AspectRatio -> 0.1], SpanFromLeft, 
   SpanFromLeft, Graphics[subtitle2, AspectRatio -> 0.1], 
   SpanFromLeft, SpanFromLeft},
  {Graphics[Rectangle[]], Graphics[Rectangle[]], 
   Graphics[Rectangle[]], Graphics[Rectangle[]], 
   Graphics[Rectangle[]], Graphics[Rectangle[]]},
  {Graphics[Rectangle[{0, 0}, {6, 1}], ImageSize -> 600], 
   SpanFromLeft, SpanFromLeft, 
   Graphics[Rectangle[{0, 0}, {6, 1}], ImageSize -> 600], 
   SpanFromLeft, SpanFromLeft},
  {Graphics[Rectangle[], ImageSize -> 200], SpanFromLeft, 
   Graphics[Rectangle[]], Graphics[Rectangle[], ImageSize -> 200], 
   SpanFromLeft, Graphics[Rectangle[]]},
  {SpanFromAbove, SpanFromBoth, Graphics[Rectangle[]], SpanFromAbove, 
   SpanFromBoth, Graphics[Rectangle[]]},
  {Graphics[Rectangle[], AspectRatio -> 0.2, ImageSize -> 700], 
   SpanFromLeft, SpanFromLeft, SpanFromLeft, SpanFromLeft, 
   SpanFromLeft}}, Frame -> All, Alignment -> {Center, Center}]

enter image description here

$\endgroup$
2
  • $\begingroup$ Oh, I thought the rectangles were just there to show the grid structure and avoid distracting data. But yes, sizing the individual cell contents is an option. You might also be interested to know that for any particular cell, you can wrap the contents in Item and then provide any Grid options directly to that Item. $\endgroup$
    – lericr
    Apr 27, 2022 at 22:57
  • $\begingroup$ And of course there is the Spacings option, for another level of control of the layout. And sometimes Dividers works better than Frame (and maybe that would provide a workaround for your stray vertical lines--I haven't tried it). $\endgroup$
    – lericr
    Apr 27, 2022 at 23:00
1
$\begingroup$

I'm not seeing the stray vertical line (I'm using version 13).

The title and subtitle are large because you're creating graphics. If that's not necessary in your context, then just using Style should work:

title = Style["Title", 26, Black];
subtitle1 = Style["Subtitle 1", 26, Black];
subtitle2 = Style["Subtitle 2", 26, Black];

For the other sizing issue, Grid has an ItemSize option. It has many forms, so you should research it, but in your specific case this works to change the height of the 4th row:

ItemSize -> {{}, {4 -> 10}}

Replace the 10 with whatever you need.

$\endgroup$
1
  • $\begingroup$ Thanks, I am using 12.3 $\endgroup$
    – Maubro
    Apr 27, 2022 at 22:48

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.