Programmatically converting/parsing LaTeX code to plain text

前端 未结 8 1326
梦毁少年i
梦毁少年i 2021-02-04 10:06

I have a couple of code projects in C++/Python in which LaTeX-format descriptions and labels are used to generate PDF documentation or graphs made using LaTeX+pstricks. However,

8条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-04 11:02

    Necroing this old thread, but found this nifty library called pylatexenc that seems to do almost exactly what the OP was after:

    from pylatexenc.latex2text import LatexNodes2Text
    
    
    LatexNodes2Text().latex_to_text(r"""\
    \section{Euler}
    \emph{This} bit is \textbf{very} clever:
    \begin{equation}
        \mathrm{e}^{i \pi} + 1 = 0  % wow!!
    \end{equation}
    where
    \[
    \mathrm{e} = \lim_{n \to \infty} \left(1 + \frac{1}{n}\right)^n
    \]
    """)
    

    which produces

    
    § EULER
    
    This bit is very clever:
    
        e^i π + 1 = 0
    
    where
    
        e = lim_n →∞(1 + 1/n)^n
    

    As you can see, the result is not perfect for the equations, but it does a great job of stripping and converting all the tex commands.

提交回复
热议问题