可以不用每次都创建新的 Mobject,而是可以交替使用 MathTex() 和 Text() 吗?

huangapple go评论69阅读模式
英文:

Is it possible to use MathTex() and Text() alternately without always creating a new Mobject?

问题

Here's the translated code you requested:

类ZWSAufgabe场景):
    def constructself):
        Text.set_defaultfont = Poppins”, color = BLACK line_spacing = 3
        MathTex.set_defaultcolor = BLACK

        def custom_textstring** kwargs):
            text = Textstring** kwargs
            text.scale.5.set_strokeBLACK0
            return text

        def custom_mtexstring** kwargs):
            text = MathTexstring** kwargs
            text.scale.75
            return text

        aufgabe1 = custom_text(“ Zeige, dass die Funktion”).to_edgeUL
        aufgabe2 = custom_mtexr fx):= sinx - e^{-x}”).next_toaufgabe1RIGHT
        aufgabe3 = custom_text(“ im Intervall”).next_toaufgabe2RIGHT
        aufgabe4 = custom_mtexr [0\ frac {\ pi} {2}]”).next_toaufgabe3RIGHT
        aufgabe5 = custom_text(“ genau eine Nullstelle besitzt.”).next_toaufgabe1DOWNaligned_edge = LEFT

        self.playWriteaufgabe1))
        self.playWriteaufgabe2))
        self.playWriteaufgabe3))
        self.playWriteaufgabe4))
        self.playWriteaufgabe5))
        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).可以不用每次都创建新的 Mobject,而是可以交替使用 MathTex() 和 Text() 吗?

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))

可以不用每次都创建新的 Mobject,而是可以交替使用 MathTex() 和 Text() 吗?


<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&quot;Zeige, dass die Funktion $f(x) := sin(x) - e^{-x}$\\ im Intervall $[0, \frac{\pi}{2}]$ genau eine Nullstelle besitzt.&quot;).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&#39;&#39;&#39;Zeige, dass die Funktion $f(x) := sin(x) - e^{-x}$\\ 
im Intervall $[0, \frac{\pi}{2}]$ genau eine Nullstelle 
besitzt.&#39;&#39;&#39;,
tex_template=myTexTemplate,
tex_environment=&quot;flushleft&quot;
).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>

huangapple
  • 本文由 发表于 2023年5月21日 17:49:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76299269.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定