#8340·mermaid

Flowchart: a label that contains a math formula is never wrapped

Author: jjdeharoCreated Sep 28, 2026Updated Sep 28, 2026
LabelsType: Bug / ErrorStatus: Triage

Description

In an HTML label, text that contains a $$…$$ formula is not wrapped at the wrapping width. The same text without the formula wraps onto four lines in a 152 px node; with a formula at the end, it stays on a single line and the node grows to 429 px (Chromium) or 432 px (Firefox).

renderKatexUnsanitized (packages/mermaid/src/diagrams/common/common.ts) puts every line that contains a formula in <div style="display: flex; align-items: center; justify-content: center; white-space: nowrap;">. The text and the <math> element become items of a flex row, which cannot break, so addHtmlSpan gets a label as wide as the whole sentence.

Steps to reproduce

  1. Render the code below with Mermaid 12.0.0 and the default config.
  2. Compare the two nodes.

Node A wraps at the wrapping width. Node B is one line of 397 px, 429 px with the padding.

Screenshots

Left: node A, without a formula. Right: node B, the same text with a formula.

The same sentence wrapped onto four lines without a formula, and on a single wide line with a formula at the end

Code Sample

flowchart LR
  A["This is a long sentence that Mermaid wraps onto several lines"]
  B["This is a long sentence that Mermaid wraps onto several lines $$x^2$$"]

Setup

  • Mermaid version: 12.0.0 (the same code is in develop at dd5637d, 2026-09-28)
  • Browser and Version: Chromium 149.0.7827.55 and Firefox 151.0 (same result)

Suggested Solutions

Lay out a line that contains a formula as ordinary text, and keep the formula inline in it:

  • the line wrapper as a block with normal white space (display: block; white-space: normal) instead of a nowrap flex row;
  • the formula as an inline box (math { display: inline-block; }). It is rendered with displayMode: true, so <math display="block"> would otherwise take a line of its own.

We applied these two changes in the browser to the label Mermaid produced for node B, with the label width of the node (120 px). The text wraps onto five lines and the formula stays on the line of the last word, in Chromium and Firefox. It keeps the display style of the formula, so a fraction is as large as it is now.

Additional Context

The same wrapper is behind #6690 (missing spaces between a formula and the text around it). #7440 fixes the spaces with white-space: pre, but keeps the flex row, so the label still does not wrap. The change above also brings the spaces back ("lines x²" instead of "linesx²"), so a single fix could close both.