'How to pretty print to a file in sympy?

Suppose I have the following code:

import sympy as sp
from sympy.physics.quantum import TensorProduct

s=sp.eye(2)
a=TensorProduct(s*x,TensorProduct(s,s)).subs(x,x**2+2*x+1)
sp.pprint(a)

The code will generate an output with a limited widths (which I hate):

enter image description here

My questions are:

  1. Why there is a width limit while my window has enough space and how to change it?
  2. How to print such an output to a file?


Solution 1:[1]

sympy.preview:

sp.preview(a, viewer='file', filename="out.png", dvioptions=['-D','300'])

Would allow you to save the expression as LaTeX/png/pdf.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 idanp