Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am wanting to make a Pane scale to fit (if Spacings are zero) a Grid element.

Grid[{{x, x,
     ImageSize -> {Scaled[1], Scaled[1]},
     ImageMargins -> 0,
     Alignment -> Center],
    FrameMargins -> 0,
    ImageMargins -> 0], x, x}, {x, x, x, x, x}, {x, x, x, x, x}}, 
 Background -> {None, None, {{1, 3} -> Red}}, ItemSize -> {5, 5},
 Frame -> All,
 Spacings -> {0, 0}]

Note that I added the Frame solely to outline the Pane so that the dimensions could be seen.

On my system, OS X 10.6.8, Mma V8.0.4, the scaling works in the x direction but not in the y direction. Can anyone offer an explanation and solution (or could this be a bug)?

enter image description here

share|improve this question
All these annoying quirks of the 2D layout engine... Also note that none of the cells are actually squares, though item size is 5x5. – István Zachar Oct 27 '12 at 10:47
@IstvánZachar I'd completely overlooked that the cells aren't squares! – Mike Honeychurch Oct 27 '12 at 22:00

Quite a long saga but as an FYI this is what I have learned from tech support:

Firstly the documentation for ItemSize, under more information, says

In Grid, w is measured in ems, and h in line heights.

I'd overlooked that. With that information some control should be possible:

magicRatio = CurrentValue["FontMWidth"]/CurrentValue["FontLineHeight"];
itemSize = {5, magicRatio*5};

 {{x, x, x, x, x}, {x, x, 
     ImageSize -> 
     ImageMargins -> 0,
     FrameMargins -> 0,
     Alignment -> Center],
    FrameMargins -> 0,
    ImageMargins -> 0], x, x}, {x, x, x, x, x}}, 
 Background -> {None, None, {{2, 3} -> Red}}, ItemSize -> itemSize, 
 Frame -> All, Spacings -> {0, 0}, Alignment -> {Center, Center}]

On Mac 10.6.8 Mma V8.0.4

enter image description here

This is close -- may be good enough in most cases -- but no cigar. The problem is that even if this is close enough for someones usage requirements it only works when notebook magnification is set to 100%. For example


enter image description here


enter image description here

In theory you would think you could correct for this by getting the magnification

CurrentValue[EvaluationNotebook[], Magnification] /. Inherited -> 1

and you can as long as you don't make the image size Dynamic because if you do adding the magnification correction breaks the Pane image size:

dynamic image size, magnification 75%

enter image description here

static image size, magnification 75%

enter image description here

And an added note/FYI is that the line height is different within a Dynamic

enter image description here

In the end tech support have said that the developers don't see any easy way around this sort of problem.

see also this Q&A though note that even allowing for frame line width I couldn't get the pane to fit precisely in the grid cell.

share|improve this answer

Too long for a comment but I want to point out some observations. I don't think, that Grid is the bad guy here. It is the use of Scaled in this context where it does not seem to be clear, what exactly it means.

When we use Scaled inside a Graphics, than the situation seems clear and predictable. We can draw a Rectangle exactly from bottom to top, no matter what the specific settings for PlotRange are

Graphics[{Red, Rectangle[Scaled[{0.4, 0.0}], Scaled[{0.6, 1.0}]]}, 
 PlotRange -> {{-5, 5}, {-5, 5}}, Frame -> True]

Mathematica graphics

Once we leave this, it is not so clear anymore. We find for instance this example in the help of Scaled

Cell sizes by a fraction of the document width: Mathematica graphics

But what does this mean for specifying heights in such a setting? We can simply test this

Framed[Pane[Null, {Scaled[1], Scaled[1]}]]

Here, no matter what I put as value in the second Scaled, I always have a frame going over the whole width of the document having the same small height.

I believe, that in Grid the behavior is the same. While it is perfectly fine to specify the width with Scaled, for the height that does not work.

One possible solution is to use Grid only as a layout without specifying the content size and create equally big contents. This is how it is done in most GUI layout managers too: Choose the layout and let the content decide how big it has to be:

 Table[Framed[Pane["x", ImageSize -> {50, 50}, Alignment -> Center], 
   Background -> ColorData[4, RandomInteger[100]]], {5}, {5}], 
 Spacings -> {0, 0}]

Mathematica graphics

share|improve this answer
And in this way we can create true square cells! God bless the lord almighty! – István Zachar Oct 27 '12 at 11:52
Note that Pane[Item[x,ItemSize->{5,5}],ImageSize -> {Scaled[1], Scaled[1]}] fails as well. To me this is unintuitive. In the x direction the scaling is not based on document width but on grid element width. It scales to fill the grid element. Therefore why wouldn't/shouldn't the y axis scaling be based on grid element height? In other words given that it works analogous to Graphics in the x direction I don't see why it doesn't in the y direction. – Mike Honeychurch Oct 27 '12 at 21:53

One ugly workaround is to give explicit size values, converted from lineheight to printers points using a system-dependent magic number (just like here and here):

magic = 8;
item = Framed[Pane[x, ImageSize -> {5, 5}*magic, Alignment -> Center],
    Background -> Pink];
 Grid[{{x, x}, {x, x}}, Alignment -> {Center, Center}, 
  ItemSize -> {5, 5},
  Frame -> All, Spacings -> {0, 0}], 
 Grid[{{x, item}, {x, x}}, Alignment -> {Center, Center}, 
  ItemSize -> {5, 5},
  Frame -> All, Spacings -> {0, 0}]

Mathematica graphics

I've included the standard grid on the left for comparison so that the magic number is chosen correctly.

share|improve this answer

Your Answer


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.