Skip to content

acemoglu/LaTeXBoard

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LaTeXBoard


LaTeXBoard is a custom iOS keyboard extension designed to make communicating with math and science easier. It instantly parses, previews, and renders LaTeX code directly within your keyboard, allowing you to copy the result as a PNG and send it in any chat application.

🚀 Currently available exclusively for iOS. An Android version is in the roadmap for future releases.

Features

  • Paste LaTeX: Paste LaTeX from the current input context and preview it instantly inside the keyboard.
  • Image Export: Once your formula is ready, tap "Send" to render the preview into a high-quality UIImage and automatically copy it to your general pasteboard (UIPasteboard).
  • Universal Chat Support: Works across WhatsApp, Telegram, Discord, iMessage, and any other app that supports image pasting.
  • 100% Offline & On-Device: All parsing and rendering are done locally on your device. No network requests are made.

Usage Example

LaTeXBoardPreview.mp4

Supported LaTeX Features

Our custom parsing engine supports a wide array of mathematical notations:

  • Fractions & Roots: \frac, \dfrac, \sqrt, nth roots, etc.
  • Subscripts & Superscripts: ^, _, and combined usage.
  • Big Operators: \sum, \int, \prod, \bigcup (with limits).
  • Matrices & Environments: matrix, pmatrix, bmatrix, array.
  • Brackets & Sizing: \left, \right, \middle, \Big, etc.
  • Quantum Mechanics: \bra, \ket, \braket.
  • Styling: \textbf{}, \mathit{}, \color, \colorbox.
  • Extensive Symbol Map: Greek letters, arrows, relations, and operators.

Technical Architecture & Performance (Deep Dive)

Building an iOS Custom Keyboard Extension (UIInputViewController) requires operating under extremely strict system memory limits (the Jetsam limit is typically ~30-50 MB). LaTeXBoard is built from the ground up for stability and speed, featuring a completely native pipeline:

1. Project Structure

  • Host App: SwiftUI-based onboarding, guide, and redirection to Settings.
  • Keyboard Extension: A UIInputViewController-based interface, responsible for the entire parsing, rendering, and clipboard workflow.

2. Rendering Pipeline

  • Plain TeX Normalization: Before tokenization, the parser converts Plain TeX variants (e.g., \over\frac, \atop\atopfrac) into standard LaTeX commands for consistency.
  • Tokenization: A single-pass tokenizer converts input into structured LaTeXToken enums.
  • Native Core Graphics Draw: We don't use web views or JavaScript engines. LaTeXBoard draws formulas directly to the screen using Core Graphics (drawFraction, drawIntegral, drawMatrix, drawXarrow, etc.), ensuring zero input lag and memory efficiency.

3. RAM & Performance Strategy

iOS memory warnings or a "Jetsam" crash occur if memory usage spikes too high. We mitigate this through:

  • NSCache-Based Caching: Tokens and fonts are cached (tokenCache, fontCache) with strict count limits (maxCachedTokenCount). This prevents re-parsing the same LaTeX frequently.
  • Aggressive Memory Reuse: Cache is cleared immediately upon memory warnings or if didReceiveMemoryWarning is called. NSCache automatically evicts before the system kills the process.
  • Strict View Recycling: UI components within the keyboard extension are recycled.
  • Autorelease Pools: Memory-intensive sections of the rendering code are wrapped in autoreleasepool blocks to release temporary allocations immediately.
  • PNG Export Caps: Exported images are capped at a max size (600x400 at 2x scale) for guaranteed stability during final render.

Privacy & Full Access

Full Access is required to write rendered PNG output to UIPasteboard.

Why does it need "Full Access"? iOS requires "Full Access" (RequestsOpenAccess = true) for keyboard extensions to read or write to the general clipboard (UIPasteboard). LaTeXBoard uses this permission only to copy the final rendered PNG image to your pasteboard so you can paste it into your chat input.

Technical Note: This is defined in the Info.plist by setting RequestsOpenAccess to true for the NSExtensionPrincipalClass (KeyboardViewController).

Installation & Build

From the App Store

  1. Download on the App Store
  2. Follow the onboarding guide to enable the keyboard in Settings > General > Keyboard > Keyboards.

Building from Source (Technical)

  1. Clone the repository:
    git clone https://github.com/acemoglu/LaTeXBoard.git
  2. Open the project in Xcode (LaTeXBoard.xcodeproj).
  3. Select the LaTeXBoard scheme and build for your target device (iOS 16.0 deployment target, Swift 5.0).
  4. On your device, follow the same Settings steps to enable LaTeXBoard.
  5. Be sure to toggle Allow Full Access for clipboard functionality.

Usage

  1. Open any messaging application.
  2. Switch to LaTeXBoard using the globe icon.
  3. Tap Paste & Preview to capture text around the cursor and render supported LaTeX.
  4. Review the natively rendered math on the keyboard preview.
  5. Tap Send (PNG). The keyboard will render the equation as an image and copy it to your clipboard.
  6. Paste the image into the chat input and send!

Known Limitations

  • Not a full TeX distribution: LaTeXBoard uses a custom, lightweight native parser/renderer optimized for iOS keyboard extensions. Some advanced package-dependent macros (e.g., TikZ or complex custom macro stacks) may not be fully supported.
  • Performance-first rendering scope: Feature support is intentionally bounded to keep latency low and memory usage stable under keyboard extension limits.
  • PNG export size cap: Exported images are capped to a safe maximum size to reduce memory spikes and avoid extension crashes on lower-memory devices.

License

This project is licensed under the MIT License.

About

LaTeXBoard is a custom iOS keyboard extension designed to make communicating with math and science easier. It instantly parses, previews, and renders LaTeX code directly within your keyboard, allowing you to copy the result as a PNG and send it in any chat application.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages