8
$\begingroup$

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

Grid[{{x, x,
   Framed[
    Pane[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

$\endgroup$
2
  • 3
    $\begingroup$ All these annoying quirks of the 2D layout engine... Also note that none of the cells are actually squares, though item size is 5x5. $\endgroup$ Commented Oct 27, 2012 at 10:47
  • $\begingroup$ @IstvánZachar I'd completely overlooked that the cells aren't squares! $\endgroup$ Commented Oct 27, 2012 at 22:00

3 Answers 3

4
$\begingroup$

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};

Grid[
 {{x, x, x, x, x}, {x, x, 
   Framed[Pane[x, 
     ImageSize -> 
      Dynamic[itemSize*{CurrentValue["FontMWidth"], 
        CurrentValue["FontLineHeight"]}],
     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

125%

enter image description here

50%

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.

$\endgroup$
3
$\begingroup$

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:

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

Mathematica graphics

$\endgroup$
2
  • $\begingroup$ And in this way we can create true square cells! God bless the lord almighty! $\endgroup$ Commented Oct 27, 2012 at 11:52
  • $\begingroup$ 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. $\endgroup$ Commented Oct 27, 2012 at 21:53
1
$\begingroup$

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.

$\endgroup$

Your Answer

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

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