0

I wrote two practically identical files that use SageTeX to determine whether a positive integer is odd or even. The first example uses tabs for indentation.

\documentclass{article}
\usepackage{sagetex}
\begin{document}
\begin{sagesilent}
def oddeven(n):
    if n % 2:
        return "ODD"
    else:
        return "EVEN"
\end{sagesilent}
$3$ is $\sagestr{oddeven(3)}$.

$16$ is $\sagestr{oddeven(16)}$. \end{document}

Output:

3 is ??.
16 is ??.

Somewhere in the logs:

File "/sagetex_with_tabs.sagetex.sage.py", line 14
    if n % _sage_const_2 :
    ^
IndentationError: expected an indented block

If I change each tab to four spaces, the code compiles without issues and works as expected.

\documentclass{article}
\usepackage{sagetex}
\begin{document}
\begin{sagesilent}
def oddeven(n):
    if n % 2:
        return "ODD"
    else:
        return "EVEN"
\end{sagesilent}
$3$ is $\sagestr{oddeven(3)}$.

$16$ is $\sagestr{oddeven(16)}$. \end{document}

Output:

3 is ODD.
16 is EVEN.

I have never had issues using tabs for indentation in both Python and LaTeX. It feels very weird that SageTeX does not recognize tabs as a valid method of indentation. How do I get it to recognize tabs?

The following Python code works just fine even though it uses tabs:

def oddeven(n):
    if n % 2:
        return "ODD"
    else:
        return "EVEN"
print("3 is {}.".format(oddeven(3)))
print("16 is {}.".format(oddeven(16)))

p.s. Both tex files were compiled in the following order: pdflatex -> sage -> pdflatex

vp2523
  • 1
  • Welcome to TeX.SE! – Mensch Jul 14 '23 at 17:35
  • When I copied/pasted your code into Cocalc, everything compiled without error. However, a search of this site gives your problem here with a proposed solution. Perhaps this is a problem in an earlier version of Sage/sagetex which has been patched in later versions? You might try checking if proposed solution works for you. If not, maybe updating your system will fix it? – DJP Jul 14 '23 at 21:09
  • I know little about Sage TeX, but I'm very suspicious of the percent sign in the line if n % 2: . Percent by default begins a comment, so if it hasn't been changed, everything in that line after n will be ignored. Please check. – barbara beeton Jul 14 '23 at 21:11
  • The % sign is in a sagesilent block. This is okay, the environment is Python there and comments require #. In Python, n % 2 is giving us n mod 2. – DJP Jul 14 '23 at 21:13

0 Answers0