##// END OF EJS Templates
Minor fix to sympy latex printing....
Brian Granger -
Show More
@@ -1,90 +1,91 b''
1 """A print function that pretty prints sympy Basic objects.
1 """A print function that pretty prints sympy Basic objects.
2
2
3 Authors:
3 Authors:
4 * Brian Granger
4 * Brian Granger
5 """
5 """
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7 # Copyright (C) 2008-2011 The IPython Development Team
7 # Copyright (C) 2008-2011 The IPython Development Team
8 #
8 #
9 # Distributed under the terms of the BSD License. The full license is in
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
10 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 from IPython.lib.latextools import latex_to_png
17 from IPython.lib.latextools import latex_to_png
18 from IPython.testing import decorators as dec
18 from IPython.testing import decorators as dec
19 # use @dec.skipif_not_sympy to skip tests requiring sympy
19 # use @dec.skipif_not_sympy to skip tests requiring sympy
20
20
21 try:
21 try:
22 from sympy import pretty, latex
22 from sympy import pretty, latex
23 except ImportError:
23 except ImportError:
24 pass
24 pass
25
25
26
26
27 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
28 # Definitions of magic functions for use with IPython
28 # Definitions of magic functions for use with IPython
29 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
30
30
31 def print_basic_unicode(o, p, cycle):
31 def print_basic_unicode(o, p, cycle):
32 """A function to pretty print sympy Basic objects."""
32 """A function to pretty print sympy Basic objects."""
33 if cycle:
33 if cycle:
34 return p.text('Basic(...)')
34 return p.text('Basic(...)')
35 out = pretty(o, use_unicode=True)
35 out = pretty(o, use_unicode=True)
36 if '\n' in out:
36 if '\n' in out:
37 p.text(u'\n')
37 p.text(u'\n')
38 p.text(out)
38 p.text(out)
39
39
40
40
41 def print_png(o):
41 def print_png(o):
42 """A function to display sympy expression using LaTex -> PNG."""
42 """A function to display sympy expression using LaTex -> PNG."""
43 s = latex(o, mode='inline')
43 s = latex(o, mode='inline')
44 # mathtext does not understand certain latex flags, so we try to replace
44 # mathtext does not understand certain latex flags, so we try to replace
45 # them with suitable subs.
45 # them with suitable subs.
46 s = s.replace('\\operatorname','')
46 s = s.replace('\\operatorname','')
47 s = s.replace('\\overline', '\\bar')
47 s = s.replace('\\overline', '\\bar')
48 png = latex_to_png(s)
48 png = latex_to_png(s)
49 return png
49 return png
50
50
51
51
52 def print_latex(o):
52 def print_latex(o):
53 """A function to generate the latex representation of sympy expressions."""
53 """A function to generate the latex representation of sympy expressions."""
54 s = latex(o, mode='equation', itex=True)
54 s = latex(o, mode='equation', itex=True)
55 s = s.replace('\\dag','\\dagger')
55 return s
56 return s
56
57
57
58
58 _loaded = False
59 _loaded = False
59
60
60 def load_ipython_extension(ip):
61 def load_ipython_extension(ip):
61 """Load the extension in IPython."""
62 """Load the extension in IPython."""
62 global _loaded
63 global _loaded
63 if not _loaded:
64 if not _loaded:
64 plaintext_formatter = ip.display_formatter.formatters['text/plain']
65 plaintext_formatter = ip.display_formatter.formatters['text/plain']
65
66
66 for cls in (object, tuple, list, set, frozenset, dict, str):
67 for cls in (object, tuple, list, set, frozenset, dict, str):
67 plaintext_formatter.for_type(cls, print_basic_unicode)
68 plaintext_formatter.for_type(cls, print_basic_unicode)
68
69
69 plaintext_formatter.for_type_by_name(
70 plaintext_formatter.for_type_by_name(
70 'sympy.core.basic', 'Basic', print_basic_unicode
71 'sympy.core.basic', 'Basic', print_basic_unicode
71 )
72 )
72 plaintext_formatter.for_type_by_name(
73 plaintext_formatter.for_type_by_name(
73 'sympy.matrices.matrices', 'Matrix', print_basic_unicode
74 'sympy.matrices.matrices', 'Matrix', print_basic_unicode
74 )
75 )
75
76
76 png_formatter = ip.display_formatter.formatters['image/png']
77 png_formatter = ip.display_formatter.formatters['image/png']
77
78
78 png_formatter.for_type_by_name(
79 png_formatter.for_type_by_name(
79 'sympy.core.basic', 'Basic', print_png
80 'sympy.core.basic', 'Basic', print_png
80 )
81 )
81
82
82 latex_formatter = ip.display_formatter.formatters['text/latex']
83 latex_formatter = ip.display_formatter.formatters['text/latex']
83 latex_formatter.for_type_by_name(
84 latex_formatter.for_type_by_name(
84 'sympy.core.basic', 'Basic', print_latex
85 'sympy.core.basic', 'Basic', print_latex
85 )
86 )
86 latex_formatter.for_type_by_name(
87 latex_formatter.for_type_by_name(
87 'sympy.matrices.matrices', 'Matrix', print_latex
88 'sympy.matrices.matrices', 'Matrix', print_latex
88 )
89 )
89 _loaded = True
90 _loaded = True
90
91
General Comments 0
You need to be logged in to leave comments. Login now