# How to use sagetex package

I'm trying to use sagetex package, but then I found this kind of problem when running the code, may anyone help?

Here is the code:

\documentclass{article}
\usepackage{sagetex}

\begin{document}
Using Sage\TeX, one can use Sage to 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 some Sage code:
\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)}
\sageplot[scale=.5]{plot3d(sin(pi*(x^2+y^2))/2,(x,-1,1),(y,-1,1))}
we know that 2010 factors to $\sage{factor(2010)}$
\begin{sagesilent}
m=identity_matrix(QQ,3)
m[0]=m[0]+m[1]
m[1]=m[1]-m[2]
m[2]=m[2]-2*m[1]
m[1]=m[1]+3*m[0]
m[0]=2*m[0]
\end{sagesilent}
Compute the rref of $\sage{m}$
\begin{sageblock}
g(x)=taylor(tan(x),x,0,10)
\end{sageblock}
$$\tan(x)=\sage{g(x)}$$
\end{document}

• Welcome to TEX.SE, could you show us your MWE through which we could reproduce your problem? – Raaja is not active on TEXSE Aug 5 '19 at 3:51
• done, I've edited my post... – Mohammad Agung Aug 5 '19 at 4:18
• You need to install sagetex first on your computer: doc.sagemath.org/html/en/tutorial/sagetex.html – AndréC Aug 6 '19 at 11:45
• There really isn't enough information here to answer the question. I suspect you haven't downloaded SAGE. See my post below for some possible answers. – DJP Aug 7 '19 at 12:33
• If none of that is helpful, you'll need to give us more information about what the log file from LaTeX is complaining about. – DJP Aug 7 '19 at 12:38

Running

pdflatex myfile.tex


produces an auxiliary file that contains all the computations Sage needs to do:

myfile.sagetex.sage


Running Sage on this file:

sage myfile.sagetex.sage


computes the results of all these computations, so that the next time you run

pdflatex myfile.tex


the results of the computations are included and appear in the resulting pdf.

In order to use the sagetex package you need LaTeX, SAGE, sagetex. After that the 3 pieces need to communicate with each other. The easiest solution is to open a free Cocalc account, create a LaTeX document, copy/paste your code above into it, save the document, and press build. You're done in a couple of minutes! Super simple and you can do your work anywhere around the world where you have an internet connection.

If you don't want to use Cocalc then there is a lot more things that can go wrong. Assuming you're using a LaTeX distribution where sagetex is included the most likely answer is you haven't downloaded SAGE. That's been the case with a lot of people I know who assumed that since they have sagetex they're ready to use it. The problem is SAGE requires about 5 GB of space so it's not part of your LaTeX distribution. You'll need to go to the Sagemath site and click on the 'Download' tab where you'll select either the Mac/Windows/Linux version. Note that many Linux distros have SAGE in their repositories in which case you should download from there. Once you have LaTeX, SAGE, and sagetex on your computer then processing a document is a 3 step process mentioned on the page where your code comes from: first process the .tex file, then process the .sage file, then process the .tex file. This is explained in Samuel Lelièvre's answer and Note that processing the .sage file requires the command line as shown in my answer-with-pictures here. The 3 step process can be automated with some LaTeX IDEs. For example, here Kile is discussed and here, TeXstudio for Windows. The result is pressing one button will perform the 3 steps which saves a considerable amount of time.

If you're still unable to get the 3 step process to work, then it's probably a matter of LaTeX and SAGE not communicating properly. This is discussed at the link mentioned earlier and can be difficult to figure out.

It's worth mentioning that the way you've made sagetex known to SAGE, discussed here can result in problems later where it says, "There are a couple small problems with this, however: the first is that you will end up with many unnecessary copies of sagetex.sty scattered around your computer. The second and more serious problem is that if you upgrade Sage and get a new version of SageTeX, the Python code and LaTeX code for SageTeX may no longer match, causing errors.". Using Cocalc means that someone else makes sure that all the versions of LaTeX and sagetex are working together properly.

• thank you so much, I missed the second step in the process, which is compiling using sage... – Mohammad Agung Aug 9 '19 at 2:21

In addition to the other answers here (about the basics of how to use sagetex), let me add this: there is an error in the file. As I said in an answer to https://stackoverflow.com/questions/57369317/why-does-the-result-of-my-sage-command-becomes-question-mark:

When I try to compile this, I get:

**** Error in Sage code on line 23 of file.tex! Traceback follows.
Traceback (most recent call last):
File "file.sagetex.sage.py", line 39, in <module>
_st_.plot(_sage_const_1 , format='notprovided', _p_=plot3d(sin(pi*(x**_sage_const_2 +y**_sage_const_2 ))/_sage_const_2 ,(x,-_sage_const_1 ,_sage_const_1 ),(y,-_sage_const_1 ,_sage_const_1 )))
NameError: name 'y' is not defined


The problem is this line:

\sageplot[scale=.5]{plot3d(sin(pi*(x^2+y^2))/2,(x,-1,1),(y,-1,1))}


and in particular, you have not defined y. (In SageMath, x is automatically defined to be a variable, but not y.) If you add this before the plot, it should work:

\begin{sagesilent}
var('y')
\end{sagesilent}

• Good catch! I didn't realize he had added code to the sample. – DJP Aug 7 '19 at 21:46