Computing the bounding boxes for Text objects



Regardless of AspectRatio, the Text boxes overlap.

TreeForm[#, AspectRatio -> 1/3, ImageSize -> 700]& @ 
   Import["", "XMLObject"],
   XMLElement[tag:_, _, value:_] :> (tag -> value), Infinity

Mathematica graphics

More generally, I've asked Wolfram Research, Inc. about exposing bounding box coordinates for Text objects so that layout manager or engine can test overlap or compute tilings or disjoint positioning, but it appears to be complicated by the various Text options.

Does anyone know how to at least conservatively approximate the Text bounding box?


Posted 2012-04-11T20:24:19.923

Reputation: 14 508


Consider this answer

– Dr. belisarius – 2012-04-11T20:50:14.193


In the Word Cloud question you'll probably find some methods to detemine bounding boxes of Text.

– Sjoerd C. de Vries – 2012-04-11T21:36:20.380

You may also be interested in using the function stretchText I applied in [](this answer). It's sort of the reverse, in that it takes dimensions that you specify and then deforms the text to fit into that prescribed area. – Jens – 2012-04-18T00:02:27.367



Here a test text. I use a gray background to show how large the bounding box actually is.

t = Graphics[Text["Test", BaseStyle -> {128}, Background -> Gray]]

Mathematica graphics

d = ImageDimensions[ImageCrop[t]];
  FaceForm[], EdgeForm[Red],
  Text["Test", {0, 0}, {-1, -1}, BaseStyle -> {128}],
  Rectangle[{0, 0}, Offset[d, {0, 0}]],
  PointSize[0.05], Point[{0, 0}]

Mathematica graphics

If you want a closer cut for your bounding box, just remove the Background option and ImageCrop again. However, note that offsets in the Text function are with respect to the bounding box Mathematica uses (indicated by the gray box).

t = Graphics[Text["Test", BaseStyle -> {128}]];
d = ImageDimensions[ImageCrop[t]];
  FaceForm[], EdgeForm[Red],
  Text["Test", {0, 0}, {-.97, -0.6}, BaseStyle -> {128}],
  Rectangle[{0, 0}, Offset[d, {0, 0}]],
  PointSize[0.05], Point[{0, 0}]

Mathematica graphics

Note the {-.97, -0.6} offset?

Sjoerd C. de Vries

Posted 2012-04-11T20:24:19.923

Reputation: 63 549

397 = 2 * 42 + 13. // 42 and 13 are two known universal constants. The "2" could come from the renormalization process. – Dr. belisarius – 2012-04-12T05:55:19.563

@belisarius ;-D – Sjoerd C. de Vries – 2012-04-12T08:50:19.567

What happens to these "universal constants" once Style options are applied to Text? – alancalvitti – 2012-04-12T14:34:37.360

2@alancalvitti Belisarius was joking, of course. The offset values for the close-shave bounding box depend on the size of the bounding box MMA works with, and that box depends on the Style options. – Sjoerd C. de Vries – 2012-04-12T19:23:08.623


If you are willing to settle for offset coordinates, you can use Rasterize to get the size of the text box.

text = Style["Text box box", Bold, Large, FontFamily -> "Times"]

Mathematica graphics

textbox = Rasterize[text, "RasterSize"]

pos = {5, 10}

Graphics[{Text[text, pos, {-1, -1}], FaceForm[None], EdgeForm[Black], 
  Rectangle[pos, Offset[textbox, pos]]}, Frame -> True]

Mathematica graphics

The key was the "RasterSize" element in Rasterize. You can use "BoundingBox" too, which will give you the baseline of the text.

You will notice that I drew the bounding box of the text (the Rectangle) using Offset coordinates, not in plot coordinates. You could use offset coordinates for every measurement and position specification of your text primitives to align them properly, but this will be inconvenient. It would be much better if we could easily convert between plot coordinates and offset coordinates. This is unfortunately quite messy and it will depend on the size of the graphic.

Offset coordinates are in printer's points. By default, Mathematica graphics are 360 points wide. If you know the exact PlotRange of your figure, and you can eliminate all padding outside the actual plot range (PlotRangePadding and ImagePadding, I'm not sure about ImageMargins), then you can use the known plot width to convert between offset and plot coordinates. Unfortunately, this conversion will only be valid for a given size of the graphic (the default size). It will not be valid any more if you resize it.


Posted 2012-04-11T20:24:19.923

Reputation: 213 047

Thank you for the effort. Unfortunately we can't rasterize as a key goal is to develop layout engines for real time ZUIs for Health IT (partially using W3C tech standards like HTML5/CSS/SVG, in a way similar to or ever using Stanford's Data Driven Documents (D3) where the text labels, like other graphics objects, are going to get zoomed in and out of.) – alancalvitti – 2012-05-28T01:58:17.693

Just wanted to say these are all good answers. But WRI can't come up w/ a satisfactory solution either. Likely only RWI has the resources to map out all the variations in Style for text. By the way, although bounding box coordinates are not exposed, obviously the boxes generated by expressions like TreeForm do in fact "bound tightly" around Text. – alancalvitti – 2012-05-28T02:09:26.103

@alancalvitti I wrote this answer in a rush yesterday, and it needs some improvement, which I'll do shortly ... but I wanted to note that this solution does not rasterize the text, it merely retrieves its size in pixels. – Szabolcs – 2012-05-28T06:48:18.590

ok excellent. Will review more in-depth then. – alancalvitti – 2012-05-28T16:41:48.113