Mathematics without LaTeX
November 19, 2019 — August 6, 2022
If I just want equations, I don’t need TeX-the-software, just TeX-the-mathematical-markup. Lots of things can render TeX math. Which tools work depends on what kind of document I am working with. In HTML documents, there are libraries such as MathJax and KaTeX that will hunt down math and render it.
For other markup formats, it can be complicated. There are many preferred syntax choices for markdown. In fact, too many, all with incompatible syntax, natch. The de facto standard IMO is whatever pandoc
supports. pandoc
is what I usually use for turning my markdown+math into various documents (including, for example, parts of this blog).
🚧TODO🚧 clarify: I should probably say what I mean by rendering and inlining, because it is, as with many things relating to the internet infrastructure, not quite obvious.
1 MathJax
MathJax allows us to put LaTeX equations online easily without bothering with the rest of the latex infrastructure.
tl;dr
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>MathJax example</title>
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script id="MathJax-script" async
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js">
</script>
</head>
<body>
<p>
When $a \ne 0$, there are two solutions to $ax^2 + bx + c = 0$ and they are
$$x = {-b \pm \sqrt{b^2-4ac} \over 2a}.$$
</p>
</body>
</html>
- MathJax LaTeX manual
- I find Carol Burns’ Exhaustive MathJax Page more useful, though, since it shows and tells you at the same time.
- jupyter can use MathJax out of the box.
MathJax v2 is slow and heavy but powerful. The complete rewrite for the version 3 series looks more nimble but is currently missing stuff I use on this blog, specifically, Automatic Line Breaking, which will not arrive soon. Also, because the recommended invocation has changed it is not necessarily seamless to upgrade between them and many platforms do not in practice support v3 yet.
🤓pro tip: If I install the libjs-mathjax
package under Ubuntu, I have all the (hefty) MathJax libraries available in /usr/share/javascript/mathjax/
for offline use.
🤓pro tip: There are browser extensions to automatically inject LaTeX math rendering to websites, e.g. TeX All the Things (source).
🤓pro tip: Double-struck/blackboard numerals require a hack in mathjax 2 at the moment, so that $\unicode{x1D7D8}\unicode{x1D7D9}$
gives us \(\unicode{x1D7D8}\unicode{x1D7D9}\).
2 KaTeX
KaTeX is a faster competitor to MathJax, but is not yet as widely supported. KaTeX even eliminated another pain point, having supported macros since 0.10.rc1. You couldn’t define macros inline, which was tedious because macros are ubiquitous in real mathematics work. Now you can either inject them via config, or use the esoteric low-level \gdef
to get LaTeX-like behaviour, since \renewcommand
exists but doesn’t persist between equations, which makes it pointless for anything except prototyping individual macro definitions. KaTeX support of maths markup is not as complete as MathJax. In particular
I miss thesplit
environment.- You can’t put math markup inside
\text{}
3 Inlined/server side HTML mathematics
Both MathJax and KaTeX are eventually slow if I have enough equations in a document — MathJax gets slow sooner than KaTeX, but neither scales to the documents I write as a mathematician. The Javascript magic takes longer and longer to hunt down all the latex in the page and massage it into something that looks like mathematics. I ideally want something that outputs the target HTML (say) document and all the mathematics is already rendered into the appropriate symbols, rather than breaking it into need not mess around with a complicated post hoc multi-phase javascript-based rendering procedure. A few tools attempt to do this.
plasTeX, above, is one, and seems vibrant (?).
bookish is a project by parser nerd Terrence Parr which, like my latex_fragment, translates TeX markup to SVG, but in addition works out how to align the SVG with text correctly via herculean effort.
This approach is shared with Daan Leijen’s Madoko (introductory paper), which does math rendering in PDF or HTML as embedded SVG.
Both these last 2 projects took the unusual step of not using the de facto standard for markdown/academia, pandoc
, in favour of implementing their own new engines, which makes them suspiciously niche.
Amazingly, despite pandoc already employing a somewhat obscure academic functional language (Haskell), Makodo chose an even obscurer academic functional language, koka. At least his one compiles to javascript, so I guess it’s not totally alien territory. Anyway, it also includes some nifty publish-to-the-web features, and citation support, so maybe you don’t care about this obscurity.
There is another school of methods of rendering mathematics in HTML, which is the school who note that you can take one of the javascript mathematics engines but do all the complicated transformations and calculations that the javascript engine would normally do on the server without a browser.
Which tools can do this magic? Which of those can integrate with my workflow? Unclear. But here are some examples spotted in the wild:
Tex math rendering in R via katex (may soon be in blogdown)
Converts tex-style math expressions to html and mathml for use in manual pages or markdown documents. The conversion is done in R using V8 (“server-side”), hence the resulting fragment can be inserted into an HTML document without the need for a JavaScript library like MathJax. Only the
katex.css
style file is required in the final html document.MathJax-node-page which comes with a CLI
mjpage
which will pre-render HTML for you — it even claims to render math as SVG too, through forbidden magic into which it is best not to enquire too closely.MathMD “is a simple npm package with a CLI that compiles a file written in Markdown with LaTeX expression into a single HTML file.”
Wikipedia aims to leverage this approach via their mathoid project, but I have not seen that widely deployed yet (?)
There are some full-pipeline tools which use javascript math rendering. The one that looks shiniest is ReLaXed, which also makes the case for their take on this approach targeting PDFs
Many of us prefer markup languages (Markdown, LaTeX, etc.) to GUI document-editors like MS Office or Google Docs. This is because markup languages make it easier to quickly write documents in a consistent style.
However, Markdown is limited to the title/sections/paragraphs structure, and LaTeX has obscure syntax and errors that also make it difficult to stray from the beaten track.
On the other hand, web technologies have never looked so good.
- Beautiful CSS frameworks make sure your documents look clean and modern.
- There are JavaScript libraries for pretty much anything: plotting, highlight code, rendering equations…
- Millions of people (and growing) know how to use these.
- Shorthand languages like Pug and SCSS are finally making it fun to write HTML and CSS.
- (Headless) web browsers can easily turn web documents into PDF, on any platform.
ReLaXed is an attempt at finding the most comfortable way to leverage this for desktop PDF creation.
However, I need citations in my workflow, and it is not immediately clear how I would inject those into such an end-to-end solution, at least not without writing yet another plugin— however, possibly Zotero could help with its HTML citation reports or maybe one of the word processor plugins supports HTML documents? It merits investigation. Not by me.
4 Mathcha
Mathcha - Online Math Editor is a mathematics-augmented virtual whiteboard that imports/exports LaTeX.
5 plasTeX
is a LaTeX document processing framework written entirely in Python. It currently comes bundled with an XHTML renderer (including multiple themes), as well as a way to simply dump the document to a generic form of XML. Other renderers can be added as well and are planned for future releases…
plasTeX differs from other tools like
LaTeX2HTML
,TeX4ht
,TtH
, etc in that the parsing and rendering of the document are completely separated. This separation makes it possible to render the document in multiple output formats.
It is active, being used by high-profile online projects such as the collaborative textbooks stacks and kerodon.
5.1 Gerby
Gerby, built on plasTeX, looks to be somewhere intermediate between a new markup language and HTML presentation tool; it generates a large indexed website from a latex document.
If you have a LaTeX document which
- is large (probably several hundreds of pages at least)
- is regularly updated
- needs to be externally referenced often
you will run into the problem that
- large PDFs are not easily navigable
- PDFs of any size are not very searchable
- the internal numbering changes often, making external references outdated
Gerby addresses these problems by providing an online tag-based view, instead of just having a big PDF. Gerby is tailored towards making large online textbooks and reference works more accessible.
6 Mathquill
mathquill is a real-time equation editor using latex syntax plus the mouse to get a Mathematica/MS Word like input experience.
MathQuill is an open source formula editor for the Web…
MathQuill was born of a need to communicate math. The creators,… used LaTeX for projects together, but struggled to collaborate online once they were apart. They wanted a tool to both write and read math without resorting to ugly flat text or more cumbersome conventions.
… Today, MathQuill is used in educational software, math visualizations, collaborative editors, and more.
Source here indicates that it is intermittently active.
7 Other online editors
Various.
8 MathML
Native web rendering of mathematics is provided by MathML, but it is supported, more or less, only in Firefox and Safari, and also is an ugly syntax to write by hand. It’s more of lingua franca for translating mathematical markup between equation editors and browsers which might be displayed by complicated browser-specific logic which I do not wish to know about. Unless this is more widely taken up I won’t be investigating it for my own use, and less so direct use.
There are utils to convert LaTeX math to MathML, e.g. Texzilla, or LaTeXML, which I have not tried.
The latter seems to be what drives the following magic:
Take any arxiv url, e.g. https://arxiv.org/pdf/2112.13029.pdf and replace x with 5
https://ar5iv.org/pdf/2112.13029.pdf This forwards you to an HTML5 page that does the right thing https://ar5iv.labs.arxiv.org/html/2112.13029
9 But why can’t the whole internet be LaTeX?
Why not indeed. Just for my curiosity, could you tell me: are you a tenured academic?
You can make an HTML document look like a LaTeX document at least using LaTeX.css.
This almost class-less CSS library turns your HTML document into a website that looks like a LaTeX document. Write semantic HTML, add
<link rel="stylesheet" href="https://latex.now.sh/style.css">
to the<head>
of your project and you are good to go. The source code can be found on GitHub at https://github.com/vincentdoerig/latex-css.
This will probably improve your chance of getting hired at a sufficiently reactionary maths department.
LaTeX.js (source) renders LaTeX documents using HTML5 and CSS, or at least it tries its hardest.