|
| 1 | +========================= |
| 2 | + MEP29: Text light markup |
| 3 | +========================= |
| 4 | + |
| 5 | +.. contents:: |
| 6 | + :local: |
| 7 | + |
| 8 | + |
| 9 | +Status |
| 10 | +====== |
| 11 | + |
| 12 | +Discussion |
| 13 | + |
| 14 | + |
| 15 | +Branches and Pull requests |
| 16 | +========================== |
| 17 | + |
| 18 | +None at the moment, proof of concept only. |
| 19 | + |
| 20 | +Abstract |
| 21 | +======== |
| 22 | + |
| 23 | +This MEP proposes to add lightweight markup to the text artist. |
| 24 | + |
| 25 | +Detailed description |
| 26 | +==================== |
| 27 | + |
| 28 | +Using different size/color/family in a text annotation is difficult because the |
| 29 | +`text` method accepts argument for size/color/family/weight/etc. that are used |
| 30 | +for the whole text. But, if one wants, for example, to have different colors, |
| 31 | +one has to look at the gallery where one such example is provided: |
| 32 | +http://matplotlib.org/examples/text_labels_and_annotations/rainbow_text.html |
| 33 | + |
| 34 | +This example takes a list of strings as well as a list of colors which makes it |
| 35 | +cumbersome to use. An alternative would be to use a restricted set of pango-like markup (see https://developer.gnome.org/pango/stable/PangoMarkupFormat.html) and to interpret this markup. |
| 36 | + |
| 37 | +Some markup examples:: |
| 38 | + |
| 39 | + Hello <b>world!</b>` |
| 40 | + Hello <span color="blue">world!</span> |
| 41 | + |
| 42 | + |
| 43 | +Implementation |
| 44 | +============== |
| 45 | + |
| 46 | +A proof of concept is provided in `markup_example.py <https://github.com/rougier/matplotlib/blob/markup/examples/text_labels_and_annotations/markup.py>`_ but it currently only handles the horizontal direction. |
| 47 | + |
| 48 | +Improvements |
| 49 | +------------ |
| 50 | + |
| 51 | +* This proof of concept uses regex to parse the text but it may be better |
| 52 | + to use the html.parser from the standard library. |
| 53 | + |
| 54 | +* Computation of text fragment positions could benefit from the OffsetFrom |
| 55 | + class. See for example item 5 in `Using Complex Coordinates with Annotations <http://matplotlib.org/devdocs/users/annotations.html#using-complex-coordinates-with-annotations>`_ |
| 56 | + |
| 57 | +Problems |
| 58 | +-------- |
| 59 | + |
| 60 | +* One serious problem is how to deal with text having both latex and |
| 61 | + html-like tags. For example, consider the follwing:: |
| 62 | + |
| 63 | + $<b>Bold$</b> |
| 64 | + |
| 65 | + Recommendation would be to have mutual exclusion. |
| 66 | + |
| 67 | + |
| 68 | +Backward compatibility |
| 69 | +====================== |
| 70 | + |
| 71 | +None at the moment since it is only a proof of concept |
| 72 | + |
| 73 | + |
| 74 | +Alternatives |
| 75 | +============ |
| 76 | + |
| 77 | +As proposed by @anntzer, this could be also implemented as improvements to |
| 78 | +mathtext. For example:: |
| 79 | + |
| 80 | + r"$\text{Hello \textbf{world}}$" |
| 81 | + r"$\text{Hello \textcolor{blue}{world}}$" |
| 82 | + r"$\text{Hello \textsf{\small world}}$" |
0 commit comments