Here is a solution. It is probably not very robust but it could probably be adapted to your need. They are two problems here:
1 - You need to tell sage how to latex Bra and Ket. This can be done using latex_table
which is a dict associating type to latex type-setting functions:
from sympy.physics.quantum import Bra, Ket
def latexBra(x):
if len(x.args) == 1: # x.args contains the argument of the Bra
res = latex(x.args[0])
else:
res = ",".join(latex(i) for i in x.args)
return r"{\left|%s\right\rangle}"%res
from sage.misc.latex import latex_table
latex_table[Bra] = latexBra
Of course you have to do the same for Ket. You probably want to do that also for
sympy.physics.quantum.operator.OuterProduct
sympy.physics.quantum.operator.InnerProduct
...
However, this only solve part of the problem: latexing the Bra. Note that Phi isn't latexed.
sage: bPhi = Bra(var('Phi'))
sage: print latex(bPhi)
{\left|\text{\texttt{Phi}}\right\rangle}
2 - The reason is that Sympy bPhi
argument bPhi.args[0]
is not exactly the Sage variable Phi
but a Sympy Symbol
object:
sage: type(bPhi.args[0])
<class 'sympy.core.symbol.Symbol'>
However one can get back Sage's Phi
by coercing the Symbol
object back to Sage's symbolic ring (SR
):
sage: SR(bPhi.args[0]) is Phi
True
So you can do tell Sage to to that for latexing Sympy's Symbol
:
from sympy.core.symbol import Symbol
latex_table[Symbol] = lambda x : latex(SR(x))
And then:
sage: bPhi = Bra(var('Phi'))
sage: print latex(bPhi)
{\left|\Phi\right\rangle}