Tex

Qualified name: algan.mobs.text.Tex

class Tex(*tex_strings, delimiter=' ', tex_environment=None, font_size=24, latex=True, pango_kwargs=None, pango_color_map=None, sync_stroke_color=True, **kwargs)[source]

Bases: Mob

LaTeX compiled to one packed batch of cubic bezier glyphs.

The string is typeset in LaTeX’s math mode, so Tex("x^2") is a squared x rather than the three literal characters. Pass latex=False for prose, or use Text, which is this class wired to a Pango font renderer instead.

The glyphs are outlines, not bitmaps: the text stays sharp at any scale and morphs into other 2-D shapes like any other bezier Mob. They arrive as a single packed Mob rather than one Mob per character, which is what makes a long string cheap. Index it to animate one glyph – equation[3] is a view sharing the batch’s rows, so moving it moves that glyph of the original, and it needs no spawning of its own. Several source strings become several segments, addressable with get_segment(), which is the usual way to highlight one term of an equation.

MathTex renders LaTeX too and is a different object: it is the Manim-compatibility wrapper, for when a ported script needs tex_to_color_map or Manim method delegation. This class is the native one, and the only one with per-glyph indexing, get_segment() and write().

Animation

Constructing a Tex records nothing: LaTeX runs immediately and the Mob joins the active Scene unspawned. spawn() is what plays its entrance – on_create(), a diagonal fade running down and to the right so the words arrive in reading order, lasting 1 second regardless of the enclosing context. Tex(...).spawn(False) skips it, which is what write() wants.

Parameters:
  • *tex_strings – One or more LaTeX sources. Each becomes a segment retrievable with get_segment(), and they are compiled as one document so a \left in one string can close in the next. A single list or tuple is unpacked, and no strings at all gives an empty Tex.

  • delimiter – Inserted between consecutive tex_strings in the compiled source (and used to join them when latex=False). Defaults to " ", one space.

  • tex_environment – Name of the LaTeX environment to typeset in, such as "align*" or "gather*". Defaults to None, meaning Manim’s own default of align*.

  • font_size – Glyph size in Manim’s font-size units. The batch is always built at 48 and then scaled by font_size / 48, so this is a scale factor in disguise: 48 matches Manim’s default text size and 24 is half of it. Defaults to 24.

  • latex – Whether to typeset through LaTeX. False treats the strings as plain text and routes them to Manim’s Pango renderer instead – an Algan extension, and how Text is built. Defaults to True.

  • pango_kwargs – Styling forwarded to the Pango renderer when latex=False: font, weight, slant, line_spacing, color_map, gradient and the rest of Manim’s Text arguments. Ignored under LaTeX. Defaults to None (no styling). Prefer Text, which exposes these as ordinary named arguments.

  • pango_color_map – Maps the hex strings in pango_kwargs back to the Algan Color objects they came from, so glow and opacity survive the round trip through Pango’s SVG output (a hex string cannot carry either). Defaults to None. Set for you by Text.

  • sync_stroke_color – Whether a glyph colored by Pango styling also gets that color on its border. Only has an effect when a border is actually drawn (stroke_width above 0). Defaults to True; pass an explicit stroke_color to keep one outline color across styled glyphs.

  • **kwargs – Passed to Mob and to the packed BezierCircuitCubic – notably color (defaults to WHITE), stroke_color, stroke_width (defaults to 0, no outline), location and scene. One extra keyword is consumed here: preamble, a string of LaTeX appended to Manim’s default preamble, for \usepackage lines a formula needs.

character_mobs

Lazy per-glyph views into the packed batch, in typeset order. This is what tex[i] and write() animate.

tex_strings

The source strings as given, after list/tuple unpacking, as a tuple.

latex

Whether this text was typeset by LaTeX rather than by Pango.

Examples

A formula, one segment per term, with the middle term picked out:

Example: Example1Tex

../_images/Example1Tex-1.png
from algan import *

equation = Tex(r"e^{i\pi}", "+", "1", "=", "0", font_size=48).spawn()
equation.get_segment(2).color = YELLOW

Scene.save_video()

latex=False for prose, and a larger font_size:

Example: Example2Tex

../_images/Example2Tex-1.png
from algan import *

Tex("Not a formula", latex=False, font_size=48, color=BLUE).spawn()

Scene.save_video()

Individual glyphs are views, so animating one animates the original:

Example: Example3Tex

../_images/Example3Tex-1.png
from algan import *

word = Tex("ALGAN", font_size=48).spawn()
with Sync():
    word[0].move(UP * 0.3)
    word[4].move(DOWN * 0.3)

Scene.save_video()

Methods

become

Morph this text into another Mob, keeping its glyph views usable.

get_segment

Get one of the text's LaTeX segments as a Mob.

on_create

Play the text's entrance: a fade that sweeps across the glyphs.

on_destroy

Play the text's exit: a fade that sweeps across the glyphs.

write

Animate this text appearing as if it were being hand-written.

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).

triangulated

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).

become(other_mob, *args, **kwargs)[source]

Morph this text into another Mob, keeping its glyph views usable.

As become(), with one addition: because a morph can expand the packed glyph batch, the per-character views are rebuilt against the result, so indexing (text[0]) still works afterwards.

Animation

Recorded as an animation over the current context’s runtime (1 second by default).

Parameters:
  • other_mob – The Mob to morph into. Text-to-text and text-to-bezier morphs preserve the tightest correspondence; other primitive families use geometric conversion or a dissolve according to strategy.

  • *args – Passed to become() – notably minimize_movement=True, which pairs each glyph fragment with its nearest counterpart and is usually what you want for text.

  • **kwargs – Passed to become() – notably minimize_movement=True, which pairs each glyph fragment with its nearest counterpart and is usually what you want for text.

Returns:

The morphed Mob. With the default detach_history=True this can be a different object from the one you called the method on, so use the return value afterwards. Character views are rebuilt when the result is still text.

Return type:

Mob

get_segment(index)[source]

Get one of the text’s LaTeX segments as a Mob.

Segments are the pieces the text was constructed from, so a Tex built from several strings can have each one animated separately – the usual way to highlight one term of an equation.

Parameters:

index (int) – Index of the segment.

Returns:

A Group of the glyphs in that segment, sharing data with this text.

Return type:

Group

on_create()[source]

Play the text’s entrance: a fade that sweeps across the glyphs.

Instead of the plain fade a Mob uses, text fades in as a diagonal wave running down and to the right, so the words appear to arrive in reading order.

Animation

Recorded as an animation lasting 1 second, regardless of the enclosing context’s runtime.

Returns:

This text, so calls can be chained.

Return type:

Tex

on_destroy()[source]

Play the text’s exit: a fade that sweeps across the glyphs.

The mirror of on_create() – the glyphs fade out as a diagonal wave rather than all at once.

Animation

Recorded as an animation over the current context’s runtime (1 second by default). The despawn itself is recorded at the end of the wave, so no glyph disappears before the wave reaches it.

Returns:

This text, so calls can be chained.

Return type:

Tex

write(*args, **kwargs)[source]

Animate this text appearing as if it were being hand-written.

Each glyph’s outline is traced and then filled, one glyph after another. This is DrawBorderThenFill() applied to this text’s glyphs.

Animation

Recorded as an animation. Its runtime comes from runtime and lag_ratio rather than the enclosing context, so a long string takes longer to write unless you set runtime.

Parameters:
Returns:

This text, so calls can be chained.

Return type:

Mob

Examples

Example: Example1TextWrite

from algan import *

Text('Hello World!').spawn(False).write()

Scene.save_video()