Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

More examples for the docs (#525)

kolibril13a32302e5781d
diff --git a/docs/source/examples/advanced_projects.rst b/docs/source/examples/advanced_projects.rst
index 4626cf74d..6490877ce 100644
--- a/docs/source/examples/advanced_projects.rst
+++ b/docs/source/examples/advanced_projects.rst
@@ -29,8 +29,8 @@ Advanced Projects
def add_x_labels(self):
x_labels = [
- TexMobject("\pi"), TexMobject("2 \pi"),
- TexMobject("3 \pi"), TexMobject("4 \pi"),
+ MathTex("\pi"), MathTex("2 \pi"),
+ MathTex("3 \pi"), MathTex("4 \pi"),
]
for i in range(len(x_labels)):
diff --git a/docs/source/examples/annotations.rst b/docs/source/examples/annotations.rst
index 1d1783386..33a331bd9 100644
--- a/docs/source/examples/annotations.rst
+++ b/docs/source/examples/annotations.rst
@@ -15,3 +15,35 @@ Annotations
b2text = b2.get_tex("x-x_1")
self.add(dot, dot2, line, b1, b2, b1text, b2text)
+.. manim:: ExampleArrow
+ :quality: medium
+ :save_last_frame:
+
+ class ExampleArrow(Scene):
+ def construct(self):
+ dot = Dot(ORIGIN)
+ arrow = Arrow(ORIGIN, [2, 2, 0], buff=0)
+ numberplane = NumberPlane()
+ origin_text = TextMobject('(0, 0)').next_to(dot, DOWN)
+ tip_text = TextMobject('(2, 2)').next_to(arrow.get_end(), RIGHT)
+ self.add(numberplane, dot, arrow, origin_text, tip_text)
+
+.. manim:: ExampleArrowTips
+ :quality: medium
+ :save_last_frame:
+
+ from manim.mobject.geometry import ArrowTriangleTip, ArrowSquareTip, ArrowSquareFilledTip,\
+ ArrowCircleTip, ArrowCircleFilledTip
+ class ExampleArrowTips(Scene):
+ def construct(self):
+ a00 = Arrow(start=[-2, 3, 0], end=[2, 3, 0], color=YELLOW)
+ a11 = Arrow(start=[-2, 2, 0], end=[2, 2, 0], tip_shape=ArrowTriangleTip)
+ a12 = Arrow(start=[-2, 1, 0], end=[2, 1, 0])
+ a21 = Arrow(start=[-2, 0, 0], end=[2, 0, 0], tip_shape=ArrowSquareTip)
+ a22 = Arrow([-2, -1, 0], [2, -1, 0], tip_shape=ArrowSquareFilledTip)
+ a31 = Arrow([-2, -2, 0], [2, -2, 0], tip_shape=ArrowCircleTip)
+ a32 = Arrow([-2, -3, 0], [2, -3, 0], tip_shape=ArrowCircleFilledTip)
+ b11 = a11.copy().scale(0.5, scale_tips=True).next_to(a11, RIGHT)
+ b12 = a12.copy().scale(0.5, scale_tips=True).next_to(a12, RIGHT)
+ b21 = a21.copy().scale(0.5, scale_tips=True).next_to(a21, RIGHT)
+ self.add(a00, a11, a12, a21, a22, a31, a32, b11, b12, b21)
diff --git a/docs/source/examples/formulas.rst b/docs/source/examples/formulas.rst
index c28eefca0..5f615f963 100644
--- a/docs/source/examples/formulas.rst
+++ b/docs/source/examples/formulas.rst
@@ -1,21 +1,14 @@
Formulas
=================================
-.. manim:: Formula1
- :save_last_frame:
- class Formula1(Scene):
- def construct(self):
- t = MathTex(r"\int_a^b f'(x) dx = f(b)- f(a)")
- self.add(t)
- self.wait(1)
.. manim:: MoveFrameBox
class MoveFrameBox(Scene):
def construct(self):
- text=TexMobject(
+ text=MathTex(
"\\frac{d}{dx}f(x)g(x)=","f(x)\\frac{d}{dx}g(x)","+",
"g(x)\\frac{d}{dx}f(x)"
)
@@ -35,7 +28,7 @@ Formulas
class MoveBraces(Scene):
def construct(self):
- text=TexMobject(
+ text=MathTex(
"\\frac{d}{dx}f(x)g(x)=", #0
"f(x)\\frac{d}{dx}g(x)", #1
"+", #2
diff --git a/docs/source/examples/text.rst b/docs/source/examples/text.rst
index 724dea445..d27e294ff 100644
--- a/docs/source/examples/text.rst
+++ b/docs/source/examples/text.rst
@@ -9,108 +9,6 @@ Text
text = Text('Hello world').scale(3)
self.add(text)
-.. manim:: Example2Text
- :save_last_frame:
-
- class Example2Text(Scene):
- def construct(self):
- text = Text('Hello world', color=BLUE).scale(3)
- self.add(text)
-
-
-.. manim:: Example3Text
- :save_last_frame:
-
- class Example3Text(Scene):
- def construct(self):
- text = Text('Hello world', gradient=(BLUE, GREEN)).scale(3)
- self.add(text)
-
-
-.. manim:: Example4Text
- :save_last_frame:
-
- class Example4Text(Scene):
- def construct(self):
- text = Text('Hello world', t2g={'world':(BLUE, GREEN)}).scale(3)
- self.add(text)
-
-.. manim:: Example5Text
- :save_last_frame:
-
- class Example5Text(Scene):
- def construct(self):
- text = Text('Hello world', font='Source Han Sans').scale(3)
- self.add(text)
-
-.. manim:: Example5bText
- :save_last_frame:
-
- class Example5bText(Scene):
- def construct(self):
- text = Text('Hello world', t2f={'world':'Forte'}).scale(3)
- self.add(text)
-
-.. manim:: Example6Text
- :save_last_frame:
-
- class Example6Text(Scene):
- def construct(self):
- text = Text('Hello world', slant=ITALIC).scale(3)
- self.add(text)
-
-.. manim:: Example7Text
- :save_last_frame:
-
- class Example7Text(Scene):
- def construct(self):
- text = Text('Hello world!', t2s={'world':ITALIC}).scale(3)
- self.add(text)
-
-.. manim:: Example8Text
- :save_last_frame:
-
- class Example8Text(Scene):
- def construct(self):
- text = Text('Hello world', weight=BOLD).scale(3)
- self.add(text)
-
-.. manim:: Example9Text
- :save_last_frame:
-
- class Example9Text(Scene):
- def construct(self):
- text = Text('Hello world', t2w={'world':BOLD}).scale(3)
- self.add(text)
-
-.. manim:: Example10Text
- :save_last_frame:
-
- class Example10Text(Scene):
- def construct(self):
- text = Text('Hello', size=0.3).scale(3)
- self.add(text)
-
-.. manim:: Example11Text
- :save_last_frame:
-
- class Example11Text(Scene):
- def construct(self):
- text = Text('Hello\nWorld', lsh=1.5).scale(3)
- self.add(text)
-
-.. manim:: Example12Text
- :save_last_frame:
-
- class Example12Text(Scene):
- def construct(self):
- text = Text(
- 'Google',
- t2c={'[:1]':'#3174f0', '[1:2]':'#e53125',
- '[2:3]':'#fbb003', '[3:4]':'#3174f0',
- '[4:5]':'#269a43', '[5:]':'#e53125', }).scale(3)
- self.add(text)
-
-`Text` works also with other languages like `你好` or `こんにちは` or `안녕하세요` or `مرحبا بالعالم`.
-Be sure you have the font that supports those languages!
+This is an example that illustrates how to use the :class:`~.Text` class.
+In case you want to use other alphabets like `你好` or `こんにちは` or `안녕하세요` or `مرحبا بالعالم`, you can have a look at :class:`~.PangoText`
diff --git a/manim/mobject/svg/tex_mobject.py b/manim/mobject/svg/tex_mobject.py
index 3a0efafd4..8f6230214 100644
--- a/manim/mobject/svg/tex_mobject.py
+++ b/manim/mobject/svg/tex_mobject.py
@@ -152,6 +152,21 @@ class SingleStringMathTex(SVGMobject):
class MathTex(SingleStringMathTex):
+ """
+ A class for displaying mathematical formulas with Latex syntax.
+
+
+ Examples
+ --------
+ .. manim:: Formula1
+ :save_last_frame:
+
+ class Formula1(Scene):
+ def construct(self):
+ t = MathTex(r"\int_a^b f'(x) dx = f(b)- f(a)")
+ self.add(t)
+ """
+
CONFIG = {
"arg_separator": " ",
"substrings_to_isolate": [],
diff --git a/manim/mobject/svg/text_mobject.py b/manim/mobject/svg/text_mobject.py
index a05ba6e75..da5fd26f5 100644
--- a/manim/mobject/svg/text_mobject.py
+++ b/manim/mobject/svg/text_mobject.py
@@ -75,12 +75,17 @@ class CairoText(SVGMobject):
Text objects behave like a :class:`.VGroup`-like iterable of all characters
in the given text. In particular, slicing is possible.
+
+
.. WARNING::
Using a :class:`.Transform` on text with leading whitespace can look
`weird <https://github.com/3b1b/manim/issues/1067>`_. Consider using
:meth:`remove_invisible_chars` to resolve this issue.
+
+
+
"""
CONFIG = {
@@ -917,6 +922,51 @@ class Text(CairoText):
Text objects behave like a :class:`.VGroup`-like iterable of all characters
in the given text. In particular, slicing is possible.
+ Examples
+ --------
+ .. manim:: Example1Text
+ :save_last_frame:
+
+ class Example1Text(Scene):
+ def construct(self):
+ text = Text('Hello world').scale(3)
+ self.add(text)
+
+ .. manim:: TextColorExample
+ :save_last_frame:
+
+ class TextColorExample(Scene):
+ def construct(self):
+ text1 = Text('Hello world', color=BLUE).scale(3)
+ text2 = Text('Hello world', gradient=(BLUE, GREEN)).scale(3).next_to(text1, DOWN)
+ self.add(text1, text2)
+
+ .. manim:: TextItalicAndBoldExample
+ :save_last_frame:
+
+ class TextItalicAndBoldExample(Scene):
+ def construct(self):
+ text0 = Text('Hello world', slant=ITALIC)
+ text1 = Text('Hello world', t2s={'world':ITALIC})
+ text2 = Text('Hello world', weight=BOLD)
+ text3 = Text('Hello world', t2w={'world':BOLD})
+ self.add(text0,text1, text2,text3)
+ for i,mobj in enumerate(self.mobjects):
+ mobj.shift(DOWN*(i-1))
+
+
+ .. manim:: TextMoreCustomization
+ :save_last_frame:
+
+ class TextMoreCustomization(Scene):
+ def construct(self):
+ text1 = Text(
+ 'Google',
+ t2c={'[:1]': '#3174f0', '[1:2]': '#e53125',
+ '[2:3]': '#fbb003', '[3:4]': '#3174f0',
+ '[4:5]': '#269a43', '[5:]': '#e53125'}, size=1.2).scale(3)
+ self.add(text1)
+
.. WARNING::
Using a :class:`.Transform` on text with leading whitespace can look