6
$\begingroup$

I'm trying to make a Grid that has one cell that spans horizontally and one that spans vertically, like so (generated manually with Graphics):

enter image description here

But when I try to do this with Grid, the right-most vertical cell doesn't span both rows:

Grid[{
    { Style["1", FontSize->30], SpanFromLeft, Style["4\n4", FontSize->30] },
    { Style["2", FontSize->30], Style["3", FontSize->30], SpanFromAbove }
}, Frame->All, Spacings->{3,3} ]

enter image description here

Since I have the frame active, I know the right-most cell does in fact span both rows, as we see if we remove SpanFromAbove:

Grid[{
    { Style["1", FontSize->30], SpanFromLeft, Style["4\n4", FontSize->30] },
    { Style["2", FontSize->30], Style["3", FontSize->30] }
}, Frame->All, Spacings->{3,3} ]

enter image description here

Is it not possible to combine vertical and horizontal spans in a grid? Is there a better function, like some combination of Column and Row? Thx

$\endgroup$
6
  • $\begingroup$ I'm not sure what do you mean, is the height of the first item the problem? Because except that size issue everything seems ok, with Alignment -> {Center, Center} you can see that the item with 4s is streched. Or did I miss the point? Grid[{{Style["1", FontSize -> 30], SpanFromLeft, Style["4\n4", FontSize -> 30]}, {Style["2", FontSize -> 30], Style["3", FontSize -> 30], SpanFromAbove}}, Frame -> All, Spacings -> {3, 3}, Alignment -> {Center, Center}] $\endgroup$
    – Kuba
    Commented Jul 23, 2018 at 15:54
  • $\begingroup$ Even with the alignment, the content of the right-most box never stretches to the second row. Here's the same thing with four "4"s: Grid[{{Style["1", FontSize -> 30], SpanFromLeft, Style["4\n4\n4\n4\n", FontSize -> 30]}, {Style["2", FontSize -> 30], Style["3", FontSize -> 30], SpanFromAbove}}, Frame -> All, Spacings -> {3, 3}, Alignment -> {Center, Center}] $\endgroup$ Commented Jul 23, 2018 at 15:57
  • 2
    $\begingroup$ FWIW, this works fine if you replace Grid with GraphicsGrid. Seems like a bug to me. $\endgroup$ Commented Jul 23, 2018 at 16:28
  • 2
    $\begingroup$ This is what I see - link. But I wouldn't be surprised by another bug of Grid, just want to make sure it is clear. $\endgroup$
    – Kuba
    Commented Jul 23, 2018 at 16:41
  • 1
    $\begingroup$ It is a bug, but this description doesn't quite characterize it correctly. The problem isn't that a vertical spanning member can't properly span. Changing Alignment shows this. @ChrisWilson misses seeing this in part because of an extra newline at the end...try wrapping Framed around the Style or using Bottom alignment. What it cannot do is to calculate the required height of the first row correctly. It apportions to the first row enough pixels to hold everything, although it could have subtracted the number of vertical pixels in the second row. $\endgroup$
    – John Fultz
    Commented Jul 25, 2018 at 1:38

1 Answer 1

5
$\begingroup$

I encountered a similar problem, and I found this idea that uses Grid inside Grid.

Grid[{
  {Grid[{
     {Style["1", FontSize -> 30], SpanFromLeft},
     {Style["2", FontSize -> 30], Style["3", FontSize -> 30]}
     }, Frame -> All, Spacings -> {3, 3}],
   Grid[{
     {Style["4", FontSize -> 30]},
     {Style["4", FontSize -> 30]}
     }, Frame -> True, Spacings -> {3, 2.99}]}
  }, Spacings -> -0.1]

I played with the Spacings a bit, to make the graph aligned, and here is the result.

enter image description here

$\endgroup$
1
  • $\begingroup$ Brilliant! (Though I wish it wasn't necessary...) $\endgroup$ Commented Jul 25, 2018 at 23:04

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.