# Expand Graphics based on all elements

How might I force the following Graphics element to expand to contain the Inset text?

Graphics[{White, Rectangle[{0., 0.0}, {1, 1}],
Yellow, Rectangle[{0., 0.0}, {.2, 1}],
Black,
Inset[
Style[ToString[Unevaluated@abcdefghijklmnopqr],
FontSize -> Scaled[.4]]
, ImageScaled[{0.2753623188405797,
0.48550724637681175}], {Left, Center}]
PlotRangeClipping -> True]


The following gives the desired output although it doesn't solve the issue because ultimately I am trying to nest the text inside another Graphics element: Row[{Graphics[{}, ImageSize -> {10, 23}, Background -> Yellow],Text["abcedefghijklmnopqr"]}].

Ultimately I have several nested Insets but unfortunately I can't seem to force the Insets to nest inside of each other and therefore show entirely on the screen. These plot range questions seems to be related. The first question titled "How to determine PlotRange to include all Graphics" is likely a duplicate, although I believe you must ImageSize and PlotRange to get correct values.

-
I'm not sure there's a good reliable way to expand Insets. So is your aim simply to be able to nest Insets? –  Szabolcs Mar 15 at 17:28
@Szabolcs Ideally yes I would like to be able to nest them. I initially just wanted Text[] elements to show(which are internally represented as Insets). It is interesting if you run the following g = Graphics[{GrayLevel[0], Inset[Style["abcdefghijklmnopqrstuvwxyz", FontSize -> Scaled[0.2]], Scaled[{0.27, 0.48}], {Left, Center}]}] ImportString@ExportString[g, "PNG", ImageSize -> {1000, Automatic}] you can see 1) by expanding and shrinking the nb you can see more more 2) Exporting and then importing the image results in being able to see the entire text. –  Liam William Mar 15 at 17:34

I can't address your larger example because you didn't include code for it, but for the specific graphic in question you might use this:

Graphics[{White, Rectangle[{0., 0.0}, {1, 1}], Yellow, Rectangle[{0., 0.0}, {.2, 1}],
Black, Inset[Style["abcdefghijklmnopqr", FontSize -> Scaled[.4]],
Scaled[{0.2753623188405797, 0.48550724637681175}], {Left, Center}]}]


Specifically I changed ImageScaled to Scaled and removed your options, restoring the default PlotRangeClipping -> False behavior. If this doesn't work for you please explain why.

-
What version of M are you using? In version 8 and 9 the above gets cut off at the letter h for me. –  Liam William Mar 15 at 15:27
@Liam I'm using version 7 under Windows. I also have various non-default options (e.g. font size) that might affect this. I'm sorry if this is not useful. –  Mr.Wizard Mar 15 at 15:31
There is definitely something interesting going on because I can see the entire text in this example up to character r. Table[Graphics[Inset[Style[StringJoin@CharacterRange[FromCharacterCode@97, FromCharacterCode[97 + i]],FontSize -> Scaled[.2]], Scaled[{0.27,0.48}], {Left, Center}]],{i, 1, 23}] Do you mind posting where character it cuts off for you? Maybe there is an option for text cut off? –  Liam William Mar 15 at 15:44
@Liam I get this: i.stack.imgur.com/TWphp.png –  Mr.Wizard Mar 15 at 16:01
Thanks for the response. I get the same thing unless I shrink and expand the nb window then the displayed content changes. I believe this question relates to this issue. –  Liam William Mar 15 at 17:36

The threat of nested Insets makes me quail. I find it hard to use, although occasionally I can get it right. Text in an Inset poses an additional problem because many options are automatically handled for you by the Front End, although not always the way you wanted. It seems knowledge of the plot range(s) and image size(s) is helpful in getting it right, as alluded to in the statement of the question.

Here's a way, used in this question, to convert text to Graphics primitives. Inset may be avoided altogether.

text = First /@
ImportString[
ExportString[
Style[ToString[Unevaluated@abcdefghijklmnopqr],
FontFamily -> "Times New Roman"], "PDF"], "PDF"];
{bottomLeft, topRight} =
Transpose[{Min[#], Max[#]} & /@
Transpose@Cases[text, {_Real, _Real}, Infinity]];

textheight = 1;
Graphics[{White, Rectangle[{0., 0.0}, {1, 1}], Yellow,
Rectangle[{0., 0.0}, {.2, 1}], Black,
Translate[            (* translate and scale the text to fit *)
Scale[text, textheight / Last[topRight - bottomLeft],
bottomLeft], -bottomLeft + {0.3, 0}]}]


Because the glyphs are represented as FilledCurves they are rescaled with the graphics when the image changes size.

-