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={%
\ifx\tikzchainprevious\pgfutil@empty\else(\tikzchainprevious)%
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$}; }
\end{tikzpicture}

Output:

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:

\begin{tikzpicture}
  \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]
                      {$\scriptstyle\varphi$}}];
    { [start branch=B] \chainin (m-2-3)
        [join={node[right] {$\scriptstyle\eta_2$}}];}
    \chainin (m-1-4) [join={node[above]
                      {$\scriptstyle\psi$}}];
    { [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]
                      {$\scriptstyle\varphi'$}}];
    \chainin (m-2-4) [join={node[above]
                      {$\scriptstyle\psi'$}}];
    \chainin (m-2-5); }
\end{tikzpicture}

Output:

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)

Leave a Reply