Skip to main content
Logo image

Discrete Math with SageMath: Learn math with open-source software

Section 1.6 Display Values

Sage provides multiple ways to display values on the screen. The simplest way is to type the value into a cell, and Sage will display it. Sage also offers functions to format and display output in different styles.
Sage automatically displays the value of the last line in a cell unless a specific function is used for output. Here are some key functions for displaying values:
  • print() displays the value of the expression inside the parentheses as plain text.
  • pretty_print() displays rich text as typeset output.
  • show() is an alias for pretty_print() and provides additional functionality for graphics.
  • latex() returns the raw code for the given expression, which then can be used in documents.
  • %display latex enables automatic rendering of all output in format.
  • While Python string formatting is available and can be used, it may not reliably render rich text or expressions due to compatibility issues.
Let’s explore these display methods in action.
Typing a string directly into a Sage cell displays it with quotes.
Using print() removes the quotes.
The show() function formats mathematical expressions for better readability.
To display multiple values in a single cell, use show() for each one.
The latex() function returns the raw code for an expression.
In Jupyter notebooks or SageMathCell, you can set the display mode to using %display latex.
Once set, all expressions onward will continue to be rendered in format until the display mode is changed.
To return to the default output format, use %display plain.