Wow, thanks a lot! Very nice.
I noticed you had to write:
def is_eqal(exp1, exp2):
exp1 = exp1.replace("$","")
exp2 = exp2.replace("$","")
return "$%s=%s$"%(exp1,exp2)
So I think latex() should not put those "$" in. How about the
following behavior:
In [1]: latex(x**2)
Out[1]: '{x}^{2}'
In [2]: latex(x**2, delimited=True)
Out[2]: '${x}^{2}$'
In [3]: latex(x**2, delimited=True, inline=False)
Out[3]: '\\begin{equation*}{x}^{2}\\end{equation*}'
This can be achieved by this patch:
ondra@fuji:~/sympy$ hg di
diff --git a/sympy/printing/latex.py b/sympy/printing/latex.py
--- a/sympy/printing/latex.py
+++ b/sympy/printing/latex.py
@@ -5,17 +5,21 @@ class LatexPrinter(Printer):
class LatexPrinter(Printer):
"""A printer which converts an expression into its LaTeX equivalent."""
- def __init__(self, inline=True):
+ def __init__(self, delimited=False, inline=True):
Printer.__init__(self)
self._inline = inline
+ self._delimited = delimited
def doprint(self, expr):
tex = Printer.doprint(self, expr)
- if self._inline:
- return r"$%s$" % tex
+ if self._delimited:
+ if self._inline:
+ return r"$%s$" % tex
+ else:
+ return r"\begin{equation*}%s\end{equation*}" % tex
else:
- return r"\begin{equation*}%s\end{equation*}" % tex
+ return tex
def _needs_brackets(self, expr):
return not ((expr.is_Integer and expr.is_nonnegative) or expr.is_Atom)
@@ -379,7 +383,7 @@ class LatexPrinter(Printer):
return r"\begin{Bmatrix}%s\end{Bmatrix}" % r", & ".join(items)
-def latex(expr, inline=True):
+def latex(expr, delimited=False, inline=True):
r"""Convert the given expression to LaTeX representation.
You can specify how the generated code will be delimited.
@@ -405,7 +409,7 @@ def latex(expr, inline=True):
"""
- return LatexPrinter(inline).doprint(expr)
+ return LatexPrinter(delimited, inline).doprint(expr)
def print_latex(expr):
"""Prints LaTeX representation of the given expression."""
I created an issue for that:
http://code.google.com/p/sympy/issues/detail?id=954
Ondrej
Should it be called delimited or delimiter?
Others --- ok with this behavior? If so, I'll write and adjust tests
and send it for review.
Ondrej
delimited
> Others --- ok with this behavior?
OK, just don't forget to patch TeXmacs plugin:
sympy/data/TeXmacs/bin/tm_sympy:41
Mateusz
2008/7/22 Ondrej Certik <ond...@certik.cz>:
I noticed that your patch uses the 'equation*' environment if inline is
False. Maybe you could add another kwarg, called 'label', that would use
the 'equation' environment with the label passed in. In this way, you
will be able to refer in the .tex file to the equations typeset in
Python. That would be really cool.
r.
I think we should rework the interface to latex() completely.
In [1]: latex(x**2)
Out[1]: '{x}^{2}'
In [2]: latex(x**2, type="inline")
Out[2]: '${x}^{2}$'
In [3]: latex(x**2, type="equation")
Out[3]: '\\begin{equation*}{x}^{2}\\end{equation*}'
In [4]: latex(x**2, type="equation", label="something")
Out[3]: '\\begin{equation}{x}^{2}\\label{something}\\end{equation}'
what do you think?
I didn't want this to put into 0.6.1, because it needs more thinking.
Ondrej
Maybe also:
In [5]: latex(x**2, type="$$")
Out[5]: '$${x}^{2}$$'
and there could be even more options.
Ondrej
Yes, this is much simpler. Just in [3] I slightly prefer
type="equation*", so that it corresponds to the environment - you can
use a numbered equation (type="equation") without a label after all.
r.