manim.mobject.svg.text_mobject.MarkupText

class MarkupText(text, fill_opacity=1, stroke_width=0, color='#FFFFFF', size=1, line_spacing=- 1, font='', slant='NORMAL', weight='NORMAL', gradient=None, tab_width=4, height=None, width=None, should_center=True, unpack_groups=True, disable_ligatures=False, **kwargs)[source]

Bases: manim.mobject.svg.svg_mobject.SVGMobject

Display (non-LaTeX) text rendered using Pango.

Text objects behave like a VGroup-like iterable of all characters in the given text. In particular, slicing is possible. Text can be formatted using different tags:

  • <b>bold</b>, <i>italic</i> and <b><i>bold+italic</i></b>

  • <ul>underline</ul> and <s>strike through</s>

  • <tt>typewriter font</tt>

  • <big>bigger font</big> and <small>smaller font</small>

  • <sup>superscript</sup> and <sub>subscript</sub>

  • <span underline="double">double underline</span>

  • <span underline="error">error underline</span>

  • <span font_family="sans">temporary change of font</span>

  • <color col="RED">temporary change of color</color>; colors can be specified as Manim constants like RED or YELLOW or as hex triples like #aabbaa

  • <gradient from="YELLOW" to="RED">temporary gradient</gradient>; colors specified as above

If your text contains ligatures, the MarkupText class may incorrectly determine the first and last letter to be colored. This is due to the fact that e.g. fl are two characters, but might be set as one single glyph, a ligature. If your language does not depend on ligatures, consider setting disable_ligatures=True. If you cannot or do not want to do without ligatures, the gradient and color tag support an optional attribute offset which can be used to compensate for that error. Usage is as follows:

  • <color col="RED" offset="1">red text</color> to start coloring one letter earlier

  • <color col="RED" offset=",1">red text</color> to end coloring one letter earlier

  • <color col="RED" offset="2,1">red text</color> to start coloring two letters earlier and end one letter earlier

Specifying a second offset may be necessary if the text to be colored does itself contain ligatures. The same can happen when using HTML entities for special chars.

Escaping of special characters: > should be written as &gt; whereas < and & must be written as &lt; and &amp;.

You can find more information about Pango markup formatting at the corresponding documentation page: Pango Markup. Please be aware that not all features are supported by this class and that the <color> and <gradient> tags mentioned above are not supported by Pango.

Parameters
  • text (str) – The text that need to created as mobject.

  • fill_opacity (int) – The fill opacity with 1 meaning opaque and 0 meaning transparent.

  • stroke_width (int) – Stroke width.

  • color (str) – Global color setting for the entire text. Local overrides are possible.

  • size (int) – Font size.

  • line_spacing (int) – Line spacing.

  • font (str) – Global font setting for the entire text. Local overrides are possible.

  • slant (str) – Global slant setting, e.g. NORMAL or ITALIC. Local overrides are possible.

  • weight (str) – Global weight setting, e.g. NORMAL or BOLD. Local overrides are possible.

  • gradient (tuple) – Global gradient setting. Local overrides are possible.

  • tab_width (int) –

  • height (int) –

  • width (int) –

  • should_center (bool) –

  • unpack_groups (bool) –

  • disable_ligatures (bool) –

Returns

The text displayed in form of a VGroup-like mobject.

Return type

MarkupText

Examples

../_images/BasicMarkupExample-1.png
BasicMarkupExample
class BasicMarkupExample(Scene):
    def construct(self):
        text1 = MarkupText("<b>foo</b> <i>bar</i> <b><i>foobar</i></b>")
        text2 = MarkupText("<s>foo</s> <u>bar</u> <big>big</big> <small>small</small>")
        text3 = MarkupText("H<sub>2</sub>O and H<sub>3</sub>O<sup>+</sup>")
        text4 = MarkupText("type <tt>help</tt> for help")
        text5 = MarkupText(
            '<span underline="double">foo</span> <span underline="error">bar</span>'
        )
        group = VGroup(text1, text2, text3, text4, text5).arrange(DOWN)
        self.add(group)
../_images/ColorExample-1.png
ColorExample
class ColorExample(Scene):
    def construct(self):
        text1 = MarkupText(
            'all in red <color col="YELLOW">except this</color>', color=RED
        )
        text2 = MarkupText("nice gradient", gradient=(BLUE, GREEN))
        text3 = MarkupText(
            'nice <gradient from="RED" to="YELLOW">intermediate</gradient> gradient',
            gradient=(BLUE, GREEN),
        )
        text4 = MarkupText(
            'fl ligature <color col="#00ff00">causing trouble</color> here'
        )
        text5 = MarkupText(
            'fl ligature <color col="#00ff00" offset="1">defeated</color> with offset'
        )
        text6 = MarkupText(
            'fl ligature <color col="GREEN" offset="1">floating</color> inside'
        )
        text7 = MarkupText(
            'fl ligature <color col="GREEN" offset="1,1">floating</color> inside'
        )
        group = VGroup(text1, text2, text3, text4, text5, text6, text7).arrange(DOWN)
        self.add(group)
../_images/FontExample-1.png
FontExample
class FontExample(Scene):
    def construct(self):
        text1 = MarkupText(
            'all in sans <span font_family="serif">except this</span>', font="sans"
        )
        text2 = MarkupText(
            '<span font_family="serif">mixing</span> <span font_family="sans">fonts</span> <span font_family="monospace">is ugly</span>'
        )
        text3 = MarkupText("special char > or &gt;")
        text4 = MarkupText("special char &lt; and &amp;")
        group = VGroup(text1, text2, text3, text4).arrange(DOWN)
        self.add(group)
../_images/NewlineExample-1.png
NewlineExample
class NewlineExample(Scene):
    def construct(self):
        text = MarkupText('foooo<color col="RED">oo\nbaa</color>aar')
        self.add(text)
../_images/NoLigaturesExample-1.png
NoLigaturesExample
class NoLigaturesExample(Scene):
    def construct(self):
        text1 = MarkupText('fl<color col="RED">oat</color>ing')
        text2 = MarkupText('fl<color col="RED">oat</color>ing', disable_ligatures=True)
        group = VGroup(text1, text2).arrange(DOWN)
        self.add(group)

As MarkupText uses Pango to render text, rendering non-English characters is easily possible:

../_images/MultiLanguage-1.png
MultiLanguage
class MultiLanguage(Scene):
    def construct(self):
        morning = MarkupText("வணக்கம்", font="sans-serif")
        chin = MarkupText(
            '見 角 言 谷  辛 <color col="BLUE">辰 辵 邑</color> 酉 釆 里!'
        )  # works as in ``Text``.
        mess = MarkupText("Multi-Language", style=BOLD)
        russ = MarkupText("Здравствуйте मस नम म ", font="sans-serif")
        hin = MarkupText("नमस्ते", font="sans-serif")
        japanese = MarkupText("臂猿「黛比」帶著孩子", font="sans-serif")
        group = VGroup(morning, chin, mess, russ, hin, japanese).arrange(DOWN)
        self.add(group)

Tests

Check that the creation of MarkupText works:

>>> MarkupText('The horse does not eat cucumber salad.')
MarkupText('The horse does not eat cucumber salad.')

Methods

extract_color_tags

Used to determine which parts (if any) of the string should be formatted with a custom color.

extract_gradient_tags

Used to determine which parts (if any) of the string should be formatted with a gradient.

text2hash

Generates sha256 hash for file name.

text2svg

Convert the text to SVG using Pango.

Attributes

animate

Used to animate the application of a method.

extract_color_tags()[source]

Used to determine which parts (if any) of the string should be formatted with a custom color.

Removes the <color> tag, as it is not part of Pango’s markup and would cause an error.

extract_gradient_tags()[source]

Used to determine which parts (if any) of the string should be formatted with a gradient.

Removes the <gradient> tag, as it is not part of Pango’s markup and would cause an error.

text2hash()[source]

Generates sha256 hash for file name.

text2svg()[source]

Convert the text to SVG using Pango.