XY-pic LaTeX Diagrams
Typeset commutative diagrams, automata, and categorical constructions using XY-pic in LaTeX, with inline Emacs preview via AUCTeX.
Setup
Preamble
\usepackage[all,cmtip]{xy}
allloads every XY-pic extension (matrix, arrow, curve, frame, etc.)cmtipuses Computer Modern arrowheads (cleaner than default)
Emacs / AUCTeX preview
Add to init.el for inline C-c C-p C-p preview of xymatrix environments:
(eval-after-load "preview"
'(add-to-list 'preview-default-preamble "\\PreviewEnvironment{xymatrix}" t))
Ensure PDF mode is active: C-c C-t C-p.
Core Syntax
Diagrams are matrices. & separates columns, \\ separates rows. Arrows use \ar[direction] where direction combines: u(up), d(down), l(left), r(right).
Commutative Square
\[
\xymatrix{
A \ar[r]^f \ar[d]_g & B \ar[d]^h \\
C \ar[r]_k & D
}
\]
Arrow Labels
| Syntax | Position |
|----------------|-----------------------------|
| \ar[r]^f | label above/right of arrow |
| \ar[r]_f | label below/left of arrow |
| \ar[r]\|f | label on arrow (breaks it) |
| \ar[r]|-{f} | label in gap on arrow |
Arrow Styles
\ar@{->}[r] % normal (default)
\ar@{.>}[r] % dotted
\ar@{=>}[r] % double (natural transformation)
\ar@{-->}[r] % dashed
\ar@{~>}[r] % squiggly
\ar@{->>}[r] % two-headed (epi)
\ar@{^{(}->}[r] % hook (mono)
\ar@{=}[r] % equals
\ar@{}[r] % phantom (for label placement only)
Curved Arrows
\ar@/^/[r]^f % curve up/right
\ar@/_/[r]_g % curve down/left
\ar@/^1pc/[r] % curve with specific radius
\ar@(ur,dr)[r] % loop from upper-right to down-right
Spacing Control
\xymatrix@R=2pc@C=3pc{...} % set row/column spacing
\xymatrix@C-1pc{...} % reduce column spacing by 1pc
\xymatrix@1{...} % entries are 1em wide
Common Diagram Patterns
Pullback / Fiber Product
\[
\xymatrix{
U \ar@/_/[ddr]_y \ar@/^/[drr]^x \ar@{.>}[dr]|-{(x,y)} \\
& X \times_Z Y \ar[d]^q \ar[r]_p & X \ar[d]_f \\
& Y \ar[r]^g & Z
}
\]
Short Exact Sequence
\[
\xymatrix{
0 \ar[r] & A \ar[r]^i & B \ar[r]^p & C \ar[r] & 0
}
\]
Adjunction
\[
\xymatrix@C=4pc{
\mathcal{C} \ar@<1ex>[r]^{F} \ar@{}[r]|{\perp}
& \mathcal{D} \ar@<1ex>[l]^{G}
}
\]
Natural Transformation (2-cell)
\[
\xymatrix{
\mathcal{C} \rtwocell^F_G{\alpha} & \mathcal{D}
}
\]
(Requires \usepackage[all,2cell]{xy} \UseTwocells)
Long Exact Sequence (wrapping)
\[
\xymatrix{
\cdots \ar[r] & H_n(A) \ar[r]^{i_*} & H_n(X) \ar[r]^{j_*}
& H_n(X,A) \ar[dll]_{\partial} \\
& H_{n-1}(A) \ar[r]^{i_*} & H_{n-1}(X) \ar[r] & \cdots
}
\]
Cube Diagram
\[
\xymatrix{
A \ar[rr] \ar[dd] \ar[dr] && B \ar[dd] \ar[dr] \\
& C \ar[rr] \ar[dd] && D \ar[dd] \\
E \ar[rr] \ar[dr] && F \ar[dr] \\
& G \ar[rr] && H
}
\]
Automaton (DFA)
\[
\xymatrix{
*+[o][F-]{q_0} \ar[r]^a \ar@(ul,dl)[]_b
& *+[o][F=]{q_1} \ar@/^/[r]^a \ar@(ur,dr)[]^b
& *+[o][F-]{q_2} \ar@/^/[l]^{a,b}
}
\]
*+[o][F-]= circle, single border*+[o][F=]= circle, double border (accepting state)
Tips
- Diagonal arrows:
\ar[dr]goes down-right,\ar[ull]goes up-left-left (two columns) - Phantom arrows
\ar@{}[r]|{\cong}place a symbol between entries without drawing - Compile twice if arrow positions look wrong on first pass
- Debugging: add
\entrymodifiers={+!!<0pt,\fontdimen22\textfont2>}to baseline-align entries - 2-cells: load
\usepackage[all,2cell]{xy}and\UseTwocellsfor natural transformations
Alternative: tikz-cd
For simpler syntax with TikZ backend:
\usepackage{tikz-cd}
\[
\begin{tikzcd}
A \arrow[r, "f"] \arrow[d, "g"'] & B \arrow[d, "h"] \\
C \arrow[r, "k"'] & D
\end{tikzcd}
\]
Both are available in texliveFull.
References
- XY-pic User's Guide (PDF) — official reference
- Alsani's examples (PDF) — beginner walkthrough
- Milne's CD package comparison — xy-pic vs tikz-cd vs amscd
- Debray's automata guide (PDF) — XY-pic for DFA/NFA
- Run
texdoc xypiclocally for offline docs