I always thought that Grid (and the underlying GridBox) and the general handling of tabular objects is a bit weird in Mathematica. One particularly annoying fact is that while one can set column widths explictly, the same cannot be done to row heights, because each row is set to the height of its largest content by default. I don't really understand this asymmetry between the two dimensions. Does anyone have any insight on this matter?

As a more specific question: How to set the row height to be exactly 1, by clipping the content if necessary (i.e. some part of it should not be visible)?

  {"000000000\n111111111\n222222222", "000000000"},
  {"000000000", "000000000"}
 ItemSize -> {{10, 5}, {1, 1}}, Frame -> All]

Mathematica graphics

Can this be done with only Grid or do I have to map a Pane with explicit height value to each element (converting from pixels to ems, as Pane uses the latter)?

  • 5
    $\begingroup$ In fact, the behavior of ItemSize is explained (the third example of Scope section: it sets minimal height, not fixed height) but I agree that it is not entirely consistent or desirable behavior (I do not know the rationale behind this design...). $\endgroup$ – Yu-Sung Chang Mar 27 '12 at 3:34
  • 1
    $\begingroup$ @Yu-SungChang Could we imagine this behaviour being "corrected" in the future? I have the filling that there's a lot of historic "garbage" accumulated in all these definitions, and this, mixed up with some bugs, makes a lot of confusion and frustration. For instance, I find the following behaviour extremely confusing: Grid[{{Item[Rotate["this is a long test example", Pi/2],ItemSize -> {3, 50}]}}, Frame -> True]. I know that you probably can't answer to this comment, but I would like to express my frustration. $\endgroup$ – P. Fonseca May 24 '15 at 14:26

The Pane construct is quite flexible. I cannot imagine not using it with table for fluid sizes control and features. Here are your data:


This will fix the cell size and cut off the content if it won't fit:

Grid[Map[Pane[#, ImageSize -> {80, 30}] &, data, {2}], Frame -> All]

enter image description here

This will fix the cell size and shrink the content if it won't fit

Grid[Map[Pane[#, ImageSize -> {80, 30}, ImageSizeAction -> 
"ShrinkToFit"] &, data, {2}], Frame -> All]

enter image description here

Use Scrollbars to view the content that did not fit

data2 = 200! {{1, 1}, {1, 1}};
Grid[Map[Pane[#, ImageSize -> {200, 100}, Scrollbars -> 
{False, True}] &, data2, {2}], Frame -> All]

enter image description here

Forbid line-breaks to use horizontal scrolling only for small row height (updated after @Yu-Sung comment):

data2 = 200! {{1, 1}, {1, 1}};
Grid[Map[Pane[#, ImageSize -> {150, 30}, Scrollbars -> {True, False}] &, 
data2, {2}], Frame -> All, BaseStyle -> {LineBreakWithin -> False}]

enter image description here

  • 3
    $\begingroup$ LineBreakWithin can be set as BaseStye option value of Grid, instead of individually using Style: Grid[blah..., BaseStyle -> {LineBreakWithin -> False}] $\endgroup$ – Yu-Sung Chang Mar 27 '12 at 3:29
  • $\begingroup$ @Yu-SungChang Nice catch ;-) , thanks, updated! $\endgroup$ – Vitaliy Kaurov Mar 27 '12 at 3:34

Instead of just Grid you might try putting together Row and Column expressions. And then control the height as well as width using ItemSize option in the columns.


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.