LaTeX Support

LaTeX math rendering is built in via flutter_math_fork — no setup required. Supports both inline and block equations.

Inline math

Two syntaxes are available. The backslash-paren syntax is always on. Dollar-sign syntax requires useDollarSignsForLatex: true.

inline_math.dart
1// Backslash-paren syntax (always enabled) 2GptMarkdown(r'The formula is \( E = mc^2 \) — inline.') 3 4// Dollar-sign syntax (opt-in) 5GptMarkdown( 6 r'The formula is $E = mc^2$ — inline.', 7 useDollarSignsForLatex: true, 8)

Block math

Block equations are centered and rendered at display size.

block_math.dart
1// Backslash-bracket syntax (always enabled) 2GptMarkdown(r''' 3The quadratic formula: 4\[ x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} \] 5''') 6 7// Double-dollar syntax (opt-in) 8GptMarkdown( 9 r''' 10 $$ 11 \int_0^\infty e^{-x^2} dx = \frac{\sqrt{\pi}}{2} 12 $$ 13 ''', 14 useDollarSignsForLatex: true, 15)

Syntax summary

TypeDefault syntaxDollar-sign syntax
Inline\( ... \)$...$
Block\[ ... \]$$...$$

Fixing AI output quirks

Some models (like GPT-4) double-escape backslashes. Use latexWorkaround to normalize before rendering.

workaround.dart
1// Some AI models output "\\(" instead of "\(" 2// Use latexWorkaround to normalize it 3GptMarkdown( 4 aiResponse, 5 latexWorkaround: (tex) => tex.replaceAll('\\\\', '\\'), 6)

Custom LaTeX renderer

Replace the default renderer entirely with your own widget via latexBuilder.

custom_latex.dart
1import 'package:flutter_math_fork/flutter_math.dart'; 2 3GptMarkdown( 4 content, 5 latexBuilder: (context, latex, textStyle, inline) { 6 if (inline) { 7 return Math.tex( 8 latex, 9 textStyle: textStyle, 10 ); 11 } 12 return Center( 13 child: Math.tex( 14 latex, 15 textStyle: textStyle?.copyWith(fontSize: 20), 16 ), 17 ); 18 }, 19)