Diagrams with Graphviz and Typst¶
Choose a diagram for a question. A flow chart explains a choice. A block diagram explains responsibility. A sequence explains ordering. A graph explains relationships. Use prose when it answers the question more directly.
We keep diagram source beside the explanation and let Guidedog build the image for HTML and PDF.
Graphviz and Typst are included in the executable.
We need no separate dot or typst program.
For musical scores, we use the native notation workflow in Music notation.
Choose the drawing tool¶
| Tool | Useful when | Source |
|---|---|---|
| Graphviz | We describe nodes and edges and want the layout engine to place them. | DOT inside graphviz, graph or digraph. |
| Typst with CeTZ | We need explicit geometry, custom shapes, plots or sequence layouts. | Typst inside typst-diagram. |
| Typst with Fletcher | We need positioned nodes, mathematical labels and precise arrows. | Typst inside typst-diagram with a pinned Fletcher import. |
We choose SVG for scalable drawings in both the browser and book. Typst diagrams can also produce PNG at 144 pixels per inch. Existing SVG and PNG files still work with ordinary image and figure directives.
A flow chart¶
The drawing names the decision at the growth step. It does not suggest that running out of room aborts the program: a bound produces a result the caller can report. This distinction is part of the contract.
Draw with Typst¶
The typst-diagram directive compiles its body to an image.
The page fits the drawing automatically.
We can start with Typst’s built-in shapes before adding a package.
.. typst-diagram::
:caption: A drawing from Typst source
:alt: A teal circle
:name: typst-circle
#circle(radius: 12pt, fill: rgb("#216553"), stroke: none)
The same drawing in MyST uses {typst-diagram} as the fence language.
An ordinary typst code fence displays source without compiling it.
```{typst-diagram}
:caption: A drawing from Typst source
:alt: A teal circle
:format: png
#circle(radius: 12pt, fill: rgb("#216553"), stroke: none)
```
CeTZ supplies a drawing canvas and geometric operations. We import a specific package version so the source states its dependency.
.. typst-diagram::
:caption: Two connected nodes
:alt: Two circles joined by a line
#import "@preview/cetz:0.3.4"
#cetz.canvas({
import cetz.draw: *
circle((0, 0), radius: 0.5)
circle((2, 0), radius: 0.5)
line((0.5, 0), (1.5, 0))
})
Fletcher builds node-and-arrow diagrams on CeTZ. Its explicit coordinates are useful when position carries meaning.
.. typst-diagram::
:caption: Source becomes an image
:alt: Source points to an image
#import "@preview/fletcher:0.5.8": diagram, node, edge
#diagram(
node((0, 0), [Source]),
node((1, 0), [Image]),
edge((0, 0), (1, 0), "->"),
)
Packages, limits and failures¶
The pdf_packages, pdf_fonts and pdf_font_paths settings govern Typst diagrams in both outputs.
With pdf_packages = "offline", the compiler uses only packages already installed in Typst’s package directories.
The default downloads missing packages.
After installing a previously missing package or editing fonts, we run a fresh guidedog build html -a command.
Project imports stay below the source directory, including through symlinks.
Diagrams must produce one finite page; PNG export is limited to 16 million pixels and 16,384 pixels on either side.
A failed drawing remains visible as source with a warning (build.graphviz or build.typst-diagram; missing or duplicate directive sources report sphinx.graphviz.source or sphinx.typst.source).
We build with -W when a missing diagram must prevent publication.
--untrusted still compiles Typst diagrams with the embedded compiler, which reads only the source folder and downloads no package, and refuses the files a Graphviz graph names.
Text builds show source without compiling a drawing.
The Draw diagrams with Typst guide lists every option, source limit and migration step for existing figure files.
Light, dark and print¶
Typst diagrams keep their authored colors and receive the same light panel as ordinary Graphviz images in the default dark theme. The adaptive Graphviz palette does not recolor Typst drawings.
A transparent canvas does not change a graph’s ink. Dark text can disappear on a dark page. Ordinary graphs therefore keep their chosen colors and receive a light panel in the default dark theme.
For the manual’s adaptive palette, add :class: graphviz-adaptive to the
directive. Guidedog adds a fixed
stylesheet inside the SVG image. Its dark media query follows the embedding
page’s color-scheme, including the theme control’s explicit selection.
No script, external stylesheet or inline page SVG is needed.
Use short labels, rounded boxes for steps, notes for input and cylinders for durable stores. Node classes distinguish roles in the dark palette:
| DOT class | Role |
|---|---|
gd-input |
Source, configuration or supporting material; warm neutral. |
gd-output |
A produced artifact; blue. |
gd-plugin |
An optional extension point; amber, with a dashed border. |
gd-choice |
A decision; amber, with a diamond shape. |
gd-memory |
A separate ownership or allocation boundary; violet. |
For example, source [class="gd-input", shape=note, fillcolor="#fbfaf3"]
sets both the light presentation and its dark role. Classes do not choose shapes
or add labels. We write those explicitly, so the drawing also works in monochrome.
Unclassified steps use green. This palette is opt-in. We do not apply it to artwork
whose exact colors carry data.
The light DOT attributes remain the PDF master. The book receives an SVG without the adaptive stylesheet. Printed HTML selects the light scheme too. Keep both palettes legible and test the actual book. Transparency alone proves neither. The inheritance rule is defined by the CSS color-scheme specification.
Make the result legible¶
Keep node labels short. Explain a longer rule below the drawing. Prefer one direction for the main path. Label a return edge with its condition. Use shape and text as well as color. A print reader may have no color at all.
Supply alternative text and a caption. Test narrow browser windows and the PDF page. If a diagram needs tiny type to fit, split it into two drawings. See Images, video, audio, and downloads for image and figure sizing.
On narrow screens, captioned diagrams scroll horizontally to keep labels readable. Scroll inside the figure. The page itself keeps its width.