The pretty_print command¶
Works similar to the print function, except that it always tries
to use a rich output for an object, as specified via the text display
preference. If such a rich output is not available, it falls back on the
plain text.
EXAMPLES:
sage: pretty_print(1, 2, 3)
1 2 3
sage: pretty_print(x^2 / (x + 1)) # needs sage.symbolic
x^2/(x + 1)
>>> from sage.all import *
>>> pretty_print(Integer(1), Integer(2), Integer(3))
1 2 3
>>> pretty_print(x**Integer(2) / (x + Integer(1))) # needs sage.symbolic
x^2/(x + 1)
EXAMPLES:
sage: %display ascii_art # not tested
sage: pretty_print(x^2 / (x + 1)) # needs sage.symbolic
2
x
-----
x + 1
>>> from sage.all import *
>>> %display ascii_art # not tested
>>> pretty_print(x**Integer(2) / (x + Integer(1))) # needs sage.symbolic
2
x
-----
x + 1
EXAMPLES:
Printing a graphics object just prints a string, whereas
pretty_print() does not print anything and just shows the
graphics instead:
sage: print(plot(sin)) # needs sage.plot sage.symbolic
Graphics object consisting of 1 graphics primitive
sage: pretty_print(plot(sin)) # needs sage.plot sage.symbolic
>>> from sage.all import *
>>> print(plot(sin)) # needs sage.plot sage.symbolic
Graphics object consisting of 1 graphics primitive
>>> pretty_print(plot(sin)) # needs sage.plot sage.symbolic
- class sage.repl.rich_output.pretty_print.SequencePrettyPrinter(*args, **kwds)[source]¶
Bases:
SageObjectPretty Printer for Multiple Arguments.
INPUT/OUTPUT:
Same as
pretty_print(), except that the number of arguments must be >= 2. Otherwise its not a sequence of things to print.EXAMPLES:
sage: pretty_print(1, 2, 3) # indirect doctest 1 2 3 sage: from sage.repl.rich_output.pretty_print import SequencePrettyPrinter sage: SequencePrettyPrinter(1, 2, 3).pretty_print() 1 2 3
>>> from sage.all import * >>> pretty_print(Integer(1), Integer(2), Integer(3)) # indirect doctest 1 2 3 >>> from sage.repl.rich_output.pretty_print import SequencePrettyPrinter >>> SequencePrettyPrinter(Integer(1), Integer(2), Integer(3)).pretty_print() 1 2 3
- is_homogeneous(common_type)[source]¶
Return whether the pretty print items are homogeneous.
INPUT:
common_type– a type
OUTPUT:
boolean; whether all items to be pretty printed are of said type.
EXAMPLES:
sage: from sage.repl.rich_output.pretty_print import SequencePrettyPrinter sage: seq = SequencePrettyPrinter(1, 2, 3) sage: seq.is_homogeneous(Integer) True sage: seq.is_homogeneous(str) False
>>> from sage.all import * >>> from sage.repl.rich_output.pretty_print import SequencePrettyPrinter >>> seq = SequencePrettyPrinter(Integer(1), Integer(2), Integer(3)) >>> seq.is_homogeneous(Integer) True >>> seq.is_homogeneous(str) False
- pretty_print()[source]¶
Actually do the pretty print.
EXAMPLES:
sage: from sage.repl.rich_output.pretty_print import SequencePrettyPrinter sage: SequencePrettyPrinter(1, 2, 3).pretty_print() 1 2 3
>>> from sage.all import * >>> from sage.repl.rich_output.pretty_print import SequencePrettyPrinter >>> SequencePrettyPrinter(Integer(1), Integer(2), Integer(3)).pretty_print() 1 2 3
The keyword arguments are only used the first time graphics output is generated:
sage: seq = SequencePrettyPrinter(Graph(), Graph(), edge_labels=True) # needs sage.graphs sage.plot sage: seq.pretty_print() # does not pass edge_labels to graphics object # needs sage.graphs sage.plot sage: seq._concatenate_graphs().show(edge_labels=True) # needs sage.graphs sage.plot Traceback (most recent call last): ... TypeError: ...matplotlib() got an unexpected keyword argument 'edge_labels'...
>>> from sage.all import * >>> seq = SequencePrettyPrinter(Graph(), Graph(), edge_labels=True) # needs sage.graphs sage.plot >>> seq.pretty_print() # does not pass edge_labels to graphics object # needs sage.graphs sage.plot >>> seq._concatenate_graphs().show(edge_labels=True) # needs sage.graphs sage.plot Traceback (most recent call last): ... TypeError: ...matplotlib() got an unexpected keyword argument 'edge_labels'...
- sage.repl.rich_output.pretty_print.pretty_print(*args, **kwds)[source]¶
Pretty print the arguments using rich output if available.
This function is similar to
print(), except that a rich output representation such asascii_artor Latex is printed instead of the string representation.Note that the output depends on the global display preferences specified via
preferences(). If the display preference fortextis not specified, Latex output is preferred.For graphical objects, a graphical output is used.
For certain homogeneous multiple positional arguments, a suitable combined graphical output is generated. In particular, graphs and plots are treated special. Otherwise this function concatenates the textual representations.
INPUT:
*args– any number of positional arguments; the objects to pretty print**kwds– optional keyword arguments that are passed to the rich representation. Examples include:dpi– dots per inchfigsize- [width, height] (same for square aspect)axes– (default:True)fontsize– positive integerframe– boolean (default:False); draw a MATLAB-like frame around the image
EXAMPLES:
sage: pretty_print(ZZ) Integer Ring sage: pretty_print("Integers = ", ZZ) # trac 11775 'Integers = ' Integer Ring
>>> from sage.all import * >>> pretty_print(ZZ) Integer Ring >>> pretty_print("Integers = ", ZZ) # trac 11775 'Integers = ' Integer Ring
To typeset LaTeX code as-is, use
LatexExpr:sage: pretty_print(LatexExpr(r"\frac{x^2 + 1}{x - 2}")) \frac{x^2 + 1}{x - 2}
>>> from sage.all import * >>> pretty_print(LatexExpr(r"\frac{x^2 + 1}{x - 2}")) \frac{x^2 + 1}{x - 2}
For text-based backends, the default text display preference is to output plain text which is usually the same as using
print():sage: pretty_print(x^2 / (x + 1)) # needs sage.symbolic x^2/(x + 1) sage: t = BinaryTrees(3).first() # needs sage.graphs sage: pretty_print(t) # needs sage.graphs [., [., [., .]]] sage: print(t) # needs sage.graphs [., [., [., .]]]
>>> from sage.all import * >>> pretty_print(x**Integer(2) / (x + Integer(1))) # needs sage.symbolic x^2/(x + 1) >>> t = BinaryTrees(Integer(3)).first() # needs sage.graphs >>> pretty_print(t) # needs sage.graphs [., [., [., .]]] >>> print(t) # needs sage.graphs [., [., [., .]]]
EXAMPLES:
Changing the text display preference affects the output of this function. The following illustrates a possible use-case:
sage: %display ascii_art # not tested sage: for t in BinaryTrees(3)[:3]: # needs sage.graphs ....: pretty_print(t) o \ o \ o o \ o / o o / \ o o sage: pretty_print(x^2 / (x + 1)) # needs sage.symbolic 2 x ----- x + 1
>>> from sage.all import * >>> %display ascii_art # not tested >>> for t in BinaryTrees(Integer(3))[:Integer(3)]: # needs sage.graphs ... pretty_print(t) o \ o \ o o \ o / o o / \ o o >>> pretty_print(x**Integer(2) / (x + Integer(1))) # needs sage.symbolic 2 x ----- x + 1
- sage.repl.rich_output.pretty_print.show(*args, **kwds)[source]¶
Alias for
pretty_print.This function is an alias for
pretty_print().INPUT/OUTPUT:
See
pretty_print(). Except if the argument is a graph, in which case it is plotted instead.EXAMPLES:
sage: show(1) 1
>>> from sage.all import * >>> show(Integer(1)) 1