-
Notifications
You must be signed in to change notification settings - Fork 9
/
_05_basic_tex_and_texts.py
279 lines (249 loc) · 9.26 KB
/
_05_basic_tex_and_texts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
from manim import *
class SimpleTexAndTexts(Scene):
def construct(self):
# Tex is a LaTeX plain text
tex = Tex(r"This is \LaTeX, with a formula: $x^2$")
# MathTex is Tex but with $$ $$
math_tex = MathTex(r"\frac{\rm d}{\rm d\it x}f = f'(x)")
# Sometimes it will be better to use
# {n \over d}
# instead
# \frac{n}{d}
text = Text("Normal text with PC fonts", font="Arial")
Group(tex, math_tex, text).set(width=config.frame_width-1).arrange(DOWN)
self.add(tex, math_tex, text)
self.wait()
"""
____ _
/ ___| _ ___| |_ ___ _ __ ___
| | | | | / __| __/ _ \| '_ ` _ \
| |__| |_| \__ \ || (_) | | | | | |
\____\__,_|___/\__\___/|_| |_| |_|
_ _ _ _
| |_ _____ __ | |_ ___ _ __ ___ _ __ | | __ _| |_ ___ ___
| __/ _ \ \/ / | __/ _ \ '_ ` _ \| '_ \| |/ _` | __/ _ \/ __|
| || __/> < | || __/ | | | | | |_) | | (_| | || __/\__ \
\__\___/_/\_\___\__\___|_| |_| |_| .__/|_|\__,_|\__\___||___/
|_____| |_|
"""
# Example from example_scenes/advanced_tex_fonts.py
TemplateForFrenchCursive = TexTemplate(
preamble=r"""
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[T1]{fontenc}
\usepackage[default]{frcursive}
\usepackage[eulergreek,noplusnominus,noequal,nohbar,%
nolessnomore,noasterisk]{mathastext}
"""
)
def FrenchCursive(*tex_strings, **kwargs):
return Tex(*tex_strings, tex_template=TemplateForFrenchCursive, **kwargs)
class TexFontTemplateManual(Scene):
"""An example scene that uses a manually defined TexTemplate() object to create
LaTeX output in French Cursive font"""
def construct(self):
self.add(Tex("Tex Font Example").to_edge(UL))
self.play(Write(FrenchCursive("$f: A \\longrightarrow B$").shift(UP)))
self.play(Write(FrenchCursive("Behold! We can write math in French Cursive")))
self.wait(1)
self.play(
Write(
Tex(
"See more font templates at \\\\ http://jf.burnol.free.fr/showcase.html"
).shift(2 * DOWN)
)
)
self.wait(2)
# __ __ _ _____ __ __
# | \/ |_ _ ___(_) __|_ _|__\ \/ /
# | |\/| | | | / __| |/ __|| |/ _ \\ /
# | | | | |_| \__ \ | (__ | | __// \
# |_| |_|\__,_|___/_|\___||_|\___/_/\_\
TemplateForMusicTeX = TexTemplate(
preamble=r"""
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{mtxlatex}
\usepackage{graphicx}
"""
)
def MusicTeX(*tex_strings, **kwargs):
return Tex(
*tex_strings,
tex_template=TemplateForMusicTeX,
tex_environment="music", # <- Change enviroment
**kwargs
)
class MusicTeXExample(Scene):
def construct(self):
template = MusicTeX(r"""
\parindent10mm
\instrumentnumber{1}
\setname1{Piano}
\setstaffs1{2}
\generalmeter{\meterfrac44}
\startextract
\Notes\ibu0f0\qb0{cge}\tbu0\qb0g|\hl j\en
\Notes\ibu0f0\qb0{cge}\tbu0\qb0g|\ql l\sk\ql n\en
\bar
\Notes\ibu0f0\qb0{dgf}|\qlp i\en
\notes\tbu0\qb0g|\ibbl1j3\qb1j\tbl1\qb1k\en
\Notes\ibu0f0\qb0{cge}\tbu0\qb0g|\hl j\en
\zendextract
""")
template.set(width=config["frame_width"]-1)
self.play(
Write(template)
)
self.wait(2)
"""
____ _ _ _ ___ _ _
/ ___|___ | | ___ _ __ ___ __| | | |_ _____ __ ( _ ) | |_ _____ _| |_ ___
| | / _ \| |/ _ \| '__/ _ \/ _` | | __/ _ \ \/ / / _ \/\ | __/ _ \ \/ / __/ __|
| |__| (_) | | (_) | | | __/ (_| | | || __/> < | (_> < | || __/> <| |_\__ \
\____\___/|_|\___/|_| \___|\__,_| \__\___/_/\_\ \___/\/ \__\___/_/\_\\__|___/
"""
class TexAndMathTextColors(Scene):
def construct(self):
# If the texts are simple, that is,
# without fractions, roots, subscripts and/or superscripts,
# it is possible to color the text as follows.
tex = Tex(
r"This is \LaTeX, with a formula: $x^2$",
tex_to_color_map={
r"\LaTeX": RED,
"formula": ORANGE,
"$x^2$": TEAL,
}
)
math_tex = MathTex(
r"\frac{\rm d}{\rm d\it x}f = f'(x)",
# You cannot use "d": RED, try it
tex_to_color_map={
"f'(x)": YELLOW,
# "d": RED,
}
)
text = Text(
"Normal text with PC fonts",
font="Arial",
# These arguments can present problems in
# versions prior to 0.5.0, MarkupText class
# can be used instead of Text if text with
# alot of decorations is needed.
# Use Text for simple texts.
t2c={
"Normal": RED,
},
t2w={
"text": BOLD,
},
t2s={
"fonts": ITALIC
}
)
Group(tex, math_tex, text).set(width=config.frame_width-1).arrange(DOWN)
self.add(tex, math_tex, text)
self.wait()
"""
__ __ _ _____ _
| \/ | __ _ _ __| | ___ _ _ _|_ _|____ _| |_
| |\/| |/ _` | '__| |/ / | | | '_ \| |/ _ \ \/ / __|
| | | | (_| | | | <| |_| | |_) | | __/> <| |_
|_| |_|\__,_|_| |_|\_\\__,_| .__/|_|\___/_/\_\\__|
|_|
"""
class BasicMarkupExample(Scene):
def construct(self):
text1 = MarkupText("<b>foo</b> <i>bar</i> <b><i>foobar</i></b>")
text2 = MarkupText("<s>foo</s> <u>bar</u> <big>big</big> <small>small</small>")
text3 = MarkupText("H<sub>2</sub>O and H<sub>3</sub>O<sup>+</sup>")
text4 = MarkupText("type <tt>help</tt> for help")
text5 = MarkupText(
'<span underline="double">foo</span> <span underline="error">bar</span>'
)
group = VGroup(text1, text2, text3, text4, text5).arrange(DOWN)
self.add(group)
class ColorExample(Scene):
def construct(self):
text1 = MarkupText(
f'all in red <span fgcolor="{YELLOW}">except this</span>', color=RED
)
text2 = MarkupText("nice gradient", gradient=(BLUE, GREEN))
text3 = MarkupText(
'nice <gradient from="RED" to="YELLOW">intermediate</gradient> gradient',
gradient=(BLUE, GREEN),
)
text4 = MarkupText(
'fl ligature <gradient from="RED" to="YELLOW">causing trouble</gradient> here'
)
text4s = MarkupText(
'fl ligature <gradient from="RED" to="YELLOW" offset="1">causing trouble</gradient> here'
)
text5 = MarkupText(
'fl ligature <gradient from="RED" to="YELLOW" offset="1">defeated</gradient> with offset'
)
text6 = MarkupText(
'fl ligature <gradient from="RED" to="YELLOW" offset="1">floating</gradient> inside'
)
text7 = MarkupText(
'fl ligature <gradient from="RED" to="YELLOW" offset="1,1">floating</gradient> inside'
)
group = VGroup(text1, text2, text3, text4, text4s, text5, text6, text7).arrange(DOWN)
self.add(group)
class UnderlineExample(Scene):
def construct(self):
text1 = MarkupText(
'<span underline="double" underline_color="green">bla</span>'
)
text2 = MarkupText(
'<span underline="single" underline_color="green">xxx</span><gradient from="#ffff00" to="RED">aabb</gradient>y'
)
text3 = MarkupText(
'<span underline="single" underline_color="green">xxx</span><gradient from="#ffff00" to="RED" offset="-1">aabb</gradient>y'
)
text4 = MarkupText(
'<span underline="double" underline_color="green">xxx</span><gradient from="#ffff00" to="RED">aabb</gradient>y'
)
text5 = MarkupText(
'<span underline="double" underline_color="green">xxx</span><gradient from="#ffff00" to="RED" offset="-2">aabb</gradient>y'
)
group = VGroup(text1, text2, text3, text4, text5).arrange(DOWN)
self.add(group)
"""
____ _ __ __ _ _ _
/ ___|___ __| | ___| \/ | ___ | |__ (_) ___ ___| |_
| | / _ \ / _` |/ _ \ |\/| |/ _ \| '_ \| |/ _ \/ __| __|
| |__| (_) | (_| | __/ | | | (_) | |_) | | __/ (__| |_
\____\___/ \__,_|\___|_| |_|\___/|_.__// |\___|\___|\__|
|__/
"""
class CodeFromString(Scene):
def construct(self):
code = '''
from manim import Scene, Square
class FadeInSquare(Scene):
def construct(self):
s = Square()
self.play(FadeIn(s))
self.play(s.animate.scale(3))
self.wait()'''
rendered_code = Code(
code=code,
tab_width=4,
background="window",
language="Python",
font="Monospace",
style="monokai"
)
self.draw_code_all_lines_at_a_time(rendered_code)
self.wait()
def draw_code_all_lines_at_a_time(self, code, **kwargs):
self.play(LaggedStart(*[
Write(code[i])
for i in range(len(code))
]),
**kwargs
)