FormaTeX

\usepackage{claude}

Claude LaTeX API Integration

Combine the Anthropic Python SDK with FormaTeX to generate professional PDFs entirely from AI. Claude writes the LaTeX; FormaTeX compiles it — two API calls, one PDF.

\section{Why Claude}

Why Claude + FormaTeX?

Claude writes valid LaTeX

Claude excels at producing syntactically correct LaTeX with proper document classes, packages, and mathematical notation.

FormaTeX compiles reliably

FormaTeX runs a full TeX Live distribution server-side. pdflatex, xelatex, lualatex — all engines available via a single REST call.

No local TeX installation

No TeX Live setup on your machine or server. Compilation is fully managed, with ephemeral sandboxed environments per request.

Production-grade output

Get pixel-perfect PDFs with proper hyphenation, kerning, and font rendering — the same quality as a local LaTeX install.

\section{Step 1}

Step 1 — Generate LaTeX with Claude

Prompt Claude with a system message instructing it to respond with raw LaTeX source only. The system prompt is key: without it Claude may wrap the output in markdown fences or add explanatory text.

step1_generate.py
import anthropic

client = anthropic.Anthropic(api_key="your-anthropic-key")

SYSTEM_PROMPT = """You are a LaTeX expert. When asked to create a document,
respond ONLY with valid LaTeX source code — no markdown fences, no explanation.
Always include \\documentclass, preamble, and \\begin{document}...\\end{document}."""

def generate_latex(prompt: str) -> str:
    """Ask Claude to write a complete LaTeX document."""
    message = client.messages.create(
        model="claude-opus-4-5",
        max_tokens=4096,
        system=SYSTEM_PROMPT,
        messages=[{"role": "user", "content": prompt}],
    )
    return message.content[0].text

\section{Step 2}

Step 2 — Compile to PDF with FormaTeX

Pass the LaTeX string returned by Claude directly to the FormaTeX compile endpoint. The response is raw PDF bytes — save them to disk or stream them to the client.

step2_compile.py
import requests

FORMATEX_API_KEY = "your-formatex-key"
FORMATEX_URL = "https://api.formatex.io/v1/compile/sync"

def compile_to_pdf(latex_source: str, engine: str = "pdflatex") -> bytes:
    """Send LaTeX source to FormaTeX and return PDF bytes."""
    response = requests.post(
        FORMATEX_URL,
        headers={
            "Authorization": f"Bearer {FORMATEX_API_KEY}",
            "Content-Type": "application/json",
        },
        json={"source": latex_source, "engine": engine},
        timeout=30,
    )
    response.raise_for_status()
    return response.content

\section{Complete Example}

Complete example

The full end-to-end script combines both steps. Call generate_and_compile with any natural-language prompt and receive a PDF file on disk.

claude_formatex.py
import anthropic
import requests

anthropic_client = anthropic.Anthropic(api_key="your-anthropic-key")
FORMATEX_URL = "https://api.formatex.io/v1/compile/sync"
FORMATEX_API_KEY = "your-formatex-key"

SYSTEM_PROMPT = """You are a LaTeX expert. Respond ONLY with valid LaTeX source.
Always include \\documentclass, full preamble, and \\begin{document}...\\end{document}."""

def generate_and_compile(prompt: str, engine: str = "pdflatex") -> bytes:
    # Step 1: Claude writes the LaTeX
    message = anthropic_client.messages.create(
        model="claude-opus-4-5",
        max_tokens=4096,
        system=SYSTEM_PROMPT,
        messages=[{"role": "user", "content": prompt}],
    )
    latex_source = message.content[0].text

    # Step 2: FormaTeX compiles to PDF
    response = requests.post(
        FORMATEX_URL,
        headers={
            "Authorization": f"Bearer {FORMATEX_API_KEY}",
            "Content-Type": "application/json",
        },
        json={"source": latex_source, "engine": engine},
        timeout=30,
    )
    response.raise_for_status()
    return response.content


# Generate a research summary PDF
pdf_bytes = generate_and_compile(
    "Write a 2-page LaTeX article summarising transformer architecture, "
    "including the self-attention equation and a simple diagram using TikZ."
)

with open("transformer_summary.pdf", "wb") as f:
    f.write(pdf_bytes)

print("PDF saved to transformer_summary.pdf")

\end{claude}

Generate PDFs with Claude + FormaTeX

Get a FormaTeX API key, copy the complete example above, and ship AI-generated PDFs in minutes.

One quick thing

We track anonymous usage — page views, feature usage, compilation events — to understand what works and what doesn't. No ads, no personal data, no third-party sharing.

Cookie policy