The sagetex documentation (S. 5) says
Also keep in mind that everything you send to Sage is done within one Sage session. This
means you can define variables and reuse them throughout your LaTeX document; if you tell
Sage that `foo` is 12, then anytime afterwards you can use `foo` in your Sage code and Sage will
remember that it's 12, just like in a regular Sage session
When I do
a = 12
a-2
in sage 6.3 it displays 12. A document with
\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{sagetex}
\begin{document}
\sage{12-2}
\end{document}
creates a PDF with 10 as content (when compiled with pdflatex; sage filename.sagetex.sage && pdflatex), but a document named sagetex_variable_reuse
\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{sagetex}
\begin{document}
\sage{a = 12}
\sage{a-2}
\end{document}
Fails with
$ sage sagetex_variable_reuse.sagetex.sage
Processing Sage code for sagetex_variable_reuse.tex...
**** Error in Sage code on line 6 of sagetex_variable_reuse.tex! Traceback follows.
Traceback (most recent call last):
File "sagetex_variable_reuse.sagetex.py", line 8, in <module>
_st_.inline(_sage_const_0 , latex(a = _sage_const_12 ))
TypeError: __call__() got an unexpected keyword argument 'a'
**** Running Sage on sagetex_variable_reuse.sage failed! Fix sagetex_variable_reuse.tex and try again.
I can now imagine another interpretation of what I cited, so I'd like to get it here or a confirmation that this is a bug. How to save a numeric value after calculation (logical) or assignment in one \sage statement and reuse it in another?

\sagemacro cannot handle a variable assignment because, in Python, that's a statement, but\sagecan only handle an expression. – Dan Drake Aug 24 '15 at 03:02sagesilentenvironment before working with \sage{a}. – DJP Aug 24 '15 at 23:06\myvaluewith value of3defined beforesagesilent. When I write insidesagesilentenvironmentf(x) = \myvalue * x, I get an error of undefined namemyvalue. So, how to fix this by makingsagetexrecognize all the macros known in the tex memory? Sorry for asking here! – Diaa Mar 16 '22 at 17:25sagetexrecognizes. The documentation, halfway down on page 6, gives an example. I've managed to work with combining them by not usingf(x)=\myvalue*xbut instead using\myvalue*xas needed. So: I think you can use any macro insagetex. – DJP Mar 16 '22 at 21:40