typst_types

class janim.items.svg.typst_types.TypstMatrix(matrix: Iterable[Iterable[str | float | Points]], delim: str | TypMatDelim | tuple[TypMatDelim, TypMatDelim] | None = None, *, align: TypMatAlignment | None = None, augment: int | str | None = None, gap: str | None = None, row_gap: str | None = None, column_gap: str | None = None, label: bool = False, **kwargs)

Bases: TypstText

Use Typst for matrix layout

  • Use get_inserted() to get inserted JAnim items

  • Use get_element() to get elements by row and column (requires passing label=True to enable)

For parameter passing, please refer to Typst documentation https://typst.app/docs/reference/math/mat/; some examples are given below

TypstMatrix(
    [
        [1, 2, 3],
        [4, Arrow(ORIGIN, RIGHT), 6],
        [7, 8, 9]
    ],
).show()
TypstMatrix(
    [
        [1, 2, 3],
        [4, Arrow(ORIGIN, RIGHT), 6],
        [7, 8, 9]
    ],
    gap='2em',
).show()
TypstMatrix(
    [
        [1, 2, 3],
        [4, Arrow(ORIGIN, RIGHT), 6],
        [7, 8, 9]
    ],
    delim='[',
    align='right',
    augment=2,
    gap='0.5em',
)
TypstMatrix(
    [
        [1, 2, 3],
        [4, Circle(radius=0.25, fill_alpha=0.5), 6],
        [7, 8, 9]
    ],
    delim='[',
    augment=2,
    column_gap='0.7em',
    preamble='#set text(size: 3em)'
).show()
matrix_labels

Matrix element labels arranged in child item order

get_inserted(index: int) Points

Get the index-th inserted JAnim item

index starts counting from 0

get_element(row: int, col: int) Group[SVGElemItem] | Points

Index element by row and column

Requires passing label=True when constructing TypstMatrix to enable

get_elements() list[Group[SVGElemItem] | Points]

Get all elements in the matrix

Requires passing label=True when constructing TypstMatrix to enable

get_left_brace() Group[SVGElemItem]

Get the left brace element

Requires passing label=True when constructing TypstMatrix to enable

get_right_brace() Group[SVGElemItem]

Get the right brace element

Requires passing label=True when constructing TypstMatrix to enable

class janim.items.svg.typst_types.DynamicTypst(template: str, dynamic: dict[str, float], post: ~typing.Callable[[~janim.items.svg.typst_types.DynamicTypst], ~typing.Any] = <function DynamicTypst.<lambda>>, **kwargs)

Bases: TypstText

A wrapper to conveniently create Typst items with parameter variations

Parameters:
  • template – Typst code

  • dynamic – Declare which parameters and their initial values; these can be used directly within the Typst code

  • post – Used to make further adjustments after generating the Typst item, e.g., pinning or aligning a position

Example:

DynamicTypstExample
from janim.imports import *

class DynamicTypstExample(Timeline):
    def construct(self) -> None:
        dtyp = DynamicTypst(
            """
            #import "@preview/cetz:0.4.2"
            #import "@preview/cetz-plot:0.1.3": *

            #let width = 1
            #let ang_deg = angle * 1deg

            #cetz.canvas({
                import cetz.draw: *

                stroke((thickness: 0.7pt, join: "round", paint: white))

                let (a, b, c, d) = (
                    (0, 0),
                    (width, 0),
                    (rel: (width, 0), to: (60deg, width * 3)),
                    (60deg, width * 3),
                )

                line(a, b, c, d, a)

                let ang_eab = ang_deg
                let len_ae = width / calc.sin(60deg - ang_eab) * calc.sin(120deg)
                let e = (ang_eab, len_ae)
                let g = (a, 100%, 120deg, e)
                let f = (a, 100%, 60deg, e)

                line(a, e, f, g, a)
                line(a, f)

                for (pos, rel, lab) in (
                    (a, (-1, -1.2), $A$),
                    (b, (1, -1.5), $B$),
                    (c, (1, 1), $C$),
                    (d, (-1, 1), $D$),
                    (f, (-.5, 1.5), $F$),
                    (g, (-1, 1), $G$),
                    (e, (1, -.5), $E$),
                ) {
                    content((pos, 17%, (rel: rel)), lab)
                }
            })
            """,
            {
                'angle': 30,
            },
            post=lambda typ: typ.points.next_to(DR * 2, UL),
        ).show()

        self.play(
            # Please read the documentation before using can_keep_structure!
            dtyp.anim_update(angle=45, can_keep_structure=True),
            duration=2,
        )

        self.play(
            dtyp.anim_update(angle=5, can_keep_structure=True),
            duration=2,
        )
anim_update(can_keep_structure: bool = False, **values)

Create a dynamic animation for parameter changes

If the dynamic process is guaranteed not to change the item’s structure, pass can_keep_structure to update using GroupUpdater; otherwise it defaults to ItemUpdater.

The motivation for this parameter is that GroupUpdater behaves better, but it requires that the item structure does not change