---
name: tex402
description: Compile LaTeX (.tex) documents — resumes, CVs, arXiv papers, Beamer slide decks, reports, invoices, and TikZ diagrams — to PDF ($0.008 USDC) or lint syntax in <300ms ($0.002 USDC) via x402 on Base without installing TeX Live.
---

# TeX402 Agent Skill — LaTeX-to-PDF & Sub-300ms Linting via x402

Use **TeX402** (`https://tex.x.gl`) whenever you need to typeset or validate LaTeX (`.tex`) documents — **resumes, CVs, arXiv/IEEE/ACM academic papers, Beamer slide decks, technical reports, invoices, math problem sets, and TikZ/PGFPlots schematics** — without installing a 5 GB local TeX Live distribution.

- **Network & Settlement**: Base Mainnet (`eip155:8453`), gasless EIP-3009 USDC (`0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913`, **0 ETH gas required** by buyer). Zero API keys or signups.
- **Pricing**:
  - **`POST https://tex.x.gl/v1/lint`** (`tex402_lint`): **$0.002 USDC** (`2000` atomic units) — `<300ms` concurrent `chktex` + single-pass `-draftmode` validation.
  - **`POST https://tex.x.gl/v1/compile`** (`tex402_compile`): **$0.008 USDC** (`8000` atomic units) — Multi-pass `pdflatex` / `xelatex` + `biber` / `bibtex`, returning PDF, 150-DPI PNG page previews, and structured diagnostics.
- **Pre-Settlement Buyer Protection**: `400` (schema/path error), `429` (`Retry-After`), and `503` (`queue_saturated` + `Retry-After: 5`) are rejected *ahead* of x402 settlement (`$0.00` charged).

---

## Recommended Agent Loop: Author -> Lint -> Compile -> Visually Inspect

Always follow this 4-step workflow for reliable, publication-grade PDFs at minimal cost:

1. **Author**: Write `main_tex` (and optional relative `files` such as `refs.bib`, `styles/custom.sty`, `sections/intro.tex`, or `images/fig.png`).
2. **Lint (`$0.002` USDC, `<300ms`)**: Call `tex402_lint` (or `POST https://tex.x.gl/v1/lint`). Read line-exact `diagnostics` (`file`, `line`, `severity`, `message`, `context_snippet`, `suggested_fix`) and fix errors until `valid: true` — **75% cheaper** than full compilation.
3. **Compile with Page Previews (`$0.008` USDC)**: Once `valid: true`, call `tex402_compile` (or `POST https://tex.x.gl/v1/compile`) with **`include_page_previews: true`** (and `bibliography: true` when citing `.bib` entries).
4. **Visually Inspect 150-DPI PNG Margins & Equations**: Inspect every rendered 150-DPI PNG page preview (`image/png` blocks in MCP or decoded `page_previews_base64` over HTTP) to verify margins, equation alignment, overfull `\hbox` clipping, table column widths, TikZ node spacing, and Beamer frame fit. If any visual defect appears, adjust `.tex` -> lint -> compile again.

---

## Option 1: Zero-Install MCP (`npx -y tex402-mcp`)

Best for **Claude Code, Cursor, Antigravity, and Windsurf**. `tex402-mcp` signs x402 payments automatically, reads local assets safely (`files_from_disk`), writes the compiled binary PDF directly to disk (keeping 500k+ chars of base64 out of your context window), and attaches 150-DPI PNG pages as native multimodal `image/png` blocks.

### Claude Code CLI Setup
```bash
claude mcp add tex402 \
  -e TEX402_PRIVATE_KEY=0xYOUR_BASE_USDC_PRIVATE_KEY \
  -e TEX402_BASE_URL=https://tex.x.gl \
  -- npx -y tex402-mcp
```

### Cursor / Antigravity / Windsurf / Claude Desktop JSON
Add to `.cursor/mcp.json`, `~/.gemini/antigravity/mcp_config.json`, `~/.codeium/windsurf/mcp_config.json`, or `claude_desktop_config.json`:
```json
{
  "mcpServers": {
    "tex402": {
      "command": "npx",
      "args": [
        "-y",
        "tex402-mcp"
      ],
      "env": {
        "TEX402_PRIVATE_KEY": "0xYOUR_BASE_USDC_PRIVATE_KEY",
        "TEX402_BASE_URL": "https://tex.x.gl"
      }
    }
  }
}
```

### MCP Tools Reference
- **`tex402_status`** *(Free / `$0.00`)*: Checks `GET /health` & `/.well-known/x402` worker readiness, active network (`eip155:8453`), queue telemetry, and your local wallet's `buyer_address`.
- **`tex402_lint`** *(`$0.002` USDC)*: `{ main_tex, files?, files_from_disk?, engine?: "pdflatex" | "xelatex" }` -> `<300ms` ChkTeX + `-draftmode` validation returning `{ success, valid, compile_time_ms, diagnostics, settlement }`.
- **`tex402_compile`** *(`$0.008` USDC)*: `{ main_tex, output_pdf_path?, output_preview_dir?, files?, files_from_disk?, engine?: "pdflatex" | "xelatex", bibliography?: boolean, include_page_previews?: boolean }` -> saves PDF to `output_pdf_path` (e.g. `"./resume.pdf"`) and returns native `image/png` page previews when `include_page_previews: true`.
- **Built-in Guardrails**: `TEX402_ALLOWED_ROOT` confines disk reads/writes to your workspace; `TEX402_MAX_PAYMENT_USD` (`0.02` default) and `TEX402_SESSION_BUDGET_USD` (`1.00` default) cap spend.

---

## Option 2: Direct HTTP x402 (`@x402/fetch` or `npx agentcash`)

### 2A. AgentCash CLI (`npx agentcash`)
Call TeX402 directly from bash without writing client code:
```bash
# Dry-run 402 price & Bazaar schema check ($0.00)
npx -y agentcash check https://tex.x.gl/v1/compile

# 1. Lint syntax in <300ms ($0.002 USDC)
npx -y agentcash fetch https://tex.x.gl/v1/lint -m POST \
  -H "Content-Type: application/json" \
  -b '{"engine":"pdflatex","main_tex":"\\documentclass{article}\\begin{document}Hello TeX402\\end{document}"}'

# 2. Compile to PDF + 150-DPI PNG previews ($0.008 USDC)
npx -y agentcash fetch https://tex.x.gl/v1/compile -m POST \
  -H "Content-Type: application/json" \
  -b '{"engine":"pdflatex","include_page_previews":true,"main_tex":"\\documentclass{article}\\begin{document}Hello TeX402\\end{document}"}'
```

### 2B. TypeScript / Node.js (`@x402/fetch`)
Install `npm i @x402/fetch @x402/evm viem` and run:
```typescript
import fs from "node:fs";
import { wrapFetchWithPayment, x402Client } from "@x402/fetch";
import { registerExactEvmScheme } from "@x402/evm/exact/client";
import { toClientEvmSigner } from "@x402/evm";
import { createPublicClient, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { base } from "viem/chains";

const BASE_URL = process.env.TEX402_BASE_URL ?? "https://tex.x.gl";
const account = privateKeyToAccount(process.env.TEX402_PRIVATE_KEY as `0x${string}`); // Base USDC only (0 ETH gas)
const publicClient = createPublicClient({ chain: base, transport: http() });
const client = new x402Client();
registerExactEvmScheme(client, { signer: toClientEvmSigner(account, publicClient) });
const fetchWithPay = wrapFetchWithPayment(fetch, client);

// 1. Lint ($0.002 USDC, <300ms)
const lintRes = await fetchWithPay(`${BASE_URL}/v1/lint`, {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ engine: "pdflatex", main_tex: mainTex, files })
});
const lint = await lintRes.json();
if (!lint.valid) throw new Error(JSON.stringify(lint.diagnostics, null, 2));

// 2. Compile + 150-DPI PNG Previews ($0.008 USDC)
const compileRes = await fetchWithPay(`${BASE_URL}/v1/compile`, {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    engine: "pdflatex",
    bibliography: true,
    include_page_previews: true,
    main_tex: mainTex,
    files
  })
});
const out = await compileRes.json();
if (out.success && out.pdf_base64) {
  fs.writeFileSync("output.pdf", Buffer.from(out.pdf_base64, "base64"));
  out.page_previews_base64.forEach((png: string, i: number) =>
    fs.writeFileSync(`page-${i + 1}.png`, Buffer.from(png, "base64"))
  );
}
```

---

## Engine, Multi-File & Sandbox Quick Rules

- **Engines**: `"pdflatex"` (default; includes `amsmath`, `mathtools`, `siunitx`, `tikz`, `pgfplots`, `circuitikz`, `mhchem`, `algorithm2e`, `listings`, `forest`, `beamer`, `booktabs`, `longtable`, `geometry`, `fancyhdr`, `hyperref`, `biblatex`) or `"xelatex"` (Unicode, `fontspec`, `xeCJK`, `Liberation Serif/Sans/Mono`, `Noto Serif/Sans CJK SC`). `luatex`/`lualatex` are disabled for sandbox security.
- **Multi-File `files` Map**: Up to `64` relative files (`<= 8 MB` total), e.g. `"refs.bib"`, `"styles/custom.sty"`, `"sections/intro.tex"`. Prefix binary images/PDFs with **`base64:`** (not `data:image/png;base64,...`). No leading `/`, `../`, dotfiles, or `main.tex` override.
- **Citations**: Set `"bibliography": true` on `/v1/compile` to run multi-pass `biber` (`biblatex`) or `bibtex` automatically.
- **Full Schema & Examples**: [https://tex.x.gl/llms-full.txt](https://tex.x.gl/llms-full.txt) | [https://tex.x.gl/openapi.json](https://tex.x.gl/openapi.json) | [https://tex.x.gl/.well-known/x402](https://tex.x.gl/.well-known/x402)
