# TextAlignment->Center in a notebook with WindowSize->All

I'm creating a dialog with CreateDialog. I want one of its cells to have its text centered.

By default (and I like that default), the notebook created has WindowSize set to All, which I'm guessing adjusts the window width according to the widest cell.

The problem

The centered cell doesn't show. What's a neat way to make this work? I could live with programatically querying the "real" size of the dialog, so that I can then set it explicitly in WindowSize, in which case TextAlignment->Center seems to work, but I don't know how.

Sample code

CreateDialog[{"¿Hasta dónde se lava la cara un pelado?",
Style["bo", TextAlignment -> Center]}];


Same happens with

CreateDialog[{"¿Hasta dónde se lava la cara un pelado?",
Cell["bo", TextAlignment -> Center]}];


However

CreateDialog[{"¿Hasta dónde se lava la cara un pelado?",
Style["bo", TextAlignment -> Center]}, WindowSize -> {300, 100}];

-

There is no easy solution involving multi-cells.

I want to explain why it is a problem--you can call it a bug, but it is rather a tricky situation for Mathematica.

To determine the TextAlignment or TextJustification correctly, Mathematica needs to know the width of an enclosing window of the cell. Now, the problem is, the window width can be only determined when you know the width of the contents, since it is set to WindowSize->All. So we are in effect creating a circular argument: Mathematica can't decide one value because it depends on each other.

There are a number of similar situation. Try Pane[..., {Full, All}] for instance. In this case, the width of Pane should be determined by the enclosing cell, but works only when it is explicit (otherwise, the same circular argument). Compare these two cases:

CreateDialog[Framed[Pane["A", {Full, Full}], FrameMargins -> 0]];
CreateDialog[Framed[Pane["A", {Full, Full}], FrameMargins -> 0], WindowSize -> {100, All}];


You can easily tell which one is which (besides, the second image is correct. Full means that the pane should extend to the full width of the enclosing object--in this case, the notebook).

Anything involving undetermined object size which depends on the enclosed contents (WindowSize with All, or ImageSize with All, Automatic, Full) and the content properties (such as Scaled or ImageScaled size, coordinates, alignment, etc...) which depend on the size of the enclosing object (whether it is Notebook or Graphics, etc..), it is always very tricky to get a right solution. Mathematica tries its best to do a sensible thing, but like this case, sometimes it just can't handle it well...

-

Simply avoiding celled structure:

CreateDialog[Column[{"¿Hasta dónde se lava la cara un pelado?", "bo"}, Alignment->Center]]


But if you want to use celled structure, I think it's pretty simple and neat to be able to do things like:

CreateDialog[Flatten@{
TextCell@Style["¿Hasta dónde se lava la cara un pelado?",TextAlignment->0],
Table[TextCell@Style["bo",TextAlignment->s],{s,{-1,-.5,0,.5,1}}]},
WindowSize->{230,180}]


-
This helps. I'll be using this for the case at hand for now. But I still hope for a more general answer, in which the dialog could be multi-cell – Rojo May 15 '12 at 2:58