% !TEX TS-program = pdflatexmk
% !TEX encoding = UTF-8 Unicode
% The following lines are standard LaTeX preamble statements.
\documentclass[11pt, oneside]{amsart}
\usepackage{geometry}     
\geometry{letterpaper}         
\usepackage[parfill]{parskip} 
\usepackage{graphicx}
\usepackage{amssymb}
%\usepackage{epstopdf}
\title{Using Sage with latexmk}
\author{Herbert Schulz}
\usepackage{url}

% Only one command is required to use Sage within the LaTeX source:
\usepackage{sagetex}

\begin{document}
\maketitle

\section{Sage and latexmk}

This document is independent of the other material in this folder, and explains how to use Sage with TeX if you want to use latexmk.

\section{Setup}

Download and install the \texttt{SageMath} application in \url{/Applications}. It will install as \texttt{SageMath-x.x.app} where \texttt{x.x} is a version number. Remove the \texttt{-x.x} from the application name to get \texttt{Sagemath.app}. Make a symbolic link of the \texttt{sage} executable in \texttt{/usr/local/bin}:
{\small\begin{verbatim}
   cd /usr/local/bin
   sudo ln -s /Applications/SageMath.app/Contents/Resources/sage/sage .
\end{verbatim}
}
and then make sure sage is expanded by running
{\small\begin{verbatim}
   cd
   sage --version
\end{verbatim}
}
which \emph{may} generate quite a few lines of output and finally end with a line giving the version number of the \texttt{sage} you are running.

Next create a \texttt{sagetex} folder in \url{/usr/local/texlive/texmf-local/tex/latex} and make a symbolic link of \texttt{sagetex.sty} in that folder:
{\small\begin{verbatim}
   cd /usr/local/texlive/texmf-local/tex/latex
   sudo mkdir sagetex
   cd sagetex
   sudo ln -s \
     /Applications/SageMath.app/Contents/Resources/sage/local/share/texmf/tex/latex/sagetex.sty .
   sudo mktexlsr
\end{verbatim}
}
to allow \TeX\ to use the \texttt{sagetex} package.
\newpage
\section{Using Sage and Latexmk}

Inside "TeXShop/Engines/Inactive/Sage/Sage and latexmk" there is a file named "platexmkrc". Create a folder for your new document and add a copy of platexmkrc  to this folder. Create the document as usual in the folder, making sure that the top line is
\begin{verbatim}
   % !TEX TS-program = pdflatexmk
\end{verbatim}
Then typesetting will use pdflatexmk and the resource file platexmkrc, and cooperate nicely with Sage.

\section{Introduction}

This is an example of using Sage within a \TeX\ document. We can compute extended values like 

	$$32^{31} = \sage{32^31}$$
	

We can plot functions like $x \sin x$:

 \sageplot[width=5in]{plot(x * sin( 30 * x), -1, 1)}
 
 We can integrate:
 
 $$\int {{x^2 + x + 1} \over {(x - 1)^3 (x^2 + x + 2)}}\,dx$$
 $$=  \sage{integrate( (x^2 + x + 1) / ((x - 1)^3 * (x^2 + x + 2)) )}$$
 
% \newpage
 We can perform matrix calculations:
 
$$\sage{matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])^3}$$

$$AB=  \sage{Matrix([[1, 2], [3, 4]])} \sage{Matrix([[5, 6], [6, 8]])} = \sage{Matrix([[1, 2], [3, 4]]) * Matrix([[5, 6], [6, 8]])}$$

Plots are fun; here is a second one showing $x \ln x$. The ``width'' command in the source is sent to the include graphics command in LaTeX rather than to Sage.

\sageplot[width=5in]{plot(x * ln(x), 0, 2)}

Sage understands mathematical constants and writes them symbolically unless it is told to produce a numerical approximation. The term $e \pi$ below is not in the LaTeX source; instead it is the result of a Sage calculation, as is the numerical value on the other side of the equal sign.

The product of $e$ and $\pi$ is $\sage{pi * e} = \sage{N(pi * e)}.$

\end{document}

