How to render large parenthesis correctly in sympy?

869 Views Asked by At

I am trying to render simple algebraic expressions in either qtconsole or a terminal using pprint, but the results are almost misleading. Here's a minimal example

from sympy import pprint, simplify, init_printing
from sympy.abc import a, b

init_printing()
c = a - b**2
pprint(simplify(c**2))
print(simplify(c**2))

I tried this code in both jupyer qtconsole and in rxvt-unicode terminal and both don't display multiple level parenthsis correctly. On the terminal I get rectangular symbols where the brackets should be. qtconsole is even worse as it doesn't show parenthsis.

enter image description here

This is extremely confusing when the expressions get more complicated. Please let me know if I am missing something or if sympy can be configured to display that better.

1

There are 1 best solutions below

2
On BEST ANSWER

pprint uses Unicode characters to print parentheses by default, like

        2
⎛     2⎞ 
⎝a - b ⎠ 

It seems your qtconsole isn't rendering them, which probably means the font you are using doesn't support them.

My recommendations:

  • Install DejaVu Sans Mono and set your qtconsole font to that (see https://stackoverflow.com/a/18904744/161801). DejaVu Sans Mono is the best font for the Unicode characters used by SymPy, in my experience.

  • Install LaTeX on your computer. If you run init_printing in the qtconsole SymPy detects that LaTeX is installed, it will render math using it (pprint will still render text).

If neither of the above is an option for you the workaround is to use pprint(expr, use_unicode=False), which will print the expression using only ASCII characters. It won't look as nice, but it should render correctly in any monospace font.