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

cannot put \sage{..} into fancy headers etc. #47

Open dimpase opened 4 years ago

dimpase commented 4 years ago

As reported on sage-support

\documentclass{article}
\usepackage{sagetex}
\usepackage{fancyhdr}

\begin{document}

\begin{sagesilent}
n = 1
\end{sagesilent}

\fancyhead[R]{$\sage{n}$}
\title
{$\sage{n}$}
\maketitle
\thispagestyle{fancy}

\end{document}

breaks sagetex with a funny error:

Processing Sage code for test02.tex...
Code block (line 7) begin...end
Inline formula 0 (line 14)
Sage processing complete. Run LaTeX on test02.tex again.
Inline formula 1 (line 17)

**** Error in Sage code on line 17 of test02.tex! Traceback follows.
Traceback (most recent call last):
  File "test02.sagetex.sage.py", line 24, in <module>
    _st_.inline(_sage_const_1 , latex(n))
  File "/usr/lib/python2.7/dist-packages/sagetex.py", line 121, in inline
    '}{{%\n' + s.rstrip() + '}{}{}{}{}}\n')
ValueError: I/O operation on closed file

**** Running Sage on test02.sage failed! Fix test02.tex and try again.
Traceback (most recent call last):
  File "test02.sagetex.sage.py", line 26, in <module>
    _st_.goboom(_sage_const_17 )
  File "/usr/lib/python2.7/dist-packages/sagetex.py", line 264, in goboom
    os.remove(self.filename + '.sagetex.sout.tmp')
OSError: [Errno 2] No such file or directory: 'test02.sagetex.sout.tmp'

Basically, what happends is that sagetex closes sout.tmp too soon, while there is still something to be done. Namely, a hotfix is to replace self.souttmp.close() in line 285 of (generated) sagetex.py with

   self.souttmp.flush()
    os.fsync(self.souttmp.fileno())

This results in the example above working:

Processing Sage code for hh.tex...
Code block (line 7) begin...end
Inline formula 0 (line 14)
Sage processing complete. Run LaTeX on test02.tex again.
Inline formula 1 (line 17)

So, surely enough, "complete" is not quite complete at this point.

dimpase commented 4 years ago

The problem is, however, in sagetex LaTeX class, in the part that generates the *.sage file. For the example above one gets

_st_ = sagetex.SageTeXProcessor('hh', version='2019/11/14 v3.4', version_check=True)
_st_.current_tex_line = 7
_st_.blockbegin()
try:
 n = 1
except:
 _st_.goboom(9)
_st_.blockend()
try:
 _st_.current_tex_line = 14
 _st_.inline(0, latex(n))
except:
 _st_.goboom(14)
_st_.endofdoc()
try:
 _st_.current_tex_line = 17
 _st_.inline(1, latex(n))
except:
 _st_.goboom(17)

and so endofdoc() is issued too early.

dimpase commented 4 years ago

@dandrake - perhaps you could quickly see how to fix this?

jocelynetienne commented 3 years ago

I have the same problem when using sage in a tikzpicture. Fix suggested works distributed 2020/08/12 v3.5 version of sagetex.py if applying at both line 264 and line 285.