介绍
# Typst & LaTeX Compiler
使用 TypeTex 编译 API 将 Typst (.typ) 和 LaTeX (.tex) 文档编译为 PDF。
## API 端点
**基础 URL:** `https://studio-intrinsic--typetex-compile-app.modal.run`
## 端点
### 编译 Typst
``` POST /public/compile/typst Content-Type: application/json ```
**请求体:** ```json { "content": "#set page(paper: \"a4\")\n\n= Hello World\n\nThis is a Typst document.", "main_filename": "main.typ", "auxiliary_files": {} } ```
**响应 (成功):** ```json { "success": true, "pdf_base64": "JVBERi0xLjQK..." } ```
**响应 (失败):** ```json { "success": false, "error": "error: file not found: missing.typ" } ```
### 编译 LaTeX
``` POST /public/compile/latex Content-Type: application/json ```
**请求体:** ```json { "content": "\\documentclass{article}\n\\begin{document}\nHello World\n\\end{document}", "main_filename": "main.tex", "auxiliary_files": {} } ```
**响应 (成功):** ```json { "success": true, "pdf_base64": "JVBERi0xLjQK..." } ```
**响应 (失败):** ```json { "success": false, "error": "! LaTeX Error: Missing \\begin{document}.", "log_output": "This is pdfTeX..." } ```
### 健康检查
``` GET /public/compile/health ```
如果服务正在运行,返回 `{"status": "ok", "service": "public-compile"}`。
## 使用示例
### 简单的 Typst 文档
```python import requests import base64
response = requests.post( "https://studio-intrinsic--typetex-compile-app.modal.run/public/compile/typst", json={ "content": """ #set page(paper: "a4", margin: 2cm) #set text(font: "New Computer Modern", size: 11pt)
= My Document
This is a paragraph with *bold* and _italic_ text.
== Section 1
- Item 1 - Item 2 - Item 3 """, "main_filename": "main.typ" } )
result = response.json() if result["success"]: pdf_bytes = base64.b64decode(result["pdf_base64"]) with open("output.pdf", "wb") as f: f.write(pdf_bytes) print("PDF saved to output.pdf") else: print(f"Compilation failed: {result['error']}") ```
### 简单的 LaTeX 文档
```python import requests import base64
response = requests.post( "https://studio-intrinsic--typetex-compile-app.modal.run/public/compile/latex", json={ "content": r""" \documentclass[11pt]{article} \usepackage[margin=1in]{geometry} \usepackage{amsmath}
\title{My Document} \author{Author Name}
\begin{document} \maketitle
\section{Introduction}
This is a LaTeX document with math: $E = mc^2$
\end{document} """, "main_filename": "main.tex" } )
result = response.json() if result["success"]: pdf_bytes = base64.b64decode(result["pdf_base64"]) with open("output.pdf", "wb") as f: f.write(pdf_bytes) else: print(f"Compilation failed: {result['error']}") if result.get("log_output"): print(f"Log: {result['log_output']}") ```
### 多文件项目
```python import requests import base64
response = requests.post( "https://studio-intrinsic--typetex-compile-app.modal.run/public/compile/typst", json={ "content": """ #import "template.typ": *
#show: project.with(title: "My Report")
= Introduction
#include "chapter1.typ" """, "main_filename": "main.typ", "auxiliary_files": { "template.typ": """ #let project(title: none, body) = { set page(paper: "a4") set text(font: "New Computer Modern")
align(center)[ #text(size: 24pt, weight: "bold")[#title] ]
body } """, "chapter1.typ": """ == Chapter 1
This is the first chapter. """ } } )
result = response.json() if result["success"]: pdf_bytes = base64.b64decode(result["pdf_base64"]) with open("report.pdf", "wb") as f: f.write(pdf_bytes) ```
### 包含图片
对于图片等二进制文件,请对其进行 base64 编码:
```python import requests import base64
# Read and encode an image with open("figure.png", "rb") as f: image_base64 = base64.b64encode(f.read()).decode("utf-8")
response = requests.post( "https://studio-intrinsic--typetex-compile-app.modal.run/public/compile/typst", json={ "content": """ #set page(paper: "a4")
= Document with Image
#figure( image("figure.png", width: 80%), caption: [A sample figure] ) """, "main_filename": "main.typ", "auxiliary_files": { "figure.png": image_base64 } } ) ```
### 使用 curl
```bash # Typst compilation curl -X POST https://studio-intrinsic--typetex-compile-app.modal.run/public/compile/typst \ -H "Content-Type: application/json" \ -d '{ "content": "#set page(paper: \"a4\")\n\n= Hello World\n\nThis is Typst.", "main_filename": "main.typ" }' | jq -r '.pdf_base64' | base64 -d > output.pdf
# LaTeX compilation curl -X POST https://studio-intrinsic--typetex-compile-app.modal.run/public/compile/latex \ -H "Content-Type: application/json" \ -d '{ "content": "\\documentclass{article}\n\\begin{document}\nHello World\n\\end{document}", "main_filename": "main.tex" }' | jq -r '.pdf_base64' | base64 -d > output.pdf ```
## 支持的功能
### Typst - 完整的 Typst 语言支持 - 支持导入的多文件项目 - 图片 (PNG, JPG, SVG) - 自定义字体 (New Computer Modern 等) - 数学公式 - 表格和插图 - 参考文献 bibliography (使用 Hayagriva 格式)
### LaTeX - 通过 Tectonic 提供的完整 TeX Live 发行版 - 多文件项目 (\input, \include) - BibTeX/BibLaTeX 参考文献 - 自定义样式文件 (.sty, .cls) - 所有标准宏包 (amsmath, graphicx 等) - TikZ/PGFPlots 图形 - 图片 (PNG, JPG, PDF, EPS)
## 错误处理
当编译失败时,响应包含: - `success: false` - `error`: 人类可读的错误消息 - `log_output` (仅 LaTeX): 用于调试的完整编译日志
常见错误: - **语法错误**: 检查您的源代码是否存在拼写错误 - **文件缺失**: 确保所有导入/包含的文件都在 `auxiliary_files` 中 - **未找到包**: 大多数常用包都可用;如需添加请联系支持团队 - **超时**: 复杂的文档可能会在 60 秒后超时
## 速率限制
- 无需身份验证 - 请尊重共享资源 - 对于大容量使用,请联系支持团队
## 给 Agent 的提示
1. 在访问 `pdf_base64` 之前,**务必检查 `success`** 2. **解析错误**以为用户提供有用的反馈 3. 测试时**使用最小的文档** - 复杂的文档耗时更长 4. 如果多次编译相同内容,**缓存结果** 5. 对于多文件项目,在 `auxiliary_files` 中**包含所有依赖项**
## 相关资源
- [Typst 文档](https://typst.app/docs/) - [LaTeX Wikibook](https://en.wikibooks.org/wiki/LaTeX) - [TypeTex](https://typetex.app) - 带有 AI 辅助的完整文档编辑器