One of the requirements I set myself when building this blog was that it must support LaTeX. This post shows how I achieved turning this âe=mc^2â into . Besides inline math, blocks are supported as well which turns âF=G(m1m2/r^2)â into
If youâre looking to integrate LaTeX into your website, this post should be a helpful starting point, especially if youâre using the markdown preprocessor MDSveX with SvelteKit.
At first, I gave MathJax a shot. Integration and configuration is an absolute breeze as you simply add a script tag for the source and optionally another for setting various options.
<script>
window.MathJax = {
tex: {
inlineMath: [
['$', '$'],
['\\(', '\\)']
],
displayMath: [
['$$', '$$'],
['\\[', '\\]']
],
processEscapes: true
}
};
</script>
<script
id="MathJax-script"
async
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"
></script>
I canât imagine any easier method if you want LaTeX support that simply works out of the box regardless of which frameworks youâre using.
There is a small downside however. Whenever the page has finished loading, you get a slight flicker as MathJax scans the page and replaces text with LaTeX. For websites with cookie banners, privacy consents, loads of ads, heavy analytics scripts, it likely wonât be a noticeable issue. But, since my website is light, fast and simple, it definitely became more noticeable.
Not quite being satisfied with the flicker, I turned to KaTeX, the self-proclaimed âfastest math typesetting library for the webâ. Since Iâm using MDSveX, I went with the âofficialâ integration of KaTeX which is a rehype plugin.
The installation is straightforward. First you need to install the package rehype-katex-svelte using npm, yarn or similar. Then you must add the remarkMath and rehypeKatexSvelte plugin to your MDSveX configuration. Due to some issues with double rendering I had to explicitly require output as mathml instead of the default mathml + HTML (for accessibility). I didnât delve deeper into the problem, but thereâs certainly a better solution for keeping HTML.
import { defineMDSveXConfig as defineConf } from 'mdsvex';
import rehypeKatexSvelte from 'rehype-katex-svelte';
import remarkMath from 'remark-math';
...
const config = defineConf({
remarkPlugins: [remarkMath],
rehypePlugins: [
[
rehypeKatexSvelte,
{
output: 'mathml'
}
]
]
});
The result is lightning fast and the flicker is completely eliminated due to server-side rendering
3/5/2024