4

The following code returns a framed text box that is longer than the standard width of `Graphics'.

Graphics@Text@Framed@Style[FooBarFooBarFooBarFooBarFooBarFooBar, 18]

incomplete framed box

Is there a way to automatically increase the size of the Graphics that is returned to fit the entire framed box?

(This question is related to my question Rasterize and Graphics, size difference and its answer. In particular, one cannot use `Rasterize' to determine the width of the box, it seems.)

Berg
  • 1,059
  • 5
  • 14
  • Win OS 7 x 64 Mathematica v10.2 - does not reproduce whats stated in OP – e.doroskevic Feb 05 '16 at 00:09
  • I do see what the OP described (i.e. clipping of the frame on the left and right side of the picture) on MMA 10.3.1.0 on Win7-64bit. It's quite weird actually. – MarcoB Feb 05 '16 at 00:38

2 Answers2

3

It looks like the front end method to determine the width does not account for the full size of the frame.

To fix/work around you can add margins to the text and rasterize before passing it to Graphics:

Graphics[
 Rasterize[
  Text[Framed[Style[FooBarFooBarFooBarFooBarFooBarFooBar, 18], 
   ImageMargins -> 50]], RasterSize -> 800]]

raster

EDIT

As @Algohi pointed out in the comments (thanks!) we could use a pure function to automatically determine the size of the raster, and pass to Graphics, lending to a solution using native Text object with a specified size:

Graphics[Text@#, Options[Rasterize@#, ImageSize]] &[
 Framed[Style[FooBarFooBarFooBarFooBarFooBarFooBar, 18]]]

edit raster

Peter Roberge
  • 2,357
  • 11
  • 19
3

To make it automatic without rasterizing use ImageSize->Full:

Graphics[Text@Framed@Style[FooBarFooBarFooBarFooBarFooBarFooBar, 18], 
 ImageSize -> Full]

or

Graphics[Text@Framed@Style[FooBarFooBarFooBarFooBarFooBarFooBar, 18], 
 ImageSize -> {Full, Automatic}]
Mike Honeychurch
  • 37,541
  • 3
  • 85
  • 158
  • 2
    Both options increase the maximal automatic Graphics size from 360 to 432 but the problem persists. – Berg Feb 05 '16 at 07:55
  • @Berg What OS are you using? On Mac the entire framed text is visible. i.e. it is not longer than the standard width of Graphics as per your opening sentence. – Mike Honeychurch Feb 05 '16 at 22:43
  • I use Win 10 with MMA 10.3.1. Also, I use a 4K display and because of that a magnification of 2 (because of the low quality user interface of MMA this sometimes breaks things). – Berg Feb 06 '16 at 20:50