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:


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!


You may use Item.

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

Mathematica graphics

Hope this helps.

  • $\begingroup$ Ahh, was not aware of Item - awesome, thank you! $\endgroup$ – numbermaniac Sep 23 '17 at 13:12

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


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.