AsciiMath PHP
Fork me on GitHub

About

AsciiMath is an easy-to-write markup language for mathematics. Try it:


Input:
Rendering:
`sum_(i=1)^n i^3=((n(n+1))/2)^2`

Getting Started

  1. Download AsciiMath.php from GitHub and include it in your project.

    require_once ('asciimath.php');
    $am = new AMserver();
  2. Some versions of Chrome don't offer a Math font by default. Include this link in your header:

    <link href="https://fonts.googleapis.com/css2?family=STIX Two Math" rel="stylesheet">
    
  3. Add mathematics anywhere in your web pages:

    $euler = "e^i pi + 1 = 0";
    echo "<p>Euler's Identity is {$am->parseMath($euler)}.</p>";
    

    $am->parseMath($formula, $inline) has a second boolean parameter which defaults true. Inline formulas render directly within a sentence. Set false to render block (or display) formulas as centered, standalone paragraphs. This is equivalent to MathJax's \[ and \( brackets.

Syntax

Most AsciiMath symbols attempt to mimic in text what they look like rendered, like oo for `oo`. Many symbols can also be displayed using a TeX alternative, but a preceeding backslash is not required.

Operation Symbols
TypeTeX altSee
++
-
*cdot
**ast
***star
///
setminusbackslash\
xxtimes×
-:div:
|><times
><|rtimes
|><|bowtie
@circ
o+oplus
oxotimes
o.odot
sum
prod
^^wedge
^^^bigwedge
vvvee
vvvbigvee
nncap
nnnbigcap
uucup
uuubigcup
dagdagger
ddagddagger
hbar
frown
Miscellaneous Symbols
TypeTex altSee
2/3frac{2}{3}23
2^323
sqrt xx
root(3)(x)x3
int
oint
delpartial
gradnabla
+-pm±
O/emptyset
ooinfty
aleph
:.therefore
:'because
|...||ldots||...|
|cdots|||
vdots
ddots
|\ |
|quad||  |
/_angle
frown
/_\triangle
diamond
square
|__lfloor
__|rfloor
|~lceiling
~|rceiling
CC
NN
QQ
RR
ZZ
"hi"text(hi)hi
Relation Symbols
TypeTeX altSee
==
!=ne
<lt<
>gt>
<=le
>=ge
mltll
mgtgg
-<prec
-<=preceq
>-succ
>-=succeq
in
!innotin
subsubset
!sub
supsupset
!sup
supesupseteq
!supe
subesupseteq
!sube
-=equiv
!-=notequiv
-=equiv
~=cong
~~approx
proppropto
rightleftharpoons
Logical Symbols
TypeTeX altSee
andand
oror
notneg¬
=>implies
ifif
<=>iff
AAforall
EEexists
:|:|
_|_bot
TTtop
|--vdash
|==models
square
diamond
Grouping Brackets
TypeTeX altSee
( x+y )(x+y)
[x+y][x+y]
{x+y}{x+y}
|:x+y:|| x+y |
<x+y><x+y>
{x+y}{x+y}
(:x+y:)x+y
<<x+y>>x+y
{:x+y)x+y)
(x+y:}(x+y
abs(x)|x|
floor(x)x
ceil(x)x
norm(vecx)x
Arrows
TypeTeX altSee
uarruparrow
darrdownarrow
rarrrightarrow
->to
>->rightarrowtail
->>twoheadrightarrow
>->>twoheadrightarrowtail
|->mapsto
larrleftarrow
harrleftrightarrow
rArrRightarrow
lArrLeftarrow
hArrLeftrightarrow
Accents
TypeTeX altSee
hat xx̂
bar xoverline xx¯
ul xunderline xx̲
vec xx
tilde xx~
dot xx.
ddot xx..
overset(x)(=)=x
underset(x)(=)=x
ubrace(1+2)underbrace(1+2)1+2
obrace(1+2)overbrace(1+2)1+2
overarc(AB)overparen(AB)AB
color(red)(x)x
cancel(x)x
Greek Letters
TypeSeeTypeSee
alphaα
betaβ
gammaγGammaΓ
deltaδDeltaΔ
epsilonε
varepsilonɛ
varepsilonɛ
zetaζ
etaη
thetaθThetaΘ
varthetaϑ
iotaι
kappaκ
lambdaλLambdaΛ
muμ
nuν
xiξXiΞ
piπPiΠ
rhoρ
sigmaσSigmaΣ
tauτ
upsilonυ
phiϕPhiΦ
varphiφ
chiχ
psiψPsiΨ
omegaωOmegaΩ
Font Commands
TypeTeX altSee
bb "AaBbCc"mathbf "AaBbCc"𝐀𝐚𝐁𝐛𝐂𝐜
bbb "AaBbCc"mathbb "AaBbCc"𝔸𝕒𝔹𝕓ℂ𝕔
cc "AaBbCc"mathcal "AaBbCc"𝒜𝒶ℬ𝒷𝒞𝒸
tt "AaBbCc"mathtt "AaBbCc"𝙰𝚊𝙱𝚋𝙲𝚌
fr "AaBbCc"mathfrak "AaBbCc"𝔄𝔞𝔅𝔟ℭ𝔠
sf "AaBbCc"mathsf "AaBbCc"𝖠𝖺𝖡𝖻𝖢𝖼
sfit "AaBbCc"𝘈𝘢𝘉𝘣𝘊𝘤
bbsf "AaBbCc"𝗔𝗮𝗕𝗯𝗖𝗰
bbcc "AaBbCc"𝓐𝓪𝓑𝓫𝓒𝓬
bbfr "AaBbCc"𝕬𝖆𝕭𝖇𝕮𝖈
bbit "AaBbCc"𝑨𝒂𝑩𝒃𝑪𝒄
bbsfit "AaBbCc"𝘼𝙖𝘽𝙗𝘾𝙘
bold "AaBbCc"AaBbCc
italic AaBbCc"AaBbCc
a quad ba  b
a qquad ba    b
a enspace bab
a thinspace bab
a mspace(20) bab

Standard Functions

Function names are treated as constants:
sin, cos, tan, sec, csc, cot, arcsin, arccos, arctan, sinh, cosh, tanh, sech, csch, coth, exp, log, ln, det, dim, mod, gcd, lcm, lub, glb, min, max, f, g.


Special Cases

Matrices (mix and match grouping brckets):

     [[a,b]] yields [ab]

     [(a,b)(c,d)] yields [(a,b)(c,d)]

     ((a),(b)) yields (ab)

     [[a,b],[c,d]] yields [abcd]

Augmented matrices: [[a,b,|,c],[d,e,|,f]] yields [abcdef ]

Matrices can be used for layout: {(2x,+,17y,=,23),(x,-,y,=,5):} yields {2x+17y=23xy=5

Complex subscripts: lim_(N->oo) sum_(i=0)^N yields limNi=0N

Subscripts must come before superscripts: int_0^1 f(x)dx yields 01f(x)dx

Derivatives: f(x) = dy/dx yields f(x)=dydx

For variables other than x,y,z, or t you will need grouping symbols: (dq)/(dp) yields dqdp

OverBraces and underbraces:

     ubrace(1+2+3+4)_("4 terms") yields 1+2+3+44 terms

     obrace(1+2+3+4)^("4 terms") yields 1+2+3+44 terms


Attention: Always try to surround the > and < characters with spaces so that the html parser does not confuse it with an opening or closing tag!

Some Examples

x^2+y_1+z_12^34x2+y1+z1234subscripts as in TeX, but numbers are treated as a unit
sin^-1(x)sin 1(x)function names are treated as constants
d/dxf(x)=lim_(h->0)(f(x+h)-f(x))/hddxf(x)=limh0f(x+h)f(x)hcomplex subscripts are bracketed, displayed under lim
\frac{d}{dx}f(x)=\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}ddxf(x)=limh0f(x+h)f(x)hstandard LaTeX notation is an alternative
f(x)=sum_(n=0)^oo(f^((n))(a))/(n!)(x-a)^nf(x)=n=0f (n)(a)n!(xa)nf^((n))(a) must be bracketed, else the numerator is only 'a'
f(x)=\sum_{n=0}^\infty\frac{f^{(n)}(a)}{n!}(x-a)^nf(x)=n=0f (n)(a)n!(xa)nstandard LaTeX produces a similar result
int_0^1f(x)dx01f(x)dxsubscripts must come before superscripts
[[a,b],[c,d]]((n),(k))[abcd](nk)matrices and column vectors are simple to type
x/x={(1,if x!=0),("undefined",if x=0):}xx={1ifx0undefinedifx=0piecewise defined functions are based on matrix notation
a//ba/buse //// for inline fractions
(a/b)/(c/d)abcdwith brackets, multiple fraction work as expected
a/b/c/dab/cdwithout brackets the parser chooses this particular expression
((a*b))/c(ab)conly one level of brackets is removed; * gives standard product
sqrt sqrt root3xx3spaces are optional, only serve to split strings that should not match
<< a,b >> and {:(x,y),(u,v):}a,bandxyuvangle brackets and invisible brackets
(a,b)={x in RR | a < x < = b}(a,b)={xa<x<=b}grouping brackets don't have to match
abc-123.45^-1.1abc123.451.1non-tokens are split into single characters, but decimal numbers are parsed with possible sign
[[a,b,|,c],[d,e,|,f]][abcdef ]augmented matrices
{(2x,+,17y,=,23),(x,-,y,=,5):}{2x+17y=23xy=5Matrices can be used for layout
lim_(N->oo) sum_(i=0)^NlimNi=0NComplex subscripts
(dq)/(dp)dqdpFor variables other than x,y,z, or t you will need grouping symbols