2

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.

2 Answers2

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

Simon Mengong
  • 2,625
  • 10
  • 22
  • 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. –  Oct 30 '17 at 16:28
  • try the asana-math you can get it here https://drive.google.com/open?id=0B3XGn7xdfoSNNDRXZE50dWtXaXM – Simon Mengong Oct 30 '17 at 16:37
  • Thanks again. I have registered it using `LabelBase.register`. But I don't think this has something to do with `sympy`. –  Nov 03 '17 at 16:26
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

berk berk
  • 36
  • 3