3

I would like to ask on how to display a math-text in a kivy app..? For example : How to do the same thing as $ \frac{1}{2} $ or $ 2^{3} $ does in LaTeX (omitting the font type, as long as it is math-readable).

I know that in matplotlib.pyplot module, I can use string : r'$ \frac{1}{2} $' or r'$ 2^{3} $' to produce a math-text. But this does not produce the desired result in kivy :

Label(text = r'$\frac{1}{2}$' ) will reproduce $\frac{1}{2}$.

Thanks.

0

3 Answers 3

2

Please Take a look at the sympy module, then in your label you must use a font_name that can display math-text

but If you want to use Latex formula then you have to find a font_name that can display those formula like you want

Update: Otherwise you can generate your Latex in a png then integrate it in your kivy app

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I have just tried the sympy module. I have used one of the syntax and converted it to string, to put it on the Label, but this also does not work. What font_name that can display math-text..? I tried several names..only Arial that does not produce an Error.
try the asana-math you can get it here drive.google.com/open?id=0B3XGn7xdfoSNNDRXZE50dWtXaXM
Thanks again. I have registered it using LabelBase.register. But I don't think this has something to do with sympy.
0

Actually I tried 2 ways to display mathematical notation on Kivy Label: (Each of way includes SymPy or Matplotlib)

1- Use SymPy printing module

from sympy import *
...
init_printing(use_unicode=True)

Do not forget this piece display mathematical notation but you have to use needed font name for Kivy label to display mathematical notation perfectly. I know one, it's "dejavusans mono" yeah this is monospace font.

2- Use Matplotlib. turn to png then display the png on Kivy Image here is good example display math symbols with matplotlib

*(here is sample code):

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.lang.builder import Builder

from sympy import *
x,y,z = symbols('x y z')
init_printing(use_unicode=True)

kiv=Builder.load_string("""
<MainScreen>:
    BoxLayout:
        orientation:'vertical'
        Label:
            id:lbl1
            text:'math-equation'
            markup:True
            #font_name:'dejavusansmono.ttf' ----here your monospace font
        Label:
            id:lbl2
            text:'math-equation'
            markup:True
            #font_name:'dejavusansmono.ttf'  ----here your monospace font
            
        Button:
            id:btn
            text:'Display'
            on_press:root.display()       
""")

class MainScreen(BoxLayout):
    def display(self):
        expression=Derivative(sqrt(x+2)*cos(x+2), x)
        answer=expression.doit()
        good_symbols1=pretty(expression)
        good_symbols2=pretty(answer)

        self.ids.lbl1.text=good_symbols1
        self.ids.lbl2.text=good_symbols2
        
class Example(App):

    def build(self):
        scr=MainScreen()
        return scr

Example().run()

[Out]: enter image description here

Comments

0

I have recently developed a package that allows to include latex equations in a kivy label.

You can install it with :

pip install kivy-latex-label

Here is a quick demo of how to use it :

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy_latex_label import LatexLabel

class DemoApp(App, Widget):

    def build(self):
        Window.clearcolor = (1, 1, 1, 1)

if __name__ == "__main__":
    DemoApp().run()
#:kivy 2.0.0

FloatLayout:
    size_hint: (1,1)

    LatexLabel:
        pos_hint: {"top":1,"x":0.05}
        size_hint:(0.9,1)
        text: r"The SINDy method is a recently developed technique that leverages sparse regression to identify the governing equations from a given time series (Figure 1). We consider a system with state $\boldsymbol{x}(t)=\left[x_{1}(t), x_{2}(t), \ldots x_{d}(t)\right]^{\top} \in$ $\mathbb{R}^{d}$ governed by the differential equation: $\dot{\boldsymbol{x}}=\boldsymbol{f}(\boldsymbol{x})$."
        color: (0,0,0,1)
        text_size: root.size
        valign: "top"
        font_size: 20

demo of latex label in kivy

The project is on github if you want to adapt the code to your application: https://github.com/PaulCreusy/kivy-latex-label

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.