A backend application built with Express.js that converts mathematical expressions in uploaded images into LaTeX code using the Google Gemini API.
This project exposes an API endpoint that accepts up to 5 image files at once, sends them to Gemini for analysis, and returns a LaTeX document that can be used in tools like Overleaf.
- Convert handwritten or printed math expressions in images to LaTeX
- Upload up to 5 images in a single request
- Uses Google Gemini for image understanding and LaTeX generation
- Saves generated
.texfiles to anoutput/directory - Automatically cleans up uploaded temporary files
- Built with Node.js, Express, and Multer
- Backend: Node.js, Express.js
- AI Model: Google Gemini (
gemini-2.0-flash) - File Uploads: Multer
- Environment Variables: dotenv
- Utilities: fs, path, mime-types
Before running the project, make sure you have:
- Node.js 18+
- npm
- A valid Google Gemini API key
- Clone the repository:
git clone https://github.com/NTiger07/gemini-latex.git
cd gemini-latex- Install dependencies:
npm install- Create a
.envfile in the project root:
GOOGLE_GEMINI_API_KEY=your_gemini_api_key_here
PORT=3000npm run devnpm startThe server will run on the port defined in .env, or 3000 by default.
Uploads image files and returns generated LaTeX code.
- Method:
POST - Content-Type:
multipart/form-data - Field name:
file - Max files: 5
curl -X POST http://localhost:3000/upload \
-F "file=@image1.png" \
-F "file=@image2.png"{
"latexCode": "\\documentclass{article}\\begin{document}...\\end{document}"
}- The client uploads one or more images to the
/uploadendpoint. - Multer stores the images temporarily in the
uploads/folder. - The server converts each image into a Gemini-compatible input.
- Gemini generates a minimal complete LaTeX document.
- The server writes the output to a timestamped
.texfile inside theoutput/folder. - Temporary uploaded files are deleted after processing.
.
├── app.js
├── package.json
├── package-lock.json
├── uploads/
└── output/
- The current implementation expects the uploaded files to be sent using the field name
file. - The project supports up to 5 images per request.
- Make sure the Gemini API key is valid, otherwise the conversion will fail.
- Generated LaTeX is intended to be compatible with Overleaf.
The API returns errors in cases such as:
- No file uploaded
- Gemini API failure
- File processing failure
Example error response:
{
"error": "Failed to convert to Latex"
}- Add a frontend UI for uploading images
- Improve prompt tuning for more accurate LaTeX output
- Support PDF uploads
- Add OCR pre-processing and image cleanup
- Add authentication and rate limiting