2
$\begingroup$

I'd like to represent a long String as Text in Graphics. The Graphics will only contain this text, or possibly a list of texts. I'd like to automatically fit all the text within the PlotRange.

For my specific application, the following criteria must also be met:

  1. The method must not impose any line wrapping of the text.
  2. I need to be able to specify the FontSize.
  3. The Graphics PlotRange must not cut off the text.
  4. The PlotRange must minimize padding.
  5. The method should work for any ImageSize.

Essentially, I'd like the PlotRange to shrinkwrap around the text boxes.

How can one achieve this?

Illustration of the issue:

Graphics[Text[Style[StringRiffle[RandomWord[100],", "], {Bold}, FontSize -> 20]], 
 Frame -> True]

Graphics with awful PlotRange default, not accounting for text dimensions

$\endgroup$
3
  • $\begingroup$ Maybe your example is too minimal or I am confused because you want to scale the text but also specify the fontsize. Do you want to scale the text to the graphics size? Then you could use e.g. Fontsize->Scaled[0.005] as long as your text is consistent in length. There are probably even better and more automatic ways. Or do you want to actually scale the Graphics to the text? That's probably not so easy because the text is only anchored to a single point. $\endgroup$
    – ZehDeckel
    Commented Oct 10 at 18:54
  • $\begingroup$ Actually, this related question looks like it could answer your question, if I understand it correctly. $\endgroup$
    – ZehDeckel
    Commented Oct 10 at 18:57
  • $\begingroup$ Thank you for the suggestion. To clarify: I'd like to be able to write: Graphics[{..., Text["something very long"]},PlotRange->something] and have the resulting Graphics show the full text without imposing any line wrapping, and without any unnecessary white space or padding around the text. $\endgroup$
    – phileasdg
    Commented Oct 10 at 19:15

2 Answers 2

4
$\begingroup$
str = Table[
   StringJoin[
    Riffle[RandomChoice[DictionaryLookup[], RandomInteger[{1, 10}]], 
     " "]], 10];

Graphics[Cases[
     ImportString[
      ExportString[
       Text[Style[#, FontFamily -> "Lucida Console", 
         LineBreakWithin -> False]], "PDF"], {"PDF", "PageGraphics"}, 
      "TextOutlines" -> True], _FilledCurve, -1], 
    PlotRangePadding -> None] & /@ str;
GraphicsColumn[%, ImageSize -> 600]

enter image description here

Here SortBy[str, StringLength] was used instead of str.

enter image description here

$\endgroup$
0
$\begingroup$

How about this?

verylongtext = Graphics[
    Text[Style[StringRiffle[RandomWord[100], ", "], {Bold}, FontSize -> 20]], 
    PlotRange -> Full];
Show[verylongtext, Plot[Sin[x], {x, -10 Pi, 10 Pi}]]

enter image description here

$\endgroup$
2
  • $\begingroup$ The text is cut on the left and right. $\endgroup$ Commented Oct 11 at 10:03
  • $\begingroup$ @azerbajdzan You are absolutely right. MMA frontend is trying its best to show all strings --- you can see this by resizing the window. If not everything is displayed, this is due to poor font size selection. $\endgroup$
    – A. Kato
    Commented Oct 11 at 11:24

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.