# Why is Alignment not honored in the first two headline columns?

Consider this example:

Grid[{{ TextCell[Row[{"This goes to the first column"}]
, PageWidth -> 80, Bold  (* sufficient to state width only on first row of Grid *)
]
, TextCell[Row[{"and this to the second one"}]
, PageWidth -> 142, Bold
]
, TextCell[Row[{"3rd Col"}]
, PageWidth -> 30, Bold
]
, TextCell[Row[{"4th"}]
, PageWidth -> 80, Bold
]
} (* first grid row finished *)
, { TextCell[Row[{"x=", x}]]
, TextCell[Row[{"x^2="}]]
, TextCell[Row[{x^2}]]
, TextCell[Row[{"Squares are easily computed by hand."}]]
}  (* 2nd grid row finished *)
, { TextCell[Row[{"x=", x}]]
, TextCell[Row[{"Sqrt[x]="}]]
, TextCell[Row[{Sqrt[x]}]]
, TextCell[Row[{"Square roots are more difficult."}]]
}  (* 3rd grid row finished *)
, { TextCell[Row[{"x=", 2}]]
, TextCell[Row[{"Sqrt[x]="}]]
, TextCell[Row[{Sqrt[2]}]], TextCell[Row[{"easy as a symbol"}]]
}  (* 4th grid row finished *)
, { TextCell[Row[{"x=", 2.}]]
, TextCell[Row[{"Sqrt[x]="}]]
, TextCell[Row[{1.4142135623730951}]]
, TextCell[Row[{"endless numerical work."}]]
}  (* 5th grid row finished *)
}
, Alignment -> {Right, Top}  (* why does it not work on the top of the first 2 cols? *)
, ItemSize -> {Automatic, Automatic} (* by this rewrap if window becomes too narrow *)
, Frame -> {All, 1 -> True}
] (*Grid*)


Why are the Grid-options {Right, Top} not honored in the first two headline columns of the grid? Is it a bug in Mma 12.3?

Why is the third column's headline wrapped although it would fit into the column?

The resultant Grid has the nice property that it is re-wrapped if the notebook window becomes too narrow to display the whole grid, which I consider to be very useful.

Background: The problem arose when developing something to print to columns with the hints I got from Is there a function similar to Print which really respects tabs and newlines?

• Add CellFrame -> True to each TextCell to see how Grid is aligning each grid item. Didn't you ask a similar question before in there was white space in some of the grid items that made it look like Grid was failing to align them properly? Jun 28 at 1:01
• Maybe I'm thinking of this question in which the explicit alignment was given in the grid-item object and not to Grid. In formatter[objects, Alignment -> a], the alignment is applied to each object, not to the things (such as text) inside the object. If an object is given an alignment option, it will apply to the things inside the object, and not to the object itself. (BaselinePosition has an effect on or outside the object, arguably.) Jun 28 at 1:13
• Michael E2, you are right: the cell frames are aligned as set forth by option Alignment of Grid. The text in the box however is aligned to the left by default. It seems that one has to expressively request the alignment both on the Row-level and on the Grid-level, i.e. twice! It seems that there are bugs in Alignment->Right and Alignment->Center: If wrapping happens in the text, only one of the wrapped parts is really aligned to the right or center. Jun 29 at 7:12

Why is the third column's headline wrapped although it would fit into the column?

Because you didn't set the width to be wide enough. From what I read in the documentation, it seems that for cells, PageWidth is in printers points. If you set it to 60 or more, the wrapping is eliminated.

Why are the Grid-options {Right, Top} not honored in the first two headline columns of the grid?

They are honored, but the content of the first grid-cell is a TextCell. The entire TextCell is right-aligned, but the text inside the TextCell is aligned according to the options of the TextCell. If you replace the first TextCell with the following, you'll see its text aligned to the right.

TextCell[Row[{"This goes to the first column"}],
PageWidth -> 180, Bold , TextAlignment -> Right]


For what it's worth, you might have an easier time if you use Style instead of TextCell and TextGrid instead of Grid.

• If I increase the width of the first column alone from 80 to 160, 240 and 320, the resultant width is not exactly doubled. In fact, it does not grow from PageWidth->240 to PageWidth->320! (It might depend on the amount necessary for the maximum width in that column). Same with TextGrid[{{TextCell[Row[...]] }}]. I tires TextGrid[{{Style[Row[...]] }}] too, but what is the difference (except that TextGrid instead of Grid uses another font). Aug 3 at 17:41
• It just seems like you're trying to display text, and TextGrid already has some built-in, text-based assumptions. You'll just have to play around with alternatives until you get what you want. But I'd suggest you remove as many of these intermediate expressions as you can. I don't see any need for TextCell, for example. The Rows also seem excessive. Aug 3 at 17:54