TikZ: chains with labeled edges
19 October 2008 by Stefan Kottwitz
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 long 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:
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:

See full LaTeX source code.
During writing of this entry I’ve applied that modification also on CQF.info.
This entry was posted on 19 October 2008 at 2:50 AM and is filed under Graphics, Mathematics. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.




11 November 2008 at 12:50 AM
I LOVE THIS WEBSITE AND I LOVE LATEX MORE!!!
12 November 2008 at 10:49 PM
if you are a mathematician you would use xymatrix to produce diagrams. It is easier and the code is shorter.
14 November 2008 at 6:32 PM
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.
Stefan
7 December 2008 at 5:57 AM
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.
http://www.fauskes.net/nb/introducing-dot2texi
Alex