Grid with Frame->All: How to not draw frames around empty cells?

7

1

If I have a grid of values that doesn't divide evenly, for example a list of 17 values, and split it into a 7x3 grid, then draw frames around each element:

Grid[Partition[Range@17, UpTo@7], Frame -> All]

This creates grid lines around the 4 empty elements in the last row:

grid

How do I get Grid to not frame those empty elements? I'd still want the bottom borders around 11-14 and the right border on 17, if that makes sense.

I can sort of do it with it TableForm:

TableForm[Partition[Framed[#, ImageSize -> 40] & /@ Range@17, UpTo@7], TableSpacing -> 0]

...but the frame lines look a little bit weird. There's gaps at the top of each vertical line, and they're twice as thick as the horizontal lines (probably because there's the left and right sides of each being pushed together).

Grid's lines are much cleaner; is there a way to do it? Thanks!

numbermaniac

Posted 2017-09-23T11:44:22.733

Reputation: 597

Answers

9

You may use Item.

Grid@Map[Item[#, Frame -> All] &, Partition[Range@17, UpTo@7], {2}]

Mathematica graphics

Hope this helps.

Edmund

Posted 2017-09-23T11:44:22.733

Reputation: 35 657

Ahh, was not aware of Item - awesome, thank you! – numbermaniac – 2017-09-23T13:12:31.970

6

Adapted from the "Frame specific elements" example in the docs for Grid under the subsection Options > Frame:

With[{items = Partition[Range@17, UpTo@7]},
 Grid[items, Frame -> {None, None, Thread[Position[items, _, {2}] -> True]}]
 ]

Mathematica graphics

Michael E2

Posted 2017-09-23T11:44:22.733

Reputation: 190 928