Repository navigation
Static formulas in HTML
This page covers how TeXmacs can export mathematical formulas to HTML as inline SVG, typeset by TeXmacs itself at export time. The page then needs no MathJax, no scripts and no web fonts, and formulas look as they do in the editor, in the fonts the document selects. It sums up the design decisions and lists what is open for discussion.
The work is PR #770 (branch html-static-math, against wip_fixes). It is not merged yet. It fixed #779. Problems found on the way are in #780 (the LaTeX importer) and #781 (included files lost in all HTML modes).
The user page is Help > "Exporting to Html" (TeXmacs/doc/main/convert/html/man-html.en.tm).
Preferences > Converters > TeXmacs -> Html > Mathematical formulas (or the same choice in the preferences dialog) offers five modes. They replace the former separate toggles:
| Mode | What the page gets |
|---|---|
| Static SVG | formulas typeset by TeXmacs, as SVG, with their source and a hidden MathML |
| MathJax | LaTeX, typeset by the MathJax script in the browser |
| MathML | MathML (the file must be saved as .xhtml) |
| Images | one PNG per formula (the default) |
| HTML tables | formulas made of HTML and CSS |
The preference behind the static mode is texmacs->html:static-math. The default does not change.
-
src/src/Plugins/Html_math/: a renderer which records what a box draws (glyphs, lines, fills, clips, colours, transformations) and writes it as SVG, with one shared definition per glyph for the whole document. -
src/src/Typeset/Env/env_exec.cpp,src/src/Edit/Editor/edit_typeset.*: the recording of the environment of each formula during the expansion for the export (exec_html). -
src/src/Edit/Editor/edit_main.cpp:html_math_snippet(SVG) andprint_snippet_in(images), which typeset a formula in its recorded environment. -
TeXmacs/progs/convert/html/tmhtml.scm: the converter (the static mode, the tables of displayed formulas, the hidden MathML and the source);tmhtml-expand.scm: the tags kept for displayed formulas;html-formulas.scm: the choice of mode. -
TeXmacs/progs/convert/html/htmltm.scm: the import reads a static formula back from its source. - Tests: the groups
html static formulasinTeXmacs/progs/check/formats-test.scm, andconvert-more-test.scm.
- TeXmacs typesets the formula; the browser only shows it. No layout of mathematics is done in the browser. The formula is typeset by the normal typesetter and redrawn on a capture renderer. Other renderers are unchanged.
-
Each formula is typeset in its own environment, as the expansion of the document finds it. While the buffer is expanded for the export,
exec_withrecords the variables that changed since the start of the document at each formula. The number of that environment is the first variable of the formula'swith(html-formula-env), so the converter and the plain-text paths (the page title) see an ordinarywith. Equal environments share a number, and numbers are never reused. Preambles, macros, fonts, colours and display style are therefore those of the document at the place of the formula, and the preamble is evaluated once, not once per formula. The images mode uses the same recorded environment. -
The layout of the page is HTML, not SVG. A displayed formula laid out on the page (
align,gather,eqnarray, parts betweenhtab...) becomes an HTML table. Its cells are formulas, and its equation numbers are text, so they line up with those ofequation. Each other formula is one SVG. -
Sizes are relative and colours inherited. Dimensions are in
em, so a formula follows the size of the text around it. What has the colour of the surrounding text is written ascurrentColor. - Glyphs are outlines where possible. Outlines come from FreeType, defined once per font and glyph. Glyphs without outlines (Metafont fonts, synthetic blackboard bold, stretched symbols of virtual fonts) are bitmaps at print resolution.
-
The page can be read back. Each formula keeps its TeXmacs source (
data-tm), from which the HTML import restores it. A hidden<math>serves screen readers. The import takes the source only when it is of reasonable size and has none of a list of refused tags (includes, images, scripts, packages, assignments, macros, links); otherwise it reads the MathML. The list is not complete:with-package,writeorpatternpass it, so a page from elsewhere can, once read back, load a local style file or write to the document. - Line breaks. Inline formulas wider than 12em are cut where the typesetter could break a line, so the browser can wrap them. Punctuation after a formula stays with its last box. Displayed formulas are not cut.
- Fallbacks. A formula containing a picture falls back to PNG, or to MathML when there is no target file.
-
SVG is written as text, not as converter trees (
*RAW*inhtmlout.scm). With trees, the export of 9000 formulas took 18.7 s.
- Export. 3100 formulas take 3.2 s, against 2.8 s for MathML. 1000 formulas with 1000 macros take 2.0 s, against 1.4 s.
- Page load. With 9000 short formulas, in Firefox on an Apple M4: about 1.4 s, against 3.6 to 4.5 s for MathJax.
- File size. The same page is 10.0 MB (129 KB gzipped), against 0.46 MB (27 KB gzipped) plus scripts and fonts for MathJax.
- A long displayed formula that is not a table does not wrap: the page scrolls sideways.
- A long inline formula wholly inside a decoration such as
underlineis not cut. - In tables of displayed formulas:
- a table narrower than the paragraph is as wide as its content;
- cell borders are not drawn;
- the number of an equation whose body has several rows sits on its first row.
- Stretched symbols are bitmaps, so they show steps at extreme zoom.
- Formulas with decorations or
rotatehave an empty hidden MathML (they are read back from their source). - A tree given to
convert, outside the export of a buffer, is expanded in the current buffer, with its style. - SVG glyphs cannot be selected or copied as text.
- The import of
data-tmrefuses a list of tags, which is not complete (see design decision 6). - The new menu entries are translated only in French and Dutch (Italian partly).
- The default. Should static SVG become the default mode, instead of images? Should MathJax stay at all?
- File size. Pages are much larger than with MathJax, although they compress well. Should glyph definitions be shared between the pages of a website, in a separate file?
- Copying and search. Should formulas carry selectable text (for example the LaTeX or the MathML as text under the SVG), so that copying a formula and searching the page work?
- Narrow screens. How should long displayed formulas behave on phones: scroll, shrink, or be cut like inline formulas?
- Stretched symbols as outlines. Slices under clips left seams in Firefox. Is there a way to have outlines without seams?
-
Other builds. It has been tested with the Guile/Qt build only. S7, Vue and the browser version need to be built and tested before a merge into
upsilon_texmacs.
Add your comments below each point, or start a new section.