Documentation version dev
Documentation
On this page

Conversion options

The source and target formats of a conversion and every option they take: math delimiters, equation labels and carried TeX.

You are reading the documentation of the development version on the dev branch, which is not released. The latest release is 0.1.0a4: this page in 0.1.0a4.

Formats#

Each conversion has a source format, the format of its input, and a target format, the format of its output. The source format is mathml, and the target formats are latex and text. The defaults are mathml and latex.

import mathconv

mathml = "<math><msqrt><msup><mi>x</mi><mn>2</mn></msup><mo>+</mo><mn>1</mn></msqrt></math>"
mathconv.convert(mathml, source="mathml", target="latex")
# '\\sqrt{x^2 + 1}'
mathconv.convert(mathml, target="text")
# 'sqrt(x^2 + 1)'

Options#

Each option belongs to a format and is passed as a keyword argument to Converter or mathconv.convert. Passing an option that does not belong to the chosen formats raises ValueError.

OptionBelongs toValuesDefault
delimiterslatex"none", "dollar", "paren""none"
drop_labelsmathmlTrue, FalseFalse
prefer_carried_texmathml, with target latexTrue, FalseFalse

delimiters#

delimiters selects the math delimiters around the output. Delimiters mark where a formula starts and ends inside a document, and the formula without them is its body.

ValueInline formulaDisplay formula
"none"the body alonethe body alone
"dollar"$…$$$…$$
"paren"\(…\)\[…\]

The delimiters follow from the formula's display flag, so inline and display formulas in one batch each get their own:

import mathconv

formulas = ["<math><mi>a</mi></math>", '<math display="block"><mi>b</mi></math>', "<math></math>"]
mathconv.Converter().convert_batch(formulas)
# ['a', 'b', '']
mathconv.Converter(delimiters="dollar").convert_batch(formulas)
# ['$a$', '$$b$$', '']
mathconv.Converter(delimiters="paren").convert_batch(formulas)
# ['\\(a\\)', '\\[b\\]', '']

A formula with an empty body gives empty output, whatever the option, as rule D1 of the style states. The text target writes no delimiters and does not take the option.

drop_labels#

drop_labels=True leaves out the labels of table rows, such as equation numbers, for callers that number equations themselves. By default each label is written at the end of its line. In LaTeX, a label of a display formula that the table makes up is a \tag*, and any other label follows its line after a \qquad; in text, it follows its line after a space.

import mathconv

numbered = '<math display="block"><mtable><mlabeledtr><mtd><mtext>(1)</mtext></mtd><mtd><mi>E</mi><mo>=</mo><mi>m</mi><msup><mi>c</mi><mn>2</mn></msup></mtd></mlabeledtr></mtable></math>'
mathconv.convert(numbered)
# 'E = mc^2 \\tag*{(1)}'
mathconv.convert(numbered, drop_labels=True)
# 'E = mc^2'
mathconv.convert(numbered, target="text")
# 'E = mc^2 (1)'
mathconv.convert(numbered, target="text", drop_labels=True)
# 'E = mc^2'

Using the LaTeX output explains how labels are written and rendered.

prefer_carried_tex#

Many MathML formulas are generated from LaTeX and keep the LaTeX source alongside the markup, in an annotation element inside semantics or in the alttext attribute of math. LaTeX found in either place is carried TeX. With prefer_carried_tex=True, mathconv returns the carried TeX as its author wrote it instead of converting the markup, when the formula has suitable carried TeX, and converts the markup otherwise. Result.taken_from says which happened.

import mathconv

carried = '<math><semantics><msup><mi>x</mi><mn>2</mn></msup><annotation encoding="application/x-tex">x^{2}</annotation></semantics></math>'
mathconv.convert(carried)
# 'x^2'
mathconv.convert(carried, prefer_carried_tex=True)
# 'x^{2}'
result = mathconv.Converter(prefer_carried_tex=True, delimiters="dollar").result(
    '<math display="block" alttext="\\[ E=mc^2 \\]"><mi>E</mi><mo>=</mo><mi>m</mi><msup><mi>c</mi><mn>2</mn></msup></math>'
)
result.output, result.taken_from
# ('$$E=mc^2$$', 'alttext')

Carried TeX is found in these places, and the first suitable one is taken:

  1. An annotation element whose encoding is one of application/x-tex, application/x-latex, application/tex, text/x-tex, text/x-latex, text/tex, TeX or LaTeX, compared case-insensitively and ignoring surrounding whitespace. It counts only when its semantics element covers the whole formula: it is the only child of math, or the only child of an mrow that is the only child of math.
  2. The alttext attribute of math.

Carried TeX is first stripped of surrounding whitespace and of one pair of surrounding delimiters ($…$, $$…$$, \(…\) or \[…\]). It is suitable when the stripped text is not empty and contains no control character and nothing that looks like the start of a markup tag: < followed by a letter, /, ! or ?. When the output is wrapped in delimiters, it must also not break the closing delimiter: it may not end in an unpaired backslash or contain an unescaped %, and between dollar signs it may not contain an odd number of unescaped $.

The display flag still comes from the math element, and the delimiters option applies to carried TeX as to converted markup. A result taken from carried TeX has no fallbacks. Carried TeX is returned unchanged, so it has none of the guarantees of converted output: it may use macros defined elsewhere in its document, and renderers may reject it. The option belongs to the latex target only.

Edit this page on GitHub