I'm trying to create a lot of graphics that include boxes of (multi-sentence) text placed at particular coordinates, and I would really appreciate some help in wrestling control of the size of the text away from Mathematica's inconvenient (for my purposes) defaults. (I'm using Mathematica 13 on a Mac.)

  • What I want is for the text to have a fixed size in the coordinate system that the graphics is using.
  • In particular, if I change the ImageSize in the notebook interface (either through a code command or by dragging the corners of the image), I want the text to change size along with the graphics elements. This seems to rule out a numerical FontSize as far as I can tell.
  • However, I also want the text to keep its size relative to graphics elements if we choose a different PlotRange (for example, if we zoom in on a portion of the graphics). This seems to rule out using Scaled with FontSize as far as I can tell.

Already I don't know how to achieve the above properties. But I would also like a solution that does the following as well:

  • Once I decide on the rectangle that the text should be placed in, I want Mathematica to do all the line breaking/hyphenation/justification automatically. Inset[TextCell[...]] does this nicely, but it would need to work with the above criteria as well.
  • If there is too much text to be fully displayed in the rectangle, I ideally want Mathematica to automatically use a smaller font size to make it fit. (ImageSizeAction -> "ShrinkToFit" accomplishes this for Panes, although I've had problems getting it to work consistently in the context of text within graphics.) This is an exception to "I want to control the font size", but still in the same spirit: once Mathematica chooses a font size that makes the text fit, I want that font size to remain fixed regardless of the ImageSize and PlotRange being used at that moment.
  • (I'll also be exporting these graphics to files, so I would want the version I polish in my notebook to look the same as the version that gets saved to disk.)

The bottom line: I want Mathematica to treat text like other graphics elements. How can I accomplish this? Is this a job for Rasterize? (I tried this with partial success, but the resolution of the resulting text was poorer than I would like.) I'll add that there will be thousands of these text elements to deal with, so any process that involves changing formats or exporting/importing needs to be automatable and fast.

This feels like it could be an already solved problem, or not; I would be very grateful for any helpful responses, from pointing me to existing solutions (provided they have all the above properties) to giving me examples I can work from.

  • $\begingroup$ Wouldn't it be better if instead of lots of words you provide simple example of code and then add one sentence how the text in graphics behave and another sentence how you wish it should behave? $\endgroup$ Sep 19, 2023 at 12:47
  • 1
    $\begingroup$ Without example code to test the solution on I can't say if this will work for you, but one method would be to convert all text to filled curves. $\endgroup$
    – Jason B.
    Sep 19, 2023 at 14:53
  • $\begingroup$ @JasonB. The filled curves tip certainly works for the top three points! I'll keep experimenting. Do you want to post something as an answer (today) so that I can give you the bounty? $\endgroup$ Sep 25, 2023 at 17:24


Your Answer

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

Browse other questions tagged or ask your own question.