Sage is a cool and free open source mathematics software system that strives to be an alternative to Magma, Maple, Mathematica, and Matlab. SageTex is a Latex package that can make life easier or more interesting for those of us who use Latex for writing technical documents and occasionally need to include plots, numerical calculations, or symbolic derivations. I came across this in a tweet and decided to give it a go. I have two machines at home: a Linux laptop and a Windows one. Making it work on the Linux machine is very straightforward, doing the same on the Windows machine proved otherwise. Here I summarise the steps I needed to take so that it might be helpful for other people.

First, one needs to install Sage by following these steps. It is straightforward enough. Let’s assume that the installation folder is

C:\SageMath 9.0

Second, you need to grab sagetex.sty from this folder

C:\SageMath 9.0\runtime\opt\sagemath-9.0\local\share\texmf\tex\latex\sagetex

and copy it somewhere that your Tex typesetting system is aware of. You are better off using the sagetex.sty that comes with a given version of Sage. See this page for more information. This will possibly require some mocking about with the package trees.

Third, start your favourite latex editor and put together a minimal example like

\documentclass[11pt, oneside]{article} 
\usepackage{amssymb,amsthm,amsmath}
\usepackage{graphicx,xcolor}
\usepackage{sagetex}

\title{A Minimal Example with SageTex}
\author{Marcus Aurelius}

\begin{document}
\maketitle

%A sage block that will not appear in the typeset file...
\begin{sagesilent}
  f(x) =  x^4 + sin(x)
\end{sagesilent}
Let $$f(x) = \sage{f(x)}.$$ 
The function $f(x)$ for $x\in[-1,1]$ is depicted in Figure~\ref{fig:example}.
\begin{figure}
  \centering
  \sageplot[width=.7\textwidth]{plot(f,-1,1)}
  \caption{A plot of $f(x)$.}
  \label{fig:example}
\end{figure}
\end{document}

Let’s assume this file is called example.tex and lives in C:\Example. Ordinarily on a Linux or Mac machine you get your typeset file by running the following commands.

pdflatex example.tex
sage example.sagetex.sage
pdflatex example.tex

In windows you need to do a bit more. While in

C:\Example

you need to first run

pdflatex example.tex

Then, from

C:\SageMath 9.0\runtime\bin

you need to run (with no line breaks)

bash -l "C:/SageMath 9.0/runtime/opt/sagemath-9.0/sage" -c "os.chdir(r'C:\Example'); load('example.sagetex.sage')"

And finally, from C:\Example, you need to re-run

pdflatex example.tex

Now you can compile Latex with a touch of Sage in it. Enjoy (or just stick to Linux.)