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.

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

enter image description here

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:

enter image description here

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

share|improve this question
    
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
2  
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

3 Answers 3

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[
 Button["Click Here", Print[10!], ImageSize -> {100, 100}]]

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]]
share|improve this answer
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]

enter image description here

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

enter image description here

share|improve this answer
up vote 6 down vote accepted

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.

share|improve this answer
2  
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 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 at 22:48

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.