The width returned by Rasterize
is not always correct, as you found out, and since you aren't going to Rasterize
before building your tree then the size of the raster graphics is irrelevant in this case.
This is the best way to get the size of the text cell (thanks to Sjoerd C. de VriesSjoerd C. de Vries),
{w1, h1} = ImageDimensions@ImageCrop@Graphics@ExpBox[Fooooo/2];
{w2, h2} = ImageDimensions@ImageCrop@Graphics@ExpBox[Bar];
Of course, you can just grab the width by using First
as in your code, but I also noticed that the height needs adjusting in the latter case.
Then you can plot
With[{h = Max[{h1, h2}]},
Graphics[{ExpBox[Fooooo/2, {w1/2, 0}], ExpBox[Bar, {w1 + w2/2, 0}]},
PlotRange -> {{0, w1 + w2}, h {-.5, .5}},
ImageSize -> {w1 + w2, h}]
]
I'm the type that always makes a function for something I'm doing more than twice, so you can wrap it all up like this:
combBoxes[exps__] := Module[{widths, heights, boxes, totalsize},
{widths, heights} =
Transpose@(ImageDimensions@ImageCrop@Graphics@ExpBox@# & /@ {exps});
heights = {Total@widths, Max@heights};
widths =
Total[widths[[;; #]]] - widths[[#]]/2 & /@ Range@Length@widths;
boxes = ExpBox[#1, {#2, 0}] & @@@ Transpose[{{exps}, widths}];
Graphics[boxes,
PlotRange -> {{0, heights[[1]]}, heights[[2]] {-.5, .5}},
ImageSize -> heights]
]
combBoxes[Foooooo/2, Bar]
combBoxes[This, is, how, I, would, make, these, boxes,
FooooooooBaaarrrrr^2/2]