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

`from __future__` imports not working #39

Closed dsejas closed 5 years ago

dsejas commented 5 years ago

Description: When using SageTeX, trying to make from __future__ imports from a sagesilent environment produces the following error message:

File "test.sagetex.sage.py", line 14 from __future__ import print_function SyntaxError: from __future__ imports must occur at the beginning of the file

Version: SageTeX v3.2, dated 2019/01/09; SageMath v8.8; TeXLive 2019. How to reproduce: The following MWE tries to make a from __future__ import print_function as soon as SageTeX allows it (after \begin{document}:

\documentclass{article}

\usepackage{sagetex}

\begin{document}
\begin{sagesilent}
    from __future__ import print_function
\end{sagesilent}
\end{document}

Possible cause: The .sty file has the following lines:

\ST@wsf{%
# -*- encoding: utf-8 -*-^^J%
# This file (\jobname.sagetex.sage) was *autogenerated* from \jobname.tex with
sagetex.sty version \ST@ver.^^J%
import sagetex^^J%
_st_ = sagetex.SageTeXProcessor('\jobname', version='\ST@ver', version_check=\ST@versioncheck)}}%

This already starts the main .sage file with some imports. This causes the first time we use the

\begin{sagesilent}
    from __future__ import print_function
\end{sagesilent}

not to be the first import, producing the error message.

Possible solution: Define an new environment "sageimports" or "sagepreamble" that adds its code just before the import sage of the lines mentioned in the possible cause section.

dimpase commented 5 years ago

IMHO everything that one needs to import from __future__ is already imported by Sage itself, no?

dsejas commented 5 years ago

Not completely true! On one hand, Sage makes some from __future__ imports internally, but, on the other hand, they are not available while using SageTeX. The user has to explicitly make their imports. As a particular case, Sage makes from __future__ import print_function in many parts of its code, but the user has only the print statement available. If they want to use the print function, the import must be manually made in the user layer.

dimpase commented 5 years ago

Thanks for clarification. Sage is switching to Python 3 --- in particular as many of its dependencies already did so or will by 2020. The upcoming 8.9 should already be useful with Python 3. I'd rather not touch specifically Python 2 things - in partiular modifyng desigm to support Python 2 better is very low on the priorites list.