Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Is there a way to get the dimensions that the results of a Text[] will occupy within a Graphics[]? Specifically, if I run something like

Graphics[Text[
  Style["how quickly daft jumping zebras vex", 
   FontFamily -> "Verdana", FontSize -> 20]], PlotRange -> 1]

how can I get the dimensions relative to the coordinate system of the text?

edit: I have the PlotRange->1 in there because I'm using the text on a larger field and manipulating the text and other graphics using transformations that depend on the coordinate system. The text also is likely to change (hence my using sample text), so I'd like to have the transformations depend on measurements derived from the text (taking into account the styling of it).

share|improve this question
    
Just a point, PlotRange is irrelevant in this object, even though it is visible in the AbsoluteOptions. You probably want Graphics[Text[ Style["how quickly daft jumping zebras vex", FontFamily -> "Verdana", FontSize -> 20]], ImageSize -> 400, AspectRatio -> 0.1] –  Verbeia Feb 12 '12 at 23:21
1  
@Verbeia: but why 400 and 0.1? I had the PlotRange->1 in there because I'm using text on a larger field and manipulating the text and other graphics using transformations that depend on the coordinate system... and I suppose I should edit that information into the question. –  Isaac Feb 12 '12 at 23:25
    
Isaac, they were the round numbers that fit nicely on my system. I should probably have said "something like" that code. By the say, Chris' answer is a much better approach for your particular problem so I will delete my partial answer. Hopefully someone poses a question soon where these CurrentValue options are the answer, because I think they are worth documenting. –  Verbeia Feb 13 '12 at 0:27
1  
@Verbeia please pose a question yourself even if you know the answer. I would like to read more about CurrentValue and fonts. –  Mike Honeychurch Feb 13 '12 at 4:39
    
@MikeHoneychurch Ok :) mathematica.stackexchange.com/q/1684/8 and I actually don't know the answer –  Verbeia Feb 13 '12 at 4:56

2 Answers 2

up vote 7 down vote accepted

Here you go:

t = Text[Style["how quickly daft jumping zebras vex", 
    FontFamily -> "Verdana", FontSize -> 20]];
{l, h} = d = Rasterize[t, "RasterSize"];
Graphics[{Green, Rectangle[{0, 0}, d], Black,
  Inset[t]}, PlotRange -> {{0, l}, {0, h}}, ImageSize -> l]

enter image description here

share|improve this answer
1  
I can definitely use this to get the job done, but it's in terms of image size/pixels, rather than coordinates... –  Isaac Feb 13 '12 at 0:22
1  
Actually... it looks like it's rasterizing the output form of the Text[] command: i.stack.imgur.com/YkeeK.png is the output of Rasterize[Text[Style["Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris varius imperdiet lectus nec bibendum. Maecenas felis ipsum, elementum mattis tristique id, mollis a nisl. Nulla magna dolor, ullamcorper eleifend posuere non, vehicula ut augue.", FontFamily -> "Hypatia Sans Pro", FontSize -> 20], {0, 0}, {0, 0}]], so Rasterize[..,"RasterSize"] gives {724, 104} which isn't the length of the text on a single line... –  Isaac Feb 13 '12 at 0:34
    
@ Isaac - You can inset the resultant graphics into another graphic using coordinates, e.g. Graphics[{Red, Rectangle[{0, 0}, {1, 0.2}], Inset[%, {0, 0}, {0, 0}]}, PlotRange -> {{0, 1}, {0, 0.2}}, ImageSize -> 400] –  Chris Degnen Feb 13 '12 at 0:44
    
The problem is the line wrapping—I want the width of the rendered text all on one line, but Rasterize[] is wrapping the text at the width of the window. –  Isaac Feb 13 '12 at 0:46
4  
You can also use the option LineBreakWithin -> False in the options passed to Style if line wrapping is what you are trying to avoid. –  Andy Ross Feb 13 '12 at 2:35

Faced with the same problem, I found this solution:

a = Text[Style["vvddfhfh", 40], {0, 0}];
{width,height} = ImageDimensions[ImageCrop[Graphics[a]]]

which gives me the size of the actual rendering of the styled text, as opposed to the render of the output form of the Text command seen in the other answer

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.