Skip to content

feat(ios): integrate RaTeX for LaTeX math rendering, replacing iosMath#357

Open
hryhoriiK97 wants to merge 9 commits into
mainfrom
chore/ios/replace-ios-math-with-ratex
Open

feat(ios): integrate RaTeX for LaTeX math rendering, replacing iosMath#357
hryhoriiK97 wants to merge 9 commits into
mainfrom
chore/ios/replace-ios-math-with-ratex

Conversation

@hryhoriiK97

@hryhoriiK97 hryhoriiK97 commented May 27, 2026

Copy link
Copy Markdown
Collaborator

What/Why?

Replace iosMath with RaTeX for iOS LaTeX math rendering, aligning iOS with the Android migration (PR #355).

iosMath only supports a limited subset of LaTeX and is no longer actively maintained. RaTeX is a modern, Rust-based, KaTeX-compatible engine that renders math natively via CoreGraphics — no WebView, no JavaScript. This gives iOS feature parity with web (KaTeX) and Android (RaTeX).

Changes

New file:

  • ios/math/ENRMRaTeXBridge.swift — Swift-to-ObjC bridge exposing ENRMRaTeXBridge.parse() and ENRMRaTeXRenderResult (width, ascent, descent, draw) to existing Objective-C code.

Podspec (ReactNativeEnrichedMarkdown.podspec):

  • Removed iosMath dependency.
  • Added conditional spm_dependency for RaTeX v0.1.9+ (requires cocoapods-spm plugin).
  • Swift source files conditionally included only when math is enabled.
  • C/C++ headers marked as private to prevent module map conflicts with use_frameworks!.

Migrated ObjC files:

  • ENRMMathContainerView.m — Replaced MTMathUILabel with a custom ENRMRaTeXCanvasView that draws via ENRMRaTeXRenderResult. Added alignment support (center/left/right) and correct overflow scrolling.
  • ENRMMathInlineAttachment.m — Replaced MTMathUILabel with ENRMRaTeXBridge.parse(). Uses ceil() for dimension rounding to prevent sub-pixel clipping.
  • ENRMMathInlineAttachment+macOS.m — Same migration for macOS. Added CTM flip since RaTeX expects top-left origin (UIKit-style) but NSImage.lockFocus uses bottom-left origin.
  • ENRMMathInlineAttachmentShared.h — Replaced MTMathListDisplay ivar with ENRMRaTeXRenderResult.
  • ENRMFeatureFlags.h — Auto-detection now checks for RaTeX headers instead of iosMath.

Example app & docs:

  • Podfile — Sets USE_FRAMEWORKS=dynamic when math is enabled (required for SPM interop).
  • LATEX_MATH.md — Updated references from iosMath to RaTeX, documented use_frameworks! requirement.

Consumer impact

  • Math disabled (ENRICHED_MARKDOWN_ENABLE_MATH=0): No change — no RaTeX dependency, no dynamic frameworks required.

Testing

Screenshots

Screen.Recording.2026-05-27.at.15.28.33.mov

PR Checklist

  • Code compiles and runs on iOS
  • Code compiles and runs on Android
  • Updated documentation/README if applicable
  • Ran example app to verify changes
  • E2E tests are passing
  • Required E2E tests have been added (if applicable)

@hryhoriiK97 hryhoriiK97 requested a review from eszlamczyk May 27, 2026 14:07
@hryhoriiK97 hryhoriiK97 marked this pull request as ready for review May 27, 2026 14:08

@eszlamczyk eszlamczyk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just one nit

Comment thread ReactNativeEnrichedMarkdown.podspec Outdated
Comment on lines +22 to +26
if enable_math
s.source_files = "ios/**/*.{h,m,mm,cpp,swift}", "cpp/md4c/*.{c,h}", "cpp/parser/*.{hpp,cpp}"
else
s.source_files = "ios/**/*.{h,m,mm,cpp}", "cpp/md4c/*.{c,h}", "cpp/parser/*.{hpp,cpp}"
end

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Im wondering if we shouldn't futureproof it. For example what if we add another dependency that needs swift bridge, then this code would scilently ignore this dependency when math is disabled.

Maybe something like this would be better?

Suggested change
if enable_math
s.source_files = "ios/**/*.{h,m,mm,cpp,swift}", "cpp/md4c/*.{c,h}", "cpp/parser/*.{hpp,cpp}"
else
s.source_files = "ios/**/*.{h,m,mm,cpp}", "cpp/md4c/*.{c,h}", "cpp/parser/*.{hpp,cpp}"
end
s.source_files = "ios/**/*.{h,m,mm,cpp,swift}", "cpp/md4c/*.{c,h}", "cpp/parser/*.{hpp,cpp}"
if enable_math
s.exclude_files = "ios/math/ENRMRaTeXBridge.swift"
end

eszlamczyk and others added 2 commits June 3, 2026 13:24
…oapods-spm

cocoapods-spm adds SWIFT_INCLUDE_PATHS on the ReactNativeEnrichedMarkdown target
pointing to the build directory, which contains include/module.modulemap defining
RaTeXFFI. With use_frameworks! :linkage => :dynamic, RaTeXFFI is also embedded in
its XCFramework, so the compiler finds it twice and raises "redefinition of module
'RaTeXFFI'". A before-compile script phase removes the duplicate entry from the
shared module map, leaving the XCFramework copy as the authoritative definition.
Also adds cocoapods-spm to the example app Gemfile for local bundle exec users.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@hryhoriiK97 hryhoriiK97 changed the title feat: integrate RaTeX for LaTeX math rendering, replacing iosMath feat(ios): integrate RaTeX for LaTeX math rendering, replacing iosMath Jun 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants