Search code examples
symbolic-mathsage

Pretty printing with Sagemath command line


How do I "pretty-print" something in the Sagemath command line (as in sympy)? The only solution I found was to use get_test_shell() function, according to this entry in the docs. But basically I have to use

from sage.repl.interpreter import get_test_shell
shell = get_test_shell()
shell.run_cell('%display ascii_art')
shell.run_cell('integral(x^2/pi^x, x)')

I find this to be too cumbersome (4 complicated lines just to display things nicely!) and what you want to display even has to be given as a string. So I'm looking for an easier alternative. I'm sure there's another way; I just can't find it.

I think I might be using the wrong terms because according to the manual itself:

Pretty printing means rendering things so that MathJax or some other latex-aware front end can render real math.

So if I use pretty_print_default(True) it's going to always print latex, which isn't what I want for now.

One thing I tried that I thought would work was to do

from sympy import pretty_print

Which should use Sympy's pretty-print (which is what I actually want), but it doesn't make any difference.


Solution

  • You ran across the problem that we want to be able to doctest things that are interactive by their nature. Try just this at the command line:

    sage: %display ascii_art
    sage: integral(x^2/pi^x, x)
    
     / 2    2                      \  -x*log(pi) 
    -\x *log (pi) + 2*x*log(pi) + 2/*e           
    ---------------------------------------------
                          3                      
                       log (pi)       
    

    This is one of the "magic" percent directives in IPython. I agree it could be documented better.