Skip to content

Commit 20028ce

Browse files
uweziMrDiverbehackl
authored
feat: DecimalNumber() - added spacing between values and unit (#3366)
* feat: DecimalNumber() - added spacing between values and unit * Update manim/mobject/text/numbers.py Co-authored-by: Benjamin Hackl <[email protected]> * Update manim/mobject/text/numbers.py Co-authored-by: Benjamin Hackl <[email protected]> * Update manim/mobject/text/numbers.py Co-authored-by: Benjamin Hackl <[email protected]> * Update manim/mobject/text/numbers.py Co-authored-by: Benjamin Hackl <[email protected]> * Update manim/mobject/text/numbers.py Co-authored-by: Benjamin Hackl <[email protected]> * Update manim/mobject/text/numbers.py Co-authored-by: Benjamin Hackl <[email protected]> * Update manim/mobject/text/numbers.py Co-authored-by: Benjamin Hackl <[email protected]> --------- Co-authored-by: Tristan Schulz <[email protected]> Co-authored-by: Benjamin Hackl <[email protected]>
1 parent 3962a12 commit 20028ce

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

manim/mobject/text/numbers.py

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,34 @@
2222
class DecimalNumber(VMobject, metaclass=ConvertToOpenGL):
2323
"""An mobject representing a decimal number.
2424
25+
Parameters
26+
----------
27+
number
28+
The numeric value to be displayed. It can later be modified using :meth:`.set_value`.
29+
num_decimal_places
30+
The number of decimal places after the decimal separator. Values are automatically rounded.
31+
mob_class
32+
The class for rendering digits and units, by default :class:`.MathTex`.
33+
include_sign
34+
Set to ``True`` to include a sign for positive numbers and zero.
35+
group_with_commas
36+
When ``True`` thousands groups are separated by commas for readability.
37+
digit_buff_per_font_unit
38+
Additional spacing between digits. Scales with font size.
39+
show_ellipsis
40+
When a number has been truncated by rounding, indicate with an ellipsis (``...``).
41+
unit
42+
A unit string which can be placed to the right of the numerical values.
43+
unit_buff_per_font_unit
44+
An additional spacing between the numerical values and the unit. A value
45+
of ``unit_buff_per_font_unit=0.003`` gives a decent spacing. Scales with font size.
46+
include_background_rectangle
47+
Adds a background rectangle to increase contrast on busy scenes.
48+
edge_to_fix
49+
Assuring right- or left-alignment of the full object.
50+
font_size
51+
Size of the font.
52+
2553
Examples
2654
--------
2755
@@ -34,6 +62,8 @@ def construct(self):
3462
show_ellipsis=True,
3563
num_decimal_places=3,
3664
include_sign=True,
65+
unit=r"\text{M-Units}",
66+
unit_buff_per_font_unit=0.003
3767
)
3868
square = Square().to_edge(UP)
3969
@@ -59,6 +89,7 @@ def __init__(
5989
digit_buff_per_font_unit: float = 0.001,
6090
show_ellipsis: bool = False,
6191
unit: str | None = None, # Aligned to bottom unless it starts with "^"
92+
unit_buff_per_font_unit: float = 0,
6293
include_background_rectangle: bool = False,
6394
edge_to_fix: Sequence[float] = LEFT,
6495
font_size: float = DEFAULT_FONT_SIZE,
@@ -75,6 +106,7 @@ def __init__(
75106
self.digit_buff_per_font_unit = digit_buff_per_font_unit
76107
self.show_ellipsis = show_ellipsis
77108
self.unit = unit
109+
self.unit_buff_per_font_unit = unit_buff_per_font_unit
78110
self.include_background_rectangle = include_background_rectangle
79111
self.edge_to_fix = edge_to_fix
80112
self._font_size = font_size
@@ -89,6 +121,7 @@ def __init__(
89121
"digit_buff_per_font_unit": digit_buff_per_font_unit,
90122
"show_ellipsis": show_ellipsis,
91123
"unit": unit,
124+
"unit_buff_per_font_unit": unit_buff_per_font_unit,
92125
"include_background_rectangle": include_background_rectangle,
93126
"edge_to_fix": edge_to_fix,
94127
"font_size": font_size,
@@ -130,15 +163,25 @@ def _set_submobjects_from_number(self, number):
130163
self._string_to_mob("\\dots", SingleStringMathTex, color=self.color),
131164
)
132165

133-
if self.unit is not None:
134-
self.unit_sign = self._string_to_mob(self.unit, SingleStringMathTex)
135-
self.add(self.unit_sign)
136-
137166
self.arrange(
138167
buff=self.digit_buff_per_font_unit * self._font_size,
139168
aligned_edge=DOWN,
140169
)
141170

171+
if self.unit is not None:
172+
self.unit_sign = self._string_to_mob(self.unit, SingleStringMathTex)
173+
self.add(
174+
self.unit_sign.next_to(
175+
self,
176+
direction=RIGHT,
177+
buff=(self.unit_buff_per_font_unit + self.digit_buff_per_font_unit)
178+
* self._font_size,
179+
aligned_edge=DOWN,
180+
)
181+
)
182+
183+
self.move_to(ORIGIN)
184+
142185
# Handle alignment of parts that should be aligned
143186
# to the bottom
144187
for i, c in enumerate(num_string):

0 commit comments

Comments
 (0)