How to use Sage with Latex (Sagetex) ?

Firstly install Sagetex. For this open terminal and give the following commands.


sage -i sagetex-2.0.1
sudo apt-get install texlive-full
vi priya.tex

Here sage -i sagetex-version, where version can be any latest version.

Type in the following code:

 \title{Analysis and Design by SAGE in \LaTeX}
 \author{Priyanka Kapoor}

 A(l,b) = l*b
 l = 2
 b= 4
 area = A(l,b)

Using formula
 A = \sage{latex(A(l,b))}
 for area $A = \sage{area.n(digits=2)}$

Press Escape then :wq (Save and quit from vi editor)

Now write a script for running sage with latex. Here are its contents:

 latex priya.tex
 sage priya.sagetex.sage
 pdflatex priya.tex
 evince priya.pdf

Press Escape then type :wq

Now run the script by the following command.


All commands will be executed and you will get a pdf file showing the result of program.


If evince file_name don’t work then try acroread file_name

Problem Faced By Me:

Usually when you upgarde your sage version then there is possibility of one basic problem that:

sagetex.sty file of sage and sagetex.sty file of texlive are of different versions. So whenn we run command latex filename.tex, it compile and give filename.sagetex.sage . Now to compile this, we do sage filename.sagetex.sage. Here it gives error that

filename.sagetex.sage was generated by sagetex.sty version “None”, but
is being processed by version “2012/01/16 v2.3.3-69dcb0eb93de”.
Please make sure that TeX is using the sagetex.sty
from your current version of Sage.

To solve this:

1. Firstly remove sagetex.sty file of Tex from /usr/share/texmf-texlive/tex/latex/sagetex.

to make TeX aware of sagetex.sty is to copy that file into a convenient place in your home directory. In most TeX distributions, the texmfdirectory in your home directory is automatically searched for packages. To find out exactly what this directory is, do the following on the command line:

kpsewhich -var-value=TEXMFHOME

which will print out a directory, such as /home/priyanka/texmf or /Users/drake/Library/texmf. Copy the tex/ directory from SAGE_ROOT/local/share/texmf/ into your home texmf directory with a command like (SAGE_ROOT=/home/priyanka/sage)

cp -R SAGE_ROOT/local/share/texmf/tex TEXMFHOME

where SAGE_ROOT is, as usual, replaced with the location of your Sage installation and TEXMFHOME is the result of the kpsewhich command above.

Check home folder, is there any texmf folder? If not , then TEXMFHOME will surely there. Rename it to texmf and now sagetex is known to tex.

If you upgrade Sage and discover that SageTeX no longer works, you can simply repeat these steps and the Sage and TeX parts of SageTeX will again be synchronized.

For more reference:!topic/sage-support/0_ySEucndFU

About Priyanka Kapoor

Simple, Hardworking & friendly.....
This entry was posted in Uncategorized. Bookmark the permalink.

One Response to How to use Sage with Latex (Sagetex) ?

  1. Anirban Mandal says:

    A very informative tutorial indeed.

    After a sage upgrade, and having followed your instructions I still am having a certain problem.
    I have a simple tex file “Test.tex” with a plot function.
    If I try ………
    geralt@localhost:~/Documents$ sage Test.sagetex.sage
    Processing Sage code for Test.tex…
    Inline formula 0
    Code block begin…end
    Inline formula 1
    Inline formula 2
    Inline formula 3
    Inline formula 4
    Initializing plots directory

    **** Error in Sage code on line 39 of Test.tex! Traceback follows.
    Traceback (most recent call last):
    File “”, line 34, in
    _st_.plot(_sage_const_0 , format=’notprovided’, _p_=plot(sin(x),-_sage_const_0p5 , _sage_const_0p5 ))
    File “/usr/lib/sagemath/local/lib/python2.7/site-packages/”, line 237, in plot
    File “/usr/lib/sagemath/local/lib/python2.7/site-packages/”, line 98, in initplot
    File “/usr/lib/sagemath/local/lib/python/”, line 250, in rmtree
    onerror(os.remove, fullname, sys.exc_info())
    File “/usr/lib/sagemath/local/lib/python/”, line 248, in rmtree
    OSError: [Errno 13] Permission denied

    **** Running Sage on Test.sage failed! Fix Test.tex and try again.

    But with sudo………
    geralt@localhost:~/Documents$ sudo sage Test.sagetex.sage
    [sudo] password for geralt:
    Processing Sage code for Test.tex…
    Inline formula 0
    Code block begin…end
    Inline formula 1
    Inline formula 2
    Inline formula 3
    Inline formula 4
    Initializing plots directory
    Plot 0
    Sage processing complete. Run LaTeX on Test.tex again.

    I guess I have to change permissions somewhere, but I can’t figure out anything else.
    I would be highly obliged if you can suggest a fix.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s