Put mathjax fraction in addon

Good afternoon guys, I was trying to make an addon that used mathjax, in it I would put a math fraction, as shown in the image

image

In Anki just put this code and it works
\ce{\frac{3}{4}}
\frac{3}{4}

But when I put it in the Python code, it doesn’t work, would anyone have any tips on how to make it work?

1 Like

Could you post the code you have that doesn’t work? And describe what happens in more detail.

1 Like

I wanted to put mathjax inside a dialog box in an Anki addon, to show the fraction inside, but when I put it in an addon, unfortunately it doesn’t show.
I don’t know if you need to import a library, so if anyone knows, please let me know, thank you

I had to make the code putting these   which are spaces in html, so that one number can be placed on top of another

 def show_formula_arranjosimples(self):
 n = random.randint(3, 10)
 p = random.randint(1, n-1)

 example_text = (
 f"<span style='color: red;'>Combinatorial Analysis -> Simple Arrangement</span><br>"
 f"How many arrangements are there of {n} elements taken from {p} into {p}?<br><br>"
 f"An,k = n!<br>"
 f"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_____<br>"
 f"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(n-k)!<br><br>"
 f"An,k = {n}!<br>"
 f"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_____<br>"
 f"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;({n}-{p})!<br><br>"
 f"An,k = {n}!<br>"
 f"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_____<br>"
 f"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{n-p}!<br><br>"
 f"An,k = {n} x {' x '.join(str(i) for i in range(n-1, n-p, -1))} = {self.calculate_arrangement(n, p)}"
 )

 QMessageBox.information(self, "Simple Arrangement Example", example_text)

 def calculate_arrangement(self, n, p):
 result = 1
 for i in range(n, n-p, -1):
 result *= i
 return result

########################################

When I try to put mathjax inside a dialog box, it doesn’t work, I tried that and it didn’t work

 f"How many arrangements are there of {n} elements taken from {p} into {p}?<br><br>"
 f"An,k = \\(\\left(\\frac{{{n!}}}{{{({n}-{p})!}}}\\right)\\)<br> "

########################################

When I put mathjax to work within the front and back fields, it worked normally, as you can see in the code below

def insert_produto_potenciacao_fracao(editor):

 # Generate random numbers for numerator, denominator and exponent
 numerator = random.randint(1, 9)
 denominator = random.randint(1, 9)
 while numerator == denominator:
 denominator = random.randint(1, 9)

 exponent = random.randint(-3, 3)
 while exponent == 0:
 exponent = random.randint(-3, 3)

 # Assembling the expression to the front of the letter
 front_text = (f"<b>Power - Fraction</b><br>"
 f"<font color='blue'>Solve the following potentiation:<br>\\(\\left(\\frac{{{numerator}}}{{{denominator}}}\\right)^{{{ exponent}}}\\)</font>")

 # Calculating development
 if exponent > 0:
 num_exp = numerator ** exponent
 denominator_exp = denominator ** exponent
 back_text = (f"<b>Development:</b><br>"
 f"\\(\\left(\\frac{{{numerator}}}{{{denominator}}}\\right)^{{{exponent}}} = \\frac{{{numerator}^{{ {exponent}}}}}{{{denominator}^{{{exponent}}}}} = \\frac{{{num_exp}}}{{{denom_exp}}}\\) ")
 else:
 inverse_numerator = denominator
 inverse_denominator = numerator
 positive_exponent = abs(exponent)
 num_exp = inverse_numerator ** positive_exponent
 denom_exp = inverse_denominator ** positive_exponent
 back_text = (f"<b>Development:</b><br>"
 f"\\(\\left(\\frac{{{numerator}}}{{{denominator}}}\\right)^{{{exponent}}} = \\left(\\frac{{{inverse_numerator }}}{{{inverse_denominator}}}\\right)^{{{positive_exponent}}} = "
 f"\\frac{{{inverse_numerator}^{{{positive_exponent}}}}}{{{inverse_denominator}^{{{positive_exponent}}}}} = \\frac{{{num_exp}}}{{{denom_exp }}}\\)")

Anki’s mathjax rendering happens only within the card template or note editor. If you want the mathjax to be rendered anywhere else, you will indeed need to import a mathjax library in your addon and process the text with it. A quick google got me this suggesting to use matplotlib.

Anki’s internal mathjax rendering is done within webviews using the mathjax javascript package so using it in your python code would (probably?) require using webviews instead PyQt elements.

3 Likes

Thanks, I’ll try to do that

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.