TikZ: chains with labeled edges

These days I wanted to include some commutative diagrams in a math text. There are already packages designed for this purpose, like amscd and xy-pic. I’ve used xy-pic before and didn’t like its usability and output much. The most recent documents I found on CTAN were dated 1999, many links on its homepage were dead, though xy-pic still works fine today, also with pdflatex. But I decided to use pgf/TikZ now because it can be used to create graphics in many different ways. For instance the beamer class is using pgf already, so why not use it also for math diagrams.

For writing exact sequences the chains library seemed very useful, but I missed the feature to label the edges of a chain. Just arrows weren’t enough, I needed to write maps over, under or just next to it.

I decided to make a workaround by modifying the join method of the chain library. Its syntax is join=with<node> by <options>, I’m changing the syntax to join={node[options] {label}}, this is the code to achieve the effect wanted:

\tikzset{join/.code=\tikzset{after node path={%
edge[every join]#1(\tikzchaincurrent)\fi}}}

Some general settings before starting the diagrams:

\tikzset{>=stealth',every on chain/.append style={join},
         every join/.style={->}}

Now a short exact sequence can be written for example:

\begin{tikzpicture}[start chain] {
    \node[on chain] {$0$};
    \node[on chain] {$A$} ;
    \node[on chain, join={node[above]
          {$\scriptstyle\varphi$}}] {$B$};
    \node[on chain, join={node[above]
          {$\scriptstyle\psi$}}] {$C$};
    \node[on chain] {$0$}; }


long exact sequence

For more complex diagrams you could use the matrix library to create matrices of math nodes, connecting the nodes by chains. Here’s the code for the Short 5-Lemma as example:

  \matrix (m) [matrix of math nodes, row sep=3em,
    column sep=3em]
    { 0 & A  & B  & C  & 0 \\
      0 & A' & B' & C' & 0 \\ };
  { [start chain] \chainin (m-1-1);
    \chainin (m-1-2);
    { [start branch=A] \chainin (m-2-2)
        [join={node[right] {$\scriptstyle\eta_1$}}];}
    \chainin (m-1-3) [join={node[above]
    { [start branch=B] \chainin (m-2-3)
        [join={node[right] {$\scriptstyle\eta_2$}}];}
    \chainin (m-1-4) [join={node[above]
    { [start branch=C] \chainin (m-2-4)
        [join={node[right] {$\scriptstyle\eta_3$}}];}
    \chainin (m-1-5); }
  { [start chain] \chainin (m-2-1);
    \chainin (m-2-2);
    \chainin (m-2-3) [join={node[above]
    \chainin (m-2-4) [join={node[above]
    \chainin (m-2-5); }


long exact sequence

See full LaTeX source code.
During writing of this entry I’ve applied that modification also on CQF.info.

19. October 2008 by stefan
Categories: Uncategorized | 13 comments

Comments (13)


  2. if you are a mathematician you would use xymatrix to produce diagrams. It is easier and the code is shorter.

  3. Hi Thiago,

    thank you for posting your comment. I’ve written above why I’m using TikZ instead of xymatrix/xypic. I’ve used xypic earlier, it’s a great tool and still recommendable. There could be advantages using a general purpose tool like TikZ, for instance if you want to combine it with other features, like overlays within a beamer presentation.
    If just diagrams are considered xypic would be enough. My posting above is aimed at TikZ users, discussing production of diagrams without having to learn the xypic syntax.


  4. Have you seen dot2texi yet? I don’t think it is really for doing what your talking about; though I’ve only been using it a couple of weeks, so I could be wrong. Anyway, I use it for some graphing and thought you or another reader might find it useful.



  5. Thanks! thats what i need :)

  6. How would one do to number the diagram as if it was an equation, for later reference? I’m convinced by Tikz flexibility and output, but without that it’s almost useless :(

  7. Hi vargonis,

    you could put the tikzpicture environment into an equation environment to get it numbered. Perhaps consider modifying the baseline of the tikz picture to position the equation tag at the center like

        bounding box.center)]


  8. Hi Stefan,

    I really appreciated your post. However need to have access to the options field of the edge command, e.g., for changing the style of my arrow tips. What I discovered is that this has a very simple solution. I just had to change the key handler /.code to the key handler /.code 2 args, that supports two arguments:

    tikzset{join/.code 2 arg=tikzset{after node path={%

    The first argument are the edge options and the second argument is the node command, corresponding to the sintax join={edge options}{node[options] {label}}.


  9. Hi Stefan!
    I believe your sequence is actually short exact. A quite acceptable long exact sequence (in homology with boundary homomorphisms) may be achieved by

    $xymatrix{H_k(C_*)ar[r]^{H_k(i_*)}& H_k(D_*)ar[r]^{H_k(q_*)} & H_k(E_*) ar @{->} `r/8pt[d] `/10pt[l]^partial `^dl[ll] `^r/3pt[dll] [dll] \ H_{k-1}(C_*)ar[r]ar[r]^{H_{k-1}(i_*)} & H_{k-1}(D_*)ar[r]^{H_{k-1}(q_*)} & H_{k-1}(E_*)} $


  10. Hi Markus,

    thank you for the tipp! I’ve corrected it above. I have compiled your sequence in order to see it and further to post it here as well:

    Long exact sequence


  11. Thank you for sharing all this interesting stuff. I’m loving this website.

  12. hi,

    thanks a lot,
    that is what i am looking for
    but i need the options for the edge command as well.
    i have tried pedros modifications, but this results in an error:
    Use of tikz@lib@parse@join@by doesn’t match its definition
    does someone knows, how to solve this?


  13. Got the same problem as you, Jan. Did you solve it?

Leave a Reply

Required fields are marked *