最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

animation - Slideshow from LaTeX file using Manim - Stack Overflow

programmeradmin2浏览0评论

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.

Share Improve this question asked Jan 18 at 18:51 digital-Inkdigital-Ink 513 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I 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!

发布评论

评论列表(0)

  1. 暂无评论