4
$\begingroup$

From

G[align_] := 
  Module[{}
  , Framed[align
  , ImageSize -> {{60, 100}, {60, 100}}    (* {{wmin, 
wmax}, {hmin, hmax}} in 1/72 inch  units *)
  , Alignment -> align
  , FrameStyle -> None
  , FrameMargins -> None
  ]
];
Print @ Grid[#, Frame -> {All, All}] &  @
Partition[(G /@ 
Flatten[Outer[List, {Left, Center, Right}, {Bottom, Center, Top}], 1]) , 3]
Print @ Grid[#, Frame -> {All, All}] &  @
Partition[(G /@ Flatten[Outer[List, {-1, 0, 1}, {-1, 0, 1}], 1]) ,3]

I get

Wrong alignment in the result marked red (by hand)

I consider the entries marked red to be wrong:

  • {Left, Top} is not aligned left,
  • {Right, Bottom}, {Right, Center} and {Right, Top} are not horizontally right-aligned.

Can please somebody explain why this happens?

The numerical ones are aligned as expected, however.

When I use Grid[Framed[...]] the result does not have the nice feature PageWidth->width (in 1/72 inch units) of

Grid[{{TextCell[Row[{"..."}], PageWidth -> width, ...]
   , {{TextCell[Row[{"..."}], PageWidth -> width, ...]
   ,
   }
  ,{...}
  }
, ItemSize -> {Automatic, Automatic}
, Frame -> {All, 1 -> True}
]

where the columns become smaller if the window becomes narrower than necessary to accommodate what shall be printed. However, with this construct, alignment also goes wrong.

$\endgroup$
2
  • 2
    $\begingroup$ Try: ImageSize -> {{240, 200}, {60, 100}} $\endgroup$
    – Syed
    Jun 11, 2022 at 15:19
  • 3
    $\begingroup$ I'm curious about the title. It asks about an option of Grid, but you do not use the option in Grid; rather, it's in Frame instead. $\endgroup$
    – Michael E2
    Jun 11, 2022 at 15:45

2 Answers 2

5
$\begingroup$

Try redefining your G like this:

G[align_] := 
  Module[{}, 
    Framed[align, 
      ImageSize -> {{60, 100}, {60, 100}}, 
      Alignment -> align, 
      FrameStyle -> Blue]]

(FrameStyle and FrameMargins are changed.)

Now run the rest of your code just as it is. You should see how the Framed items are placed inside the Grid (specifically, they're placed in each cell with the default alignment for Grid).

To make this more clear, you could add ItemSize to your grid-function:

Grid[#, Frame -> {All, All}, ItemSize -> {10, 10}] &

Now, if you leave the FrameStyle and FrameMargins as I've set them above, but set ImageSize to {{150, 150}, {150, 150}}, you'll see the alignments show up as if they were applied to the grid, which is probably what you were expecting.

$\endgroup$
5
$\begingroup$

What's happening is that your construction is a Grid of boxes (of "FrameBoxes" to be precise). You are aligning the content of each box on the sides of the box itself but the box remains still centered in its environnment (which is a rectangle of the Grid)

This is your code :

     Framed[{Left, Top}, ImageSize -> {{60, 100}, {60, 100}}
     , Alignment -> {Left, Top}
     , FrameStyle -> None
     , FrameMargins -> None]  

enter image description here

The box you create in your code is the red box below :

redBox = Framed[{Left, Top}, ImageSize -> {{60, 100}, {60, 100}}
  , Alignment -> {Left, Top}
  , FrameStyle -> Red
  , FrameMargins -> None]  

enter image description here

A modification of the option ImageSize shows that the option Alignment -> {Left, Top} works fine :

Framed[{Left, Top}, ImageSize -> {150, 50}
 , Alignment -> {Left, Top}
 , FrameStyle -> Red
 , FrameMargins -> None
 ]   

enter image description here

Back to previous result (named redBox), if one put it in a Grid, it gives :

Grid[{{"----------------------"}
  , {redBox}}]

![enter image description here

As the defaut alignment of Grid is Center, it has put the box logically in the middle.

The solution is simply the put the option Alignment -> {Left, Top} in the Grid[] (and not in the box generated by Framed[ ...])

Grid[{{"----------------------"}
  , {redBox}}
 , Alignment -> {Left, Top}]  

![enter image description here

$\endgroup$
0

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