This extension adds support for Graphviz diagrams using the DOT language in odoc documentation. Graphviz is a powerful graph visualization tool that can render complex node-edge diagrams.
opam install odoc-dot-extensionOnce installed, the extension is automatically loaded by odoc.
Use the {@dot[...]} tag to embed Graphviz diagrams:
{@dot[
digraph {
A -> B -> C
}
]}digraph G {
A -> B
A -> C
B -> D
C -> D
}digraph {
node [shape=box, style=filled, fillcolor=lightblue]
start [shape=circle, fillcolor=green, label="Start"]
end [shape=doublecircle, fillcolor=red, label="End"]
start -> process1 [label="begin"]
process1 -> decision [label="check"]
decision -> process2 [label="yes"]
decision -> end [label="no"]
process2 -> end [label="done"]
}graph {
a -- b -- c
b -- d
c -- e
d -- e -- f
}digraph {
subgraph cluster_0 {
label="Process A"
style=filled
color=lightgrey
a0 -> a1 -> a2
}
subgraph cluster_1 {
label="Process B"
color=blue
b0 -> b1 -> b2
}
start -> a0
start -> b0
a2 -> end
b2 -> end
}digraph {
node [shape=record]
struct1 [label=" left| mid| right"]
struct2 [label=" one| two"]
struct3 [label="hello\nworld|{b|{c| d|e}|f}|g|h"]
struct1:f1 -> struct2:f0
struct1:f2 -> struct3:here
} digraph FSM {
rankdir=LR
node [shape=circle]
start [shape=point]
q0 [label="q₀"]
q1 [label="q₁"]
q2 [label="q₂", shape=doublecircle]
start -> q0
q0 -> q0 [label="0"]
q0 -> q1 [label="1"]
q1 -> q0 [label="0"]
q1 -> q2 [label="1"]
q2 -> q2 [label="0,1"]
}digraph Tree {
node [shape=circle]
8 -> 4
8 -> 12
4 -> 2
4 -> 6
12 -> 10
12 -> 14
}The extension supports the following options:
layout - Graphviz layout engine: dot (default), neato, fdp, sfdp, circo, twopi, osage, patchworkwidth - CSS width (e.g., 500px, 100%)height - CSS heightformat - Output format: omit for client-side JS, or png/svg for server-side rendering (requires graphviz CLI tools)Different layout engines produce different arrangements:
{@dot layout=circo[
digraph {
a -> b -> c -> d -> e -> a
}
]}digraph {
a -> b -> c -> d -> e -> a
}By default, diagrams are rendered client-side using the Viz.js library (a WebAssembly port of Graphviz) loaded from a CDN. The extension injects the necessary <script> tags into the HTML output.
For server-side rendering, install the graphviz package and use format=png or format=svg.
DOT syntax basics:
digraph \{ ... \}graph \{ ... \}A -> B (directed) or A -- B (undirected)A [label="text", shape=box]A -> B [label="edge", style=dashed]See the DOT language documentation for the complete syntax reference.