Repository: sympy/sympy

Issue:
latex bug for commutator output
There is a latex bug in the output of the function sympy.physics.quantum.commutator.doit that gives incorrect result if there is overall negative sign involved. For example, the following code does not give the correct expression for the latex output.

```python
from sympy.physics.quantum import Commutator, Operator
import sympy
A = Operator('A')
B = Operator('B')
comm = Commutator(B, A)
print comm.doit()
sympy.latex(comm.doit())
```

Discussion:

Patch:
diff --git a/sympy/printing/latex.py b/sympy/printing/latex.py
index ba22308b1e..ddbc887714 100644
--- a/sympy/printing/latex.py
+++ b/sympy/printing/latex.py
@@ -322,9 +322,13 @@ def _print_Float(self, expr):
             return str_real
 
     def _print_Mul(self, expr):
+        include_parens = False
         if _coeff_isneg(expr):
             expr = -expr
             tex = "- "
+            if expr.is_Add:
+                tex += "("
+                include_parens = True
         else:
             tex = ""
 
@@ -405,6 +409,8 @@ def convert(expr):
             else:
                 tex += r"\frac{%s}{%s}" % (snumer, sdenom)
 
+        if include_parens:
+            tex += ")"
         return tex
 
     def _print_Pow(self, expr):


Test patch:
diff --git a/sympy/printing/tests/test_latex.py b/sympy/printing/tests/test_latex.py
index 05eac5e4ae..c663785e4c 100644
--- a/sympy/printing/tests/test_latex.py
+++ b/sympy/printing/tests/test_latex.py
@@ -27,6 +27,7 @@
 from sympy.functions import DiracDelta, Heaviside, KroneckerDelta, LeviCivita
 from sympy.logic import Implies
 from sympy.logic.boolalg import And, Or, Xor
+from sympy.physics.quantum import Commutator, Operator
 from sympy.core.trace import Tr
 from sympy.core.compatibility import range
 from sympy.combinatorics.permutations import Cycle, Permutation
@@ -601,6 +602,12 @@ def test_latex_AccumuBounds():
 def test_latex_emptyset():
     assert latex(S.EmptySet) == r"\emptyset"
 
+def test_latex_commutator():
+    A = Operator('A')
+    B = Operator('B')
+    comm = Commutator(B, A)
+    assert latex(comm.doit()) == r"- (A B - B A)"
+
 
 def test_latex_union():
     assert latex(Union(Interval(0, 1), Interval(2, 3))) == \