FAQ and troubleshooting
Answers to common questions about converting MathML with mathconv: whole documents, empty output, renderer errors and unexpected LaTeX.
How do I convert the formulas of a whole document?#
mathconv converts one formula per input, so find the math elements
first and pass each one as a string. For XML and XHTML documents, the
standard library is enough:
import xml.etree.ElementTree as ET
import mathconv
page = """<html xmlns="http://www.w3.org/1999/xhtml"><body><p>
Let <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>x</mi><mn>2</mn></msup></math> be positive:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mn>1</mn><mi>n</mi></mfrac></math>
</p></body></html>"""
formulas = [
ET.tostring(element, encoding="unicode")
for element in ET.fromstring(page).iter("{http://www.w3.org/1998/Math/MathML}math")
]
mathconv.Converter(delimiters="dollar").convert_batch(formulas)
# ['$x^2$', '$$\\frac{1}{n}$$']
ElementTree writes the elements with a generated namespace prefix,
such as ns0:, and adds the text that follows each element; mathconv
removes the prefixes and ignores the text after the formula. For HTML
pages, use an HTML parser, such as the one of lxml or
BeautifulSoup, and pass the markup of each math element in the same
way.
Why is the output empty?#
The input had no math element, and the result has the fallback
no-math. The usual cause is markup that was escaped before it reached
mathconv, such as <math> read from HTML source as text: unescape
it first, for example with html.unescape. An empty math element
also gives empty output, without a fallback.
import html
import mathconv
escaped = "<math><mi>x</mi></math>"
mathconv.Converter().result(escaped).fallbacks[0].code
# 'no-math'
mathconv.convert(html.unescape(escaped))
# 'x'
Why does KaTeX reject \tag* or align*?#
KaTeX accepts \tag*, align* and gather* only in display mode, and
mathconv writes them only in display formulas. Render each formula in
the mode that Result.display gives, or convert with the delimiters
option and let the delimiters select the mode, as
Using the LaTeX output
describes.
Why does the output differ from the LaTeX in the source document?#
mathconv writes every formula in one canonical style, whatever tool
produced its MathML, so its LaTeX can differ in spelling from the LaTeX
an author typed, such as \leq for \le or x^2 for x^{2}, while
showing the same formula. When the MathML carries its LaTeX source in
an annotation or in alttext and you want that source,
convert with prefer_carried_tex=True.
Why is a character written in \text{…}?#
The character has no LaTeX command, so it is written as text, and the
result has the fallback unmapped-character with the character as its
detail. KaTeX and MathJax show it when the browser's fonts have it.
Characters without a command
gives the rules.
Where do I report a wrong conversion?#
Open an issue with the wrong-conversion form. Contributing lists what makes a report most useful.