使用 Manim 时,如何仅对
MathTex
对象中的数字的指数着色?
在我要着色的公式中,所有“1”都写为幂。
equation = r"\frac{6}{4^1-2}-\frac{5}{4^1+1}=2"
我尝试过这样做:
obj = MathTex(equation, substrings_to_isolate="^1")
obj.set_color_by_tex("1", RED)
self.play(Write(obj))
但是执行代码时,出现错误:
LaTeX 编译错误:lign* 的参数有一个额外的 }。
错误上下文:\begin{document} -> \begin{align*} -2}-\frac{5}{4 \end{align*}
Manim 在使用
isolate...
、双大括号、作为单独的子字符串或任何其他选项拆分 LaTeX 代码时的工作方式是将各个片段发送到 LaTeX 进行渲染。
但是,某些 LaTeX 命令(例如
\frac
)无法拆分其参数,因为生成的代码不再是有效的 LaTeX 代码。一种方法是使用 a \over b
而不是 \frac{a}{b}
,但即使这样也不能保证完美工作。
我想说自动化在大多数时候并不值得付出努力,所以你可以只计算要突出显示的字形的相应索引位置,功能
self.add(index_labels())
可以在这里进一步帮助你:
class testMathColor(Scene):
def construct(self):
matht9 = MathTex(
r"\frac{6}{4^1-2}-\frac{5}{4^1+1}=2",
).move_to([0, 0, 0])
# uncomment to find indices
self.add(matht9, index_labels(matht9[0]).set_color(RED).shift(0.15*UP))
#matht9.set_color_by_tex("1", RED)
#self.play(Write(matht9, run_time=1))
class testMathColor(Scene):
def construct(self):
matht9 = MathTex(
r"\frac{6}{4^1-2}-\frac{5}{4^1+1}=2",
).move_to([0, 0, 0])
# uncomment to find indices
#self.add(matht9, index_labels(matht9[0]).set_color(RED).shift(0.15*UP))
for i in [3,10]:
matht9[0][i].set_color(RED)
self.play(Write(matht9, run_time=1))
来到 Discord 了解更多关于如何隔离和着色方程部分的其他方法和/或关于 Manim 的一般信息。