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
if n % 2:. Percent by default begins a comment, so if it hasn't been changed, everything in that line afternwill be ignored. Please check. – barbara beeton Jul 14 '23 at 21:11sagesilentblock. 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