34

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.

Henri Menke
  • 109,596
smiley06
  • 637

2 Answers2

54

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.

Henri Menke
  • 109,596
egreg
  • 1,121,712
  • tikz-cd is significantly better. It is more flexible and powerful, and just compare the hooked arrows with those of xy-pic. – Gaussler Feb 18 '16 at 17:05
  • 1
    @Gaussler I fully agree! I have used Xy-pic quite extensively, with good results, but tikz-cd is much more powerful and intuitive. You won't find a single hook arrow in my older diagrams. ;-) – egreg Feb 18 '16 at 17:09
  • How can I arrange for the triangle to be equilateral? In other words, how do I centralize $C$ above? – Eduardo Longa Feb 15 '17 at 20:12
  • 2
    A \arrow{rr}{f} \arrow[swap]{dr}{g\circ f} && B \arrow{dl}{g} \\ & C (you may have to adjust the column sep) – egreg Feb 15 '17 at 21:50
  • can I put the diagram inside a colorbox? – fdzsfhaS Mar 19 '20 at 16:58
  • 1
    @DavidWarrenKatz Of course you can: \colorbox{red}{\xymatrix{...}} – egreg Mar 19 '20 at 17:00
  • 1
    The tikzcd GUI editor is fantastic! Thank you. – Nat Kuhn Apr 27 '21 at 00:03
  • Thanks! This website is indeed amazing. If someone is looking for a website to draw commutative diagrams online, this will be what they are exactly looking for. I even suggest that you should post a question "Which website allows drawing commutative diagrams online " and answer it yourself, so that others can more easily search it! – Asigan Nov 24 '23 at 03:10
  • Do you mind also adding https://q.uiver.app/ to the post? It’s similar but seems more powerful – Gareth Ma Mar 20 '24 at 13:42
  • @GarethMa Sorry, but I can't understand how it works. – egreg Mar 20 '24 at 22:53
  • @egreg Umm you double click on a square to add a vertex. Then type some latex say $\mathbb{Z}$ to render it on that square. Then drag an arrow from one square to another (using mouse / touch screen both work) – Gareth Ma Mar 21 '24 at 00:46
8

Another way is to use the psmatrix environment, from pst-node. The objects are first described as nodes in a matrix, then the arrows are described. In this description, nodes can be given a name, or are described by their pair of indices i, j in the matrix. See documentation of pst-node for details on how to connect nodes or more generally how to fine-tune the look of a diagram.

You can compile with pdflatex if you use the --shell-escape switch (TeX Live, MacTeX) or --enable-write18 (MiKTeX), and use the pdf option for the document class: this loads the auto-pst-pdf package. Alternatively, you can load the latter package, after pstricks and its family.

Here is a simple example:

\documentclass[pdf]{article}
\usepackage{pst-node}

\begin{document}

\[ \psset{arrows=->, arrowinset=0.25, linewidth=0.6pt, nodesep=3pt, labelsep=2pt, rowsep=1.2cm}
\begin{psmatrix}
  (X, d) & (X_1 ,d_1 )\\%
   & (X_2 ,d_2)
%%%
 \ncline{1,1}{1,2}\naput{T_1} \ncline{1,1}{2,2}\nbput{T_2 }
 \ncline{1,2}{2,2}\naput[npos=0.45]{T}
\end{psmatrix}
\]
\end{document} 

enter image description here

Bernard
  • 271,350