% !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{\textsf{Sage} and \textsf{latexmk}}
\author{}
%\usepackage{url}

\usepackage{fourier}
\usepackage[scaled=0.85]{berasans}
\usepackage[scaled=0.85]{beramono}
\usepackage{microtype}

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

\usepackage{xcolor}
\usepackage[colorlinks, urlcolor=darkgray, linkcolor=darkgray]{hyperref}

\newcommand{\TS}{\textsf{\TeX Shop}}

\begin{document}
\begin{center}
\Large\bfseries\textsf{Sage} and \textsf{latexmk}
\end{center}

\section{Setup}

Go to <\url{https://github.com/3-manifolds/Sage_macOS/releases}> and download the latest version of \textsf{SageMath-x.x.dmg} (where x.x is the version number of \textsf{Sage} in the app); it was \textsf{SageMath-9.4.dmg} with version 1.2.1 of the applications at the time of this writing. Open the \textsf{.dmg} file and copy the \textsf{SageMath-x.x.app} to the \path{/Applications} folder. \emph{Don't rename it}.

Double-Click the \textsf{Recommended\_x\_x.pkg} and follow the instructions. This installs an executable \textsf{sage} script in \path{/usr/local/bin} as well as additional information for those folks using \textsf{Jupyter} (not relevant for \textsf{sagetex}).

The \textsf{sagetex} folder contains the \textsf{sagetex.sty} package file. Move that folder into the \path{~/Library/texmf/tex/latex/} (you may have to create some of this string of embedded folders). NOTE: \path{~/Library} is the \textsf{Library} folder in your \textsf{HOME} folder, \emph{not} the one at the root of your hard drive which is \path{/Library}. To open \path{~/Library} open the \textsf{Go} menu in \textsf{Finder} and hold down the \textsf{Opt} key to reveal an item to open that \textsf{Library} folder.

Finally open the \textsf{SageMath-x.x} application and follow the directions until you get a dialog box that gives you a choice of opening sage in a \textsf{Terminal} window or \textsf{Jupyter Notebook} and \textsf{Quit}. This initializes the application in \textsf{macOS}.

When the \textsf{SageMath} application is updated (e.g., to \textsf{SageMath-9.5}) simply follow the directions above and finally remove the older version.

\section{Using \textsf{SageTeX} with \TS's \textsf{latexmk} based engines}

You can use any of \TS's basic \textsf{latexmk} engines, \textsf{(pdf/xe/lua)latexmk}, with \textsf{SageTeX}. Just have the enclosed \textsf{platexmkrc} file, written by John Collins, the maintainer of \textsf{latexmk}, in the same folder as the file that gets typeset.

\section{Sample}

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

	$$32^{31} = \sage{32^31}$$
	
%\newpage
We can plot functions like $x \sin x$:

\begin{center}
 \sageplot[width=4in]{plot(x * sin( 30 * x), -1, 1)}
\end{center}

 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)), x )}$$
 
%\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.

\begin{center}
\sageplot[width=4in]{plot(x * ln(x), 0, 2)}
\end{center}

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}

