Convert sympy results to svg string

45 views
Skip to first unread message

Anton Makarov

unread,
Nov 17, 2022, 7:05:17 AM11/17/22
to sympy
Hi, i am new to sympy. I need to convert the results of sympy calculations to svg string.
How can i do this?

Anton Makarov

unread,
Nov 17, 2022, 8:54:48 AM11/17/22
to sympy
I'm taking about result of calculations, for instance:
expr = sympify("1+2/3*sin(x)")
result = str(expr)
and i need to convert result to svg. How can i do this?


четверг, 17 ноября 2022 г. в 15:05:17 UTC+3, Anton Makarov:

gu...@uwosh.edu

unread,
Nov 17, 2022, 8:59:38 AM11/17/22
to sympy
Are you trying to convert typeset math into a scalable vector graphic image (.svg) or do you mean something else?

Anton Makarov

unread,
Nov 17, 2022, 9:01:56 AM11/17/22
to sympy
I need to convert sympy tree expression to svg, for instance:
a = sympify("1+2/3")
b = sympify("x+5/x+3*sin(x)")
c = a + b
and i need to convert c expression to svg string




четверг, 17 ноября 2022 г. в 16:59:38 UTC+3, gu...@uwosh.edu:

Anton Makarov

unread,
Nov 17, 2022, 9:08:37 AM11/17/22
to sympy
I don't want an svg file, i want the string. Something like this:
svg_string = <svg width="134" height="32" viewBox="-1 -1 134 32"><path d="M 0 14.3 L 7.169531 14.3 " fill="none" stroke="rgb(0,0,0)" stroke-width="1"></path><text x="0" y="9.962011" font-family="'Segoe UI'" font-size="13.3" textLength="7.169531" fill="rgb(0,0,0)">5</text><text x="0" y="26.26201" font-family="'Segoe UI'" font-size="13.3" textLength="7.169531" fill="rgb(0,0,0)">3</text><path d="M 11 15 L 21 15 " fill="none" stroke="rgb(0,0,0)" stroke-width="1"></path><path d="M 16 9.8 L 16 19.8 " fill="none" stroke="rgb(0,0,0)" stroke-width="1"></path><text style="font-style: italic" x="25.16953" y="18.28633" font-family="'Segoe UI'" font-size="14" textLength="6.371094" fill="rgb(0,0,0)">x</text><path d="M 36 15 L 46 15 " fill="none" stroke="rgb(0,0,0)" stroke-width="1"></path><path d="M 41 9.8 L 41 19.8 " fill="none" stroke="rgb(0,0,0)" stroke-width="1"></path><path d="M 49.54063 14.3 L 56.71016 14.3 " fill="none" stroke="rgb(0,0,0)" stroke-width="1"></path><text x="49.54063" y="9.962011" font-family="'Segoe UI'" font-size="13.3" textLength="7.169531" fill="rgb(0,0,0)">5</text><text style="font-style: italic" x="50.09912" y="26.26201" font-family="'Segoe UI'" font-size="13.3" textLength="6.052539" fill="rgb(0,0,0)">x</text><path d="M 61 15 L 71 15 " fill="none" stroke="rgb(0,0,0)" stroke-width="1"></path><path d="M 66 9.8 L 66 19.8 " fill="none" stroke="rgb(0,0,0)" stroke-width="1"></path><text x="74.71016" y="18.28633" font-family="'Segoe UI'" font-size="14" textLength="7.546875" fill="rgb(0,0,0)">3</text><rect x="85.25703" y="14.8" width="1" height="1" fill="none" stroke="rgb(0,0,0)" stroke-width="1"></rect><text x="89.25703" y="18.28633" font-family="'Segoe UI'" font-size="14" textLength="17.25391" fill="rgb(0,0,255)">sin</text><path d="M 115 8 A 4 7 0 0 0 115 22" fill="none" stroke="rgb(0,100,0)" stroke-width="1"></path><text style="font-style: italic" x="116.173" y="18.28633" font-family="'Segoe UI'" font-size="14" textLength="6.371094" fill="rgb(0,0,0)">x</text><path d="M 124 22 A 4 7 0 0 0 124 8" fill="none" stroke="rgb(0,100,0)" stroke-width="1"></path></svg>

четверг, 17 ноября 2022 г. в 17:01:56 UTC+3, Anton Makarov:

Oscar Benjamin

unread,
Nov 17, 2022, 9:13:16 AM11/17/22
to sy...@googlegroups.com
On Thu, 17 Nov 2022 at 14:01, Anton Makarov <antonv...@gmail.com> wrote:
>
> I need to convert sympy tree expression to svg, for instance:
> a = sympify("1+2/3")
> b = sympify("x+5/x+3*sin(x)")
> c = a + b
> and i need to convert c expression to svg string

Your question is very ambiguous. Since SVG is an image format then
this implies that you want an image of the expression somehow but
there are many different ways that an expression could be represented
as an image. Do you want an image of the tree or a typeset equation
(like from LaTeX) or something else?

> I don't want an svg file, i want the string. Something like this:
> svg_string = <svg width="134" height="32" viewBox="-1 -1 134 32">
> ...

That's just the contents of an SVG file. If you had an SVG file then
you could just read the file to get the string.

An SVG file represents an image but you haven't said what you want an
image of. Also you don't say *why* you want an SVG image. The usual
reason for wanting SVG is to have an image in a webpage but the
easiest and potentially best way to have a typeset equation in a
webpage (assuming that is what you want) is to use Mathjax in which
case you should just convert your expression to LaTeX:

In [3]: latex(x**2)
Out[3]: 'x^{2}'

--
Oscar

Anton Makarov

unread,
Nov 17, 2022, 9:17:53 AM11/17/22
to sympy
Yes i need typeset equation in a webpage. I know about mathjax, i understand that there is a possibility to convert sympy tree expression to latex and then handle it on client web page and convert to svg with mathjax.
But is there any way to do this on server (not in client web page) in order to send svg result to the client then.

четверг, 17 ноября 2022 г. в 17:13:16 UTC+3, Oscar:

Oscar Benjamin

unread,
Nov 17, 2022, 9:37:20 AM11/17/22
to sy...@googlegroups.com
On Thu, 17 Nov 2022 at 14:17, Anton Makarov <antonv...@gmail.com> wrote:
>
> Yes i need typeset equation in a webpage. I know about mathjax, i understand that there is a possibility to convert sympy tree expression to latex and then handle it on client web page and convert to svg with mathjax.
> But is there any way to do this on server (not in client web page) in order to send svg result to the client then.

You can run Mathjax on the server:
https://github.com/mathjax/MathJax-node

--
Oscar

Anton Makarov

unread,
Nov 17, 2022, 9:46:18 AM11/17/22
to sympy
Yes, but this is not the same server where the sympy runs ...


четверг, 17 ноября 2022 г. в 17:37:20 UTC+3, Oscar:

Aaron Meurer

unread,
Nov 17, 2022, 3:39:13 PM11/17/22
to sy...@googlegroups.com
SymPy doesn't directly support SVG output. Your best bet would be to
find some tool that supports converting LaTeX or MathML to SVG, as
SymPy can export to both of those, or something that converts PDF to
SVG (SymPy can generate a PDF with preview()).

This page suggests some options for converting LaTeX to SVG
https://askubuntu.com/questions/33196/how-to-convert-latex-equations-to-svg.

If one of these tools works well maybe we can add support for it to preview().

Aaron Meurer

On Thu, Nov 17, 2022 at 5:05 AM Anton Makarov <antonv...@gmail.com> wrote:
>
> Hi, i am new to sympy. I need to convert the results of sympy calculations to svg string.
> How can i do this?
>
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/f87eed03-8444-48e5-aff1-effaaf28d432n%40googlegroups.com.

Антон Макаров

unread,
Nov 17, 2022, 3:43:47 PM11/17/22
to sy...@googlegroups.com
Thank you for answer.



Отправлено из мобильной Почты Mail.ru


четверг, 17 ноября 2022 г., 23:39 +0300 от asme...@gmail.com <asme...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages