TexSoup
TexSoup copied to clipboard
Problem with invalid Mathmode "AssertionError: Command \item invalid in math mode."
import TexSoup latex_soup = TexSoup.TexSoup("""($3\times2\times$$\sim$5=$\sim$30 minutes each)""") print(latex_soup)
I got the following "AssertionError: Command \item invalid in math mode." error in inline math equation. Please provide the solution for the above mentioned issue.
It would be helpful if the error assertion referred to the line in the .tex file.
up
how to solve it?
Hi,
I ran into the same issue and can't figure out why it is happening. Any updates on a solution?
@TobiBu Could you post an minimal example tex source code and the error you get? We might be able to suggest workarounds.
Also, please try using the latest source code installed directly from github (pip install "git+https://github.com/alvinwan/TexSoup")? (it has some fixes that have not been released)
Hi,
thanks for the quick response. I have created a google collar notebook with the tex source that created the error. you can find the colab here
btw the latest source code does not fix this issue.
thanks for helping out!
Thx for the colab notebook.
I played with it a bit, and I managed to make a minimal reproducible code snippet where the error occurs:
texsrc = r"""
\documentclass[useAMS,usenatbib]{mnras}
\newcommand{\beq}{\begin{equation}}
\newcommand{\eeq}{\end{equation}}
\begin{document}
\begin{itemize}
\item something
\end{itemize}
\end{document}
"""
soup = TexSoup(texsrc)
RAISES: AssertionError: Command \item invalid in math mode.
In this situation, the shorthand macros \beq and \eeq screws up the parser for some reason, but I don't know why. I'm guessing the \begin{equation} in the macro definition puts us in math mode, and we never exit.
If you need to work on one manuscript specifically, I would recommend you remove the \beq and \eeq macro dfinitions would allow you to move forward... I just tried this deleting lines 58-61 in the file 1804.04667/main.tex and TexSoup was able to parse it
Thanks for figuring this out.In fact I will need to parse a couple manuscripts and not just this one. So having a better fix than deleting lines in the manuscript would help a lot.CheersTobiasAm 05.09.2023 um 00:50 schrieb Ivan Savov @.***>: Thx for the colab notebook. I played with it a bit, and I managed to make a minimal reproducible code snippet where the error occurs: texsrc = r""" \documentclass[useAMS,usenatbib]{mnras} \newcommand{\beq}{\begin{equation}} \newcommand{\eeq}{\end{equation}}
\begin{document} \begin{itemize} \item something \end{itemize}
\end{document} """ soup = TexSoup(texsrc)
RAISES: AssertionError: Command \item invalid in math mode. In this situation, the shorthand macros \beq and \eeq screws up the parser for some reason, but I don't know why. I'm guessing the \begin{equation} in the macro definition puts us in math mode, and we never exit. If you need to work on one manuscript specifically, I would recommend you remove the \beq and \eeq macro dfinitions would allow you to move forward... I just tried this deleting lines 58-61 in the file 1804.04667/main.tex and TexSoup was able to parse it
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: @.***>
My guess is that the parser doesn't account for the preamble, i.e., doesn't see \begin{document} and \end{document}.
As a workaround (note that in LaTeX you can redefine anything so this will only work for a limited subset of cases), you can use the following:
import re
from TexSoup import TexSoup
text = ... # your TeX content
pattern = re.compile(r"\\begin{document}(.*?)\\end{document}", re.DOTALL)
result = pattern.search(text)
if result:
soup = TexSoup(result.group(1))
else:
soup = TexSoup(text)
What this does is that it uses a regex to find all of the content between \begin{document} and \end{document}, then passes that to TexSoup, thereby skipping the whole preamble.
Some pathological cases (not a complete list):
- the
verbatimenv doesn't care about macros, so if there's, for whatever reason, a\begin{document}or\end{document}in one of those, the above fails. A possible workaround is to use(.*)instead of(.*?)because then only the last\end{document}will be matched (not tested) - character code changing
- if
\begin{document}or\end{document}are in a comment somewhere - if the author decided to define new macros after
\begin{document}