名称: Typst & LaTeX 编译器
描述: 通过 API 将 Typst 和 LaTeX 文档编译为 PDF。发送源代码,返回 PDF。
元数据:
clawdbot:
config:
requiredEnv: []
stateDirs: []
使用 TypeTex 编译 API,将 Typst (.typ) 和 LaTeX (.tex) 文档编译为 PDF。
基础 URL: https://studio-intrinsic--typetex-compile-app.modal.run
POST /public/compile/typst
Content-Type: application/json
请求体:
{
"content": "#set page(paper: \"a4\")\n\n= Hello World\n\nThis is a Typst document.",
"main_filename": "main.typ",
"auxiliary_files": {}
}
响应(成功):
{
"success": true,
"pdf_base64": "JVBERi0xLjQK..."
}
响应(失败):
{
"success": false,
"error": "error: file not found: missing.typ"
}
POST /public/compile/latex
Content-Type: application/json
请求体:
{
"content": "\\documentclass{article}\n\\begin{document}\nHello World\n\\end{document}",
"main_filename": "main.tex",
"auxiliary_files": {}
}
响应(成功):
{
"success": true,
"pdf_base64": "JVBERi0xLjQK..."
}
响应(失败):
{
"success": false,
"error": "! LaTeX Error: Missing \\begin{document}.",
"log_output": "This is pdfTeX..."
}
GET /public/compile/health
如果服务正常运行,返回 {"status": "ok", "service": "public-compile"}。
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']}")
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']}")
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 编码:
import requests
import base64
# 读取并编码图片
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
}
}
)
# 编译 Typst
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
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
当编译失败时,响应包含:
- success: false
- error: 人类可读的错误信息
- log_output (仅限 LaTeX): 用于调试的完整编译日志
常见错误:
- 语法错误: 检查源代码中的拼写错误
- 文件缺失: 确保所有导入/包含的文件都在 auxiliary_files 中
- 宏包未找到: 大多数常用宏包已可用;如需添加请联系支持
- 超时: 复杂文档可能在 60 秒后超时
success 字段,再访问 pdf_base64auxiliary_files 中