
High-performance LaTeX math parser and renderer supporting full syntax, \newcommand macros, incremental parsing, chemical-formula (\ce{}) support, and extensive style customization (colors, boxes, display modes).
A high-performance LaTeX mathematical formula parsing and rendering library developed based on Kotlin Multiplatform (KMP). It supports consistent rendering effects on Android, iOS, Desktop (JVM), and Web (Wasm/JS) platforms.
\newcommand macro definitions, including parameter replacement (#1-#9), nested definitions, and command overriding.\ce{...} plugin.\color), boxes (\boxed), and math mode switching (\displaystyle, etc.).The project includes a Demo App (composeApp/androidApp) showcasing various complex LaTeX scenarios:
| Basic Math | Chemical Formulas | Incremental Parsing |
|---|---|---|
![]() |
![]() |
![]() |
| Basic Math Rendering | Supports \ce{...} syntax |
Real-time preview for incomplete input |
In a Compose Multiplatform project, you can use the Latex component directly. The component handles incremental parsing automatically and supports real-time preview:
import com.hrm.latex.renderer.Latex
import com.hrm.latex.renderer.model.LatexConfig
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.sp
@Composable
fun MyScreen() {
Latex(
latex = "\\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}",
config = LatexConfig(
fontSize = 20.sp,
color = Color.Black,
darkColor = Color.White // Automatic dark mode support
)
)
}For long formulas that need to wrap within the container width, use LatexAutoWrap:
import com.hrm.latex.renderer.LatexAutoWrap
@Composable
fun MyScreen() {
LatexAutoWrap(
latex = "E = mc^2 + \\frac{p^2}{2m} + V(x) + \\frac{1}{2}kx^2",
modifier = Modifier.fillMaxWidth(),
config = LatexConfig(fontSize = 20.sp)
)
}Line breaks occur at mathematically valid points: relation operators (=, <, >), then additive operators (+, -), then multiplicative operators (×, ÷). Atomic structures like fractions, roots, and matrices are never broken.
Export rendered LaTeX formulas as PNG, JPEG, or WEBP images. Use rememberLatexExporter() in a Composable scope, then call export() on a background thread:
import com.hrm.latex.renderer.export.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@Composable
fun MyScreen() {
val exporter = rememberLatexExporter()
val scope = rememberCoroutineScope()
Button(onClick = {
scope.launch(Dispatchers.Default) {
// Export as PNG (default, 2x resolution)
val result = exporter.export("E = mc^2")
val pngBytes = result?.bytes // PNG byte array
val bitmap = result?.imageBitmap // For in-app display
// Export as JPEG (3x resolution, quality 85)
val jpegResult = exporter.export(
latex = "\\frac{a}{b}",
exportConfig = ExportConfig(
scale = 3f,
format = ImageFormat.JPEG,
quality = 85
)
)
// Export with transparent background (PNG only)
val transparentResult = exporter.export(
latex = "x^2 + y^2 = r^2",
exportConfig = ExportConfig(transparentBackground = true)
)
}
}) {
Text("Export")
}
}ExportConfig parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
scale |
Float |
2f |
Resolution multiplier (1x, 2x, 3x, etc.) |
format |
ImageFormat |
PNG |
ImageFormat.PNG, ImageFormat.JPEG, or ImageFormat.WEBP
|
transparentBackground |
Boolean |
false |
Use transparent background (PNG and WEBP only; JPEG always uses opaque background) |
quality |
Int |
90 |
Compression quality (1–100) for JPEG and WEBP; ignored for PNG |
The library provides built-in accessibility support for screen readers. When enabled, each Latex component exposes a MathSpeak-style natural language description via Compose semantics, making math formulas readable by TalkBack (Android), VoiceOver (iOS), and other assistive technologies.
Latex(
latex = "\\frac{1}{2}",
config = LatexConfig(accessibilityEnabled = true)
)
// Screen reader reads: "fraction: 1 over 2"The AccessibilityVisitor converts the LaTeX AST into descriptive text covering fractions, roots, superscripts/subscripts, matrices, Greek letters, operators, and more.
Note: The editor API is experimental and may change in future versions. All editor APIs require the
@ExperimentalComposeUiApiannotation.
The library includes a built-in WYSIWYG (What You See Is What You Get) LaTeX editor component. Users can edit LaTeX source text and see the rendered formula in real-time, with cursor position synchronized between the source and the rendered output.
@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun MyEditor() {
val editorState = rememberEditorState(initialText = "x^{2} + y^{2} = r^{2}")
LatexEditor(
editorState = editorState,
config = LatexConfig(fontSize = 20.sp),
showSourceText = true // Show source text input field
)
}Add dependencies in gradle/libs.versions.toml:
[versions]
latex = "1.1.1"
[libraries]
latex-base = { module = "io.github.huarangmeng:latex-base", version.ref = "latex" }
latex-parser = { module = "io.github.huarangmeng:latex-parser", version.ref = "latex" }
latex-renderer = { module = "io.github.huarangmeng:latex-renderer", version.ref = "latex" }Reference in your module's build.gradle.kts:
dependencies {
implementation(libs.latex.base) // Basic logging
implementation(libs.latex.renderer) // Rendering logic
implementation(libs.latex.parser) // Parsing logic
}:latex-base: Base data structures and interfaces.:latex-parser: Core parsing engine, responsible for converting LaTeX strings to AST.:latex-renderer: Responsible for rendering AST into Compose UI components.:latex-preview: Preview components and sample datasets.:composeApp: Cross-platform Demo application.:androidApp: Android Demo application../gradlew :androidApp:assembleDebug
./gradlew :composeApp:run
./gradlew :composeApp:wasmJsBrowserDevelopmentRun
iosApp/iosApp.xcworkspace in Xcode to run../run_parser_tests.shFor a detailed list of supported features, please refer to: PARSER_COVERAGE_ANALYSIS.md
This project is licensed under the MIT License - see the LICENSE file for details.
MIT License
Copyright (c) 2026 huarangmeng
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
A high-performance LaTeX mathematical formula parsing and rendering library developed based on Kotlin Multiplatform (KMP). It supports consistent rendering effects on Android, iOS, Desktop (JVM), and Web (Wasm/JS) platforms.
\newcommand macro definitions, including parameter replacement (#1-#9), nested definitions, and command overriding.\ce{...} plugin.\color), boxes (\boxed), and math mode switching (\displaystyle, etc.).The project includes a Demo App (composeApp/androidApp) showcasing various complex LaTeX scenarios:
| Basic Math | Chemical Formulas | Incremental Parsing |
|---|---|---|
![]() |
![]() |
![]() |
| Basic Math Rendering | Supports \ce{...} syntax |
Real-time preview for incomplete input |
In a Compose Multiplatform project, you can use the Latex component directly. The component handles incremental parsing automatically and supports real-time preview:
import com.hrm.latex.renderer.Latex
import com.hrm.latex.renderer.model.LatexConfig
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.sp
@Composable
fun MyScreen() {
Latex(
latex = "\\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}",
config = LatexConfig(
fontSize = 20.sp,
color = Color.Black,
darkColor = Color.White // Automatic dark mode support
)
)
}For long formulas that need to wrap within the container width, use LatexAutoWrap:
import com.hrm.latex.renderer.LatexAutoWrap
@Composable
fun MyScreen() {
LatexAutoWrap(
latex = "E = mc^2 + \\frac{p^2}{2m} + V(x) + \\frac{1}{2}kx^2",
modifier = Modifier.fillMaxWidth(),
config = LatexConfig(fontSize = 20.sp)
)
}Line breaks occur at mathematically valid points: relation operators (=, <, >), then additive operators (+, -), then multiplicative operators (×, ÷). Atomic structures like fractions, roots, and matrices are never broken.
Export rendered LaTeX formulas as PNG, JPEG, or WEBP images. Use rememberLatexExporter() in a Composable scope, then call export() on a background thread:
import com.hrm.latex.renderer.export.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@Composable
fun MyScreen() {
val exporter = rememberLatexExporter()
val scope = rememberCoroutineScope()
Button(onClick = {
scope.launch(Dispatchers.Default) {
// Export as PNG (default, 2x resolution)
val result = exporter.export("E = mc^2")
val pngBytes = result?.bytes // PNG byte array
val bitmap = result?.imageBitmap // For in-app display
// Export as JPEG (3x resolution, quality 85)
val jpegResult = exporter.export(
latex = "\\frac{a}{b}",
exportConfig = ExportConfig(
scale = 3f,
format = ImageFormat.JPEG,
quality = 85
)
)
// Export with transparent background (PNG only)
val transparentResult = exporter.export(
latex = "x^2 + y^2 = r^2",
exportConfig = ExportConfig(transparentBackground = true)
)
}
}) {
Text("Export")
}
}ExportConfig parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
scale |
Float |
2f |
Resolution multiplier (1x, 2x, 3x, etc.) |
format |
ImageFormat |
PNG |
ImageFormat.PNG, ImageFormat.JPEG, or ImageFormat.WEBP
|
transparentBackground |
Boolean |
false |
Use transparent background (PNG and WEBP only; JPEG always uses opaque background) |
quality |
Int |
90 |
Compression quality (1–100) for JPEG and WEBP; ignored for PNG |
The library provides built-in accessibility support for screen readers. When enabled, each Latex component exposes a MathSpeak-style natural language description via Compose semantics, making math formulas readable by TalkBack (Android), VoiceOver (iOS), and other assistive technologies.
Latex(
latex = "\\frac{1}{2}",
config = LatexConfig(accessibilityEnabled = true)
)
// Screen reader reads: "fraction: 1 over 2"The AccessibilityVisitor converts the LaTeX AST into descriptive text covering fractions, roots, superscripts/subscripts, matrices, Greek letters, operators, and more.
Note: The editor API is experimental and may change in future versions. All editor APIs require the
@ExperimentalComposeUiApiannotation.
The library includes a built-in WYSIWYG (What You See Is What You Get) LaTeX editor component. Users can edit LaTeX source text and see the rendered formula in real-time, with cursor position synchronized between the source and the rendered output.
@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun MyEditor() {
val editorState = rememberEditorState(initialText = "x^{2} + y^{2} = r^{2}")
LatexEditor(
editorState = editorState,
config = LatexConfig(fontSize = 20.sp),
showSourceText = true // Show source text input field
)
}Add dependencies in gradle/libs.versions.toml:
[versions]
latex = "1.1.1"
[libraries]
latex-base = { module = "io.github.huarangmeng:latex-base", version.ref = "latex" }
latex-parser = { module = "io.github.huarangmeng:latex-parser", version.ref = "latex" }
latex-renderer = { module = "io.github.huarangmeng:latex-renderer", version.ref = "latex" }Reference in your module's build.gradle.kts:
dependencies {
implementation(libs.latex.base) // Basic logging
implementation(libs.latex.renderer) // Rendering logic
implementation(libs.latex.parser) // Parsing logic
}:latex-base: Base data structures and interfaces.:latex-parser: Core parsing engine, responsible for converting LaTeX strings to AST.:latex-renderer: Responsible for rendering AST into Compose UI components.:latex-preview: Preview components and sample datasets.:composeApp: Cross-platform Demo application.:androidApp: Android Demo application../gradlew :androidApp:assembleDebug
./gradlew :composeApp:run
./gradlew :composeApp:wasmJsBrowserDevelopmentRun
iosApp/iosApp.xcworkspace in Xcode to run../run_parser_tests.shFor a detailed list of supported features, please refer to: PARSER_COVERAGE_ANALYSIS.md
This project is licensed under the MIT License - see the LICENSE file for details.
MIT License
Copyright (c) 2026 huarangmeng
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.