Skip to content

Commit a32302e

Browse files
kolibril13leotrsbehackl
authored
More examples for the docs (#525)
Added Text examples, Annotations, and some other additional things Co-authored-by: Leo Torres <[email protected]> Co-authored-by: Benjamin Hackl <[email protected]>
1 parent e0d62d7 commit a32302e

File tree

6 files changed

+103
-115
lines changed

6 files changed

+103
-115
lines changed

docs/source/examples/advanced_projects.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ Advanced Projects
2929

3030
def add_x_labels(self):
3131
x_labels = [
32-
TexMobject("\pi"), TexMobject("2 \pi"),
33-
TexMobject("3 \pi"), TexMobject("4 \pi"),
32+
MathTex("\pi"), MathTex("2 \pi"),
33+
MathTex("3 \pi"), MathTex("4 \pi"),
3434
]
3535

3636
for i in range(len(x_labels)):

docs/source/examples/annotations.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,35 @@ Annotations
1515
b2text = b2.get_tex("x-x_1")
1616
self.add(dot, dot2, line, b1, b2, b1text, b2text)
1717

18+
.. manim:: ExampleArrow
19+
:quality: medium
20+
:save_last_frame:
21+
22+
class ExampleArrow(Scene):
23+
def construct(self):
24+
dot = Dot(ORIGIN)
25+
arrow = Arrow(ORIGIN, [2, 2, 0], buff=0)
26+
numberplane = NumberPlane()
27+
origin_text = TextMobject('(0, 0)').next_to(dot, DOWN)
28+
tip_text = TextMobject('(2, 2)').next_to(arrow.get_end(), RIGHT)
29+
self.add(numberplane, dot, arrow, origin_text, tip_text)
30+
31+
.. manim:: ExampleArrowTips
32+
:quality: medium
33+
:save_last_frame:
34+
35+
from manim.mobject.geometry import ArrowTriangleTip, ArrowSquareTip, ArrowSquareFilledTip,\
36+
ArrowCircleTip, ArrowCircleFilledTip
37+
class ExampleArrowTips(Scene):
38+
def construct(self):
39+
a00 = Arrow(start=[-2, 3, 0], end=[2, 3, 0], color=YELLOW)
40+
a11 = Arrow(start=[-2, 2, 0], end=[2, 2, 0], tip_shape=ArrowTriangleTip)
41+
a12 = Arrow(start=[-2, 1, 0], end=[2, 1, 0])
42+
a21 = Arrow(start=[-2, 0, 0], end=[2, 0, 0], tip_shape=ArrowSquareTip)
43+
a22 = Arrow([-2, -1, 0], [2, -1, 0], tip_shape=ArrowSquareFilledTip)
44+
a31 = Arrow([-2, -2, 0], [2, -2, 0], tip_shape=ArrowCircleTip)
45+
a32 = Arrow([-2, -3, 0], [2, -3, 0], tip_shape=ArrowCircleFilledTip)
46+
b11 = a11.copy().scale(0.5, scale_tips=True).next_to(a11, RIGHT)
47+
b12 = a12.copy().scale(0.5, scale_tips=True).next_to(a12, RIGHT)
48+
b21 = a21.copy().scale(0.5, scale_tips=True).next_to(a21, RIGHT)
49+
self.add(a00, a11, a12, a21, a22, a31, a32, b11, b12, b21)

docs/source/examples/formulas.rst

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
Formulas
22
=================================
33

4-
.. manim:: Formula1
5-
:save_last_frame:
64

7-
class Formula1(Scene):
8-
def construct(self):
9-
t = MathTex(r"\int_a^b f'(x) dx = f(b)- f(a)")
10-
self.add(t)
11-
self.wait(1)
125

136

147
.. manim:: MoveFrameBox
158

169
class MoveFrameBox(Scene):
1710
def construct(self):
18-
text=TexMobject(
11+
text=MathTex(
1912
"\\frac{d}{dx}f(x)g(x)=","f(x)\\frac{d}{dx}g(x)","+",
2013
"g(x)\\frac{d}{dx}f(x)"
2114
)
@@ -35,7 +28,7 @@ Formulas
3528

3629
class MoveBraces(Scene):
3730
def construct(self):
38-
text=TexMobject(
31+
text=MathTex(
3932
"\\frac{d}{dx}f(x)g(x)=", #0
4033
"f(x)\\frac{d}{dx}g(x)", #1
4134
"+", #2

docs/source/examples/text.rst

Lines changed: 2 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -9,108 +9,6 @@ Text
99
text = Text('Hello world').scale(3)
1010
self.add(text)
1111

12-
.. manim:: Example2Text
13-
:save_last_frame:
14-
15-
class Example2Text(Scene):
16-
def construct(self):
17-
text = Text('Hello world', color=BLUE).scale(3)
18-
self.add(text)
19-
20-
21-
.. manim:: Example3Text
22-
:save_last_frame:
23-
24-
class Example3Text(Scene):
25-
def construct(self):
26-
text = Text('Hello world', gradient=(BLUE, GREEN)).scale(3)
27-
self.add(text)
28-
29-
30-
.. manim:: Example4Text
31-
:save_last_frame:
32-
33-
class Example4Text(Scene):
34-
def construct(self):
35-
text = Text('Hello world', t2g={'world':(BLUE, GREEN)}).scale(3)
36-
self.add(text)
37-
38-
.. manim:: Example5Text
39-
:save_last_frame:
40-
41-
class Example5Text(Scene):
42-
def construct(self):
43-
text = Text('Hello world', font='Source Han Sans').scale(3)
44-
self.add(text)
45-
46-
.. manim:: Example5bText
47-
:save_last_frame:
48-
49-
class Example5bText(Scene):
50-
def construct(self):
51-
text = Text('Hello world', t2f={'world':'Forte'}).scale(3)
52-
self.add(text)
53-
54-
.. manim:: Example6Text
55-
:save_last_frame:
56-
57-
class Example6Text(Scene):
58-
def construct(self):
59-
text = Text('Hello world', slant=ITALIC).scale(3)
60-
self.add(text)
61-
62-
.. manim:: Example7Text
63-
:save_last_frame:
64-
65-
class Example7Text(Scene):
66-
def construct(self):
67-
text = Text('Hello world!', t2s={'world':ITALIC}).scale(3)
68-
self.add(text)
69-
70-
.. manim:: Example8Text
71-
:save_last_frame:
72-
73-
class Example8Text(Scene):
74-
def construct(self):
75-
text = Text('Hello world', weight=BOLD).scale(3)
76-
self.add(text)
77-
78-
.. manim:: Example9Text
79-
:save_last_frame:
80-
81-
class Example9Text(Scene):
82-
def construct(self):
83-
text = Text('Hello world', t2w={'world':BOLD}).scale(3)
84-
self.add(text)
85-
86-
.. manim:: Example10Text
87-
:save_last_frame:
88-
89-
class Example10Text(Scene):
90-
def construct(self):
91-
text = Text('Hello', size=0.3).scale(3)
92-
self.add(text)
93-
94-
.. manim:: Example11Text
95-
:save_last_frame:
96-
97-
class Example11Text(Scene):
98-
def construct(self):
99-
text = Text('Hello\nWorld', lsh=1.5).scale(3)
100-
self.add(text)
101-
102-
.. manim:: Example12Text
103-
:save_last_frame:
104-
105-
class Example12Text(Scene):
106-
def construct(self):
107-
text = Text(
108-
'Google',
109-
t2c={'[:1]':'#3174f0', '[1:2]':'#e53125',
110-
'[2:3]':'#fbb003', '[3:4]':'#3174f0',
111-
'[4:5]':'#269a43', '[5:]':'#e53125', }).scale(3)
112-
self.add(text)
113-
114-
`Text` works also with other languages like `你好` or `こんにちは` or `안녕하세요` or `مرحبا بالعالم`.
115-
Be sure you have the font that supports those languages!
12+
This is an example that illustrates how to use the :class:`~.Text` class.
11613

14+
In case you want to use other alphabets like `你好` or `こんにちは` or `안녕하세요` or `مرحبا بالعالم`, you can have a look at :class:`~.PangoText`

manim/mobject/svg/tex_mobject.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,21 @@ def organize_submobjects_left_to_right(self):
152152

153153

154154
class MathTex(SingleStringMathTex):
155+
"""
156+
A class for displaying mathematical formulas with Latex syntax.
157+
158+
159+
Examples
160+
--------
161+
.. manim:: Formula1
162+
:save_last_frame:
163+
164+
class Formula1(Scene):
165+
def construct(self):
166+
t = MathTex(r"\int_a^b f'(x) dx = f(b)- f(a)")
167+
self.add(t)
168+
"""
169+
155170
CONFIG = {
156171
"arg_separator": " ",
157172
"substrings_to_isolate": [],

manim/mobject/svg/text_mobject.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,17 @@ class CairoText(SVGMobject):
7575
Text objects behave like a :class:`.VGroup`-like iterable of all characters
7676
in the given text. In particular, slicing is possible.
7777
78+
79+
7880
.. WARNING::
7981
8082
Using a :class:`.Transform` on text with leading whitespace can look
8183
`weird <https://github.com/3b1b/manim/issues/1067>`_. Consider using
8284
:meth:`remove_invisible_chars` to resolve this issue.
8385
86+
87+
88+
8489
"""
8590

8691
CONFIG = {
@@ -917,6 +922,51 @@ class Text(CairoText):
917922
Text objects behave like a :class:`.VGroup`-like iterable of all characters
918923
in the given text. In particular, slicing is possible.
919924
925+
Examples
926+
--------
927+
.. manim:: Example1Text
928+
:save_last_frame:
929+
930+
class Example1Text(Scene):
931+
def construct(self):
932+
text = Text('Hello world').scale(3)
933+
self.add(text)
934+
935+
.. manim:: TextColorExample
936+
:save_last_frame:
937+
938+
class TextColorExample(Scene):
939+
def construct(self):
940+
text1 = Text('Hello world', color=BLUE).scale(3)
941+
text2 = Text('Hello world', gradient=(BLUE, GREEN)).scale(3).next_to(text1, DOWN)
942+
self.add(text1, text2)
943+
944+
.. manim:: TextItalicAndBoldExample
945+
:save_last_frame:
946+
947+
class TextItalicAndBoldExample(Scene):
948+
def construct(self):
949+
text0 = Text('Hello world', slant=ITALIC)
950+
text1 = Text('Hello world', t2s={'world':ITALIC})
951+
text2 = Text('Hello world', weight=BOLD)
952+
text3 = Text('Hello world', t2w={'world':BOLD})
953+
self.add(text0,text1, text2,text3)
954+
for i,mobj in enumerate(self.mobjects):
955+
mobj.shift(DOWN*(i-1))
956+
957+
958+
.. manim:: TextMoreCustomization
959+
:save_last_frame:
960+
961+
class TextMoreCustomization(Scene):
962+
def construct(self):
963+
text1 = Text(
964+
'Google',
965+
t2c={'[:1]': '#3174f0', '[1:2]': '#e53125',
966+
'[2:3]': '#fbb003', '[3:4]': '#3174f0',
967+
'[4:5]': '#269a43', '[5:]': '#e53125'}, size=1.2).scale(3)
968+
self.add(text1)
969+
920970
.. WARNING::
921971
922972
Using a :class:`.Transform` on text with leading whitespace can look

0 commit comments

Comments
 (0)