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" underline_color="green">double underline</span>

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

  • <span overline="single" overline_color="green">overline</span>

  • <span strikethrough="true" strikethrough_color="red">strikethrough</span>

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

  • <span foreground="red">temporary change of color</span>

  • <span fgcolor="red">temporary change of color</span>

  • <gradient from="YELLOW" to="RED">temporary gradient</gradient>

For <span> markup, colors can be specified either as hex triples like #aabbcc or as named CSS colors like AliceBlue. The <gradient> tag being handled by Manim rather than Pango, supports hex triplets or Manim constants like RED or RED_A. If you want to use Manim constants like RED_A together with <span>, you will need to use Python’s f-String syntax as follows: f'<span foreground="{RED_A}">here you go</span>'

If your text contains ligatures, the MarkupText class may incorrectly determine the first and last letter when creating the gradient. 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 tag supports an optional attribute offset which can be used to compensate for that error. Usage is as follows:

  • <gradient from="RED" to="YELLOW" offset="1">example</gradient> to start the gradient one letter earlier

  • <gradient from="RED" to="YELLOW" offset=",1">example</gradient> to end the gradient one letter earlier

  • <gradient from="RED" to="YELLOW" offset="2,1">example</gradient> to start the gradient two letters earlier and end it 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.

When using underline, overline or strikethrough together with <gradient> tags, you will also need to use the offset, because underlines are additional paths in the final SVGMobject, check out the corresponding example.

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 <gradient> tag mentioned above is 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

Example: BasicMarkupExample

../_images/BasicMarkupExample-1.png
from manim import *

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)

Example: ColorExample

../_images/ColorExample-1.png
from manim import *

class ColorExample(Scene):
    def construct(self):
        text1 = MarkupText(
            f'all in red <span fgcolor="{YELLOW}">except this</span>', 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 <gradient from="RED" to="YELLOW">causing trouble</gradient> here'
        )
        text5 = MarkupText(
            'fl ligature <gradient from="RED" to="YELLOW" offset="1">defeated</gradient> with offset'
        )
        text6 = MarkupText(
            'fl ligature <gradient from="RED" to="YELLOW" offset="1">floating</gradient> inside'
        )
        text7 = MarkupText(
            'fl ligature <gradient from="RED" to="YELLOW" offset="1,1">floating</gradient> inside'
        )
        group = VGroup(text1, text2, text3, text4, text5, text6, text7).arrange(DOWN)
        self.add(group)

Example: UnderlineExample

../_images/UnderlineExample-1.png
from manim import *

class UnderlineExample(Scene):
    def construct(self):
        text1 = MarkupText(
            '<span underline="double" underline_color="green">bla</span>'
        )
        text2 = MarkupText(
            '<span underline="single" underline_color="green">xxx</span><gradient from="#ffff00" to="RED">aabb</gradient>y'
        )
        text3 = MarkupText(
            '<span underline="single" underline_color="green">xxx</span><gradient from="#ffff00" to="RED" offset="-1">aabb</gradient>y'
        )
        text4 = MarkupText(
            '<span underline="double" underline_color="green">xxx</span><gradient from="#ffff00" to="RED">aabb</gradient>y'
        )
        text5 = MarkupText(
            '<span underline="double" underline_color="green">xxx</span><gradient from="#ffff00" to="RED" offset="-2">aabb</gradient>y'
        )
        group = VGroup(text1, text2, text3, text4, text5).arrange(DOWN)
        self.add(group)

Example: FontExample

../_images/FontExample-1.png
from manim import *

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)

Example: NewlineExample

../_images/NewlineExample-1.png
from manim import *

class NewlineExample(Scene):
    def construct(self):
        text = MarkupText('foooo<span foreground="red">oo\nbaa</span>aar')
        self.add(text)

Example: NoLigaturesExample

../_images/NoLigaturesExample-1.png
from manim import *

class NoLigaturesExample(Scene):
    def construct(self):
        text1 = MarkupText('fl<gradient from="RED" to="GREEN">oat</gradient>ing')
        text2 = MarkupText('fl<gradient from="RED" to="GREEN">oat</gradient>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:

Example: MultiLanguage

../_images/MultiLanguage-1.png
from manim import *

class MultiLanguage(Scene):
    def construct(self):
        morning = MarkupText("வணக்கம்", font="sans-serif")
        chin = MarkupText(
            '見 角 言 谷  辛 <span fgcolor="blue">辰 辵 邑</span> 酉 釆 里!'
        )  # 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.

depth

The depth of the mobject.

height

The height of the mobject.

width

The width of the mobject.

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.

Note: Using the <color> tags is deprecated. As soon as the legacy syntax is gone, this function will be removed.

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.