Python inside LaTeX

5 views
Skip to first unread message

kib2

unread,
Jul 22, 2008, 12:01:00 PM7/22/08
to sympy
Hi,

I've just discovered something that maybe useful for some of us :

http://thewikiblog.appspot.com/blog/python-inside-latex

cheers, and a big thanks to to SymPy team for all their work.

Kib.

Ondrej Certik

unread,
Jul 22, 2008, 1:11:40 PM7/22/08
to sy...@googlegroups.com
Hi Kib!

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

kib2

unread,
Jul 22, 2008, 1:23:54 PM7/22/08
to sympy
Hi Ondrej ,

You're right, the latex() function should have a delimiter option.
Thanks for the patch !

Ondrej Certik

unread,
Jul 22, 2008, 1:33:04 PM7/22/08
to sy...@googlegroups.com
On Tue, Jul 22, 2008 at 7:23 PM, kib2 <kibleur.c...@gmail.com> wrote:
>
> Hi Ondrej ,
>
> You're right, the latex() function should have a delimiter option.


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

Mateusz Paprocki

unread,
Jul 22, 2008, 1:40:42 PM7/22/08
to sy...@googlegroups.com
> Should it be called delimited or delimiter?

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>:

Robert Cimrman

unread,
Jul 23, 2008, 6:03:52 AM7/23/08
to sy...@googlegroups.com
Hi Ondrej!

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.

Ondrej Certik

unread,
Jul 23, 2008, 6:13:28 AM7/23/08
to sy...@googlegroups.com

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

Ondrej Certik

unread,
Jul 23, 2008, 6:14:37 AM7/23/08
to sy...@googlegroups.com

Maybe also:

In [5]: latex(x**2, type="$$")
Out[5]: '$${x}^{2}$$'


and there could be even more options.

Ondrej

Robert Cimrman

unread,
Jul 23, 2008, 6:18:46 AM7/23/08
to sy...@googlegroups.com

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.

Reply all
Reply to author
Forward
0 new messages