AgentSkillsCN

typst

提供 Typst(.typ)文件编写的语法指南与生态参考。当您需要编写、编辑或调试 Typst 文档时,可使用此技能。内容涵盖核心语法、常见错误、常用包,以及最佳实践。

SKILL.md
--- frontmatter
name: typst
description: Syntax guide and ecosystem reference for writing Typst (.typ) files. Use this skill when writing, editing, or debugging Typst documents. Covers core syntax, common errors, packages, and best practices.
license: MIT

Typst Syntax & Ecosystem Guide

Use this skill when writing or editing Typst (.typ) files.

Current Typst version: 0.14.x (Dec 2025)

Documentation

Reference Files

What's New in 0.13-0.14

FeatureVerDescription
Tagged PDFs by default0.14Accessible PDFs out of the box, PDF/UA-1 support
figure.alt / image(alt:)0.14Alt text for screen readers
pdf.attach0.14Attach files to PDFs (replaces deprecated pdf.embed)
Character justification0.14par.justification-limits for microtypography
Multithreaded layout0.142-3x speedup for large documents
curve function0.13Bezier curve drawing (replaces deprecated path)
First-line indent0.13#set par(first-line-indent: 1em) with all: true

Deprecated Functions

  • path -> use curve instead (see layout-patterns.md)
  • pdf.embed -> use pdf.attach instead
  • image.decode -> pass bytes directly to image
  • polylux:0.3.1 is incompatible with Typst 0.14 -> use touying instead

Core Syntax

Arrays and Dictionaries

Typst uses parentheses for both (not square brackets like Python/JS):

typst
// Arrays - use ()
let colors = (red, green, blue)
let first = colors.at(0)        // NOT colors[0]
let length = colors.len()

// Dictionaries - use () with colons
let person = (name: "Alice", age: 30)
let name = person.name          // or person.at("name")

// WRONG - common mistakes
let arr = [1, 2, 3]             // This is a content block, not array!
let item = arr[0]               // Wrong access syntax

Content Blocks vs Code Blocks

typst
// Content blocks [] - for markup/text
[This is *bold* and _italic_ text]

// Code blocks {} - for logic
{
  let x = 5
  if x > 3 { "big" } else { "small" }
}

// In markup mode, use # to switch to code
This is text. #let x = 5 Now x is #x.

The # Prefix

In markup context, # switches to code mode:

typst
// Markup mode (default at start of file)
Hello world.
#let name = "Alice"           // # needed for code
My name is #name.             // # needed to evaluate

// In code block, # not needed
#{
  let x = 5                   // No # needed inside {}
  x + 3
}

Special Characters

CharacterProblemSolution
#Command prefixEscape with \# in content
_Triggers emphasisUse ...... for blanks, not _____
*Triggers boldEscape with \* if needed literally
@Reference/citationEscape with \@ in plain text
$Math mode delimiterEscape with \$ for currency; NEVER use inside $ $
<>Label referenceUse words ("below", "above") or escape with \< \>
£ Unknown in math modeKeep currency symbols OUTSIDE math expressions

Common Errors

ErrorCauseFix
"unclosed delimiter"_ or $ in []Escape: \$, use ...... not ____
"duplicate argument"Same param twiceRemove duplicate
"unexpected token"Unescaped special charEscape: \$, \#, \@
"unknown variable"Missing # or $NPV$Add #, or $"NPV"$
"context is known"Counter in headerWrap in context [...]
"element functions"Old packageUpdate package or use touying

See references/math-pitfalls.md for full math mode pitfalls.

CLI Commands

bash
typst compile document.typ                     # Compile to PDF
typst watch document.typ                       # Watch and recompile
typst compile document.typ out.pdf --pages 1-5 # Specific pages
typst fonts                                    # List available fonts

Images & Visual Tools

When a Typst document needs an image, follow this priority order:

  1. Can Typst draw it natively (table, grid, box, simple flowchart)? → Typst native
  2. Is it a real photograph, logo, or existing graphic? → /image-search
  3. Is it a mind map or hierarchical overview? → /mindmap
  4. Is it a data-driven chart needing numerical precision? → matplotlib/Python
  5. Otherwise (conceptual illustration, metaphor, artistic visual) → /nano-banana

Full routing reference:

NeedToolWhy
Callout boxes, styled rect() layoutsTypst nativeMatches fonts, editable in source
Simple flowcharts (< 10 nodes)Typst native (fletcher or cetz package)Precise labels, version-friendly
Tables, grids, comparison layoutsTypst nativePerfect fit
Data-driven charts (bar, scatter, line)matplotlib/PythonNumerical precision
Conceptual illustrations or metaphors/nano-bananaArtistic visuals Typst can't draw
Photorealistic or decorative images/nano-bananaOnly AI generation can do this
Complex diagrams where label accuracy doesn't matter/nano-bananaFaster than 100+ lines of Typst
Company/brand logos/image-search --logoExact logo from Logo.dev
Real-world photos (not AI)/image-searchGoogle Images via SerpAPI
Stock photos (license-clear)/image-search --stockUnsplash/Pexels APIs
Existing graphics from a URL/image-search --urlDirect download

Auto-invoke rule: When the user requests a conceptual diagram, illustration, or visual that cannot be reasonably drawn with Typst native tools or packages, automatically run /nano-banana to generate it via Gemini. Pass --typst to get ready-to-paste #figure(image(...)) code. Do NOT ask the user whether to use /nano-banana — just use it.

Do NOT use /nano-banana when the image needs precise text labels (Gemini often misspells), or when the visual is a structured layout that Typst handles natively (boxes, tables, grids).

Accessibility (Typst 0.14+): Always add alt: to embedded images:

typst
#figure(image("path.png", alt: "Description for screen readers"), caption: [...])

Example:

bash
/nano-banana "three-legged stool analogy for risk parity" --dir images --width 80%

Mind Map Generation (/mindmap)

When a Typst document needs a mind map or concept overview:

NeedToolWhy
Hierarchical topic overview (3+ branches)/mindmapOrganic layout, curved connectors, color-coded
Simple 2-level listTypst nativeOverkill to launch Puppeteer for a flat list
Flowchart with directional logicTypst native (fletcher)Mind maps show hierarchy, not flow

Auto-invoke rule: When the user requests a mind map, concept map, topic tree, or overview diagram for a Typst document, automatically run /mindmap to generate it. Pass --typst to get ready-to-paste #figure(image(...)) code. Do NOT ask the user whether to use /mindmap — just use it.

Example:

bash
/mindmap "Portfolio Theory" --typst --caption "Portfolio Theory Overview"

Default theme (academic) uses blue/orange colors matching the Typst study materials palette. Use --theme latte for a pastel alternative or --theme dark for dark backgrounds.

Web Image Search (/image-search)

When a Typst document needs real-world images — company logos, product photos, charts, or any existing graphic from the web:

NeedToolWhy
Company/brand logo/image-search --logo "Stripe"Exact logo via Logo.dev, fallback to search
Real-world photograph/image-search "golden gate bridge"Google Images via SerpAPI (or DuckDuckGo)
License-clear stock photo/image-search --stock "office meeting"Unsplash/Pexels APIs
Image from a known URL/image-search --url "https://..." "desc"Direct download
Multiple images/image-search "query" -n 3Downloads top N results

Auto-invoke rule: When the user needs company logos, real-world photos, or web graphics for a Typst document, automatically run /image-search with --typst. Do NOT ask the user whether to use /image-search — just use it.

Do NOT use /image-search when the user needs AI-generated or conceptual images (use /nano-banana instead), or when the visual can be drawn natively in Typst.

Examples:

bash
/image-search --logo "Goldman Sachs" --width 40%
/image-search "electric vehicle charging station" --size large
/image-search --stock "sustainable energy" -n 2

Debugging

  1. Compile incrementally — Don't write 200 lines then compile
  2. Check line numbers — Errors show exact location
  3. Watch mode — Auto-recompile on save:
    bash
    typst watch file.typ file.pdf
    
  4. Isolate problems — Comment out sections with /* ... */