Lemma: Cursor for LaTeX

Inspiration

For students, LaTeX is everywhere: writing homework assignments with complex equations, formatting resumes for internships, collaborating on research papers with professors, and preparing lab reports. As software engineers gained AI-powered coding assistants like Cursor that could understand their code, fix bugs autonomously, and write entire features, we saw an opportunity to transform technical writing in LaTeX.

We asked ourselves: Why didn't LaTeX writers have this?

Traditional LaTeX editors such as Overleaf are excellent for collaboration and compilation but lack intelligent assistance. Lemma changes that by bringing Claude’s reasoning and autonomy directly into the academic writing process, whether it is a problem set due at midnight or a research paper submission.

What We Built

Lemma is "Cursor for Overleaf," a fork of Overleaf Community Edition enhanced with Anthropic’s Claude AI. The system can read documents, understand LaTeX syntax and academic writing conventions, fix compilation errors, make targeted edits, compile to verify changes, and iterate autonomously until the document is complete.

Lemma is built on a microservices architecture running on AWS, combining MongoDB Atlas, ElastiCache Redis, and S3 for scalable performance. By leveraging Overleaf’s robust real-time collaboration, compilation pipeline, and version control, we focused our efforts on AI innovation rather than rebuilding the editor from scratch.

At its core is a recursive agent loop: Claude can list files, read content, edit documents, compile projects, check for errors, and search the web. It performs up to 30 autonomous iterations, learning from each result and refining the output until success.

Challenges and Learnings

EC2 Microservices Setup

Configuring all services to communicate correctly on EC2 was our first major challenge. Docker networking, environment variables, MongoDB connections, and Redis configuration each introduced potential failure points. After days of debugging, we learned the value of infrastructure-as-code and consistent environment management.

S3 Image Storage

S3 file storage proved more complex than expected. We encountered timeouts, CORS issues, and access policy problems before mastering presigned URLs, bucket policies, and IAM roles. Serving images reliably in compiled PDFs was especially challenging compared to local file storage.

Agent Tool Call Design

Our agent initially made inefficient decisions such as reading entire files or compiling too early. We iterated through multiple tool configurations before finding the right balance between flexibility and structure. Targeted, purpose-driven tools like read_file, edit_file, and compile significantly improved performance and accuracy.

System Prompt Engineering

Early prompt versions produced unpredictable results. We learned that specificity is crucial. Clear, step-by-step instructions such as “read the error output, identify the line number, check that specific line, and make a targeted edit” improved success rates dramatically. Including examples of good tool usage in the system prompt also helped the model learn effective patterns.

User Control vs Autonomy

Full autonomy initially overwhelmed users who wanted to understand the agent’s edits. We introduced two modes:

  • Ask Mode: Explanations only, ideal for learning LaTeX.
  • Agent Mode: Autonomous editing with real-time diff views and live action streaming for full transparency.

Future Directions

LEAN Formal Verification

Integrating the LEAN theorem prover will enable mathematical proof verification within LaTeX documents. Students will be able to validate logical correctness before submission, extending Lemma beyond syntax to semantic validation.

PDF Screenshot Tool

We plan to add a seamless screenshot system for capturing figures from PDFs, textbooks, or lecture slides and inserting them with proper formatting and captions. This will streamline lab reports and visual references.

RAG on LaTeX

We are developing Retrieval-Augmented Generation (RAG) tuned to LaTeX documentation, Stack Overflow solutions, and academic writing standards. This will help Claude access domain-specific knowledge for obscure packages and edge cases.

Our current focus is optimizing the agent iteration strategy to balance efficiency and accuracy. Empirical testing suggests that 15 iterations is the optimal balance between cost and reliability, keeping Lemma affordable for students without compromising results.

Technical Stack

Frontend: React, TypeScript, CodeMirror, WebSocket
Backend: Node.js, Express, Bull, Socket.io
AI: Anthropic Claude Sonnet 4 with Tool Use API
Databases: MongoDB Atlas, Redis ElastiCache
Storage: AWS S3 with multi-region replication
Compilation: TexLive 2025 with Docker sandboxing
Infrastructure: Docker, Kubernetes, Nginx, AWS EC2

Acknowledgments

Lemma would not exist without the Overleaf team for their open-source foundation, Anthropic for Claude’s exceptional LaTeX capabilities, AWS for reliable scalable infrastructure, and the LaTeX community for decades of tools and documentation.

Built With

Share this project:

Updates