7
$\begingroup$

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!

$\endgroup$

2 Answers 2

9
$\begingroup$

You may use Item.

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

Mathematica graphics

Hope this helps.

$\endgroup$
1
  • $\begingroup$ Ahh, was not aware of Item - awesome, thank you! $\endgroup$ Sep 23, 2017 at 13:12
6
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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