TikZ: chains with labeled edges
Oktober 19th, 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 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:
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 Sonntag, Oktober 19th, 2008 at 02:50 and is filed under pgf/TikZ, 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.





November 11th, 2008 at 00:50
I LOVE THIS WEBSITE AND I LOVE LATEX MORE!!!
November 12th, 2008 at 22:49
if you are a mathematician you would use xymatrix to produce diagrams. It is easier and the code is shorter.
November 14th, 2008 at 18:32
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
Dezember 7th, 2008 at 05:57
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
März 30th, 2009 at 01:32
Thanks! thats what i need
Juni 10th, 2009 at 17:58
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
Juni 10th, 2009 at 19:38
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
Stefan
Juli 20th, 2009 at 00:14
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={%
\ifx\tikzchainprevious\pgfutil@empty\else(\tikzchainprevious)%
edge[#1]#2(\tikzchaincurrent)\fi}}}
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}}.
Pedro
September 17th, 2009 at 01:53
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_*)} $
Markus
September 17th, 2009 at 13:27
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:
Stefan
Februar 21st, 2010 at 08:04
Thank you for sharing all this interesting stuff. I’m loving this website.
Juni 24th, 2011 at 15:42
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?
jan
Mai 11th, 2012 at 11:17
Got the same problem as you, Jan. Did you solve it?