MathJax LaTeX: Render Math in HTML with MathJax 4

November 11, 2026 · 10 min read

MathJax LaTeX: How to Render Math on Any Web Page

MathJax LaTeX rendering lets you type math in LaTeX notation, such as \frac{a}{b}, inside an ordinary HTML page and have the browser typeset it. You need two script tags and a choice of delimiters. This page gives you a working MathJax 4 file, explains why single dollar signs don't work out of the box, and fixes the common failures.

What Is MathJax, and How Does It Relate to LaTeX?

MathJax is an open-source JavaScript display engine for LaTeX, MathML and AsciiMath notation. It runs in the reader's browser, finds math in the page text, and draws it as HTML with CSS (called CHTML) or as SVG. It's free under the Apache License 2.0, and the current release line is version 4.

The LaTeX part needs one clarification. LaTeX is a full typesetting system that turns .tex documents into PDFs. Per the official FAQ, MathJax replicates only LaTeX's math functionality and a limited amount of its text formatting. So \sum, \int, \begin{align} and \newcommand work, but \documentclass, \usepackage and page layout don't. If you're weighing LaTeX as a writing format, our Markdown vs LaTeX comparison covers that choice. To publish a whole .tex file as a web page, convert LaTeX to HTML with a tool like make4ht instead.

Add MathJax LaTeX to an HTML Page in Two Script Tags

Save this as math.html and open it in any modern browser. The first script holds your settings, and the second loads the library from the jsDelivr CDN. The official getting-started docs say the settings must come before the loader script. Both belong in the <head>, although the docs allow the <body> if you have no other choice.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>MathJax LaTeX demo</title>
  <script>
    MathJax = {
      tex: {
        inlineMath: {'[+]': [['$', '$']]},
        tags: 'ams'
      }
    };
  </script>
  <script defer src="https://cdn.jsdelivr.net/npm/mathjax@4/tex-mml-chtml.js"></script>
</head>
<body>
  <p>Default inline delimiters: \(a^2 + b^2 = c^2\).</p>
  <p>Dollar delimiters, enabled above: $E = mc^2$.</p>
  <p>A real price that stays text: \$20.</p>
  <p>$$\int_0^\infty e^{-x^2}\,dx = \frac{\sqrt{\pi}}{2}$$</p>
</body>
</html>

When the page loads, both inline formulas render inside their sentences. The price line shows a plain "$20", and the integral sits centred on its own line.

The file name tex-mml-chtml.js is a combined component: TeX and MathML input with CHTML output. The docs list others, including tex-chtml (TeX only), tex-svg (TeX to SVG) and tex-mml-svg. Pick an -svg file if you'd rather have each formula drawn as an SVG image instead of HTML and CSS.

The mathjax@4 part of the URL always loads the newest 4.x release. On npm that's 4.1.3 as of November 2026. For production, pin an exact number such as mathjax@4.1.3 so an update can't change your pages overnight.

MathJax Inline Math: Why Doesn't $...$ Work by Default?

MathJax inline math uses \(...\) out of the box. Display math accepts both $$...$$ and \[...\]. Single dollar signs are off because, in the words of the delimiter docs, dollar signs "appear too often in non-mathematical settings." A sentence like "it costs $5 or $10" would turn into a broken formula.

To switch them on, add this to your configuration:

MathJax = {
  tex: {
    inlineMath: {'[+]': [['$', '$']]}
  }
};

The '[+]' key appends the dollar pair to the default list, so \(...\) keeps working alongside it. Once dollars are math, you have two ways to print a literal one:

  • Type \$. The processEscapes option is on by default, so you get a plain dollar sign.
  • Wrap it in a <span>. Delimiters only pair up inside the same parent element, so <span>$</span> never opens a formula.

We prefer turning dollar delimiters on and escaping the rare real price. It's the syntax GitHub, Jupyter and many Markdown tools expect, so the same source works in more places.

Display Math and Numbered Equations in MathJax

Display equations go between $$...$$ or \[...\]. MathJax also processes LaTeX environments that appear without any delimiters, because processEnvironments defaults to true. That means a bare \begin{align} block in your HTML renders as display math.

Numbering is off by default (tags: 'none'). The tags: 'ams' line in the file above numbers equations the way the amsmath package does, which makes \label and \eqref useful:

<p>
\begin{align}
f(x) &= (x+1)^2 \label{eq:square} \\
     &= x^2 + 2x + 1
\end{align}
Expanding \(\eqref{eq:square}\) gives the second line.
</p>

This renders two aligned rows, each with its own number at the right, plus a sentence that links back to equation (1). Set tags: 'all' instead if you want every display equation numbered, including $$...$$ blocks. Under 'ams', starred environments such as align* stay unnumbered, and \notag skips a single row. For matrices, piecewise functions and more aligned equations, see our copy-ready MathJax examples.

MathJax 3 vs 4: Which Snippet Are You Looking At?

Version 4.0.0 shipped on npm in August 2025, so plenty of tutorials still show older code. The docs say the API for v4 is largely the same as v3. For pages that only configure and load the library, the move is often just a URL change. You can spot the version from the snippet itself:

You seeVersionWhat to do
MathJax.Hub.Config, type="text/x-mathjax-config", MathJax.js?config=...2Rewrite it. Version 3 removed MathJax.Hub entirely.
mathjax@3/es5/tex-mml-chtml.js, MathJax.startup.promise chains3Update the URL and drop the startup.promise pattern.
mathjax@4/tex-mml-chtml.js4Current.

Older v3 configs often enable dollars with a full array: inlineMath: [['$', '$'], ['\\(', '\\)']]. That replaces the defaults, so it only works because it re-lists the parentheses. The '[+]' form avoids dropping them by accident.

Version 4 also changes how pages look. The default font is now mathjax-newcm, based on New Computer Modern, and automatic line breaking of long equations is back after being missing in version 3. The -full combined components are gone. If you depended on them, check the upgrade notes before switching.

How Do You Render Math Added After the Page Loads?

MathJax LaTeX rendering happens once, at startup. Anything you insert later, such as a chat reply, a quiz answer or a preview pane, shows raw LaTeX until you ask for another pass. The typesetting docs give the pattern: clear the old math first, change the content, then typeset the container.

const box = document.querySelector('#answer');

MathJax.typesetClear([box]);
box.innerHTML = String.raw`Roots: \(x = \frac{-b \pm \sqrt{b^2-4ac}}{2a}\)`;

MathJax.typesetPromise([box]).then(() => {
  console.log('Math rendered');
});

After the promise resolves, the quadratic formula appears typeset inside #answer. Two details matter here. Call typesetClear before you replace the content, or stale records of the old equations linger. And in a normal JavaScript string, '\frac' starts with the escape code \f, so the backslash never arrives. Double every backslash ('\\frac') or use String.raw as above.

Use typesetPromise rather than the synchronous typeset. Version 4 splits its fonts into pieces that load on demand. Even simple math may need extra files, so the docs say the promise-based calls are always the better choice. Calling typesetPromise() with no argument processes the whole page, which is fine for a one-off update but slow on long documents. Pass the container instead whenever you know where the new math landed.

Where MathJax Already Renders Your LaTeX: GitHub, Jupyter and Anki

You don't always need your own script tag. Several tools ship with MathJax LaTeX support built in, each with its own delimiters:

PlatformInlineDisplayNotes
GitHub$...$ or $`...`$$$...$$ or a math code fenceEscape a dollar inside math with \$; outside math, wrap it in <span>
Jupyter Notebook and JupyterLab$...$$$...$$ or \begin{equation}JupyterLab's MathJax extension still bundles MathJax 3 as of November 2026
Anki\(...\)\[...\]MathJax delimiters per the Anki manual

GitHub's math docs explain the backtick form: it protects expressions whose characters would otherwise clash with Markdown. For notebook-specific tips, see our Jupyter Markdown cheat sheet. For flashcards, our guide to LaTeX in Anki covers the delimiters and cloze deletions.

That clash is also why MathJax often fails on blogs and wikis that run Markdown before the math script. The parser can read the underscores in a_1 + b_1 as italics. It also treats a backslash before punctuation as an escape, so \(x\) reaches MathJax as a plain (x). If your Markdown tool has a math extension, turn it on so formulas pass through untouched.

Previewing LaTeX Math in Our Editor (KaTeX, Not MathJax)

Here's the limitation, stated plainly. Our online Markdown editor and the LaTeX equation editor render math with KaTeX, not MathJax. Inline $...$ and display $$...$$ work in the live preview. The \(...\) and \[...\] delimiters don't, and MathJax-only commands such as \require won't render either. Our KaTeX vs LaTeX comparison lists what KaTeX can't render.

Most everyday LaTeX math looks the same in both engines, so the preview is still a quick way to check a formula before it goes into a MathJax LaTeX page. Edit the sample below to try your own formulas. For a deeper look at how the two renderers differ, see our guide to writing equations in Markdown.

LaTeX math, rendered live

Inline: the roots are x=−b±b2−4ac2ax = \frac{-b \pm \sqrt{b^2-4ac}}{2a}.

Display:

∫0∞e−x2 dx=π2\int_0^\infty e^{-x^2}\,dx = \frac{\sqrt{\pi}}{2} (a+b)2=a2+2ab+b2eiπ+1=0\begin{aligned} (a+b)^2 &= a^2 + 2ab + b^2 \\ e^{i\pi} + 1 &= 0 \end{aligned}
38 words242 characters16 lines
Markdown

The aligned environment works here inside $$...$$. Paste the same formulas into a MathJax page with dollar delimiters enabled and you get the same math.

MathJax Questions, Answered

A working MathJax LaTeX setup comes down to four habits. Put the configuration before the loader, and enable dollar delimiters if your content expects them. Call typesetPromise for anything added later, and pin the version once the page goes live. To draft and check formulas before you paste them into your page, open the editor and watch the KaTeX preview render them as you type.