[Tex/LaTex] Having trouble getting SageTeX working


According to the Make SageTeX known to TeX, the preferred way to let TeX know about SageTeX is to copy the tex folder found at SAGE_ROOT/local/share/texmf/tex (which in my OS X case is /Applications/Sage-6.4.1.app/Contents/Resources/sage/local/share/texmf/tex) into the value returned by running kpsewhich -var-value=TEXMFHOME in the shell, which in my case is /Users/chuck/Library/texmf.

I've done that, and now sagetex.sty (and many other files) are found at /Applications/Sage-6.4.1.app/Contents/Resources/sage/local/share/texmf/tex/generic/sagetex.

So I open the example.tex file (using TeXShop) also found in that generic folder and click the Typeset button. The PDF is generated and opened in TeXShop, but everywhere that should have the results of Sage operations instead has double question marks.

The console that appeared when I clicked Typeset has numerous errors, all of the same type:

LaTeX Warning: Reference `@sageinline0' on page 1 undefined on input line 40.
LaTeX Warning: Reference `@sageinline1' on page 1 undefined on input line 41.

Lines 40 and 41 look like this:

This is an example $2+2=\sage{2+2}$. If you raise the current year mod
$100$ (which equals $\sage{mod(\the\year, 100)}$) to the power of the

The PDF output at that point has the following text:

This is an example 2 + 2 = ??. If you raise the current year mod 100 (which equals ??) to the power of the

Other simple tests of both Sage and TeX have proven successful. For example, launching the Sage app and selecting "Terminal Session>Sage" correctly outputs 4 with input 2+2, and creating a very simple TeX document and running it using the latex command in the Terminal is also successful, so each of these seem to work separately without trouble.

What can I do to get them to work together?

Best Answer

TeXShop uses scripts it calls 'Engines' to typeset each document. It is already set up to deal with Sage, but you need to activate the SageTeX engine.

In the Finder, navigate to ~/Library/TeXshop/Engines and in that folder there will be an Inactive folder. Inside is the file sage.engine. Move this into the Engines folder, and restart TeXshop.

(You will also find some other documentation in the Sage folder, including a sample document. It also recommends a different method of accessing Sage properly without copying it to your local texmf folder.)

Once you have restarted TeXShop, in the pulldown menu next to the Typeset button, the Sage engine will appear. Select it and then compile your document. Everything should now work.

You can add the line:

% !TEX TS-program = sage

to first line of your Sage documents and the sage engine will be chosen automatically.

Related Question