Let's start with a LaTeX file (called latex_slides.tex
), as bellow:
\documentclass{article}%
\def\pause{}
\pagestyle{empty}
\begin{document}
\pause
$E=mc^2$\pause
\begin{enumerate}
\item One\pause
\item Two\pause
\item Three\pause
\end{enumerate}
\end{document}
I want to create an animation that simulates the handwriting of the text, using Manim, that takes the tex file as input. When it encounters \pause
, it should insert a 2 second pause in the animation.
I wrote the following Python script:
from manim import *
class LaTeXAnim(Scene):
def construct(self):
myfile = TexTemplate.from_file("latex_slides.tex")
tex = Tex(r"{}", tex_template = myfile)
self.play(Write(tex, rate_func = smooth, lag_ratio = 0.25, run_time = 5))
self.wait(2)
This works just fine by "handwriting" the text. I don't know how to make Manim treat \pause
as a special object with the meaning of self.wait(2)
. Is it even possible?
Maybe \pause
could create a special object that could be identified in the SVG file that Manim generates from the DVI file. Then, this object becomes an Mobject that can be transformed in a command.
Let's start with a LaTeX file (called latex_slides.tex
), as bellow:
\documentclass{article}%
\def\pause{}
\pagestyle{empty}
\begin{document}
\pause
$E=mc^2$\pause
\begin{enumerate}
\item One\pause
\item Two\pause
\item Three\pause
\end{enumerate}
\end{document}
I want to create an animation that simulates the handwriting of the text, using Manim, that takes the tex file as input. When it encounters \pause
, it should insert a 2 second pause in the animation.
I wrote the following Python script:
from manim import *
class LaTeXAnim(Scene):
def construct(self):
myfile = TexTemplate.from_file("latex_slides.tex")
tex = Tex(r"{}", tex_template = myfile)
self.play(Write(tex, rate_func = smooth, lag_ratio = 0.25, run_time = 5))
self.wait(2)
This works just fine by "handwriting" the text. I don't know how to make Manim treat \pause
as a special object with the meaning of self.wait(2)
. Is it even possible?
Maybe \pause
could create a special object that could be identified in the SVG file that Manim generates from the DVI file. Then, this object becomes an Mobject that can be transformed in a command.
1 Answer
Reset to default 0I don't think that Manim can natively interpret a LaTeX macro like \pause, but you can preprocess the LaTeX file like this:
from manim import *
class LaTeXAnim(Scene):
def construct(self):
with open("latex_slides.tex", "r") as f:
tex_content = f.read()
# Split the content on occurrences of "\pause"
parts = tex_content.split(r"\pause")
parts = [part.strip() for part in parts if part.strip() != ""]
for part in parts:
tex = Tex(part)
self.play(Write(tex, rate_func=smooth, lag_ratio=0.25, run_time=5))
self.wait(2)
In this example, the \pause
token is used to split the LaTeX content and insert a wait function.
Hope it wass usefull, happy coding!