py_asciimath.translator.translator

class py_asciimath.translator.translator.ASCIIMath2MathML(log=False, **kwargs)

Bases: py_asciimath.translator.translator.LarkTranslator

Class that handle the translation from ASCIIMath to MathML

Parameters:
  • inplace (bool, optional) – If True, parse the input inplace. See Lark. Defaults to True.
  • lexer (str, optional) – Lexer used during parsing. See Lark. Defaults to “contextual”.
  • log (bool, optional) – If True log the parsing process. Defaults to False.
  • parser (str, optional) – Parser algorithm. See Lark. Defaults to “lalr”.
  • **kwargs – Additional keyword arguments to the Lark class.
translate(exp, displaystyle=False, dtd=None, dtd_validation=False, from_file=False, output='string', network=False, pprint=False, to_file=None, xml_declaration=False, xml_pprint=True, **kwargs)

Translates an ASCIIMath string to MathML

Parameters:
  • exp (str) – String to translate. If from_file is True, then s must represent the file’s path
  • displaystyle (bool, optional) – Add displaystyle attribute. Defaults to False.
  • dtd (str, optional) – MathML DTD version to validate the output against. It can be: mathml1, mathml2 or mathml3. Defaults to None.
  • dtd_validation (bool, optional) – If True validate output against the DTD version specified by dtd. By default, if one of dtd, dtd_validation, xml_declaration or xml_pprint is True or is not None, then dtd_validation will be set to True. This is because if either one of them set to True, then py_asciimath must parse the input XML, but in order to do that it needs to know how to intepret the entities from the DTD. Defaults to False.
  • from_file (bool, optional) – If True, load the string to translate from the file specified by s. Defaults to False.
  • network (bool, optional) – If True validate the output against a remote DTD. Defaults to False.
  • output (str, optional) – Output mode: string to return the string representation of the MathML-converted exxpression, etree to return a lxml.etree.ElementTree object. Defaults to False.
  • pprint (bool, optional) – Abstract Syntax Tree pretty print. Defaults to False.
  • to_file (str, optional) – If specified, save the translation to to_file. Defaults to None.
  • xml_declaration (bool, optional) – If True, include the XML declaration at the beginning of the file. Defaults to False.
  • xml_pprint (bool, optional) – XML pretty print. Defaults to True.
  • **kwargs – Additional ~lxml.extree.XMLParser options
Returns:

MathML translated expression

Return type:

str

class py_asciimath.translator.translator.ASCIIMath2Tex(log=False, **kwargs)

Bases: py_asciimath.translator.translator.LarkTranslator

Class that handle the translation from ASCIIMath to LaTeX

Parameters:
  • inplace (bool, optional) – If True, parse the input inplace. See Lark. Defaults to True.
  • lexer (str, optional) – Lexer used during parsing. See Lark. Defaults to “contextual”.
  • log (bool, optional) – If True log the parsing process. Defaults to False.
  • parser (str, optional) – Parser algorithm. See Lark. Defaults to “lalr”.
  • **kwargs – Additional keyword arguments to the Lark class.
translate(exp, displaystyle=False, from_file=False, pprint=False, to_file=None)

Translates an ASCIIMath string to LaTeX

Parameters:
  • exp (str) – String to translate. If from_file is True, then s must represent the file’s path
  • displaystyle (bool, optional) – Add displaystyle attribute. Defaults to False.
  • from_file (bool, optional) – If True, load the string to translate from the file specified by s. Defaults to False.
  • pprint (bool, optional) – Abstract Syntax Tree pretty print. Defaults to False.
  • to_file (str, optional) – If specified, save the translation to to_file. Defaults to None.
Returns:

LaTeX translated expression

Return type:

str

class py_asciimath.translator.translator.LarkTranslator(grammar, transformer, inplace=True, lexer='contextual', parser='lalr', **kwargs)

Bases: py_asciimath.translator.translator.Translator

Class that handle the translation from a Lark parsed language to the one specified by a Transformer.

A LarkTranslator translates a string, parsed with Lark, into another language, specified by the transformer parameter

Parameters:
  • grammar (str) – BNF grammar to parse the input
  • transformer (lark.Transformer) – A transformer instance to transform parsed input. See Transformer
  • inplace (bool, optional) – If True, parse the input inplace. See Lark. Defaults to True.
  • lexer (str, optional) – Lexer used during parsing. See Lark. Defaults to “contextual”.
  • parser (str, optional) – Parser algorithm. See Lark. Defaults to “lalr”.
  • **kwargs – Additional keyword arguments to the Lark class.
translate(exp, from_file=False, to_file=None, pprint=False, **kwargs)

Translates an input expression exp applying the transformation specified by self.transformer

Parameters:
  • exp (str) – String to translate. If from_file is True, then s must represent the file’s path
  • from_file (bool, optional) – If True, load the string to translate from the file specified by s. Defaults to False.
  • to_file (str, optional) – If specified, save the translation to to_file. Defaults to None.
  • pprint (bool, optional) – Abstract Syntax Tree pretty print. Defaults to False.
Returns:

Translated expression

Return type:

str

class py_asciimath.translator.translator.MathML2Tex

Bases: py_asciimath.translator.translator.Translator

Class that handle the translation from MathML to LaTeX

The translation from MathML to LaTeX is done via the XSLT provided by https://sourceforge.net/projects/xsltml/

translate(exp, from_file=False, network=False, to_file=None, **kwargs)

Translates a MathML string to LaTeX

Parameters:
  • exp (str) – String to translate. If from_file is True, then s must represent the file’s path
  • from_file (bool, optional) – If True, load the string to translate from the file specified by s. Defaults to False.
  • network (bool, optional) – If True validate the output against a remote DTD. Defaults to False.
  • to_file (str, optional) – If specified, save the translation to to_file. Defaults to None.
  • **kwargs – ~lxml.extree.XMLParser options
Returns:

LaTeX translated expression

Return type:

str

class py_asciimath.translator.translator.Tex2ASCIIMath(log=False, **kwargs)

Bases: py_asciimath.translator.translator.LarkTranslator

Class that handle the translation from LaTeX to ASCIIMath

Parameters:
  • inplace (bool, optional) – If True, parse the input inplace. See Lark. Defaults to True.
  • lexer (str, optional) – Lexer used during parsing. See Lark. Defaults to “contextual”.
  • log (bool, optional) – If True log the parsing process. Defaults to False.
  • parser (str, optional) – Parser algorithm. See Lark. Defaults to “lalr”.
  • **kwargs – Additional keyword arguments to the Lark class.
translate(exp, from_file=False, pprint=False, to_file=None)

Translates an ASCIIMath string to LaTeX

Parameters:
  • exp (str) – String to translate. If from_file is True, then s must represent the file’s path
  • from_file (bool, optional) – If True, load the string to translate from the file specified by s. Defaults to False.
  • pprint (bool, optional) – Abstract Syntax Tree pretty print. Defaults to False.
  • to_file (str, optional) – If specified, save the translation to to_file. Defaults to None.
Returns:

LaTeX translated expression

Return type:

str

class py_asciimath.translator.translator.Translator

Bases: object

Abstract Translator class

Abstract class of type Translator. Every subclass must implement the _translate(self, exp, **kwargs) method in order to correctly expose the translate(exp, **kwargs) method

translate(exp, from_file=False, to_file=None, **kwargs)

Translates an input expression s

Parameters:
  • exp (str) – String to translate. If from_file is True, then s must represent the file’s path
  • from_file (bool, optional) – If True, load the string to translate from the file specified by s. Defaults to False.
  • to_file (str, optional) – If specified, save the translation to to_file. Defaults to None.
Returns:

Translated expression

Return type:

str