示例
这里是一个使用SageTex的很简短的例子。完整文档可以在SAGE_ROOT/local/share/texmf/tex/generic/sagetex
找到,其中SAGE_ROOT
是您Sage的安装目录。该目录包含文档、一个示例文件,在SAGE_ROOT/local/share/texmf/tex/latex/sagetex
中有一些可能有用的Python脚本。
警告:如果您在”实时”帮助中查看的话,下面的文本将有几个关于未知控制序列的错误。使用静态版本来查看正确的文本。
\documentclass{article}
\usepackage{sagetex}
\begin{document}
UsingSage\TeX, one can useSageto compute things and put them into
your \LaTeX{} document. For example, there are
$\sage{number_of_partitions(1269)}$integer partitions of$1269$.
You don't need to compute the number yourself, or even cut and paste
it from somewhere.
Here's someSagecode:
\begin{sageblock}
f(x) = exp(x) * sin(2*x)
\end{sageblock}
The second derivative of$f$is
\[
\frac{\mathrm{d}^{2}}{\mathrm{d}x^{2}} \sage{f(x)} =
\sage{diff(f, x, 2)(x)}.
\]
Here's a plot of $f$ from $-1$ to $1$:
\sageplot{plot(f, -1, 1)}
\end{document}
如通常那样在st_example.tex运行LaTex。注意LaTex将有一些警告,可能会包括:
Package sagetex Warning: Graphics file
sage-plots-for-st_example.tex/plot-0.eps on page 1 does not exist. Plot
command is on input line 25.
Package sagetex Warning: There were undefinedSageformulas and/or
plots. RunSageon st_example.sagetex.sage, and then run LaTeX on
st_example.tex again.
注意,除了LaTex生成的通常文件集,还有一个名为st_example.sage
的文件。那是您在st_example.tex
上运行LaTex时生成的Sage脚本。警告信息告诉您要在st_example.sage
上运行Sage,按照提示进行。它将会告诉您再次在st_example.tex
上运行LaTex,但是在您做之前,注意一个新文件被创建了:st_example.sagetex.sout
。那个文件保存了Sage以LaTex可以用来插入到您文本中的格式的计算结果,同时创建了一个包含EPS绘图文件的新目录。再次运行LaTex,您将看到Sage计算和绘出的所有东西都包含到了文档中。
上面使用的不同宏应该很容易理解。sageblock
环境使您的代码能逐字排版并在您运行Sage时执行代码。当您运行\sage{foo}
时,放入文档中的结果是您在Sage中运行latex(foo)
得到的内容。绘图命令有一些复杂,不过在最简单的形式下,\sageplot{foo}
将插入您执行foo.save('filename.eps')
所得到的图像。
总之,一般操作为:
- 在您的.tex文件中运行LaTex;
- 在产生的.sage文件中运行Sage;
- 再次运行LaTex。
当你没有对tex文档中的sage代码进行任何更改时,可以跳过执行Sage这一步。
关于SageTex还有很多内容。因为Sage和LaTex都是复杂、强有力的工具,阅读SageTex的文档是很好的主意,它位于SAGE_ROOT/local/share/texmf/tex/generic/sagetex
中。