Updating MathTex using a ValueTracker #3327
-
I want to update a from manim import *
class Parameters(Scene):
def __init__(self):
super().__init__()
def construct(self):
a = ValueTracker(1)
b = ValueTracker(0)
a_targets = [4, -2, 1]
b_targets = [1, -1, 0]
a_text = MathTex(f"a = {a.get_value()}").move_to(UP * 0.5)
b_text = MathTex(f"b = {b.get_value()}").move_to(DOWN * 0.5)
a_text.add_updater(
lambda mob: mob.set_value(f"a = {a.get_value()}")
)
b_text.add_updater(
lambda mob: mob.set_value(f"b = {b.get_value()}")
)
self.play(FadeIn(a_text), FadeIn(b_text))
self.wait(0.5)
for target in a_targets:
self.play(a_text.animate.set_value(target), rate_func=rate_functions.linear)
self.wait(0.5)
for target in b_targets:
self.play(b_text.animate.set_value(target), rate_func=rate_functions.linear)
self.wait(0.5) I ran the code with the following command: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
You cannot just set the "value" of a So in your case you can use your updaters together with the For a better help forum come over to Discord class Parameters(Scene):
# I don't think this is a good idea!
#def __init__(self):
# super().__init__()
def construct(self):
a = ValueTracker(1)
b = ValueTracker(0)
a_targets = [4, -2, 1]
b_targets = [1, -1, 0]
a_text = MathTex(f"a = {a.get_value()}").move_to(UP * 0.5)
b_text = always_redraw(lambda:
MathTex(f"b = {b.get_value()}").move_to(DOWN * 0.5)
)
a_text.add_updater(
lambda mob: mob.become(MathTex(f"a = {a.get_value()}").move_to(UP * 0.5))
)
c_everything = Variable(0, label="c", num_decimal_places=2).move_to(DOWN * 1.5)
self.play(FadeIn(a_text), FadeIn(b_text), FadeIn(c_everything))
self.wait(0.5)
for target in a_targets:
self.play(a.animate.set_value(target), rate_func=rate_functions.linear)
self.wait(0.5)
for target in b_targets:
self.play(b.animate.set_value(target), rate_func=rate_functions.linear)
self.wait(0.5)
for target in a_targets:
self.play(c_everything.tracker.animate.set_value(target), rate_func=rate_functions.linear)
self.wait(0.5) Parameters.mp4 |
Beta Was this translation helpful? Give feedback.
You cannot just set the "value" of a
MathTex
object and expect something to happen.MathTex()
and related objects are sent to LaTeX as the external program to do the actual conversion only when they are first created. Afterward you cannot really change how they look. Also I am not quite sure if you are really after the final looks with a varying number of decimal places... you could add format specifiers to your python code though...So in your case you can use your updaters together with the
.become
method, or usealways_redraw
from the beginning. And that's apart from the fact that you are animating the wrong objects in yourself.play
.And then there is a much more suitable object for t…