Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

feat: DecimalNumber() - added spacing between values and unit (#3366)

Uwe Zimmermann20028cef46b3
diff --git a/manim/mobject/text/numbers.py b/manim/mobject/text/numbers.py
index f5eff7fad..10bf2e782 100644
--- a/manim/mobject/text/numbers.py
+++ b/manim/mobject/text/numbers.py
@@ -22,6 +22,34 @@ string_to_mob_map = {}
class DecimalNumber(VMobject, metaclass=ConvertToOpenGL):
"""An mobject representing a decimal number.
+ Parameters
+ ----------
+ number
+ The numeric value to be displayed. It can later be modified using :meth:`.set_value`.
+ num_decimal_places
+ The number of decimal places after the decimal separator. Values are automatically rounded.
+ mob_class
+ The class for rendering digits and units, by default :class:`.MathTex`.
+ include_sign
+ Set to ``True`` to include a sign for positive numbers and zero.
+ group_with_commas
+ When ``True`` thousands groups are separated by commas for readability.
+ digit_buff_per_font_unit
+ Additional spacing between digits. Scales with font size.
+ show_ellipsis
+ When a number has been truncated by rounding, indicate with an ellipsis (``...``).
+ unit
+ A unit string which can be placed to the right of the numerical values.
+ unit_buff_per_font_unit
+ An additional spacing between the numerical values and the unit. A value
+ of ``unit_buff_per_font_unit=0.003`` gives a decent spacing. Scales with font size.
+ include_background_rectangle
+ Adds a background rectangle to increase contrast on busy scenes.
+ edge_to_fix
+ Assuring right- or left-alignment of the full object.
+ font_size
+ Size of the font.
+
Examples
--------
@@ -34,6 +62,8 @@ class DecimalNumber(VMobject, metaclass=ConvertToOpenGL):
show_ellipsis=True,
num_decimal_places=3,
include_sign=True,
+ unit=r"\text{M-Units}",
+ unit_buff_per_font_unit=0.003
)
square = Square().to_edge(UP)
@@ -59,6 +89,7 @@ class DecimalNumber(VMobject, metaclass=ConvertToOpenGL):
digit_buff_per_font_unit: float = 0.001,
show_ellipsis: bool = False,
unit: str | None = None, # Aligned to bottom unless it starts with "^"
+ unit_buff_per_font_unit: float = 0,
include_background_rectangle: bool = False,
edge_to_fix: Sequence[float] = LEFT,
font_size: float = DEFAULT_FONT_SIZE,
@@ -75,6 +106,7 @@ class DecimalNumber(VMobject, metaclass=ConvertToOpenGL):
self.digit_buff_per_font_unit = digit_buff_per_font_unit
self.show_ellipsis = show_ellipsis
self.unit = unit
+ self.unit_buff_per_font_unit = unit_buff_per_font_unit
self.include_background_rectangle = include_background_rectangle
self.edge_to_fix = edge_to_fix
self._font_size = font_size
@@ -89,6 +121,7 @@ class DecimalNumber(VMobject, metaclass=ConvertToOpenGL):
"digit_buff_per_font_unit": digit_buff_per_font_unit,
"show_ellipsis": show_ellipsis,
"unit": unit,
+ "unit_buff_per_font_unit": unit_buff_per_font_unit,
"include_background_rectangle": include_background_rectangle,
"edge_to_fix": edge_to_fix,
"font_size": font_size,
@@ -130,15 +163,25 @@ class DecimalNumber(VMobject, metaclass=ConvertToOpenGL):
self._string_to_mob("\\dots", SingleStringMathTex, color=self.color),
)
- if self.unit is not None:
- self.unit_sign = self._string_to_mob(self.unit, SingleStringMathTex)
- self.add(self.unit_sign)
-
self.arrange(
buff=self.digit_buff_per_font_unit * self._font_size,
aligned_edge=DOWN,
)
+ if self.unit is not None:
+ self.unit_sign = self._string_to_mob(self.unit, SingleStringMathTex)
+ self.add(
+ self.unit_sign.next_to(
+ self,
+ direction=RIGHT,
+ buff=(self.unit_buff_per_font_unit + self.digit_buff_per_font_unit)
+ * self._font_size,
+ aligned_edge=DOWN,
+ )
+ )
+
+ self.move_to(ORIGIN)
+
# Handle alignment of parts that should be aligned
# to the bottom
for i, c in enumerate(num_string):