MarkupText

Qualified name: algan.mobs.manim\_compat.MarkupText

class MarkupText(text, fill_opacity=1, stroke_width=0, color=None, font_size=48, line_spacing=-1, font='', slant='NORMAL', weight='NORMAL', justify=False, gradient=None, tab_width=4, height=None, width=None, should_center=True, disable_ligatures=False, warn_missing_font=True, **kwargs)

Bases: ManimCompatMob

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.

What is PangoMarkup?

PangoMarkup is a small markup language like html and it helps you avoid using “range of characters” while coloring or styling a piece a Text. You can use this language with MarkupText.

A simple example of a marked-up string might be:

<span foreground="blue" size="x-large">Blue text</span> is <i>cool</i>!"

and it can be used with MarkupText as

A more elaborate example would be:

PangoMarkup can also contain XML features such as numeric character entities such as &#169; for © can be used too.

The most general markup tag is <span>, then there are some convenience tags.

Here is a list of supported tags:

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

  • <u>underline</u> 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 is handled by Manim rather than Pango, and 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:

MarkupText(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 fl are two separate characters, but might be set as one single glyph - a ligature. If your language does not depend on ligatures, consider setting disable_ligatures to True. If you must use ligatures, the gradient tag supports an optional attribute offset which can be used to compensate for that error.

For example:

  • <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 following 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 needs to be created as mobject.

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

  • stroke_width (float) – Stroke width.

  • font_size (float) – Font size.

  • line_spacing (float) – 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 (Iterable[ParsableManimColor] | None) – Global gradient setting. Local overrides are possible.

  • warn_missing_font (bool) – If True (default), Manim will issue a warning if the font does not exist in the (case-sensitive) list of fonts returned from manimpango.list_fonts().

  • color (ParsableManimColor | None)

  • justify (bool)

  • tab_width (int)

  • height (int | None)

  • width (int | None)

  • should_center (bool)

  • disable_ligatures (bool)

  • kwargs (Any)

Returns:

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

Return type:

MarkupText

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

Attributes

animation_manager

This mob's scene-owned animation manager.

basis

The Mob's orientation and scale, as a flattened 3x3 matrix of shape (*, 9).

casts_shadows

Whether this Mob's geometry blocks light on its way from a light source to another surface -- whether it casts a shadow.

children

The Mobs attached below this one, in attachment order.

closed_shell

Whether this Mob's triangles form a CLOSED shell -- every camera ray that enters the geometry crosses a second time on its way out.

draws_descendants

Whether get_render_primitives returns geometry belonging to this Mob's DESCENDANTS as well as its own.

forward

Get the direction the Mob is facing.

lifespan

This mob's [spawn, despawn) interval on its Scene timeline (a Lifespan).

location

The Mob's position in world space, shape (*, 3).

normalized_basis

The Mob's orientation with scale divided out, shape (*, 9).

parents

The Mobs this one is attached to, in attachment order.

receives_shadows

Whether this Mob's surfaces are darkened by shadows cast onto them.

right

Get the Mob's own rightward direction.

scale_coefficient

The Mob's scale along its own right, up and forward axes, shape (*, 3).

stroke_color

The color of the circuit's border stroke.

two_sided

Whether this Mob's geometry should be lit from whichever side the ray arrives on.

up

Get the Mob's own upward direction.

x

The Mob's x coordinate in world units, shape (*, 1).

xy

The Mob's x and y coordinates in world units, shape (*, 2).

y

The Mob's y coordinate in world units, shape (*, 1).

z

The Mob's z coordinate in world units, shape (*, 1).

z_index

Which of two exactly coplanar circuits draws in front (higher wins).