英文:
Is it possible to use MathTex() and Text() alternately without always creating a new Mobject?
问题
Here's the translated code you requested:
类ZWSAufgabe(场景):
def construct(self):
Text.set_default(font =“ Poppins”, color = BLACK, line_spacing = 3)
MathTex.set_default(color = BLACK)
def custom_text(string,** kwargs):
text = Text(string,** kwargs)
text.scale(.5).set_stroke(BLACK,0)
return text
def custom_mtex(string,** kwargs):
text = MathTex(string,** kwargs)
text.scale(.75)
return text
aufgabe1 = custom_text(“ Zeige, dass die Funktion”).to_edge(UL)
aufgabe2 = custom_mtex(r“ f(x):= sin(x) - e^{-x}”).next_to(aufgabe1,RIGHT)
aufgabe3 = custom_text(“ im Intervall”).next_to(aufgabe2,RIGHT)
aufgabe4 = custom_mtex(r“ [0,\ frac {\ pi} {2}]”).next_to(aufgabe3,RIGHT)
aufgabe5 = custom_text(“ genau eine Nullstelle besitzt.”).next_to(aufgabe1,DOWN,aligned_edge = LEFT)
self.play(Write(aufgabe1))
self.play(Write(aufgabe2))
self.play(Write(aufgabe3))
self.play(Write(aufgabe4))
self.play(Write(aufgabe5))
self.wait()
Please note that code translation might not always capture the full context, and you may need to adjust some parts to match the syntax and libraries you are using.
英文:
Optimize code for using MathTex()
and Text()
alternately in Manim
I would like to have the following output (image).
For this output, my code is as follows:
class ZWSAufgabe(Scene):
def construct(self):
Text.set_default(font="Poppins", color=BLACK, line_spacing=3)
MathTex.set_default(color=BLACK)
def custom_text(string, **kwargs):
text = Text(string, **kwargs)
text.scale(.5).set_stroke(BLACK, 0)
return text
def custom_mtex(string, **kwargs):
text = MathTex(string, **kwargs)
text.scale(.75)
return text
aufgabe1 = custom_text("Zeige, dass die Funktion").to_edge(UL)
aufgabe2 = custom_mtex(r"f(x) := sin(x) - e^{-x}").next_to(aufgabe1, RIGHT)
aufgabe3 = custom_text("im Intervall").next_to(aufgabe2, RIGHT)
aufgabe4 = custom_mtex(r"[0, \frac{\pi}{2}]").next_to(aufgabe3, RIGHT)
aufgabe5 = custom_text("genau eine Nullstelle besitzt.").next_to(aufgabe1, DOWN, aligned_edge=LEFT)
self.play(Write(aufgabe1))
self.play(Write(aufgabe2))
self.play(Write(aufgabe3))
self.play(Write(aufgabe4))
self.play(Write(aufgabe5))
self.wait()
How can I improve my code so that I don't always have to write so much?
答案1
得分: 1
Sure, here are the translated parts:
是的,你可以,但不是使用Text()
而是Tex()
来排版文本。然后你可以在数学模式和文本模式之间切换使用$..$
或者\(..\)
。
使用XeTeX,你也可以轻松地在Tex()
中使用你喜欢的字体进行排版 - 来到Discord,在那里更容易回答这些问题。
还有一些方法可以通过使用不同的LaTeX环境和命令来改进排版和布局。
以下是示例代码的翻译:
class ZWSAufgabe(Scene):
def construct(self):
Tex.set_default(color=BLACK)
self.camera.background_color = WHITE
myTexTemplate = TexTemplate(
tex_compiler="xelatex",
output_format='.xdv',
)
myTexTemplate.add_to_preamble(r"\usepackage{fontspec}\setmainfont{Poppins}\renewcommand{\baselinestretch}{2}")
aufgabe = Tex(
r'''Show that the function $f(x) := \sin(x) - e^{-x}$\\
in the interval $[0, \frac{\pi}{2}]$ has exactly one root.''',
tex_template=myTexTemplate,
tex_environment="flushleft"
).to_edge(UL)
aufgabe.scale(0.75)
self.play(Write(aufgabe))
<details>
<summary>英文:</summary>
Yes you can, but not by utilizing `Text()` but `Tex()` for the typesetting of the text. Then you can switch with `$..$` or `\(..\)` between math mode and text mode.
Using XeTeX you could also use your desired font easily for the typesetting in `Tex()` - come over to [Discord][1] where it is easier to answer these questions in a forum.
There also ways to improve the typesetting and layout by using different LaTeX environments and commands here.
class ZWSAufgabe(Scene):
def construct(self):
#Text.set_default(font="Poppins", color=BLACK, line_spacing=3)
#MathTex.set_default(color=BLACK)
aufgabe = Tex(r"Zeige, dass die Funktion $f(x) := sin(x) - e^{-x}$\\ im Intervall $[0, \frac{\pi}{2}]$ genau eine Nullstelle besitzt.").to_edge(UL)
aufgabe.scale(0.75)
self.play(Write(aufgabe))
self.wait()
So using XeTeX, a left-aligned layout and your Poppy-font the solution could look like this:
class ZWSAufgabe(Scene):
def construct(self):
Tex.set_default(color=BLACK)
self.camera.background_color = WHITE
myTexTemplate = TexTemplate(
tex_compiler="xelatex",
output_format='.xdv',
)
myTexTemplate.add_to_preamble(r"\usepackage{fontspec}\setmainfont{Poppins}\renewcommand{\baselinestretch}{2}")
aufgabe = Tex(
r'''Zeige, dass die Funktion $f(x) := sin(x) - e^{-x}$\\
im Intervall $[0, \frac{\pi}{2}]$ genau eine Nullstelle
besitzt.''',
tex_template=myTexTemplate,
tex_environment="flushleft"
).to_edge(UL)
aufgabe.scale(0.75)
self.play(Write(aufgabe))
[![enter image description here][2]][2]
[1]: https://manim.community/discord
[2]: https://i.stack.imgur.com/BHgkk.png
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论