diff --git a/.changeset/katex-label-line-breaks.md b/.changeset/katex-label-line-breaks.md
new file mode 100644
index 00000000000..48294f39fe8
--- /dev/null
+++ b/.changeset/katex-label-line-breaks.md
@@ -0,0 +1,5 @@
+---
+'mermaid': patch
+---
+
+fix: line breaks are no longer ignored in flowchart node and edge labels that contain `$$…$$` KaTeX math expressions. Both `
` and hard line breaks in the label source now render on their own row, matching the behavior of labels without math.
diff --git a/packages/mermaid/src/rendering-util/createText.spec.ts b/packages/mermaid/src/rendering-util/createText.spec.ts
index 0a46222682d..6e385b15fcd 100644
--- a/packages/mermaid/src/rendering-util/createText.spec.ts
+++ b/packages/mermaid/src/rendering-util/createText.spec.ts
@@ -99,4 +99,75 @@ describe('createText', () => {
expect(output.textContent).toEqual(expected);
}
);
+
+ it('renders text after
on a new line in labels containing KaTeX math', async () => {
+ const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
+ const svgGroup = svg.appendChild(document.createElementNS('http://www.w3.org/2000/svg', 'g'));
+ const output = await createText(
+ select(svgGroup),
+ 'Input
$$x$$',
+ { useHtmlLabels: true },
+ { forceLegacyMathML: true, securityLevel: 'strict' }
+ );
+
+ const span = output.querySelector('span.nodeLabel');
+ const rows = [...(span?.children ?? [])];
+ expect(rows).toHaveLength(2);
+ expect(rows[0].outerHTML).toEqual('