TextMobject class
object > Container > Mobject > VMobject > SVGMobject > SingleStringTexMobject > TexMobject
manimlib.mobject.svg.text_mobject.TexMobject(self, *tex_strings, **kwargs)
Makes the tex_string string given as an parameter into an object that can be rendered by Manim. The tex_string is a formula string expressed as a LaTex expression.
If multiple formula strings are entered, each string becomes one element of the list.
That is, for text = TexMobject('a','+','b'), the string of text[0] will be 'a'.
In the case of the promised escape character to represent a formula in LaTex, the backslash (\) is placed before the reserved word to distinguish it from the normal character.
When entering this reserved word into TexMobject, two backslashes are used consecutively.
For example, for $\sqrt{ 2 } $, TexMobject("\sqrt {2 }")
Parameters: *text_string
Formula string to process. Follow LaTex expression.
Parameters: **kwargs
- stroke_width = 0: Text outline thickness. default value is zero
- fill_opacity=1.0: Text color transparency. 1 is opaque, 0 is transparent
- height: Text's height
In LaTex expression, 'a^2' becomes $a^2$.
Therefore, if you want to create a formula like $a^2$ using TexMobject, you can do t=TexMobject("a^2").
It is convenient to use a website such as Daum Equation Editor to express how a formula is expressed in LaTex. You can create LaTex expressions for the formulas you want on your web page, and copy and paste those expressions into the arguments of TexMobject.
Splitting formulas
In many cases, you need to change the color of each letter for the formula or move or delete some formulas.
In this case, you can create each formula by dividing it into sub-parts, and process each formula.
tex = TexMobject("a^2","+", "b^2","=","c^2")
self.add(tex)
self.wait()
self.play(
tex[0].set_color, RED,
tex[2].set_color, RED,
tex[4].become, TexMobject("d^2").move_to(tex[4]),
# Transform(tex[4], TexMobject("d^2").move_to(tex[4])),
run_time=2,
)
Specifying different colors for formulas
As in the example code above, when you want to express part of a formula in a different color, you can decompose each formula string into multiple numbers and set them differently through set_color method.
However, it is more convenient to specify the color for each formula from the beginning when creating a TexMobject object, or through the set_color_by_tex or set_color_by_tex_to_color_map method after creation.
It is a sample code for the first method.
When creating a TexMobject object, color is specified for each character. It is convenient because you do not need to separate the input of each formula by comma (,).
t1 = TexMobject(
"P(A|B) = {P(A)P(B|A) \\over P(B)}",
tex_to_color_map={
"A": YELLOW,
"B": BLUE,
},
)
The second sample code uses the set_color_by_tex method. In this case, you must enter each formula in TexMobject, separated by commas(,).
eq = TexMobject("x", "=", "L", "\\theta")
eq.set_color_by_tex("\\theta", BLUE)
Of course, you can specify eq[3].set_color(BLUE).
The third method is to use set_color_by_tex_to_map, and when you specify different colors for multiple words, you can specify a value to change the dictionary.
all_words = VGroup(
TextMobject("You have a\\\\", "hypothesis"),
TextMobject("You've observed\\\\some ", "evidence"),
TexMobject(
"\\text{You want}\\\\",
"P", "(", "H", "|", "E", ")\\\\",
"P", "\\left(",
"\\substack{""\\text{Hypothesis} \\\\",
"\\textbf{given} \\\\",
"\\, \\text{the evidence} \\,}",
"\\right)",
),
)
HYPOTHESIS_COLOR = YELLOW
EVIDENCE_COLOR1 = BLUE_C
EVIDENCE_COLOR2 = BLUE_E
for words in all_words:
words.set_color_by_tex_to_color_map({
"hypothesis": HYPOTHESIS_COLOR,
"H": HYPOTHESIS_COLOR,
"evidence": EVIDENCE_COLOR1,
"E": EVIDENCE_COLOR2,
})
all_words.arrange(DOWN)
self.add(all_words)
Formula conversion animation
When expressing a formula, there are many cases where animation is performed in which some formula values are replaced with other values. In this case, use the mob.become method or Transform class.
For example, if you write an animation that changes $a^2+b^2=c^2$ to $3^2+4^2=5^2$,
eq = TexMobject("a^2", "+", "b^2", "=", "c^2")
self.add(eq)
self.play(
eq[0].become,TexMobject("3^2").move_to(eq[0]),
eq[2].become,TexMobject("4^2").move_to(eq[2]),
eq[4].become,TexMobject("5^2").move_to(eq[4]),
run_time=2,
)
self.wait()
Note that the position of the newly created TexMobject("3^2") object should be moved to the position of eq[0].
Transform(eq[0], TexMobject("3^2").move_to(eq[0])),
You can use 'Transform' class instead of 'become' method.
self.add()
self.play(
Transform(eq[0], TexMobject("3^2").move_to(eq[0])),
Transform(eq[2], TexMobject("4^2").move_to(eq[2])),
Transform(eq[4], TexMobject("5^2").move_to(eq[4])),
run_time=2,
)
self.wait()
In the above two methods, in converting $a^2+b^2=c^2$ to $3^2+4^2=5^2$, all three values of a, b, and c are changed at once.
To animate this to a->3 first, then b->4, then c->5, you can use the Succession class. (These are covered in more detail in [06] Animation.)
self.add()
self.play(
Succession(
Transform(eq[0], TexMobject("3^2").move_to(eq[0])),
Transform(eq[2], TexMobject("4^2").move_to(eq[2])),
Transform(eq[4], TexMobject("5^2").move_to(eq[4])),
),
run_time=2,
)
Next: [05-2-C] TextMobject: for Equation and Text
Go To: [99] Table of Contents
'Programming > Manim Lectures' 카테고리의 다른 글
[05-3]Shapes (0) | 2020.06.07 |
---|---|
[05-2-C] TextMobject: for Equation and Text (0) | 2020.06.06 |
[05-2-A] Text: for general text string (0) | 2020.06.06 |
[05-2] Text, Equation (0) | 2020.06.06 |
[05-1-F]Status methods: copy, save_state, restore (0) | 2020.06.05 |