Skip to content

Commit

Permalink
Use \left [/(/{ \right ]/)/} for latex of lists, tuples and dicts
Browse files Browse the repository at this point in the history
This makes them still work with the matplotlib.mathtext renderer, which
doesn't support matrices.
  • Loading branch information
asmeurer committed Jul 7, 2014
1 parent 17cd7db commit e532688
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion sympy/interactive/tests/test_ipythonprinting.py
Expand Up @@ -82,7 +82,7 @@ def test_print_builtin_option():
text = app.user_ns['a'][0]['text/plain']
latex = app.user_ns['a'][0]['text/latex']
assert text == u('{n\u1d62: 3, \u03c0: 3.14}')
assert latex == '$$\\begin{Bmatrix}n_{i} : 3, & \\pi : 3.14\\end{Bmatrix}$$'
assert latex == r'$$\left \{ n_{i} : 3, \quad \pi : 3.14\right \}$$'

app.run_cell("inst.display_formatter.formatters['text/latex'].enabled = True")
app.run_cell("init_printing(use_latex=True, print_builtin=False)")
Expand Down
10 changes: 5 additions & 5 deletions sympy/printing/latex.py
Expand Up @@ -1316,15 +1316,15 @@ def _print_Identity(self, I):
return r"\mathbb{I}"

def _print_tuple(self, expr):
return r"\begin{pmatrix}%s\end{pmatrix}" % \
r", & ".join([ self._print(i) for i in expr ])
return r"\left ( %s\right )" % \
r", \quad ".join([ self._print(i) for i in expr ])

def _print_Tuple(self, expr):
return self._print_tuple(expr)

def _print_list(self, expr):
return r"\begin{bmatrix}%s\end{bmatrix}" % \
r", & ".join([ self._print(i) for i in expr ])
return r"\left [ %s\right ]" % \
r", \quad ".join([ self._print(i) for i in expr ])

def _print_dict(self, d):
keys = sorted(d.keys(), key=default_sort_key)
Expand All @@ -1334,7 +1334,7 @@ def _print_dict(self, d):
val = d[key]
items.append("%s : %s" % (self._print(key), self._print(val)))

return r"\begin{Bmatrix}%s\end{Bmatrix}" % r", & ".join(items)
return r"\left \{ %s\right \}" % r", \quad ".join(items)

def _print_Dict(self, expr):
return self._print_dict(expr)
Expand Down
44 changes: 22 additions & 22 deletions sympy/printing/tests/test_latex.py
Expand Up @@ -240,9 +240,9 @@ def test_latex_functions():
assert latex(Order(x, x)) == r"\mathcal{O}\left(x\right)"
assert latex(Order(x, (x, 0))) == r"\mathcal{O}\left(x\right)"
assert latex(Order(x, (x, oo))) == r"\mathcal{O}\left(x; x\rightarrow\infty\right)"
assert latex(Order(x, x, y)) == r"\mathcal{O}\left(x; \begin{pmatrix}x, & y\end{pmatrix}\rightarrow\begin{pmatrix}0, & 0\end{pmatrix}\right)"
assert latex(Order(x, x, y)) == r"\mathcal{O}\left(x; \begin{pmatrix}x, & y\end{pmatrix}\rightarrow\begin{pmatrix}0, & 0\end{pmatrix}\right)"
assert latex(Order(x, (x, oo), (y, oo))) == r"\mathcal{O}\left(x; \begin{pmatrix}x, & y\end{pmatrix}\rightarrow\begin{pmatrix}\infty, & \infty\end{pmatrix}\right)"
assert latex(Order(x, x, y)) == r"\mathcal{O}\left(x; \left ( x, \quad y\right )\rightarrow\left ( 0, \quad 0\right )\right)"
assert latex(Order(x, x, y)) == r"\mathcal{O}\left(x; \left ( x, \quad y\right )\rightarrow\left ( 0, \quad 0\right )\right)"
assert latex(Order(x, (x, oo), (y, oo))) == r"\mathcal{O}\left(x; \left ( x, \quad y\right )\rightarrow\left ( \infty, \quad \infty\right )\right)"
assert latex(lowergamma(x, y)) == r'\gamma\left(x, y\right)'
assert latex(uppergamma(x, y)) == r'\Gamma\left(x, y\right)'

Expand Down Expand Up @@ -553,19 +553,19 @@ def test_latex():
"\\begin{equation*}8 \\sqrt{2} \\mu^{\\frac{7}{2}}\\end{equation*}"
assert latex((2*mu)**Rational(7, 2), mode='equation', itex=True) == \
"$$8 \\sqrt{2} \\mu^{\\frac{7}{2}}$$"
assert latex([2/x, y]) == "\\begin{bmatrix}\\frac{2}{x}, & y\\end{bmatrix}"
assert latex([2/x, y]) == r"\left [ \frac{2}{x}, \quad y\right ]"


def test_latex_dict():
d = {Rational(1): 1, x**2: 2, x: 3, x**3: 4}
assert latex(d) == '\\begin{Bmatrix}1 : 1, & x : 3, & x^{2} : 2, & x^{3} : 4\\end{Bmatrix}'
assert latex(d) == r'\left \{ 1 : 1, \quad x : 3, \quad x^{2} : 2, \quad x^{3} : 4\right \}'
D = Dict(d)
assert latex(D) == '\\begin{Bmatrix}1 : 1, & x : 3, & x^{2} : 2, & x^{3} : 4\\end{Bmatrix}'
assert latex(D) == r'\left \{ 1 : 1, \quad x : 3, \quad x^{2} : 2, \quad x^{3} : 4\right \}'


def test_latex_list():
l = [Symbol('omega1'), Symbol('a'), Symbol('alpha')]
assert latex(l) == r'\begin{bmatrix}\omega_{1}, & a, & \alpha\end{bmatrix}'
assert latex(l) == r'\left [ \omega_{1}, \quad a, \quad \alpha\right ]'


def test_latex_rational():
Expand Down Expand Up @@ -752,7 +752,7 @@ def test_latex_Lambda():
assert latex(Lambda(x, x + 1)) == \
r"\left( x \mapsto x + 1 \right)"
assert latex(Lambda((x, y), x + 1)) == \
r"\left( \begin{pmatrix}x, & y\end{pmatrix} \mapsto x + 1 \right)"
r"\left( \left ( x, \quad y\right ) \mapsto x + 1 \right)"


def test_latex_PolyElement():
Expand Down Expand Up @@ -953,22 +953,22 @@ def test_categories():
assert latex(d) == "\emptyset"

d = Diagram({f1: "unique", f2: S.EmptySet})
assert latex(d) == "\\begin{Bmatrix}f_{2}\\circ f_{1}:A_{1}" \
"\\rightarrow A_{3} : \\emptyset, & id:A_{1}\\rightarrow " \
"A_{1} : \\emptyset, & id:A_{2}\\rightarrow A_{2} : " \
"\\emptyset, & id:A_{3}\\rightarrow A_{3} : \\emptyset, " \
"& f_{1}:A_{1}\\rightarrow A_{2} : \\left\\{unique\\right\\}, " \
"& f_{2}:A_{2}\\rightarrow A_{3} : \\emptyset\\end{Bmatrix}"
assert latex(d) == r"\left \{ f_{2}\circ f_{1}:A_{1}" \
r"\rightarrow A_{3} : \emptyset, \quad id:A_{1}\rightarrow " \
r"A_{1} : \emptyset, \quad id:A_{2}\rightarrow A_{2} : " \
r"\emptyset, \quad id:A_{3}\rightarrow A_{3} : \emptyset, " \
r"\quad f_{1}:A_{1}\rightarrow A_{2} : \left\{unique\right\}, " \
r"\quad f_{2}:A_{2}\rightarrow A_{3} : \emptyset\right \}"

d = Diagram({f1: "unique", f2: S.EmptySet}, {f2 * f1: "unique"})
assert latex(d) == "\\begin{Bmatrix}f_{2}\\circ f_{1}:A_{1}" \
"\\rightarrow A_{3} : \\emptyset, & id:A_{1}\\rightarrow " \
"A_{1} : \\emptyset, & id:A_{2}\\rightarrow A_{2} : " \
"\\emptyset, & id:A_{3}\\rightarrow A_{3} : \\emptyset, " \
"& f_{1}:A_{1}\\rightarrow A_{2} : \\left\\{unique\\right\\}," \
" & f_{2}:A_{2}\\rightarrow A_{3} : \\emptyset\\end{Bmatrix}" \
"\\Longrightarrow \\begin{Bmatrix}f_{2}\\circ f_{1}:A_{1}" \
"\\rightarrow A_{3} : \\left\\{unique\\right\\}\\end{Bmatrix}"
assert latex(d) == r"\left \{ f_{2}\circ f_{1}:A_{1}" \
r"\rightarrow A_{3} : \emptyset, \quad id:A_{1}\rightarrow " \
r"A_{1} : \emptyset, \quad id:A_{2}\rightarrow A_{2} : " \
r"\emptyset, \quad id:A_{3}\rightarrow A_{3} : \emptyset, " \
r"\quad f_{1}:A_{1}\rightarrow A_{2} : \left\{unique\right\}," \
r" \quad f_{2}:A_{2}\rightarrow A_{3} : \emptyset\right \}" \
r"\Longrightarrow \left \{ f_{2}\circ f_{1}:A_{1}" \
r"\rightarrow A_{3} : \left\{unique\right\}\right \}"

# A linear diagram.
A = Object("A")
Expand Down

0 comments on commit e532688

Please sign in to comment.