0
$\begingroup$

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?

$\endgroup$

2 Answers 2

1
$\begingroup$

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.

$\endgroup$
8
  • $\begingroup$ Thanks but unfortunately it didn't work. I'm having a blank image for both cases. $\endgroup$
    – Miguel
    Sep 14, 2017 at 15:47
  • $\begingroup$ What version/OS are you using? For me, the above code works fine on a fresh kernel in MMA 11.1, Win 10 $\endgroup$
    – Lukas Lang
    Sep 14, 2017 at 16:01
  • $\begingroup$ I'm using Mathematica 11.1.1.0 with macOS Sierra 10.12.6 $\endgroup$
    – Miguel
    Sep 14, 2017 at 16:02
  • $\begingroup$ Did you try in an empty notebook with a fresh kernel? And can anyone else test the code on OS X/macOS? $\endgroup$
    – Lukas Lang
    Sep 14, 2017 at 16:13
  • $\begingroup$ I was able to made it work but with a different parameterisation. Thanks! $\endgroup$
    – Miguel
    Sep 14, 2017 at 16:18
0
$\begingroup$

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
]
$\endgroup$

Your Answer

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

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