NLP pictures for teaching. Draw tokens, embeddings, networks, and transformers in Typst. Supply the data; the helpers place the components, arrows, and labels. Highlight a computation, hide answers for an exercise, or reveal a calculation step by step.
Illustrated catalogue · API reference · Runnable examples
Quick start
Requires Typst 0.15.0+. Import the package, then call a diagram helper:
#import "@preview/nlpict:0.1.0": *
#lstm-cell(highlight: "forget", dim: true)
The same highlight, dim, and blank controls work across diagram families.
Exercise and solution views keep the same layout. Numerical helpers also work
through embeddings, Word2Vec, Q/K/V projections, attention, and transformer
calculations by hand.
Examples
| Text structure | Embedding lookup |
|---|---|
![]() |
![]() |
| LSTM gates | Attention by hand |
|---|---|
![]() |
![]() |
| Transformers | Highlights and exercises |
|---|---|
![]() |
![]() |
Also includes finite automata, weighted MLPs, RNNs, GRUs, tokenization, subwords, spans/BIO tags, CBOW, skip-gram, attention masks, positions/RoPE, normalization, feed-forward layers, generation, and KV caches.
All diagrams use native Typst vector shapes and a consistent math font, with transparent annotations and no drawing dependencies.
API overview
Diagram helpers render immediately. Functions ending in -scene return reusable
scene records; -data functions return calculations or annotations without drawing.
The API reference documents parameters, defaults, and selectors.
| Area | Public functions | Purpose |
|---|---|---|
| Primitives | node, anchor, state, transition, enclosure |
Define custom nodes, connection points, edges, and background regions. |
| Rendering and composition | diagram, scene, combine, render |
Render primitives, collect scene records, and combine reusable components. |
| Tokens and arrays | token-row, vector, matrix-grid |
Build token rows and labelled vectors or matrices with automatically sized numeric columns. |
| Tokenization | tokenization, tokenization-scene, tokenization-data |
Align tokens with source characters and offsets. |
| Subwords | subword-tokenization, subword-scene, subword-data |
Align word pieces with their source words. |
| Spans and BIO | span, span-annotations, span-scene, span-data, bio-tags, bio-spans |
Define and draw spans, allocate nested annotation lanes, and convert BIO tags. |
| Dependencies | dependencies, dependency-scene, dependency-data |
Draw dependency arcs, relation labels, and optional POS tags. |
| Embeddings | embedding-lookup, embedding-scene, embedding-data |
Show one-hot lookup, the selected matrix row, and its dense vector. |
| Word2Vec | cbow, cbow-scene, cbow-data, skip-gram, skip-gram-scene, skip-gram-data |
Show context pooling and word prediction, symbolically or numerically. |
| Q/K/V projections | qkv-projection, qkv-scene, qkv-data |
Project input vectors through query, key, and value weights. |
| Single-query attention | attention, attention-scene, attention-data |
Calculate scores, softmax weights, and the weighted output. |
| Worked attention | attention-calculation, attention-calculation-scene |
Trace projections and attention with selected arithmetic steps and staged reveals. |
| Dense networks | network |
Draw MLPs with neuron labels, biases, and inline weights. |
| Recurrent networks | recurrent, recurrent-scene |
Unroll recurrent steps with optional cell-state connections. |
| LSTM cells | lstm-cell, lstm-scene |
Draw expanded or compact gate paths. |
| GRU cells | gru-cell, gru-scene, gru-data, gru-calculation |
Draw gates or work through a numerical recurrent step. |
| Transformer architectures | transformer, transformer-scene, transformer-block, transformer-block-scene |
Show encoder/decoder stacks, block internals, and normalization order. |
| Attention matrices and maps | attention-matrix, attention-matrix-scene, attention-matrix-data, attention-map, attention-map-scene |
Calculate full attention and display token-to-token heatmaps. |
| Masks | attention-mask, attention-mask-scene, mask-data |
Show causal, padding, prefix, or custom visibility masks. |
| Attention heads | multi-head-attention, multi-head-scene, multi-head-data, multi-head-calculation, multi-head-calculation-scene |
Explain MHA/GQA/MQA and calculate head concatenation and output projection. |
| Positions | positional-encoding, positional-encoding-scene, positional-data, rotary-encoding, rotary-encoding-scene, rotary-data |
Show learned or sinusoidal positions and rotary Q/K transformations. |
| Feed-forward layers | feed-forward, feed-forward-scene, feed-forward-data, feed-forward-calculation, feed-forward-calculation-scene |
Draw or calculate position-wise FFNs, including SwiGLU. |
| Normalization | normalization, normalization-scene, normalization-data |
Calculate LayerNorm or RMSNorm with optional gain and bias. |
| Residuals | residual-connection, residual-scene, residual-data |
Explain the bypass path and residual addition. |
| Whole-block calculations | transformer-block-calculation, transformer-block-calculation-scene, transformer-block-data |
Trace numerical attention, normalization, residuals, and feed-forward results. |
| Training and generation | teacher-forcing, teacher-forcing-scene, autoregressive, autoregressive-scene |
Align training inputs and targets or illustrate supplied generation steps. |
| KV caches | kv-cache, kv-cache-scene |
Show prefill and decode cache updates and shapes. |
| Vocabulary head | language-model-head, language-model-scene, language-model-data |
Project hidden states onto vocabulary logits, probabilities, and optional loss. |
Documentation
- Illustrated catalogue: figures and the exact code that generates them.
- API reference: functions, parameters, selectors, and numerical conventions.
- Runnable galleries and catalogue snippets: starting points for your own materials.
The catalogue uses Helvetica for headings and descriptions outside diagrams; everything inside diagrams uses Typst’s bundled New Computer Modern Math.
MIT licensed. See LICENSE.





