Text::ASCIIMathML - Perl extension for parsing ASCIIMathML text into MathML
SYNOPSIS
use Text::ASCIIMathML;
$parser=new ASCII::MathML();
$parser->SetAttributes(ForMoz => 1);
$mathML = $parser->TextToMathML($ASCIIMathML);
$mathML = $parser->TextToMathML($ASCIIMathML, [title=>$ASCIIMathML]);
$mathML = $parser->TextToMathML($ASCIIMathML, undef, [displaystyle=>1]);
DESCRIPTION
Text::ASCIIMathML is a parser for ASCIIMathML text which produces MathML XML markup strings that are suitable for rendering by any MathML-compliant browser.
The parser uses the following attributes which are settable through the SetAttributes method:
ForMoz
-
Specifies that the fonts should be optimized for Netscape/Mozilla/Firefox.
The output of the TextToMathML method always follows the schema <math><mstyle>...</mstyle></math> The first argument of TextToMathML is the ASCIIMathML text to be parsed into MathML. The second argument is a reference to an array of attribute/value pairs to be attached to the <math> node and the third argument is a reference to an array of attribute/value pairs for the <mstyle> node. Common attributes for the <math> node are "title" and "xmlns"=>"&mathml;". Common attributes for the <mstyle> node are "mathcolor" (for text color), "displaystyle"=>"true" for using display style instead of inline style, and "fontfamily".
ASCIIMathML markup
The syntax is very permissive and does not generate syntax errors. This allows mathematically incorrect expressions to be displayed, which is important for teaching purposes. It also causes less frustration when previewing formulas.
If you encode 'x^2' or 'a_(mn)' or 'a_{mn}' or '(x+1)/y' or 'sqrtx', you pretty much get what you expect. The choice of grouping parenthesis is up to you (they don't have to match either). If the displayed expression can be parsed uniquely without them, they are omitted.
The parser uses no operator precedence and only respects the grouping brackets, subscripts, superscript, fractions and (square) roots. This is done for reasons of efficiency and generality. The resulting MathML code can quite easily be processed further to ensure additional syntactic requirements of any particular application.
The grammar
Here is a definition of the grammar used to parse ASCIIMathML expressions. In the Backus-Naur form given below, the letter on the left of the ::=
represents a category of symbols that could be one of the possible sequences of symbols listed on the right. The vertical bar |
separates the alternatives.
c ::= [A-z] | numbers | greek letters | other constant symbols
(see below)
u ::= 'sqrt' | 'text' | 'bb' | other unary symbols for font commands
b ::= 'frac' | 'root' | 'stackrel' binary symbols
l ::= ( | [ | { | (: | {: left brackets
r ::= ) | ] | } | :) | :} right brackets
S ::= c | lEr | uS | bSS | "any" simple expression
E ::= SE | S/S |S_S | S^S | S_S^S expression (fraction, sub-,
super-, subsuperscript)
The translation rules
Each terminal symbol is translated into a corresponding MathML node. The constants are mostly converted to their respective Unicode symbols. The other expressions are converted as follows:
lSr -> <mrow>lSr</mrow>
(note that any pair of brackets can be used to
delimit subexpressions, they don't have to match)
sqrt S -> <msqrt>S'</msqrt>
text S -> <mtext>S'</mtext>
"any" -> <mtext>any</mtext>
frac S1 S2 -> <mfrac>S1' S2'</mfrac>
root S1 S2 -> <mroot>S2' S1'</mroot>
stackrel S1 S2 -> <mover>S2' S1'</mover>
S1/S2 -> <mfrac>S1' S2'</mfrac>
S1_S2 -> <msub>S1 S2'</msub>
S1^S2 -> <msup>S1 S2'</msup>
S1_S2^S3 -> <msubsup>S1 S2' S3'</msubsup> or
<munderover>S1 S2' S3'</munderover> (in some cases)
In the rules above, the expression S'
is the same as S
, except that if S
has an outer level of brackets, then S'
is the expression inside these brackets.
Matrices
A simple syntax for matrices is also recognized:
l(S11,...,S1n),(...),(Sm1,...,Smn)r
or
l[S11,...,S1n],[...],[Sm1,...,Smn]r.
Here l
and r
stand for any of the left and right brackets (just like in the grammar they do not have to match). Both of these expressions are translated to
<mrow>l<mtable><mtr><mtd>S11</mtd>...
<mtd>S1n</mtd></mtr>...
<mtr><mtd>Sm1</mtd>...
<mtd>Smn</mtd></mtr></mtable>r</mrow>.
Note that each row must have the same number of expressions, and there should be at least two rows.
Tokenization
The input formula is broken into tokens using a "longest matching initial substring search". Suppose the input formula has been processed from left to right up to a fixed position. The longest string from the list of constants (given below) that matches the initial part of the remainder of the formula is the next token. If there is no matching string, then the first character of the remainder is the next token. The symbol table at the top of the ASCIIMathML.js script specifies whether a symbol is a math operator (surrounded by a <mo>
tag) or a math identifier (surrounded by a <mi>
tag). For single character tokens, letters are treated as math identifiers, and non-alphanumeric characters are treated as math operators. For digits, see "Numbers" below.
Spaces are significant when they separate characters and thus prevent a certain string of characters from matching one of the constants. Multiple spaces and end-of-line characters are equivalent to a single space.
Numbers
A string of digits, optionally followed by a decimal point (a period) and another string of digits, is parsed as a single token and converted to a MathML number, i.e., enclosed with the <mn>
tag.
Greek letters
- Lowercase letters
-
alpha
beta
chi
delta
epsilon
eta
gamma
iota
kappa
lambda
mu
nu
omega
phi
pi
psi
rho
sigma
tau
theta
upsilon
xi
zeta
- Uppercase letters
-
Delta
Gamma
Lambda
Omega
Phi
Pi
Psi
Sigma
Theta
Xi
- Variants
-
varepsilon
varphi
vartheta
Standard functions
sin cos tan csc sec cot sinh cosh tanh log ln det dim lim mod gcd lcm min max
Operation symbols
Type Description Entity
+ + +
- - -
* Mid dot ⋅
** Star ⋆
// / /
\\ \ \
xx Cross product ×
-: Divided by ÷
@ Compose functions ∘
o+ Circle with plus ⊕
ox Circle with x ⊗
o. Circle with dot ⊙
sum Sum for sub- and superscript ∑
prod Product for sub- and superscript ∏
^^ Logic "and" ∧
^^^ Logic "and" for sub- and superscript ⋀
vv Logic "or" ∨
vvv Logic "or" for sub- and superscript ⋁
nn Logic "intersect" ∩
nnn Logic "intersect" for sub- and superscript ⋂
uu Logic "union" ∪
uuu Logic "union" for sub- and superscript ⋃
Relation symbols
Type Description Entity
= = =
!= Not equals ≠
< < <
> > >
<= Less than or equal ≤
>= Greater than or equal ≥
-lt Precedes ≺
>- Succeeds ≻
in Element of ∈
!in Not an element of ∉
sub Subset ⊂
sup Superset ⊃
sube Subset or equal ⊆
supe Superset or equal ⊇
-= Equivalent ≡
~= Congruent to ≅
~~ Asymptotically equal to ≈
prop Proportional to ∝
Logical symbols
Type Description Entity
and And " and "
or Or " or "
not Not ¬
=> Implies ⇒
if If " if "
iff If and only if ⇔
AA For all ∀
EE There exists ∃
_|_ Perpendicular, bottom ⊥
TT Top ⊤
|-- Right tee ⊢
|== Double right tee ⊨
Grouping brackets
Type Description Entity
( ( (
) ) )
[ [ [
] ] ]
{ { {
} } }
(: Left angle bracket ⟨
:) Right angle bracket ⟩
{: Invisible left grouping element
:} Invisible right grouping element
Miscellaneous symbols
Type Description Entity
int Integral ∫
oint Countour integral ∮
del Partial derivative &del;
grad Gradient ∇
+- Plus or minus ±
O/ Null set ∅
oo Infinity ∞
aleph Hebrew letter aleph ℵ
/_ Angle ∠
:. Therefore ∴
... Ellipsis ...
cdots Three centered dots ⋯
\<sp> Non-breaking space (<sp> means space)
quad Quad space
diamond Diamond ⋄
square Square □
|__ Left floor ⌊
__| Right floor ⌋
|~ Left ceiling ⌈
~| Right ceiling ⌉
CC Complex numbers ℂ
NN Natural numbers ℕ
QQ Rational numbers ℚ
RR Real numbers ℝ
ZZ Integers ℤ
Arrows
Type Description Entity
uarr Up arrow ↑
darr Down arrow ↓
rarr Right arrow →
-> Right arrow →
larr Left arrow ←
harr Horizontal (two-way) arrow ↔
rArr Right double arrow ⇒
lArr Left double arrow ⇐
hArr Horizontal double arrow ⇔
Accents
Type Description Output
hat x Hat over x <mover><mi>x</mi><mo>^</mo></mover>
bar x Bar over x <mover><mi>x</mi><mo>¯</mo></mover>
ul x Underbar under x <munder><mi>x</mi><mo>_</mo></munder>
vec x Right arrow over x <mover><mi>x</mi><mo>→</mo><mover>
dot x Dot over x <mover><mi>x</mi><mo>.</mo><mover>
ddot x Double dot over x <mover><mi>x</mi><mo>..</mo><mover>
Font commands
Type Description
bb A Bold A
bbb A Double-struck A
cc A Calligraphic (script) A
tt A Teletype (monospace) A
fr A Fraktur A
sf A Sans-serif A
BUGS AND SUGGESTIONS
If you find bugs, think of anything that could improve Text::ASCIIMathML or have any questions related to it, feel free to contact the author.
AUTHOR
Mark Nodine <mnodine@alum.mit.edu>
SEE ALSO
MathML::Entities,
<http://www1.chapman.edu/~jipsen/mathml/asciimathsyntax.xml>
COPYRIGHT
The Text::ASCIIMathML module is copyright (c) 2006 Mark Nodine, USA. All rights reserved.
You may use and distribute them under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file.
1 POD Error
The following errors were encountered while parsing the POD:
- Around line 3:
=pod directives shouldn't be over one line long! Ignoring all 2 lines of content