19
$\begingroup$

Bug introduced in 9.0 or earlier, persisting through 13.2 or later


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?

$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$ Commented Jan 20, 2012 at 9:34
  • $\begingroup$ The documentation actually only allows for numerical settings using Alignment->x or Alignment->{x,y}. But I can't get even that to work. $\endgroup$ Commented Jan 20, 2012 at 20:29
  • 2
    $\begingroup$ 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. $\endgroup$ Commented Jan 21, 2012 at 0:00

3 Answers 3

17
$\begingroup$

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]] 
$\endgroup$
1
  • 2
    $\begingroup$ +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). $\endgroup$ Commented Jan 21, 2012 at 22:36
15
$\begingroup$

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

$\endgroup$
8
$\begingroup$

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.

$\endgroup$
3
  • 2
    $\begingroup$ Was it confirmed by WRI? Isn't this the proper time then to add bugs to the tag list? $\endgroup$ Commented Jun 23, 2012 at 7:47
  • $\begingroup$ 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. $\endgroup$ Commented Oct 11, 2014 at 7:26
  • $\begingroup$ @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 $\endgroup$ Commented Oct 11, 2014 at 22:48

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.