如何在 sagetex 中的变量中存储值?

如何在 sagetex 中的变量中存储值?

sagetex文档(第 5 条)说

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

当我做

a = 12
a-2

sage6.3 中它显示 12. 一份包含

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{sagetex}

\begin{document}
\sage{12-2}
\end{document}

创建内容为的 PDF 10(使用 进行编译时pdflatex; sage filename.sagetex.sage && pdflatex),但文档名为sagetex_variable_reuse

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{sagetex}

\begin{document}
\sage{a = 12}
\sage{a-2}
\end{document}

失败

$ 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.

我现在可以想象我所引用内容的另一种解释,所以我想在这里得到它或确认这是一个错误。如何在一个\sage语句中计算(逻辑)或赋值后保存数值并在另一个语句中重复使用它?


答案1

您的文档有两个问题。该sagetex软件包允许您即时进行数值计算(例如 \sage{3*5}),但不允许进行变量赋值(例如 \sage{a=12})。如果您进一步阅读文档,它说:“如果您已将 Sage 变量 foo 定义为 12(例如,使用 sageblock 环境)...”。通常,赋值应在 sagesilent 环境中进行,用户看不到。第二个问题是您的答案是数字,需要放在文档中的美元符号之间。下面是一个示例来说明其中一些问题:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{sagetex}
\begin{document}
\begin{sagesilent}
a = 12
MyString = r"This is interesting"
\end{sagesilent}
We can do numerical calculations with Sage, such as 
${5 \choose 2}=\sage{binomial(6,3)}$  whenever we want but 
whenever we are assigning values to variables it should be done 
in the sagesilent environment if you don't want people to see the
work and sageblock if you want them to see the work. After
that, you can do your calculations.
The value of $\sage{a}-2=\sage{a-2}$.
With the variables 
\begin{sageblock}
a = 15
b = 2
\end{sageblock}
The value of $a-b=\sage{a-b}$.\\
The value of $\sage{a}-\sage{b}=\sage{a-b}$.
This is text output:
\sagestr{MyString}
\end{document}

输出:在此处输入图片描述

您可以看到 sageblock 如何将您的工作放入文档中,而 sagesilent 却没有。请注意,将字符串插入文档不涉及数学模式,因此不使用美元符号。

答案2

在源代码中sagetex.py是评估命令的函数\sage

def inline(self, counter, s):
  self.progress('Inline formula %s' % counter)
  self.souttmp.write('\\newlabel{@sageinline' + str(counter) + '}{{' + \
               latex(s).rstrip() + '}{}{}{}{}}\n')

这假设一个可以被评估为字符串的参数,而不是赋值。s是命令的文字参数\sage(文字,而不是用字符串引用)。

拥有一个命令分配将涉及一个功能类似于sagesilent环境的新命令。我没有安装 Sage,但类似这样的命令应该可以工作。您可以在加载后将其添加到序言中sagetex

\makeatletter
\newcommand{\sageslnt}[1]{\ST@wsf{%
try:^^J
 _st_.progress('Inline assignment')^^J
 #1^^Jexcept:^^J
 _st_.goboom(\the\inputlineno)}}
\makeatother

也许可以创建一个命令来处理这两种情况,但这可能很棘手。

相关内容