Drawing Commutative Diagrams – How to Draw Commutative Diagrams with Macros and TikZ

diagramsmacrostikz-cdxy-pic

Can anyone help me in giving commands for commutative diagrams in LaTeX ? It doesn't seem to appear properly in the LaTeX guides that I have. A command for a simple triangular diagram with arrows associated to maps would be good enough, or maybe some appropriate reference.

Best Answer

For simple and complex diagrams, I'd recommend tikz-cd. If you are not comfortable with using the macros, there is also a web-based GUI editor at https://tikzcd.yichuanshen.de/. The following example can be viewed in the editor under this link (click).

Let's see an easy triangular diagram.

\documentclass{article}
\usepackage{tikz-cd}
\begin{document}
\[
  \begin{tikzcd}
    A \arrow{r}{f} \arrow[swap]{dr}{g\circ f} & B \arrow{d}{g} \\
     & C
  \end{tikzcd}
\]
\end{document}

enter image description here

An arrow takes as argument the "steps" where it has to go: r stands for "right", d stands for "down"; also u stands for "up" and l for "left".

A similar syntax is available with Xy-pic.

\documentclass{article}
\usepackage[all,cmtip]{xy}
\begin{document}
\[
\xymatrix{
A \ar[r]^{f} \ar[dr]_{g\circ f} & B \ar[d]^{g} \\
 & C
}
\]
\end{document}

Note how the labels are positioned: ^ means above the arrow, _ means below; above and below are with respect to the direction of the arrow: rotate it counterclockwise until it points from left to right.

enter image description here

As you see, the results are pretty much alike. While I used to use Xy-pic, I'm now more convinced that tikz-cd can be better, as it relies on the powerful TikZ/PGF library.