title: Left and right alignment of paragraph text (matplotlib)
body: <p>I have been looking for a way of making a a piece of text left and right aligned (i.e. "margin adjusted" or <code>ctrl-J</code> in OpenOffice for example) in a matplotlib text field, to form a nice column (delimited by the width of the <code>matplotlib.axis</code>-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:</p>
  
  <p>Example of only left-alignment, i.e. not what I want:</p>
  
  <pre><code>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.
  </code></pre>
  
  <p>Example of what I would like:</p>
  
  <pre><code>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.
  </code></pre>
  
  <p>The <code>matplotlib.text</code> has a kwarg <code>wrap=True</code> (<a href="http://matplotlib.org/examples/text_labels_and_annotations/autowrap_demo.html" rel="nofollow noreferrer">example</a>), 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.  <a href="https://stackoverflow.com/questions/4018860/text-box-with-line-wrapping-in-matplotlib">This</a> answer shows how to wrap to the targeted <code>axis</code> but without margin adjusted text.  It is using the <a href="https://docs.python.org/2/library/textwrap.html" rel="nofollow noreferrer">textwrap python module</a> as a base.</p>
  
  <p>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 <a href="https://stackoverflow.com/questions/4018860/text-box-with-line-wrapping-in-matplotlib">answer</a> 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 <code>\hspace</code>'s inside the string).  </p>
  
  <p>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.</p>