2

I have been looking for a way of making a a piece of text left and right aligned (i.e. "margin adjusted" or ctrl-J in OpenOffice for example) in a matplotlib text field, to form a nice column (delimited by the width of the matplotlib.axis-object). I'm not sure if the proper term is "left and right aligned", but I would like it to be as in the example below:

Example of only left-alignment, i.e. not what I want:

This is some sample text withsomeverylongwords
that I would like to be wrapped. It should adjust 
to the left and right margin such that they are at
the same distance.

Example of what I would like:

This is some sample text withsomevery-
longwords that I would like to be wra-
pped. It should adjust to the left and 
right margin such that they are at the 
same distance.  It should not wrap the
final line.

The matplotlib.text has a kwarg wrap=True (example), but as far as I can see it only wraps to the figure width. Further it only aligns either left, right or center but not both. This answer shows how to wrap to the targeted axis but without margin adjusted text. It is using the textwrap python module as a base.

What I would like is a function that wraps text, preferably directly in accordance to a matplotlib axis. If it needs to cut long words it should do that with a dash. Latexed equations should not be broken, which answer already takes care of. If possible it should also adjust space sizes inside each line to make th edges straight, but I believe this might be much tougher (I imagine this can be done with some well-placed Latex \hspace's inside the string).

Has this been done? I have a hard time thinking it hasn't since every modern text editor such as MS Word and OO Write has it, but I can't find any example of such code for Python. If it hasn't I would be greatful for ideas how to implement it.

Community
  • 1
  • 1
pathoren
  • 1,634
  • 2
  • 14
  • 22
  • I don't know the answer to Your question, but there is a way to insert Latex in matplot.lib text. Maybe that is the way to go. Take a look: http://matplotlib.org/users/usetex.html – Tony Babarino Mar 13 '16 at 11:06
  • Thank you for your comment. I am already aware that you can insert inline latex-equations for example. One solution I have in mind is to actually latex the entire text-field, but making it compile as I want it seem to be a nifty problem. – pathoren Mar 13 '16 at 14:04
  • mpl contains a limited port of LaTeX to python (just the math rendering). I full reimplementation of the LaTeX layout engine in python would be an interesting project, but probably out of scope for mpl. – tacaswell Mar 13 '16 at 17:35

0 Answers0