Skip to main content

Section 3.17 Sage

Sage is an open source library of computational routines for symbolic, exact and numerical mathematics. It is designed to be a “viable free open source alternative to Magma, Maple, Mathematica, and Matlab.” PreTeXt contains extensive support for including example Sage into your document.

A typical use of the <sage> tag is to include an <input> element, followed by an <output> element. The content of the <input> element may be presented statically in PDF output, or dynamically as a Sage Cell in an output format based on HTML. Of course, for output as a CoCalc or Jupyter worksheet, the Sage code is presented in the worksheet's native format.

The content of the <output> element is included in PDF output, but not in dynamic instances, since it can be re-computed. Notably, there is a conversion which pairs input and output into a single file in the format used by Sage's doctest framework. So if expected output is provided, it becomes automatic to identify when Sage has diverged from your expectations, and you can adjust your examples accordingly.

The Sage Cell Server can also be configured to interpret different languages, because Sage by default contains everything needed to evaluate code in these languages. This is done by providing a @language attribute, where possible values are sage, gap, gp, html, maxima, octave, python, r, and singular. The default is sage.

Note that the dynamic formats (including the Sage Cell) may run Sage “interacts,” so that is possible to embed interactive demonstrations into your dynamic output formats.