# Strange behaviour when line-wrapping text in a Pane

I noticed some strange behaviour of Pane.

In the result from the following code, the line wrapping is not consistent: why do the first and last line contain four times "test" while the other lines only three times?

Secondly, why doesn't it fill up the green rectangle with text, as it is supposed to do with its width of 84?

Graphics[{
Nest[Lighter, Blue, 6], Rectangle[{0, 0}, {110, 70}],
Nest[Lighter, Green, 6], Rectangle[{16, 10}, {100, 60}],
Darker@Red,
Inset[
Pane[
Text["test test test test test test test test test test test test \
test test test test test", BaseStyle -> {12}]
, 84]
, {16, 60}, {Left, Top}]
}]


Am I using Pane in a wrong way?

EDIT

As for the second question, I realised that the width I specify for Pane(which is 84) is not in the same units as the rectangles, which becomes apparent by using Framed around Pane:

But this still leaves the question why the line wrapping doesn't work the same on all lines, and how to relate the point units of the width of Pane to those used in the Graphics environment. I'd like to be able to draw a rectangle of given width and height, and wrap text inside of it (possibly two or more panes in the same rectangle).

EDIT 2

Even stranger (as I just noticed): what I get on my screen differs from the exported image (using right-click); the line wrapping is again different. I made a screenshot to illustrate the difference:

This is from the same code (even from the same execution).. Does anybody know why this happens?

Thanks a lot!

-

Note that "Pane[expr, w] makes the pane be w printer's points wide, linewrapping the contents if necessary."

Graphics coordinates and printer's points are not necessarily the same size. You can control their relationship through the ImageSize option (to Graphics). This can also explain the difference when Exporting; use the ImageSize option to Export to control this behavior, too.

The Text around the string seems to make the wrapping behave oddly. I omit it below, which seems to help. The wrapping is still "interesting."

Graphics[{Nest[Lighter, Blue, 6], Rectangle[{0, 0}, {110, 70}],
Lighter[Green, 1 - (2/3)^6], Rectangle[{16, 10}, {100, 60}],
Darker@Red,
Inset[Pane[
Style["test test test test test test test test test test test test test test test test test",
TextAlignment -> Left], 84,
BaseStyle -> {12}], {16, 60}, {Left, Top}]},
ImageSize -> 110]


Evidently, the rectangles need to be a bit taller. :)

(You might note I snuck in an alternative to nesting Lighter, just fyi.)

-

Not much different to Michael E2s answer but using your code with LineIndent added

Graphics[{Nest[Lighter, Blue, 6], Rectangle[{0, 0}, {110, 70}],
Nest[Lighter, Green, 6], Rectangle[{16, 10}, {100, 60}], Darker@Red,
Inset[Pane[
Text[Style[
"test test test test test test test test test test test test \
test test test test test", LineIndent -> 0]], 84], {16, 60}, {Left,
Top}]}]


That is not the way I would prefer text to wrap. I'd prefer lines to be completely wrapped from top to bottom rather than from bottom to top. But even in the documentation that is what occurs:

So it appears to be by design ...even though IMO it is undesirable.

-

I agree with Michael E2 that Text adds some strange behavior and I can guess it is not really needed here because you have Inset managing the coordinates where to put the text inside the graphics. Moreover, using Style directly on the text string, you can also manage the line indenting. For instance a better result is get using LineIndentMaxFraction -> 0

Graphics[{Nest[Lighter, Blue, 6], Rectangle[{0, 0}, {110, 70}],
Lighter[Green, 1 - (2/3)^6], Rectangle[{16, 10}, {100, 60}],
Darker@Red,
Inset[Pane[
Style["test test test test test test test test test test test \
test test test test test test", 12, TextAlignment -> Left,
LineIndentMaxFraction -> 0], 84], {16, 60}, {Left, Top}]}]


As you can see LineIndentMaxFraction and TextAlignment do the right job, the wrapping is now correct. For what I know, wrapping is done using some formulas and this is why in some particular cases, I guess depending on the ratio between the length of words and the total available row space, it gets strange results. In my experience adding TextAlignment or similar options normally should help. Indeed, even if you accept the default option, when you explicitly declare the option it might change the internal calculations and adjust the result.

-

If you use a TextCell and apply styles only through StyleSheets it behaves a tad better!

Graphics[{Nest[Lighter, Blue, 6], Rectangle[{0, 0}, {110, 70}],
Nest[Lighter, Green, 6], Rectangle[{16, 10}, {100, 60}], Darker@Red,
Inset[Pane[
TextCell[
"test test test test test test test test test test test test \
test test test test test", "Text"], 84], {16, 60}, {Left, Top}]}]


-