# manim.mobject.numbers.Variable¶

class Variable(var, label, var_type=<class 'manim.mobject.numbers.DecimalNumber'>, num_decimal_places=2, **kwargs)[source]

A class for displaying text that continuously updates to reflect the value of a python variable.

Automatically adds the text for the label and the value when instantiated and added to the screen.

Parameters
label

The label for your variable, for example x = ....

Type

Union[str, Tex, MathTex, Text, TexSymbol, SingleStringMathTex]

tracker

Useful in updating the value of your variable on-screen.

Type

ValueTracker

value

The tex for the value of your variable.

Type

Examples

Normal usage:

# DecimalNumber type
var = 0.5
on_screen_var = Variable(var, Text("var"), num_decimal_places=3)
# Integer type
int_var = 0
on_screen_int_var = Variable(int_var, Text("int_var"), var_type=Integer)
# Using math mode for the label
on_screen_int_var = Variable(int_var, "{a}_{i}", var_type=Integer)


Example: VariablesWithValueTracker

from manim import *

class VariablesWithValueTracker(Scene):
def construct(self):
var = 0.5
on_screen_var = Variable(var, Text("var"), num_decimal_places=3)

# You can also change the colours for the label and value
on_screen_var.label.set_color(RED)
on_screen_var.value.set_color(GREEN)

self.play(Write(on_screen_var))
# The above line will just display the variable with
# its initial value on the screen. If you also wish to
# update it, you can do so by accessing the tracker attribute
self.wait()
var_tracker = on_screen_var.tracker
var = 10.5
self.play(var_tracker.animate.set_value(var))
self.wait()

int_var = 0
on_screen_int_var = Variable(
int_var, Text("int_var"), var_type=Integer
).next_to(on_screen_var, DOWN)
on_screen_int_var.label.set_color(RED)
on_screen_int_var.value.set_color(GREEN)

self.play(Write(on_screen_int_var))
self.wait()
var_tracker = on_screen_int_var.tracker
var = 10.5
self.play(var_tracker.animate.set_value(var))
self.wait()

# If you wish to have a somewhat more complicated label for your
# variable with subscripts, superscripts, etc. the default class
# for the label is MathTex
subscript_label_var = 10
on_screen_subscript_var = Variable(subscript_label_var, "{a}_{i}").next_to(
on_screen_int_var, DOWN
)
self.play(Write(on_screen_subscript_var))
self.wait()


Methods

Attributes

 animate Used to animate the application of a method. depth The depth of the mobject. height The height of the mobject. width The width of the mobject.