5 Mathematics

§ 5.1 Math Details

LaTeXML processes mathematical material by proceeding through several stages:

  • Basic processing of macros, primitives and constructors resulting in an XML document; the math is primarily represented by a sequence of tokens (XMTok) or structured items (XMApp, XMDual) and hints (XMHint, which are ignored).

  • Document tree rewriting, where rules are applied to modify the document tree. User supplied rules can be used here to clarify the intent of markup used in the document.

  • Math Parsing; a grammar based parser is applied, depth first, to each level of the math. In particular, at the top level of each math expression, as well as each subexpression within structured items (these will have been contained in an XMArg or XMWrap element). This results in an expression tree that will hopefully be an accurate representation of the expression’s structure, but may be ambigous in specifics (eg. what the meaning of a superscript is). The parsing is driven almost entirely by the grammatical role assigned to each item.

  • Not yet implemented a following stage must be developed to resolve the semantic ambiguities by analyzing and augmenting the expression tree.

  • Target conversion: from the internal XM* representation to MathML or OpenMath.

The Math element is a top-level container for any math mode material, serving as the container for various representations of the math including images (through attributes mathimage, width and height), textual (through attributes tex, content-tex and text), MathML and the internal representation itself. The mode attribute specifies whether the math should be in display or inline mode.

§ 5.1.1 Internal Math Representation

The XMath element is the container for the internal representation

The following attributes can appear on all XM* elements:


the grammatical role that this element plays

open, close

parenthese or delimiters that were used to wrap the expression represented by this element.

argopen, argclose, separators

delimiters on an function or operator (the first element of an XMApp) that were used to delimit the arguments of the function. The separators is a string of the punctuation characters used to separate arguments.


a unique identifier to allow reference (XMRef) to this element.

Math Tags

The following tags are used for the intermediate math representation:


represents a math token. It may contain text for presentation. Additional attributes are:


the name that represents the meaning of the token; this overrides the content for identifying the token.


the OpenMath content dictionary that the name belongs to.


the font to be used for presenting the content.






whether scripts should be stacked above/below the item, instead of the usual script position.


represents the generalized application of some function or operator to arguments. The first child element is the operator, the remainig elements are the arguments. Additional attributes:


the name that represents the meaning of the construct as a whole.




combines representations of the content (the first child) and presentation (the second child), useful when the two structures are not easily related.


represents spacing or other apparent purely presentation material.


names the effect that the hint was intended to achieve.




serves to assert the expected type or role of a subexpression that may otherwise be difficult to interpret — the parser is more forgiving about these.






serves to wrap individual arguments or subexpressions, created by structured markup, such as \frac. These subexpressions can be parsed individually.


the grammar rule that this subexpression should match.


refers to another subexpression,. This is used to avoid duplicating arguments when constructing an XMDual to represent a function application, for example. The arguments will be placed in the content branch (wrapped in an XMArg) while XMRef’s will be placed in the presentation branch.


the identifier of the referenced math subexpression.

§ 5.1.2 Grammatical Roles

As mentioned above, the grammar take advantage of the structure (however minimal) of the markup. Thus, the grammer is applied in layers, to sequences of tokens or atomic subexpressions (like a fractions or arrays). It is the role attribute that indicates the syntactic and/or presentational nature of each item. On the one hand, this drives the parsing: the grammar rules are keyed on the role (say, ADDOP), rather than content (say + or -), of the nodes [In some cases, the content is used to distinguish special synthesized roles]. The role is also used to drive the conversion to presentation markup, (say, as an infix operator), especially Presentation MathML. Some values of role are used only in the grammar, some are only used in presentation; most are used both ways.

The following grammatical roles are recognized by the math parser. These values can be specified in the role attribute during the initial document construction or by rewrite rules. Although the precedence of operators is loosely described in the following, since the grammar contains various special case productions, no rigidly ordered precedence is given. Also note that in the current design, an expresssion has only a single role, although that role may be involved in grammatical rules with distinct syntax and semantics (some roles directly reflect this ambiguity).


a general atomic subexpression (atomic at the level of the expression; it may have internal structure);


a variable-like token, whether scalar or otherwise, but not a function;


a number;


a structure with internal components and alignments; typically has a particular syntactic relationship to OPEN and CLOSE tokens.


an unknown expression. This is the default for token elements. Such tokens are treated essential as ID, but generate a warning if it seems to be used as a function.


opening and closing delimiters, group expressions or enclose arguments among other structures;


a middle operator used to group items between an OPEN, CLOSE pair;


punctuation; a period ‘ends’ formula (note that numbers, including floating point, are recognized earlier in processing);


a vertical bar (single or doubled) which serves a confusing variety of notations: absolute values, “at”, divides;


a relational operator, loosely binding;


an arrow operator (with little semantic significance), but generally treated equivalently to RELOP;


an operator used for relations between relations, with lower precedence;


an atomic expression following an object that ‘modifies’ it in some way, such as a restriction (<0) or modulus expression;


an operator (such as mod) between two expressions such that the latter modifies the former;


an addition operator, between RELOP and MULOP operators in precedence;


a multiplicative operator, high precedence than ADDOOP;


a generic infix operator, can act as either an ADDOP or MULOP, typically used for cases wrapped in \mathbin;


An operator appearing in a superscript, such as a collection of primes, or perhaps a T for transpose. This is distinct from an expression in a superscript with an implied power or index operator;


for a prefix operator;


for a postfix operator;


a function which (may) apply to following arguments with higher precedence than addition and multiplication, or to parenthesized arguments (enclosed between OPEN,CLOSE);


a variant of FUNCTION which doesn’t require fenced arguments;


a variant of OPFUNCTION with special rules for recognizing which following tokens are arguments and which are not;


an explicit infix application operator (high precedence);


an infix operator that composes two FUNCTION’s (resulting in another FUNCTION);


a general operator; higher precedence than function application. For example, for an operator A, and function F, AFx would be interpretted as (A(F))(x);


a summation/union, integral, limiting, differential or general purpose operator. These are treated equivalently by the grammar, but are distinguished to facilitate (eventually) analyzing the argument structure (eg bound variables and differentials within an integral). Note are SUMOP and LIMITOP significantly different in this sense?


intermediate form of sub- and superscript, roughly as TeX processes them. The script is (essentially) treated as an argument but the base will be determined by parsing.


A special case for a sub- and superscript on an empty base, ie. {}^{x}. It is often used to place a pre-superscript or for non-math uses (eg. 10${}^{th});

The following roles are not used in the grammar, but are used to capture the presentation style; they are typically used directly in macros that construct structured objects, or used in representing the results of parsing an expression.


corresponds to stacked structures, such as \atop, and the presentation of binomial coefficients.


after parsing, the operator involved in various sub/superscript constructs above will be comverted to these;


these are special cases of the above that indicate the 2nd operand acts as an accent (typically smaller), expressions using these roles are usually directly constructed for accenting macros;


this operator is used to represent containers enclosed by OPEN and CLOSE, possibly with punctuation, particularly when no semantic is known for the construct, such as an arbitrary list.

The content of a token is actually used in a few special cases to distinguish distinct syntactic constructs, but these roles are not assigned to the role attribute of expressions:


recognizes use of < and > in the bra-ket notation used in quantum mechanics;


recognizes use of { and } on either side of stacked or array constructions representing various kinds of cases or choices;


recognizes the use of { in opening specialized set notations.