4
$\begingroup$

Consider the following Grid

Grid[{
  {7, 11, 11, 5},
  {2, 3, 4, 5},
  {Button["Hello"], SpanFromLeft},
  {Button["Hello"], SpanFromLeft, Button["Hello"], SpanFromLeft}
  }, ItemSize -> {{1, 3, 7, 1}, Full}, Frame -> All]

enter image description here

Is it possible to freely align the button items so that I can centre two or more subdivisions of a row? That is, for 2 and 3 subdivisions, get the following alignment, while keeping the upper rows item sizes

enter image description here

Note: the button items are merely an example, but it is interesting to see how SpanFromLeft works with buttons.

I thought about individually setting the item sizes, positions or even playing around with dividers, but with no real results. Any ideas?

$\endgroup$

1 Answer 1

1
$\begingroup$

In the meantime, I found some potential approaches. One is to use Row and SpanFromLeft

bl = 182;
Grid[{
  {7, 11, 11, 5},
  {2, 3, 4, 5},
  {Row@{Button["Hello", ImageSize -> {bl/3, Full}], 
     Button["Hello", ImageSize -> {bl/3, Full}], 
     Button["Hello", ImageSize -> {bl/3, Full}]}, SpanFromLeft},
  {Row @{Button["Hello", ImageSize -> {bl/2, Full}], 
     Button["Hello", ImageSize -> {bl/2, Full}]}, SpanFromLeft}
  }, ItemSize -> {{1, 3, 7, 1}, Full}, Frame -> All]

enter image description here

There are two problems with this approach: the dividers do not appear and bl is chosen by hand (automatic, depending on the grid's ItemSize would be ideal, though converting ImageSize and ItemSize units doesn't seem trivial).

Alternatively, one could think of grids within a grid

bl = 170;
Grid[{
  {Grid[{
     {7, 11, 11, 5},
     {2, 3, 4, 5}
     }, Frame -> All, ItemSize -> {{1, 3, 7, 1}, Full}]},
  {Grid[{{Button["Hello", ImageSize -> {bl/3, Full}], 
      Button["Hello", ImageSize -> {bl/3, Full}], 
      Button["Hello", ImageSize -> {bl/3, Full}]}}, Frame -> All, 
    ItemSize -> 4.3]},
  {Grid[{{Button["Hello", ImageSize -> {bl/2, Full}], 
      Button["Hello", ImageSize -> {bl/2, Full}]}}, Frame -> All, 
    ItemSize -> 6.9]}
  }, ItemSize -> {Full, Full}, GridFrameMargins -> 0]

enter image description here

This gets me close, though Frame becomes a mess. Perhaps using the old GridFrameMargins function could help somehow, but I'm not seeing how. Again, the issue of setting an automatic ItemSize becomes relevant. Any further ideas or comments are appreciated.

$\endgroup$
1
  • 1
    $\begingroup$ Adding a RowSpacings -> 0 to the outer Grid helps. But this is still far from ideal. We should really have something like TeX's hfill $\endgroup$ Commented Jul 15, 2021 at 22:27

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.