# How can I set Grid alignments using numbers?

There is an example in the Alignment documentation (it also appears in the Grid documentation):

Grid[{{Graphics[Rectangle[], ImageSize -> 20],
Graphics[Rectangle[], ImageSize -> 30]}, {Graphics[Rectangle[],
ImageSize -> 40], Graphics[Rectangle[], ImageSize -> 50]}},
Alignment -> {{Right, Left}, {Bottom, Top}}]


The Alignment documentation indicates that numbers can be used for the alignment. Therefore the example above should be able to be re-written as

Grid[{{Graphics[Rectangle[], ImageSize -> 20],
Graphics[Rectangle[], ImageSize -> 30]}, {Graphics[Rectangle[],
ImageSize -> 40], Graphics[Rectangle[], ImageSize -> 50]}},
Alignment -> {{1, -1}, {-1, 1}}]


However this has no effect:

So what is the correct way to use numbers for Alignment?

-
I have assumed that because of the two-dimensional specification accepted by Grid (as you use) it does not support the individually two-dimensional alignment specifications. –  Mr.Wizard Jan 20 '12 at 9:34
The documentation actually only allows for numerical settings using Alignment->x or Alignment->{x,y}. But I can't get even that to work. –  Verbeia Jan 20 '12 at 20:29
My interpretation of the documentation is that taking the first code you should be able to substitute 1 for Right, -1 for Left, -1 for Bottom and 1 for Top and get the same result. –  Mike Honeychurch Jan 21 '12 at 0:00

It seems that the handling of the Alignment option is not consistent for all functions using it. Panel for instance seems to support numeric values for this option

Manipulate[
Panel["\[Times]", ImageSize -> {100, 50}, Alignment -> {x, y}],
{x, -1, 1},
{y, -1, 1}
]


while with Grid this is not supported. Knowing this, you could check functions you're interested in with something simple like

SetAttributes[AlignmentTest, HoldAll];
AlignmentTest[func_] := Row[{
Manipulate[
Append[func, Alignment -> {x, y}],
{x, -1, 1},
{y, -1, 1}
],
Manipulate[
Append[func, Alignment -> {x, y}],
{x, {Left, Center, Right}},
{y, {Bottom, Center, Top}}
]
}]


You see that in many cases numeric values for the Alignment option can be used

AlignmentTest[

AlignmentTest[
Manipulate[Plot[Sin[x (1 + a x)], {x, 0, 6}], {a, 0, 2},
ContentSize -> {500, 500}]]

AlignmentTest[
Grid[{{"\[Times]", "\[Times]", "\[Times]"}, {"\[Times]", "\[Times]",
"\[Times]"}}, Frame -> All, ItemSize -> {10, 10}]]

AlignmentTest[Overlay[{Graphics[{Disk[]}], Slider2D[]}, All, 2]]

-
+1 This Manipulate is useful to show the advantage of using numbered Alignment. You can align anywhere between -1 and 1 rather than just Left, Center, Right (-1, 0, 1) and Bottom, Center, Top (-1, 0, 1). –  Mike Honeychurch Jan 21 '12 at 22:36

The answer given by @halirutan lead me to a thought that we can use Pane[..] as elements of Grid[...] to obtain desired behavior. Panel[...] can work too, but it introduces extra boundaries.

Grid@Partition[MapThread[Pane[Graphics[Rectangle[], ImageSize -> #1],
ImageSize -> {60, 60}, Alignment -> #2] &,{{20, 30, 40, 50}, {{1, -1},
{-1, -1}, {1, 1}, {-1, 1}}}], 2]


Grid[Table[Pane["X", ImageSize -> {60, 60}, Alignment ->
RandomReal[{-1, 1}, 2]], {n, 2}, {m, 5}], Frame -> All]


-

Can't currently be done -- it is supposed to be possible but doesn't work due to a bug.

To achieve this you need work around such as @Vitaliy's suggestion. Numbered alignment is quite useful so I hope this bug is fixed in the next version.

-
Was it confirmed by WRI? Isn't this the proper time then to add bugs to the tag list? –  István Zachar Jun 23 '12 at 7:47
V10.01 and it is still broken. And Mapping with Panes etc. is not a solution, for dynamic content, constant mapping affects performance. The Grid is kind of sick joke, I'm wasting lately a lot of time to do what should be possible with single option. –  Kuba Oct 11 '14 at 7:26
@Kuba I couldn't agree more. However designed this stuff has never made layouts themselves. More to the point it seems that there has been a lack of coordination or project management in the design of Grid, Pane, Panel and so on. The result is that you end up just wasting a lot of time on this stuff –  Mike Honeychurch Oct 11 '14 at 22:48