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.