The tikz-cd package
Draws commutative diagrams: objects in a grid inside tikzcd, connected with \arrow and labelled in quotes.
Loading the package
\usepackage{tikz-cd}The tikzcd environment is written like a matrix: & separates the columns, \\ ends a row, and every cell is set in math mode. An arrow is declared in the cell where it starts. Its direction is a combination of the letters r, l, u and d, so \arrow[rd] points one column to the right and one row down.
A label is written in quotes inside the options: \arrow[r, "f"]. An apostrophe after the closing quote puts the label on the other side of the arrow. The package builds on tikz and loads it.
Main commands
\begin{tikzcd} ... \end{tikzcd} | The diagram; works in text and inside display math. |
\arrow[r, "f"] | Arrow one cell to the right with the label f. Short form: \ar. |
\arrow[d, "g"'] | Arrow down with the label on the opposite side. |
\arrow[rd, dashed] | Dashed diagonal arrow. |
\arrow[r, hook] | Arrow with a hook for an inclusion; two heads marks a surjection. |
\arrow[r, bend left] | Curved arrow, for two arrows between the same objects. |
\begin{tikzcd}[row sep=large, column sep=large] | Changes the distance between rows and columns. |
Complete example
\documentclass{article}
\usepackage{tikz-cd}
\begin{document}
\[
\begin{tikzcd}[row sep=large, column sep=large]
A \arrow[r, "f"] \arrow[d, "g"'] \arrow[rd, dashed, "h"]
& B \arrow[d, "k"] \\
C \arrow[r, hook, "i"']
& D
\end{tikzcd}
\]
\end{document}Compiled with pdfLaTeX from TeX Live 2023.
Pitfalls
- With
babeland a language that makes the quote character active, such asngerman, the labels in quotes cause errors. Add\usetikzlibrary{babel}to the preamble. - Inside the argument of another command, for example
\fbox{...}, the&fails. Use the optionampersand replacement=\&and write\&between the cells. - A blank line inside
tikzcdgives Missing $ inserted.
Related pages
- How-to Draw a commutative diagram in LaTeX
- Package
tikz - Package
amsmath - Package
babel