Parses valid
Note
TexSoupcurrently only supports Python3.
Created by Alvin Wan.
Just install via pip.
$ pip install texsoup$ git clone https://github.com/alvinwan/TexSoup.git
$ cd TexSoup
$ pip install .There is one main utility, TexSoup, which translates any
There are two ways to input open('file.tex')) or (2) a string.
from TexSoup import TexSoup
soup = TexSoup("""
\begin{document}
\section{Hello \textit{world}.}
\subsection{Watermelon}
(n.) A sacred fruit. Also known as:
\begin{itemize}
\item red lemon
\item life
\end{itemize}
Here is the prevalence of each synonym.
\begin{tabular}{c c}
red lemon & uncommon \\
life & common
\end{tabular}
\end{document}
""")With the soupified
>>> soup.section # grabs the first `section`
\section{Hello \textit{world}.}
>>> soup.section.name
'section'
>>> soup.section.string
'Hello \\textit{world}.'
>>> soup.section.parent.name
'document'
>>> soup.tabular
\begin{tabular}{c c}
red lemon & uncommon \\
life & common
\end{tabular}
>>> soup.tabular.args[0]
'c c'
>>> soup.item
\item red lemon
>>> list(soup.find_all('item'))
[\item red lemon, \item life]For (slightly) more advanced searches, include arguments. For example, to
search for all references to a particular label, search for ref{<label>}. This
way you can count the number of times a particular label is referenced.
>>> soup = TexSoup("""
... \section{Heading}\label{Section:Heading}
...
... Some text about the \ref{Section:Heading} heading goes here. Yet another
... sentence about the \ref{Section:Heading} heading.
... """)
>>> soup.count('\ref{Section:Heading}')
2Additionally, modify the TexSoup parse tree in place, to generate new
>>> soup = TexSoup("""\textbf{'Hello'}\textit{'Y'}O\textit{'U'}""")
>>> soup.textbf.delete()
>>> 'Hello' not in repr(soup)
True
>>> soup.textit.replace('S')
>>> soup.textit.replace('U', 'P')
>>> soup
SOUPThere is one main parsing utility, read, which translates any
>>> from TexSoup import read
>>> expr = read('\section{textbf}')
>>> expr
TexCmd('section', [RArg('textbf')])
>>> print(expr)
\section{textbf}TexSoup has a variety of practical applications, whether it be minor
conveniences or more powerful
See the examples/ folder for example scripts and usages for TexSoup.
See slightly more complex uses for TexSoup.
-
LaTex2Python converts
$\LaTeX$ into a document tree, organizing content by either a default or custom hierarchy. -
Tex2Ipy by Prabhu Ramachandran,
converts
$\LaTeX$ beamer files to Jupyter notebooks