TexSoup icon indicating copy to clipboard operation
TexSoup copied to clipboard

Problem with invalid Mathmode "AssertionError: Command \item invalid in math mode."

Open mani8903 opened this issue 3 years ago • 9 comments

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.

mani8903 avatar Aug 17 '22 10:08 mani8903

It would be helpful if the error assertion referred to the line in the .tex file.

jimhefferon avatar Oct 22 '22 22:10 jimhefferon

up

Elowarp avatar Jun 24 '23 20:06 Elowarp

how to solve it?

SinclairCoder avatar Jul 31 '23 01:07 SinclairCoder

Hi,

I ran into the same issue and can't figure out why it is happening. Any updates on a solution?

TobiBu avatar Sep 04 '23 13:09 TobiBu

@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)

ivanistheone avatar Sep 04 '23 14:09 ivanistheone

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!

TobiBu avatar Sep 04 '23 20:09 TobiBu

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

ivanistheone avatar Sep 04 '23 22:09 ivanistheone

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: @.***>

TobiBu avatar Sep 05 '23 06:09 TobiBu

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):

  1. the verbatim env 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)
  2. character code changing
  3. if \begin{document} or \end{document} are in a comment somewhere
  4. if the author decided to define new macros after \begin{document}

JCGoran avatar Oct 16 '23 13:10 JCGoran