4
$\begingroup$

I'm looking to make a grid where the number of columns change, but the grid spacings remain roughly the same. Essentially, something like SpanFromLeft, but applied to an entire row rather than a single cell. The closest thing I've been able to get is a nested grid, but this has an unwanted padding on it that I can't figure out how to get rid of.

Grid[{{a,b,c,d},{e,f,g,h},{Grid[{{i,j,k},{l,m,n}},Frame->All],SpanFromLeft,SpanFromLeft}},Frame->All]

Results in:

result

But I'm looking to remove the empty space around {{i,j,k},{l,m,n}} and widen the bottom rows cells to match the upper rows total width.

$\endgroup$
5
$\begingroup$

Update: a more flexible approach:

griD = {Grid[{#}, Dividers -> {Center, None}, 
     Alignment -> {Center, Center},
     ItemSize -> {Scaled[1/Length[#]], 2}]} &;

data = {{a, b, c, d, 1}, {e, f, g, h, 1}, {i, j, k}, {l, m, n},
   {s, t}, {1000}, Range[7]};

Grid[griD /@ data, 
 ItemSize -> {20, 2}, Spacings -> {0, 0}, Frame -> All, Alignment -> {Center, Center}]

enter image description here

SeedRandom[123]
data = TakeList[Range @ Total @ #, #] & @ RandomChoice[Range[12], 20];
data = Map[Item[#, Background -> Opacity[.3, RandomColor[]]] &, data, {-1}];

Grid[griD /@ data, ItemSize -> {40, 2}, Spacings -> {0, 0}, 
 Frame -> All, Alignment -> {Center, Center}]

enter image description here

Original answer:

w1 = 3;
w2 = 4;
h1 = 3;

Grid[{{a, b, c, d}, {e, f, g,  h},
  {Grid[{{i, j, k}, {l, m, n}}, Dividers -> Center, ItemSize -> {w2, h1}, 
    Spacings -> {0, 0}, Alignment -> {Center, Center}],  SpanFromLeft}}, 
 ItemSize -> {w1, h1}, Spacings -> {0, 0}, Frame -> All, 
 Alignment -> {Center, Center}]

enter image description here

With

w1 = 5;
w2 = (4/3) w1;

we get

enter image description here

and with

w1 = {6, 5, 5, 6};
w2 = {6, 10, 6};
h1 = 2;

enter image description here

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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