LaTeX Math Formulas in Unity UI: Fractions, Matrices and Equations

Last reviewed

Short answer

Neither TextMesh Pro nor UI Toolkit lays out mathematics: fractions, radicals, scripts and stretchy brackets need a math layout engine and a font with an OpenType MATH table. The usual options are a dedicated asset with its own formula renderer, or formulas rendered to images outside Unity.

UniText typesets LaTeX inside ordinary text: inline $…$ and display $$…$$ formulas, numbered equations, matrices and mhchem chemistry, laid out from the math font's own OpenType MATH data and styled, animated and selected like the text around them.

Why math needs its own layout

A formula is a tree, not a line of characters. A fraction stacks two expressions around a bar, a radical grows to cover its contents, subscripts shrink and drop by amounts set by the font, and brackets stretch to the height of what they enclose. These measurements live in the OpenType MATH table, which math fonts such as STIX Two Math and Latin Modern Math carry. TextMesh Pro and UI Toolkit read no MATH table and have no formula layout.

The usual options

  • TextMesh Pro and UI Toolkit tags. Both offer <sub>, <sup> and <voffset>, which are enough for x² or H₂O but not for fractions, roots or matrices. Anything more means positioning separate text objects by hand.
  • TEXDraw, the long-standing math asset, renders a LaTeX-like syntax with fractions, roots, big operators, matrices, aligned equations, colors and links, in uGUI, as 3D meshes and with limited UI Toolkit support. Its own documentation says it does not strictly follow LaTeX and that incomplete syntax produces no error. Version 7.1 (March 2026) requires Unity 6.3.
  • Formulas rendered outside Unity. Tools such as MathJax can render LaTeX to SVG or images ahead of time. The result is static: a formula that changes at runtime needs another render, and images blur when scaled.
  • A browser view. Embedding a web view and rendering with KaTeX or MathJax brings a full browser engine into the game for what is usually a few lines of text.

How UniText sets math

Formulas set by UniText: fraction bars, radicals, script sizes and delimiters all come from the math font.
  • LaTeX in the text. MathParseRule reads inline math as $…$ or \(…\), display math as $$…$$ or \[…\], the equation, align, gather and multline environments, or <math> tags. A dollar sign followed by a digit stays text, so prices are not mistaken for formulas.
  • The usual constructs. Fractions, roots, sums, integrals and limits, binomials, matrices, cases, accents such as \hat and \vec, stretchy \left…\right delimiters, \mathbb and \mathcal alphabets, \operatorname and \text.
  • Numbered equations. Display environments number their equations, and \tag labels any display formula at the right edge of its line.
  • Chemistry. mhchem's \ce and \pu write formulas, reactions and quantities.
  • Font-driven layout. MathModifier takes its metrics and stretchy glyphs from a dedicated OpenType MATH font; the package samples include STIX Two Math.
  • Part of the text. An inline formula sits in the line with the words around it and wraps with the paragraph; display formulas take a centered line of their own.
  • Fails locally. A formula that does not parse shows its source between bars and logs one warning; the rest of the text renders normally.

Formulas are enabled per component with a math style: MathParseRule with the delimiters you write, and MathModifier with a math font. After that the text needs no markup beyond the LaTeX itself:

csharp
label.Text = @"The roots of $ax^2 + bx + c = 0$ are
$$x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$$
and water forms by $\ce{2H2 + O2 -> 2H2O}$.";

Questions

Which font do I need?

Any OpenType font with a MATH table. STIX Two Math ships with the UniText samples; Latin Modern Math, Libertinus Math, Fira Math and Noto Sans Math are other free choices under open font licenses.

Can formulas change at runtime?

Yes. Formulas are parsed and laid out from the text each time it changes, so answers, variables and generated exercises can be written into the string directly.

Does it render full LaTeX documents?

No. UniText typesets LaTeX math, not LaTeX documents: sections, packages and macros outside math mode are not interpreted.

Check it in your project

Run the browser demo, follow the setup guide, or compare the free version with the licenses.

Related problems