##// END OF EJS Templates
Only print container types with LaTeX in the notebook if all the elements can...
Aaron Meurer -
Show More
@@ -30,7 +30,6 b' try:'
30 except ImportError:
30 except ImportError:
31 pass
31 pass
32
32
33
34 #-----------------------------------------------------------------------------
33 #-----------------------------------------------------------------------------
35 # Definitions of magic functions for use with IPython
34 # Definitions of magic functions for use with IPython
36 #-----------------------------------------------------------------------------
35 #-----------------------------------------------------------------------------
@@ -55,14 +54,32 b' def print_png(o):'
55 png = latex_to_png(s)
54 png = latex_to_png(s)
56 return png
55 return png
57
56
57 def can_print_latex(o):
58 """
59 Return True if type o can be printed with LaTeX.
60
61 If o is a container type, this is True if and only if every element of o
62 can be printed with LaTeX.
63 """
64 import sympy
65 if isinstance(o, (list, tuple)):
66 return all(can_print_latex(i) for i in o)
67 elif isinstance(o, dict):
68 return all((isinstance(i, basestring) or can_print_latex(i)) and can_print_latex(o[i]) for i in o)
69 elif isinstance(o,(sympy.Basic, sympy.matrices.Matrix, int, long, float)):
70 return True
71 return False
58
72
59 def print_latex(o):
73 def print_latex(o):
60 """A function to generate the latex representation of sympy expressions."""
74 """A function to generate the latex representation of sympy
61 s = latex(o, mode='plain')
75 expressions."""
62 s = s.replace('\\dag','\\dagger')
76 if can_print_latex(o):
63 s = s.strip('$')
77 s = latex(o, mode='plain')
64 return '$$%s$$' % s
78 s = s.replace('\\dag','\\dagger')
65
79 s = s.strip('$')
80 return '$$%s$$' % s
81 # Fallback to the string printer
82 return None
66
83
67 _loaded = False
84 _loaded = False
68
85
@@ -73,6 +90,8 b' def load_ipython_extension(ip):'
73 plaintext_formatter = ip.display_formatter.formatters['text/plain']
90 plaintext_formatter = ip.display_formatter.formatters['text/plain']
74
91
75 for cls in (object, set, frozenset, str):
92 for cls in (object, set, frozenset, str):
93 # set and frozen set are currently broken with SymPy's latex()
94 # function. See http://code.google.com/p/sympy/issues/detail?id=3062.
76 plaintext_formatter.for_type(cls, print_basic_unicode)
95 plaintext_formatter.for_type(cls, print_basic_unicode)
77
96
78 plaintext_formatter.for_type_by_name(
97 plaintext_formatter.for_type_by_name(
@@ -97,7 +116,9 b' def load_ipython_extension(ip):'
97 latex_formatter.for_type_by_name(
116 latex_formatter.for_type_by_name(
98 'sympy.matrices.matrices', 'Matrix', print_latex
117 'sympy.matrices.matrices', 'Matrix', print_latex
99 )
118 )
100 for cls in (list, tuple, dict, int, long, float):
119
120 for cls in (list, tuple):
121 # Use LaTeX only if every element is printable by latex
101 latex_formatter.for_type(cls, print_latex)
122 latex_formatter.for_type(cls, print_latex)
102
123
103 _loaded = True
124 _loaded = True
General Comments 0
You need to be logged in to leave comments. Login now