Skip to main content

The PreTeXt Guide

Section 4.21 Sage

Until we can expand this section, get some ideas from Section 3.17. We will also collect a few items here, to be cleaned-up later.
For online output formats, sometimes the output of a Sage command can be overwhelming, and a bit complicated to parse. Many objects in Sage also have a representation, which can be used to create a superior output format (for some purposes). Begin a cell with the “magic”:
%display latex
Experiment with the following Sage code on the next line
integral(x^9*cos(x), x)
Boom! Very nice. Try replacing latex with None, plain, ascii_art, or unicode_art.

Subsection 4.21.1 Sage Cell Server Design

The ability to execute, and edit, chunks of Sage code is provided by a distinct project, the Sage Cell Server
 1 
sagecell.sagemath.org/
. Simplifying somewhat, the Sage code a reader sees (or has edited!) is shipped out to a running instance of Sage (on a server somewhere) and the code is executed there. The results of that computation are shipped back to the reader for display below the code.
Two implications of this design are
  • It is not within your power to add additional packages for the supported languages.
  • You cannot read a (data) file hosted on your project’s site.
Fortunately, there are workarounds.
If your code needs a Python package, or an R package, or similar, and it is a standard open source package, then make a request on the Sage Cell
 2 
groups.google.com/g/sage-cell
Google Group. Likely, it can be added/installed.
Unfortunately, the ability to read files anywhere on the internet was abused, so this capability had to be restricted to a finite list of servers. These include DropBox
 3 
www.dropbox.com
and GitHub
 4 
github.com
where you might find it convenient to place files supporting your code. Note that for GitHub, you likely want to use a URL which is a “raw” file such as for the PreTeXt repository README
 5 
github.com/PreTeXtBook/pretext/raw/master/README.md
file, written with Markdown.