How to render LaTeX to HTML with Node.js

January 24, 2021

To render LaTeX as HTML, we're going to be using the KaTeX npm package. First, install it:

npm install katex

Now, to render a string of LaTeX into HTML, we can use that package:

const katex = require('katex')

const latexString = 'e = mc^2'

const html = katex.renderToString(latexString)

Now, html will be a rendering of the LaTeX in HTML. To make it show properly, you'll need to include KaTeX's special CSS file, which styles the outputted HTML with the correct fonts and positioning. Drop this CSS file into the <head> of your HTML file along with the rendered LaTeX:

<link rel="stylesheet" href="" />

Now, opening the HTML file containing the rendered LaTeX-as-HTML string and that linked CSS file will show the LaTeX beautifully rendered.

A quick favor: was anything I wrote incorrect or misspelled, or do you still have questions? Please use this form to let me know or ask for help!