sagemath / sagetex

Embed code, results of computations, and plots from the Sage mathematics software suite (https://www.sagemath.org) into LaTeX documents. Source repository for https://pypi.org/project/sagetex/ and https://ctan.org/pkg/sagetex
https://ctan.org/pkg/sagetex
Other
58 stars 22 forks source link

run-sagetex-if-necessary.py fails with python 3 #44

Closed done-with-fish closed 4 years ago

done-with-fish commented 4 years ago

I recently upgraded to SageMath version 9.0 which used Python 3.8.1. I'm running arch linux and have checked that my version of the run-sagetex-if-necessary.py script is exactly the same as the one here.

The run-sagetex-if-necessary.py script fails when running with python 3.

Here is a mwe where I cat a simple tex document and attempt to run the script:

$ ls # I have already run pdflatex successfully on foo.tex
foo.aux foo.log foo.pdf foo.sagetex.sage foo.tex run-sagetex-if-necessary.py

$ cat foo.tex 
\documentclass{article}

\usepackage{sagetex}

\begin{document}
$\sage{1+1}$
\end{document}

$ python2 run-sagetex-if-necessary.py foo # running the script with python2 works as expected
Need to run Sage on foo.
Processing Sagecode for foo.tex...
Inline formula 0 (line 6)
Sage processing complete. Run LaTeX on foo.tex again.

$ python3 run-sagetex-if-necessary.py foo # running the script with python3 fails
Traceback (most recent call last):
  File "run-sagetex-if-necessary.py", line 56, in <module>
    if re.search(usepackage, line.replace(r'\%', '').split('%')[0]):
  File "/usr/lib/python3.8/re.py", line 199, in search
    return _compile(pattern, flags).search(string)
  File "/usr/lib/python3.8/re.py", line 302, in _compile
    p = sre_compile.compile(pattern, flags)
  File "/usr/lib/python3.8/sre_compile.py", line 764, in compile
    p = sre_parse.parse(p, flags)
  File "/usr/lib/python3.8/sre_parse.py", line 948, in parse
    p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
  File "/usr/lib/python3.8/sre_parse.py", line 443, in _parse_sub
    itemsappend(_parse(source, state, verbose, nested + 1,
  File "/usr/lib/python3.8/sre_parse.py", line 525, in _parse
    code = _escape(source, this, state)
  File "/usr/lib/python3.8/sre_parse.py", line 375, in _escape
    raise source.error("incomplete escape %s" % escape, len(escape))
re.error: incomplete escape \u at position 0
kcrisman commented 4 years ago

I think this boils down to the following Py3 behavior.

$ /Applications/SageMath-9.0.app/Contents/Resources/sage/sage -python
Python 3.7.3 (default, Jan  2 2020, 12:09:18) 
[Clang 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> Y = '\usepackage{sagetex}'
  File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \uXXXX escape
>>> Z = r'\usepackage{sagetex}'
>>> Z.replace(r'\%', '').split('%')[0]
'\\usepackage{sagetex}'

At first I thought we might have to read things in as raw strings here:

with open(src + '.tex') as texf:
    for line in texf:
        if re.search(usepackage, line.replace(r'\%', '').split('%')[0]):
            uses_sagetex = True
            break

but maybe we have to do something better for the following line - maybe removing the raw string, weird as that sounds?

usepackage = r'\usepackage(\[.*\])?{sagetex}'

See this Stackoverflow question for what seems to be the same issue.

@dimpase what do you think? @fchapoton knows an awful lot about Py3 migration so I suspect he will know as well.

dimpase commented 4 years ago

All I can say atm is that seems to be only place where this breaks (finding whether there is \usepackage{sagetex} in the file) - otherwise at least this example works.

kcrisman commented 4 years ago

Yes, I believe that is correct. @done-with-fish can you try changing the script to have

usepackage = '\usepackage(\[.*\])?{sagetex}'

or possibly

if re.search(usepackage, line.replace('\%', '').split('%')[0]):

just to see if those might possibly fix it, as indicated in the linked question?

fchapoton commented 4 years ago

Please test the fix proposal at #45

dimpase commented 4 years ago

fixed by #45