From 33ba2f2789c68b447cd36d152d930e0bba175482 Mon Sep 17 00:00:00 2001 From: Farid Inawan Date: Thu, 23 Apr 2026 09:24:38 +0700 Subject: [PATCH] fix: Update latex element builder to prevent line breaks around inline math --- lib/src/latex_element_builder.dart | 41 ++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/lib/src/latex_element_builder.dart b/lib/src/latex_element_builder.dart index 3a99030..54664f4 100644 --- a/lib/src/latex_element_builder.dart +++ b/lib/src/latex_element_builder.dart @@ -37,14 +37,39 @@ class LatexElementBuilder extends MarkdownElementBuilder { mathStyle = MathStyle.text; } - return SingleChildScrollView( - scrollDirection: Axis.horizontal, - clipBehavior: Clip.antiAlias, - child: Math.tex( - text, - textStyle: textStyle, - mathStyle: mathStyle, - textScaleFactor: textScaleFactor, + // Block math: scrollable container is appropriate. + if (mathStyle == MathStyle.display) { + return SingleChildScrollView( + scrollDirection: Axis.horizontal, + clipBehavior: Clip.antiAlias, + child: Math.tex( + text, + textStyle: textStyle, + mathStyle: mathStyle, + textScaleFactor: textScaleFactor, + ), + ); + } + + // Inline math: return Text.rich so flutter_markdown_plus's + // _mergeInlineChildren can recognise it as a text widget and merge it + // with adjacent text spans into a single RichText. Returning an opaque + // widget (e.g. SingleChildScrollView) breaks the merge chain and causes + // each surrounding text segment to become a full-width RichText, which + // produces unwanted line breaks around inline math expressions. + return Text.rich( + TextSpan( + children: [ + WidgetSpan( + alignment: PlaceholderAlignment.middle, + child: Math.tex( + text, + textStyle: textStyle, + mathStyle: mathStyle, + textScaleFactor: textScaleFactor, + ), + ), + ], ), ); }