mailing-list for TeXmacs Users

Text archives Help


[TeXmacs] TeXmacs Sympy plugin LaTeX Output Issue Solved


Chronological Thread 
  • From: "David E. Miller" <address@hidden>
  • To: François Poulain <address@hidden>, Bill Eaton <address@hidden>, address@hidden, address@hidden
  • Subject: [TeXmacs] TeXmacs Sympy plugin LaTeX Output Issue Solved
  • Date: Sat, 18 May 2013 02:15:24 -0400
  • Authentication-results: smtp04.embarq.synacor.com smtp.user=address@hidden; auth=pass (LOGIN)
  • X_cmae_category: 0,0 Undefined,Undefined

The attached TeXmacs file documents a proposed solution to the main issue of developing a TeXmacs plugin for SymPy that outputs expressions to TeXmacs formatted as LaTeX.

I think there is probably a little more work that needs to be done by those that have an interest in this plugin, but it appears the the main hurdle of formatting the output is solved.

Comments and suggestions are welcome.

David Miller

On 5/17/2013 3:30 PM, François Poulain wrote:
Le Thu, 16 May 2013 04:48:49 -0006,
tisimst <address@hidden> a écrit :

I would love to
see a good SymPy plugin!
I think it is a shared opinion.

François


<TeXmacs|1.0.7.15>

<style|generic>

<\body>
In the <TeXmacs> <name|Python> plugin file t<verbatim|m_python>, if the
following function code:

<\framed>
<\verbatim-code>
def compose_output(data):

\ \ \ \ \ """Do some parsing on the output according to its type."""\

\ \ \ \ \ if isinstance(data, str):

\ \ \ \ \ \ \ \ \ \ \ \ \ return 'verbatim:'+data.strip()\

\ \ \ \ \ if isinstance(data, int):

\ \ \ \ \ \ \ \ \ \ \ \ \ return 'verbatim: %d' % data

\ \ \ \ \ if isinstance(data, float):

\ \ \ \ \ \ \ \ \ \ \ \ \ return 'verbatim: %f' % data

\ \ \ \ \ if isinstance(data, unicode):

\ \ \ \ \ \ \ \ \ \ \ \ \ data2=r''

\ \ \ \ \ \ \ \ \ \ \ \ \ for c in data:

\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ if c not in string.printable:

\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ data2+='\\x%x'
% ord(c)

\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ else:

\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ data2+=c

\ \ \ \ \ \ \ \ \ \ \ \ \ data=data2

\ \ \ \ \ \

\ \ \ \ \ return 'verbatim: %s' % str(data)
</verbatim-code>
</framed>

\;

is edited to remove all lines of parsing except the last <verbatim|return>
line and this line is edited from:

<\framed>
<verbatim|return 'verbatim: %s' % str(data)>
</framed>

to

<\framed>
<verbatim|return 'latex: %s' % str(data)>
</framed>

as shown below:

<\framed>
<\verbatim-code>
def compose_output(data):

\ \ \ \ \ """Do some parsing on the output according to its type."""\

\;

\ \ \ \ \ \ return 'latex: %s' % str(data)
</verbatim-code>
</framed>

then the <name|SymPy> module when loaded in a <TeXmacs> <name|Python>
session will return <LaTeX> formatted output as shown below:

<\session|python|default>
<\output>
Python plugin for TeXmacs.

Please see the documentation in Help -\<gtr\> plugins -\<gtr\> Python
</output>

<\input|Python] >
from __future__ import division
</input>

<\input|Python] >
from sympy import *
</input>

<\input|Python] >
x, y, z, t = symbols('x y z t')
</input>

<\input|Python] >
k, m, n = symbols('k m n', integer=True)
</input>

<\input|Python] >
f, g, h = symbols('f g h', cls=Function)
</input>

<\input|Python] >
from sympy import Integral, latex
</input>

<\input|Python] >
from sympy.abc import x
</input>

<\unfolded-io|Python] >
latex(x**2,mode='inline')
<|unfolded-io>
<math|x<rsup|2>>
</unfolded-io>

<\unfolded-io|Python] >
latex(x**2, mode='inline')
<|unfolded-io>
<math|x<rsup|2>>
</unfolded-io>

<\unfolded-io|Python] >
latex(x**2, mode='equation')
<|unfolded-io>
<\equation>
x<rsup|2>
</equation>
</unfolded-io>

<\unfolded-io|Python] >
latex(1/x,mode='inline')
<|unfolded-io>
<math|<frac|1|x>>
</unfolded-io>

<\unfolded-io|Python] >
latex(Integral(x**2, x),mode='equation')
<|unfolded-io>
<\equation>
<big|int>x<rsup|2>*<space|0.25spc>d*x
</equation>
</unfolded-io>

<\input| >
\;
</input>

<\unfolded-io|Python] >
latex((1/cos(x)).series(x, 0, 10),mode='inline')
<|unfolded-io>

<math|1+<frac|1|2>*x<rsup|2>+<frac|5|24>*x<rsup|4>+<frac|61|720>*x<rsup|6>+<frac|277|8064>*x<rsup|8>+<with|math-font-family|rm|math-font|cal|O><around*|(|x<rsup|10>|)>>
</unfolded-io>

<\input|Python] >
from sympy import Rational
</input>

<\input|Python] >
a = Rational(1,2)
</input>

<\unfolded-io|Python] >
a
<|unfolded-io>
1/2
</unfolded-io>

<\unfolded-io|Python] >
a**2
<|unfolded-io>
1/4
</unfolded-io>

<\unfolded-io|Python] >
Rational(2)**50/Rational(10)**50
<|unfolded-io>
1/88817841970012523233890533447265625
</unfolded-io>

<\unfolded-io|Python] >
1/2
<|unfolded-io>
0
</unfolded-io>

<\unfolded-io|Python] >
\ x+y+x-y
<|unfolded-io>
2*x
</unfolded-io>

<\unfolded-io|Python] >
\ latex(x+y+x-y,mode='inline')
<|unfolded-io>
<math|2*x>
</unfolded-io>

<\unfolded-io|Python] >
latex((x+y)**2,mode='equation')
<|unfolded-io>
<\equation>
<around*|(|x+y|)><rsup|2>
</equation>
</unfolded-io>

<\unfolded-io|Python] >
latex(((x+y)**2).expand(),mode='inline')
<|unfolded-io>
<math|x<rsup|2>+2*x*y+y<rsup|2>>
</unfolded-io>

<\input|Python] >
from sympy import apart
</input>

<\unfolded-io|Python] >
latex(1/( (x+2)*(x+1) ),mode='inline')
<|unfolded-io>
<math|<frac|1|<around*|(|x+1|)>*<around*|(|x+2|)>>>
</unfolded-io>

<\unfolded-io|Python] >
latex(apart(1/( (x+2)*(x+1) ), x),mode='equation')
<|unfolded-io>
<\equation>
-<frac|1|x+2>+<frac|1|x+1>
</equation>
</unfolded-io>

<\input|Python] >
from sympy import limit, Symbol, sin, oo
</input>

<\input|Python] >
x = Symbol("x")
</input>

<\unfolded-io|Python] >
limit(sin(x)/x, x, 0)
<|unfolded-io>
1
</unfolded-io>

<\input|Python] >
from sympy import diff, Symbol, sin, tan
</input>

<\unfolded-io|Python] >
latex(diff(sin(x), x),mode='inline')
<|unfolded-io>
<math|<math-up|cos><around*|(|x|)>>
</unfolded-io>

<\unfolded-io|Python] >
latex(diff(sin(2*x), x),mode='inline')
<|unfolded-io>
<math|2<math-up|cos><around*|(|2*x|)>>
</unfolded-io>

<\unfolded-io|Python] >
latex(diff(tan(x), x),mode='equation')
<|unfolded-io>
<\equation>
<math-up|tan><rsup|2><around*|(|x|)>+1
</equation>
</unfolded-io>

<\input|Python] >
\;
</input>
</session>

\;

This is as much code as I have tried.

So if this function and/or file can be modified so that the parsing detects
that the output is <LaTeX> then the problem of formatting SymPy output
looks to have been solved. I am not sure that all output from <name|SymPy>
would need to be parsed as <LaTeX> so it looks like some more effort may be
required to figure out how to get this function to recognize <LaTeX> output
as well as the data types it currently parses as is. However, the output
looks pretty good so far as I have tried as is with this modified file.

\;
</body>

<\initial>
<\collection>
<associate|language|american>
<associate|page-type|letter>
</collection>
</initial>


Archive powered by MHonArc 2.6.19.

Top of page