Text and Maths
Words and equations are shapes too: write, move, colour and change them.
Text
from pymations import *
hello = Text("Hello!", font_size=72, color=YELLOW)
play(Write(hello))
| Option | What it does |
|---|---|
font_size=48 | The size; 48 is normal, 96 is twice as big |
color=BLUE | The colour (white unless you say) |
weight=BOLD | Bold |
slant=ITALIC | Italic |
t2c={"world": BLUE} | Colours every place a word appears |
font="serif" | A serif font (the one maths is written in) |
font="mono" | A typewriter font, every letter the same width |
Text uses built-in fonts, the same on every device, so a video looks the same wherever it is made: a clear sans-serif unless you say, "serif" or "mono". A family name picks the nearest of the three ("Courier" is mono, "Georgia" is serif).
To use your own font, put its file next to the .py file and give its name: Text("Hello", font="Brand.ttf"). .ttf, .otf and .woff2 files work.
text[0] is the first letter (spaces are not counted), so single letters can be coloured or moved.
Maths: MathTex
MathTex writes maths the way LaTeX does:
from pymations import *
formula = MathTex(r"\frac{-b \pm \sqrt{b^2 - 4ac}}{2a}", font_size=72)
play(Write(formula))
wait()
- Put
rbefore the quotes (r"..."). Without it, Python reads\fand\bas special characters. - Powers are
x^2,x^{10}; below isx_1; fractions\frac{a}{b}; roots\sqrt{x}; Greek letters\pi,\theta,\alpha. - Maths is laid out by KaTeX, which understands most of the LaTeX used in maths: fractions, roots, sums, integrals, limits, matrices,
\text{...}and\tfrac. A few rare LaTeX packages are not available; if a formula can't be read, the output under the preview says which part.
Parts of an equation
Give MathTex several strings and each one becomes a part you can reach by number:
from pymations import *
eq = MathTex("a^2", "+", "b^2", "=", "c^2", font_size=72)
eq[0].set_color(BLUE)
eq[2].set_color(GREEN)
eq[4].set_color(YELLOW)
play(Write(eq))
play(Indicate(eq[4]))
wait()
Double braces split one string into parts too: MathTex("{{ a^2 }} + {{ b^2 }} = {{ c^2 }}") has five parts, the same as above.
| Way | What it does |
|---|---|
eq[2] | The third part |
eq.get_part_by_tex("b^2") | The first part containing b^2 |
eq.set_color_by_tex("b", GREEN) | Colours every part containing b |
MathTex("x + y", tex_to_color_map={"x": BLUE}) | Makes x its own part and colours it |
MathTex("x + y = 1", substrings_to_isolate=["x"]) | Makes x its own part, to colour or move later |
Changing one equation into another
TransformMatchingTex moves the parts that appear in both equations to their new places, and fades the rest in and out. It is the usual way to show algebra steps:
from pymations import *
before = MathTex("x", "+", "3", "=", "5", font_size=72)
after = MathTex("x", "=", "5", "-", "3", font_size=72)
play(Write(before))
wait()
play(TransformMatchingTex(before, after))
wait()
Parts match when their text is the same, so split equations so that the pieces you want to move are their own parts. Transform(before, after) changes the whole shape instead, without matching.
Tex: words with maths inside
Tex is for sentences, with maths between dollar signs:
from pymations import *
line = Tex(r"The area is $\pi r^2$")
play(Write(line))
Title("Chapter 1") is a Tex placed at the top of the picture.
Numbers
DecimalNumber(3.14159, num_decimal_places=2) shows 3.14. Integer(42) shows a whole number.
Related
- Animations:
Write,Unwrite,Indicate,Circumscribe - Tutorial: an equation explainer
- Colours