Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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)?

Grid[{
  {"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)?

share|improve this question
2  
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...). –  Yu-Sung Chang Mar 27 '12 at 3:34
add comment

2 Answers 2

up vote 10 down vote accepted

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

data={{"000000000\n111111111\n222222222","000000000"},{"000000000","000000000"}}

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

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

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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