0

I need to create a Graphics object from text but it needs to be left justified. I can do this for a single text string:

Graphics[
 Text[
  Style["programa_de_estabilidade_e_crescimento", 16]
  , {1, 0}
  , {1.6, 0}
  ]
 , ImageSize -> {400, 20}
 ]

which results in:

enter image description here

But I need to do it automatically for different text sizes. E.g., using the same offset with the text string "programa" it results in:

enter image description here

So, is there a way to automatically adjust the offset?

rhermans
  • 36,518
  • 4
  • 57
  • 149
Miguel
  • 981
  • 5
  • 13

2 Answers2

1

Using the correct specification for the offset of Text (i.e. offset after coords), and specifying PlotRange, we can achieve consistent results:

textImg[txt_] := Graphics[
 Text[Style[txt, 16], {1.6, 0}, {-1, -1}], 
 ImageSize -> {400, 20},
 PlotRange -> {{0, 400}, All}
]

textImg["programa"]

textImg["programa_de_estabilidade_e_crescimento"]

Mathematica graphics

Mathematica graphics

You might also be able to use Alignment on an enclosing object to achieve the result without specifying PlotRange.

Lukas Lang
  • 33,963
  • 1
  • 51
  • 97
0

From @Mathe172 I got the following correct result (working in with MMA 11.1.1 macOS Sierra 10.12.6):

textImg[txt_] := Graphics[
 Text[Style[txt, 16], {-400, 0}, {-1, -1}], 
 ImageSize -> {400, 20},
 PlotRange -> {{-400, 5}, {0,20}},Frame->False
]
Miguel
  • 981
  • 5
  • 13