2
$\begingroup$

When I try this:

Framed@Graphics[{Text[
    Style["Text", FontSize -> 50, Bold, Black, Background -> Green, 
     FontFamily -> "Calibri"]]}]

enter image description here

I get a centered text with a large empty space around.

How can I align the text e.g. to left or how can I make the frame as large as the text is?

$\endgroup$

3 Answers 3

4
$\begingroup$
txt = Text[Framed[Style["Text", FontSize -> 150, Bold, Black, 
    FontFamily -> "Calibri"], Background -> Green, ContentPadding -> False]]; 
Graphics[txt, ImageSize -> Rasterize[txt, "RasterSize"]]

enter image description here

Alternatively, use the second argument of Text to specify the position:

txtb = Text[Framed[Style["Text", FontSize -> 150, Bold, Black, 
     FontFamily -> "Calibri"], Background -> Green, ContentPadding -> False], {0, 0}];
Graphics[txtb]

same picture

txt2 = Text[Framed[Style["TextTextTextTextText\nTextTextTextTextText", 
     FontSize -> 64, Bold, Black, FontFamily -> "Calibri", 
     LineSpacing -> {1, -10}], Background -> Green, ContentPadding -> False]];
Graphics[txt2, ImageSize -> Rasterize[txt2, "RasterSize"]]

enter image description here

Using the second approach:

txt2b = Text[Framed[Style["TextTextTextTextText\nTextTextTextTextText", 
     FontSize -> 64, Bold, Black, FontFamily -> "Calibri", 
     LineSpacing -> {1, -10}], Background -> Green, ContentPadding -> False], {0, 0}];
Graphics[txt2b]

same picture

$\endgroup$
4
$\begingroup$

I would use a slightly different approach which gives more flexibility.

Graphics[{Green, EdgeForm[Black], Rectangle[{0, 0}, {2, 1}], (*Green Background*)
 Inset[Style["Text", FontSize -> 150, Bold, Black,FontFamily -> "Calibri"],
 {0, 0}, {Left, Bottom}]}] (*position and alignment*)

enter image description here

Or some tilting

q = Pi/6;
Graphics[{Green, EdgeForm[Black], Rectangle[{0, 0}, {2, 1}], 
 Inset[Style["Text", FontSize -> 100, Bold, Black, FontFamily -> "Calibri"],
 {0.5, 0}, {-1,-0.5}, {Cos[q],Sin[q]}]}]

enter image description here

Look at Inset for more options.


For Text in a box

wordbox[text_, fontsize_, fontcolor_, font_, background_] := 
       Framed[ImageCrop@Rasterize@Style[text, FontSize -> fontsize, Bold, fontcolor, 
       FontFamily -> font, Background -> background],
       Background -> background, FrameMargins -> 0]

wordbox["Text", 100, Blue, "Calibri", Red]
wordbox["Texture", 100, Black, "Calibri", Green]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thank you for the solution. How to you set the rectangle to the minimal size which holds the word "Text"? $\endgroup$
    – mrz
    Dec 27, 2018 at 12:26
3
$\begingroup$

You need to adjust ImageSize

Framed[Graphics[{Text[
    Style["Text", FontSize -> 150, Bold, Black, 
     FontFamily -> "Calibri"]]},  ImageSize -> {275, 105}], 
 Background -> Green]

enter image description here

Or

   Framed[Text[
  Style["Text", FontSize -> 150, Bold, Black, 
   FontFamily -> "Calibri"]], Alignment -> Center, 
 Background -> Green, FrameMargins -> 0, ContentPadding -> False, 
 ImageSize -> {277, 108}]

enter image description here

Framed[Text[
  Style["Red Fox jr", FontSize -> 150, Bold, Black, 
   FontFamily -> "Calibri"]], Alignment -> Center, 
 Background -> Green, FrameMargins -> 0, ContentPadding -> False]

enter image description here

$\endgroup$
2
  • $\begingroup$ This looks good, but the values {275, 105} of ImageSize are set manually. Is it possible to find these values from the given "Text"? $\endgroup$
    – mrz
    Dec 27, 2018 at 13:54
  • $\begingroup$ I don't know how to reduce top and bottom space.. $\endgroup$ Dec 27, 2018 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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