<p>I understand this is an old post, but since this post comes up often in latex-python-parsing searches (as evident by <a href="https://stackoverflow.com/questions/49779853/extract-only-body-text-from-arxiv-articles-formatted-as-tex">Extract only body text from arXiv articles formatted as .tex</a>), leaving this here for folks down the line: Here's a LaTeX parser in Python that supports search over and modification of the parse tree, <a href="https://github.com/alvinwan/texsoup" rel="noreferrer">https://github.com/alvinwan/texsoup</a>. Taken from the README, here is sample text and how you can interact with it via TexSoup.</p>

<pre><code>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 &amp; uncommon \\
life &amp; common
\end{tabular}

\end{document}
""")
</code></pre>

<p>Here's how to navigate the parse tree.</p>

<pre><code>&gt;&gt;&gt; soup.section  # grabs the first `section`
\section{Hello \textit{world}.}
&gt;&gt;&gt; soup.section.name
'section'
&gt;&gt;&gt; soup.section.string
'Hello \\textit{world}.'
&gt;&gt;&gt; soup.section.parent.name
'document'
&gt;&gt;&gt; soup.tabular
\begin{tabular}{c c}
red lemon &amp; uncommon \\
life &amp; common
\end{tabular}
&gt;&gt;&gt; soup.tabular.args[0]
'c c'
&gt;&gt;&gt; soup.item
\item red lemon
&gt;&gt;&gt; list(soup.find_all('item'))
[\item red lemon, \item life]
</code></pre>

<p>Disclaimer: I wrote this lib, but it was for similar reasons. Regarding the post by Little Bobby Tales (regarding <code>def</code>), TexSoup doesn't handle definitions.</p>
Programmatically converting/parsing LaTeX code to plain text
<p>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, we also have some plain text outputs, such as an HTML version of the documentation (I already have code to write minimal markup for that) and a non-TeX-enabled plot renderer.</p>

<p>For these I would like to eliminate the TeX markup that is necessary for e.g. representing physical units. This includes non-breaking (thin) spaces, \text, \mathrm etc. It would also be nice to parse down things like \frac{#1}{#2} into #1/#2 for the plain text output (and use MathJax for the HTML). Due to the system that we've got at the moment, I need to be able to do this from Python, i.e. <em>ideally</em> I'm looking for a Python package, but a non-Python executable which I can call from Python and catch the output string would also be fine.</p>

<p>I'm aware of the <a href="https://tex.stackexchange.com/questions/6431/options-for-converting-latex-to-plain-text">similar question on the TeX StackExchange site</a>, but there weren't any really programmatic solutions to that: I've looked at detex, plasTeX and pytex, which they all seem a bit dead and don't really do what I need: programmatic conversion of a TeX string to a representative plain text string.</p>

<p>I could try writing a basic TeX parser using e.g. pyparsing, but a) that might be pitfall-laden and help would be appreciated and b) surely someone has tried that before, or knows of a way to hook into TeX itself to get a better result?</p>

<p><strong>Update:</strong> Thanks for all the answers... it does indeed seem to be a bit of an awkward request! I can make do with less than general parsing of LaTeX, but the reason for considering a parser rather than a load of regexes in a loop is that I want to be able to handle nested macros and multi-arg macros nicely, and get the brace matching to work properly. Then I can e.g. reduce txt-irrelevant macros like \text and \mathrm first, and handle txt-relevant ones like \frac last... maybe even with appropriate parentheses! Well, I can dream... for now regexes are not doing such a terrible job.</p>