import pytest
from lxml import etree
from conftest import PNG, SVG, Book, Doc
from epub2md.mathml import Unsupported, mathml_to_tex
M = 'xmlns="http://www.w3.org/1998/Math/MathML"'
def tex(xml: str) -> str:
return mathml_to_tex(etree.fromstring(f""))
@pytest.mark.parametrize(
("xml", "expected"),
[
("ab", r"\frac{a}{b}"),
("x2", "x^{2}"),
("ai", "a_{i}"),
("xi2", "x_{i}^{2}"),
("x", r"\sqrt{x}"),
("x3", r"\sqrt[3]{x}"),
("∑i=1nai",
r"\sum_{i = 1}^{n} a_{i}"),
("α≤β", r"\alpha \leq \beta"),
("x^", r"\hat{x}"),
("R", r"\mathbb{R}"),
("für alle", r"\text{für alle}"),
("(10"
"01)",
r"\left( \begin{matrix} 1 & 0 \\ 0 & 1 \end{matrix} \right)"),
],
)
def test_mathml_to_tex(xml, expected):
assert tex(xml) == expected
def test_tex_annotation_is_preferred():
xml = ('x\\mathbf{x}')
assert tex(xml) == r"\mathbf{x}"
@pytest.mark.parametrize("xml", ["x", "x",
"1"])
def test_unsupported_mathml_raises(xml):
with pytest.raises(Unsupported):
tex(xml)
BODY = (
"
Formeln
"
f'Inline im Satz.
'
f' (3.7)
'
f'Unbekannt:
'
'SVG: 
'
'Bild ohne TeX: 
'
'Bild mit TeX: 
'
)
def math_book() -> Book:
return Book(docs=[Doc("m.xhtml", BODY)], media={"f.svg": SVG, "eq.png": PNG, "eq2.png": PNG})
def test_math_auto(convert):
run = convert(math_book())
text = run.chapter("001")
assert r"Inline $\frac{a}{b}$ im Satz." in text
assert "$$\nx^{2}\n$$" in text
assert "(3.7)" in text # equation number visible
assert "' in text
assert "frac{" not in text
def test_math_image_mode(convert):
run = convert(math_book(), "--math", "image")
text = run.chapter("001")
assert text.count("![") == 3 # svg + both formula images
assert "