smart_latex 2.0.0 copy "smart_latex: ^2.0.0" to clipboard
smart_latex: ^2.0.0 copied to clipboard

Self-contained LaTeX rendering for Flutter. Renders mixed text and math with a bundled, layout-fixed KaTeX engine — no extra math package required. Repairs malformed input (double backslashes, Persian [...]

smart_latex #

Robust LaTeX rendering for Flutter. Renders mixed text and math, repairs malformed input, and works cleanly inside custom-font and right-to-left layouts.

Features #

  • Renders inline ($...$, \(...\)) and display ($$...$$, \[...\]) math.
  • Repairs malformed LaTeX automatically:
    Input problem Fix applied
    \\right) (double backslash) \\command\command
    \sqt{x} typo \sqrt{x}
    Persian / Arabic digits ۱٫۵ 1.5
    \text{0}\text{.}\text{5} 0.5
    Unmatched { or } balanced automatically
  • RTL-safe: keeps KaTeX metrics isolated from the surrounding font/line-height so math renders correctly inside Persian/Arabic text and doesn't trigger flutter_math_fork layout assertions.
  • hideTrailingIncompleteMath flag for streaming / typewriter animations.
  • Selectable text, custom error builders, text scaling, and per-rule sanitizer options.

Installation #

dependencies:
  smart_latex: ^2.0.0

Or simply run:

flutter pub add smart_latex

No dependency_overrides and no separate math package are required — the layout-fixed rendering engine is bundled.

Usage #

import 'package:smart_latex/smart_latex.dart';

// Inline math mixed with text
SmartLatexText(r'The answer is $\frac{1}{2}$.')

// RTL text with a formula
SmartLatexText(
  r'قضیه فیثاغورس: $a^2 + b^2 = c^2$',
  textDirection: TextDirection.rtl,
  style: const TextStyle(fontSize: 16, height: 1.8),
)

// A single expression
SmartMath(r'e^{i\pi} + 1 = 0', display: true)

// Streaming / animated text — hides an unclosed formula while it is typed
SmartLatexText(currentStreamedText, hideTrailingIncompleteMath: true)

Sanitize without rendering #

final clean = sanitizeLatex(r'\frac{{\text{0}\text{٫}\overline{\text{3}}}}{{1}}');
// → r'\frac{{0.\overline{3}}}{{1}}'

Pick which rules run:

sanitizeLatex(
  source,
  options: const LatexSanitizeOptions(
    normalizeDigits: false, // keep Persian digits
  ),
);

Configuration #

SmartLatexText parameters:

Parameter Default Description
style null Base text style (merged over DefaultTextStyle).
textDirection ltr Text direction of the surrounding text.
textAlign start Alignment of the text.
selectable false Make plain-text runs selectable.
sanitize true Run sanitizeLatex on each math segment.
sanitizeOptions all on Which sanitizer rules to apply.
hideTrailingIncompleteMath false Hide an unclosed trailing $.
scrollDisplayMath true Horizontally scroll wide block equations.
errorBuilder null Custom widget when a formula fails to render.
textScaler inherited Scale factor for both text and math.

Using with gpt_markdown #

smart_latex pairs well with full Markdown renderers. Hand each LaTeX segment to SmartMath, which sanitizes the input and forwards only fontSize/color to the bundled engine — no flutter_math_fork import needed:

import 'package:gpt_markdown/gpt_markdown.dart';
import 'package:smart_latex/smart_latex.dart';

GptMarkdown(
  source,
  useDollarSignsForLatex: true,
  latexWorkaround: sanitizeLatex,
  latexBuilder: (ctx, tex, style, inline) => SmartMath(
    tex,
    display: !inline,
    style: style,
  ),
)

Why a bundled engine? #

Upstream flutter_math_fork can throw a layout assertion (RenderResetDimension does not meet its constraints) for some complex formulas — most visibly \sqrt nested inside \frac with \left(...\right) delimiters. The root cause is in its layout code, which sets sizes without clamping them to the incoming constraints, so it could not be worked around from the outside.

smart_latex ships a vendored, layout-fixed copy of that engine inside the package, so this crash is resolved out of the box. See PATCHES.md for the exact changes.

License #

smart_latex itself is released under the MIT License.

It bundles a modified copy of flutter_math_fork, which is licensed under Apache-2.0. Its license is retained at LICENSE-flutter_math_fork as required.

1
likes
0
points
267
downloads

Publisher

verified publishertartanak.com

Weekly Downloads

Self-contained LaTeX rendering for Flutter. Renders mixed text and math with a bundled, layout-fixed KaTeX engine — no extra math package required. Repairs malformed input (double backslashes, Persian/Arabic digits, unbalanced braces, single-char \text{}) and works cleanly inside custom-font and RTL layouts.

Repository (GitHub)
View/report issues

Topics

#latex #math #katex #rtl #rendering

License

unknown (license)

Dependencies

collection, flutter, flutter_svg, meta, provider, tuple

More

Packages that depend on smart_latex