LaTeX Math Formulas in Unity UI: Fractions, Matrices and Equations
Last reviewed
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
- LaTeX in the text.
MathParseRulereads inline math as$…$or\(…\), display math as$$…$$or\[…\], theequation,align,gatherandmultlineenvironments, 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
\hatand\vec, stretchy\left…\rightdelimiters,\mathbband\mathcalalphabets,\operatornameand\text. - Numbered equations. Display environments number their equations, and
\taglabels any display formula at the right edge of its line. - Chemistry. mhchem's
\ceand\puwrite formulas, reactions and quantities. - Font-driven layout.
MathModifiertakes 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:
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?
Can formulas change at runtime?
Does it render full LaTeX documents?
Check it in your project
Run the browser demo, follow the setup guide, or compare the free version with the licenses.
Related problems
- 3D Text in Unity: Extruded, Bevelled and Lit at RuntimeTextMesh Pro and UI Toolkit text is flat. UniText3D extrudes any text into lit solids at runtime with bevels, per-surface materials, layers, deformers, prefabs on letters, colliders and mesh export.
- TextMeshPro Alternative for Unity: UniText vs TMP vs UI ToolkitUniText replaces TextMesh Pro with a HarfBuzz-based text engine: shaping for Arabic and Indic scripts, Unicode BiDi, emoji sequences, variable fonts and system-font fallback on uGUI and in world space.
- Typewriter Effect and Text Animation in UnityUniText reveals text with nineteen appearance effects and animates it with wave, shake, bounce and other effects from markup, revealing whole grapheme clusters so every script and emoji appears intact.
