File: MEP29.rst

package info (click to toggle)
matplotlib 3.10.1%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 78,352 kB
  • sloc: python: 147,118; cpp: 62,988; objc: 1,679; ansic: 1,426; javascript: 786; makefile: 104; sh: 53
file content (84 lines) | stat: -rw-r--r-- 2,327 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
=========================
 MEP29: Text light markup
=========================

.. contents::
   :local:


Status
======

Discussion


Branches and Pull requests
==========================

None at the moment, proof of concept only.

Abstract
========

This MEP proposes to add lightweight markup to the text artist.

Detailed description
====================

Using different size/color/family in a text annotation is difficult because the
`~.Axes.text` method accepts argument for size/color/family/weight/etc. that are used
for the whole text. But, if one wants, for example, to have different colors,
one has to look at the gallery where one such example is provided:
:doc:`/gallery/text_labels_and_annotations/rainbow_text`

This example takes a list of strings as well as a list of colors which makes it
cumbersome to use. An alternative would be to use a restricted set of pango_-like markup and to interpret this markup.

.. _pango: https://docs.gtk.org/Pango/pango_markup.html#pango-markup

Some markup examples::

   Hello <b>world!</b>`
   Hello <span color="blue">world!</span>


Implementation
==============

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.

Improvements
------------

* This proof of concept uses regex to parse the text but it may be better
  to use the html.parser from the standard library.

* Computation of text fragment positions could benefit from the OffsetFrom
  class. See for example item 5 in `Using Complex Coordinates with Annotations <https://matplotlib.org/devdocs/users/explain/text/annotations.html#using-complex-coordinates-with-annotations>`_

Problems
--------

* One serious problem is how to deal with text having both LaTeX and
  HTML-like tags. For example, consider the following::

     $<b>Bold$</b>

  Recommendation would be to have mutual exclusion.


Backward compatibility
======================

None at the moment since it is only a proof of concept


Alternatives
============

As proposed by @anntzer, this could be also implemented as improvements to
mathtext. For example::

  r"$\text{Hello \textbf{world}}$"
  r"$\text{Hello \textcolor{blue}{world}}$"
  r"$\text{Hello \textsf{\small world}}$"