Bigger, faster, smarter - the monster returns.
Objectives
Create a javascript program to convert LaTeX math formula's into responsive MathML. In order of importance
- User friendly LaTeX math input.
- Mobile phone friendly « responsive » math web pages output.
- Clean, compliant, browser-supported MathML generated.
- Fast execution, small footprint.
- Compatibility with MathJax and KaTeX.
Reference Material
- TeXZilla 1 - FontDrop! - Wakamai Fondue
- MathML Core - Mathematical Alphanumeric Symbols: MathML, Wikipedia
- Unicode Planes - Math Characters - Standardized Variants - Named Character References
- Latex / Plain TeX - Mathematics - Advanced Mathematics - AMS Package - CTAN Packages - Maths Symbols - Comprehensive LaTeX Symbol List
- Mathematics With TeX - Tex Tutorial - TeXeR online LaTeX renderer
- TeX Commands Available in MathJax - MathJax Supported TeX/LaTeX commands - KaTeX Support Table
Issues
In hindsight the main issues to implementation have not been the TeX syntax itself rather they have been
-
The lack of a definitive set of Latex commands to implement.
Because LaTeX is an amalgam of hundreds if not thousands of packages each built from low-level TeX
commands and somewhat adhoc in nature it has been hard to determine what to implement.
Up to this point the TeXZilla command set is made up of (almost) all the math commands from the TeXBook,
the commands from Mathjax 2, the commands from the
baseandamspackages and other useful commands from theunicodeandmathtoolspackages. Also a few gaps such as missing Latin-like Capital Greek letters and various fences, brackets and a few other symbols have been added. - The need to completely avoid layout computation logic. Because the intention is to defer that to the Browser's MathML layout engine.
-
What was also unexpected was the release of the MathML 3 Core spec which deprecates
many MathML 4 features and instead asks that they be implemented using CSS.
MathML 3 Core is the spec implemented by Chrome and Edge and it is the spec targeted by TeXZilla.
Because of that the major blockers to widespread TeXZilla browser support are now Firefox and Safari. Which is rather ironic since those browsers have up until now been the leaders in providing MathML support. That is not to say that Chrome does a better job that Firefox - far from it. There are many minor inexplicable issues in Chrome which affect some Math fonts but not others, that simply do not occur in Firefox.
But the big issue in Firefox is the lack of CSS support for<mrow>elements. It is in clear contravention of the MathML 3 Core spec in this regard. -
What has not been an issue is the availability of several very nice open source OpenType Math fonts.
The main issues with the fonts (in Chrome) is that most of them fail to work properly with prime marks.
Also many have issues with horizontally stretchy glyphs.
And there is no font independent way to enable some rarely used special glyph variants.
And most lack full support for both the chancery and roundhand script alphabets.
Because of that TeXZilla always uses the STIX Two Math font for those particular features.
In addition it should be noted that TeXZilla has gone down the unicode math path where, as far as possible, all glyphs have their own unique unicodes and come from a single OpenType Math font. -
The following glyph mapping convention has been adopted because of this (in addition to the fact that
the current scheme(s) in LaTeX seem excessively complicated).
Each font name has the form
\math(bold)(family)(italic)-
bold=bforblankwhich means normal font weight -
family=sf,bb,cal,frak,scr,ttorblankwhich means serif -
italic=it,rm,uporblankwhich means automatic according to the glyph's alphabetic category
-
-
Only Roman letters, numerals and Greek letters which have been entered via the Greek commands
\alphaand\Alphaetc. have mapping rules applied to them. Other character codes such as those entered directly as unicodes or as commands like\mitRor\mbfAlphahave no glyph mapping applied.
Implementation
Input
All input is Unicode, it's not limited to Ascii. TeXZilla interprets it's input as a stream of tokens.
- Whitespace: tab, newline, carriage return and space characters. Whitespace is mostly ignored.
-
Command characters:
{ } _ ^ &represent begin group, end group, subscript, superscript and alignment respectively -
Hyphen:
'-'represents mathematical minus sign -
Colon:
':'represents mathematical logical colon or ratio -
Dashes:
','','''etc. represent single, double, triple primes etc. -
Commands:
\xxxwhere xxx are letters a-z or A-Z or a single special character. For example\frac -
Arguments:
#nwhere n is a single digit 1-9, these are the names of macro arguments and mark insertion points during macro expansion -
Comments: characters between a
%and the end of line, these characters are discarded - Other Characters: any single Unicode code point excluding those above represent a symbol or operator
Mathematical Symbols
Almost all mathematical symbols have been assigned unicode's in the extended plane
and are supported in the common MathML fonts.
So there is no need for any other special fonts at all.
Commands like \mathfrak and \mathcal are implemented simply by mapping A-Z etc. into this plane.
Macros
Are user-defined commands. Perform text substitution - using strings of tokens. Arguments for macro's are marshalled by looking ahead in the input stream for the next token, or string of tokens contained between balanced pairs of { }. As a result of this rule macro argument values always contain balanced sets of braces. The outer pair of braces are not considered to be part of the macro value. In TeX there is no implicit boxing of command arguments. However many of the AMS commands appear to get their arguments automatically boxed in braces. I'm guessing the reason for this discrepancy is that the AMS commands were once macro's which boxed their arguments internally.
Design (in a state of flux)
Tokeniser
Input text is converted to tokens and macro substitution is performed resulting in a stream of tokens with all macro commands and arguments resolved to other tokens.
-
Implement
\def
Interpreter
Tokeniser's output is interpreted and converted to a stream of MathML rendering instructions. It is here where context-specific parsing rules are implemented to ensure compatibility with LaTeX or MathJax etc. There are a number of differences in interpretation between the two.
- Add more boxing to match AMS LaTeX.
Renderer
The renderer processes the instructions from the interpreter and builds a MathML element tree. The tree structure is obvious and quite easy to achieve. The correct styling of the tree elements is not so easy, and still a work in progress.
- Improve the rendering of primes.
- Improve LaTeX / MathJax compatibility by addressing styling issues.
- Add support for many more commands.
Accents
Despite what AI says using combining diacritics for accents does not work well in MathML in Chrome. This is because it typesets the combining diacritic at a fixed position for all glyphs. The result is the accent often does not appear in the usual position expected.
For example \\not X is resolved by the interpreter as follows.
If X is a glyph which has a unicode not form then is converted to that form.
Otherwise it is rendered as sub-formula overlayed with a near vertical line through it.
Math Style
The following user selectable italicization styles have been implemented.
| Math Style | Roman | Greek | Numbers | ||
|---|---|---|---|---|---|
| Upper | Lower | Upper | Lower | ||
| ISO | italic | italic | italic | italic | upright |
| TeX | italic | italic | upright | italic | upright |
| French | upright | italic | upright | upright | upright |
| Upright | upright | upright | upright | upright | upright |
LaTeX Math Fonts
The math fonts are designed to conform to the Mathematical Alphanumeric Symbols table. They do this by translating the glyph characters into the symbol table. So each standard LaTeX character has it's own unique unicode. Because of this these alphabets differ slightly from their Latex versions. In particular they provide lowercase calligraphic characters, and consistent and universal control of italicization. In the table below Italic auto means the italicization is determined by the Math Style.
The standard math font can be left as the system default math font or defined in a CSS style sheet like this:
math {
font-family: STIX Two Math;
font-size: 120%;
}
For consistency across multiple platforms the font should be uploaded from your web site in the style sheet like this:
@font-face {
font-family: 'STIX Two Math';
src: url('myfonts/STIXTwoMath-Regular.woff2') format('woff2');
}
For better performance some fonts can be uploaded from Google by adding code like this to <head>.
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Caveat+Brush&display=swap" rel="stylesheet">
| Font | Bold | Family | Italic | Alphabets |
|---|---|---|---|---|
| \mathnormal | serif | mixed | roman, greek, numbers | |
| \mathrm | serif | mixed | roman, greek, numbers | |
| \mathup | serif | upright | roman, greek, numbers | |
| \mathit | serif | italic | roman, greek | |
| \mathbfit | bold | serif | italic | roman, greek |
| \mathbf | bold | serif | upright | roman, greek, numbers |
| \mathsf | sans-serif | upright | roman, greek, numbers | |
| \mathsfit | sans-serif | italic | roman, greek | |
| \mathbfsf | bold | sans-serif | upright | roman, greek, numbers |
| \mathbfsfit | bold | sans-serif | italic | roman, greek |
| \mathbb | double-struck (blackboard) | upright | roman, numbers | |
| \mathbbit | double-struck (blackboard) | italic | roman | |
| \mathcal | chancery script (calligraphic) | roman | ||
| \mathbfcal | bold | chancery script (calligraphic) | roman | |
| \mathfrak | fraktur | roman | ||
| \mathbffrak | bold | fraktur | roman | |
| \mathscr | roundhand script | roman | ||
| \mathbfscr | bold | roundhand script | roman | |
| \mathtt | monospace (teletype) | roman, numbers | ||
| \oldstyle | cursive (old style numbers) | numbers |
The current math font is used to provide almost all the glyphs for the above alphabets.
But because math fonts are inconsistent in providing the glyphs for \mathcal and \mathscr
those commands always use the STIX Two Math font.
In addition a system-selected font is used to provide \oldstyle numerals because they are not available in most math fonts.
User Defined Fonts
You can define your own font using this Latex code
$ \newfontfamily{\mathmkr}{mathmkr} $
at the start of the page. Then in a style sheet add a CSS font family and class, for example
@font-face {
font-family: 'Caveat Brush';
src: url('myfonts/CaveatBrush-Regular.ttf');
}
.mathmkr {
font-family: Caveat Brush;
font-size: 120%;
}
You can then use it like other Latex fonts in the TeX code for example \mathmkr{x} = ...