示例

这里是一个使用SageTex的很简短的例子。完整文档可以在SAGE_ROOT/local/share/texmf/tex/generic/sagetex找到,其中SAGE_ROOT是您Sage的安装目录。该目录包含文档、一个示例文件,在SAGE_ROOT/local/share/texmf/tex/latex/sagetex中有一些可能有用的Python脚本。

警告:如果您在”实时”帮助中查看的话,下面的文本将有几个关于未知控制序列的错误。使用静态版本来查看正确的文本。

  1. \documentclass{article}
  2. \usepackage{sagetex}
  3. \begin{document}
  4. UsingSage\TeX, one can useSageto compute things and put them into
  5. your \LaTeX{} document. For example, there are
  6. $\sage{number_of_partitions(1269)}$integer partitions of$1269$.
  7. You don't need to compute the number yourself, or even cut and paste
  8. it from somewhere.
  9. Here's someSagecode:
  10. \begin{sageblock}
  11. f(x) = exp(x) * sin(2*x)
  12. \end{sageblock}
  13. The second derivative of$f$is
  14. \[
  15. \frac{\mathrm{d}^{2}}{\mathrm{d}x^{2}} \sage{f(x)} =
  16. \sage{diff(f, x, 2)(x)}.
  17. \]
  18. Here's a plot of $f$ from $-1$ to $1$:
  19. \sageplot{plot(f, -1, 1)}
  20. \end{document}

如通常那样在st_example.tex运行LaTex。注意LaTex将有一些警告,可能会包括:

  1. Package sagetex Warning: Graphics file
  2. sage-plots-for-st_example.tex/plot-0.eps on page 1 does not exist. Plot
  3. command is on input line 25.
  4. Package sagetex Warning: There were undefinedSageformulas and/or
  5. plots. RunSageon st_example.sagetex.sage, and then run LaTeX on
  6. 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中。