Drawing Trace Diagrams with TikZ

This page describes some basic functionality of TikZ, and provides instructions for using TikZ to draw trace diagrams. Several of the author's TikZ stylistic commands are included.

# Introduction to TikZ

PGF (Portable Graphics Format) & TikZ (TikZ ist kein Zeichenprogramm) are a combination of \$\TeX\$ packages that can be used for generating high-quality graphics using a \$\LaTeX\$-like syntax. I like TikZ because it generates graphics on the fly, without the need for separate files for each picture… this is a lifesaver for papers that have hundreds of diagrams. It is also has a very natural and intuitive syntax. For example, the command to draw a line between \$(0,0)\$ and \$(1,1)\$ is

````\tikz{ \draw(0,0)to(1,1); }`
```

See this tutorial at the pracTeX Journal for help on getting started with PGF/TikZ. The instructions here require version 2.00 which is available at http://sourceforge.net/projects/pgf/, with documentation here. Another great resource is this PGF/TikZ Gallery.

PGF/TikZ is a great tool for drawing diagrams, because it allows for multiple diagrams to use the same styles. This allows one to, for instance, change the look of all matrices in a paper by changing a single line. The diagrams below are created using a combination of structure and styling. Frequently, a single diagram may be drawn using many different styles and labels, which indicate the proper way to read the diagram. So an optimal approach should separate (i) the drawing of lines, (ii) the annotation of the lines with matrices, nodes, and labels, and (iii) the appropriate styling of lines, nodes, etc.

Consistency of notation is extremely important when diagrams are an essential part of a paper. This includes consistency in style, as well as consistency in the way in which the lines are drawn. All the diagrams should have the same "look". All the lines, nodes, labels, and so on, should be drawn in a consistent manner.

Another advantage to this approach is that different styles can be used depending on whether the diagrams should be larger or smaller, black and white or colored, in paper form or in web form.

# TikZ Style Key Definitions and Usage

TikZ has keys which can be used to efficiently apply a style to lots of different diagrams. The styles can then be changed once, rather than in each diagram separately. To implement this, I wrote TikZ style definitions for several of the styles that I use most frequently. This makes it easy to adjust the format of a diagram in a consistent manner. See tikzsamples.pdf to see some examples of how these are used.

In a typical file, I will include the following lines before writing any diagram code:

``````\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{shapes}
\usetikzlibrary{decorations.markings}
\input{tikzstyledefs.tex}```
```

This loads in the appropriate packages required for the diagrams, and the custom key definitions, which are defined in tikzstyledefs.tex. The shapes and decorations.markings are special libraries required to draw certain elements. You can check out the source code or the sample file (source code for sample) to get a sense for which keys are available and how they work.

## Keys for Nodes, Vectors, and Matrices

TikZ has a \node command which is used to draw nodes at a specified point. The simplest syntax for this is

\node at(0,0){text};

Certain parameters called keys can be passed to the node to determine how it is drawn. With these parameters, the syntax is:

````{{\node[key]at(0,0){nodetext};}}`
```

Here are the keys defined in the tikzstyledefs.tex file:
[vertex]
Draw a dot at the given coordinate. Sample usage: \node[vertex]at(0,0){};
[ciliation]
Draw a small red dot useful for representing a ciliation.
[ciliatednode=angle]
Draw a dot using the vertex style, together with a ciliation at the given angle as specified with the ciliation style. Sample usage: \node[ciliatednode=150]at(0,0){}; (Draws ciliation at angle of 150degrees relative to (0,0).)
[vector]
Draw a vector, in a small yellow box. Sample usage: {{\draw(0,-1.5)node[vector]{\$u\$}; In addition, [small vector] yields a smaller yellow box, while [plain vector] yields a vector without a box.
[matrix]
Draw a matrix, oriented upward; drawn as a blue "signal" shape. In addition, [small matrix] gives a smaller matrix, and [reverse matrix], [reverse small matrix] produce matrices with opposite orientations.

When matrices are drawn along paths, it is better to use a special command to ensure they are oriented properly, as follows:

[matrix on edge]
Draw a matrix along an edge; actually uses the smaller size matrix. Ensures proper orientation. Sample usage: \draw(0,0)to node[matrix on edge]{\$A\$}(0,1); In addition, [reverse matrix on edge] yields a matrix with opposite orientation.

## Keys for Lines and Curves

I have defined several keys for use with drawing lines and curves. The syntax for these is:

````\draw[key](0,0)to(1,1);`
```

The impact varies from key to key, but generally these styles might make a line thicker or thinner, or display an orientation on an edge. The general line styles are:
[trivalent]
Draws a thicker line than normal; a pseudonym for the predefined TikZ very thick style. Sample usage: \draw[trivalent](0,0)to(.5,1);
[dotdotdot]
Draws three dots along a path instead of the path. Useful for representing an unknown number of edges.
[wavyup]
Prescribes the tangents at the beginning and end of the path to be vertical. May be used in conjunction with other keys, for example: \draw[trivalent,wavyup](0,0)to(.5,1);

## Keys Diagram Sizing

The following keys are used to adjust a diagram's baseline and scale, to make it easier to include diagrams inline with text. The syntax for usage is:

``````\tikz[key]{
...
}```
```
[heightone]
produces figure with baseline at about \$y=0.5\$
[heightoneonehalf]
produces figure with baseline at about \$y=0.75\$
[heighttwo]
produces figure with baseline at about \$y=1\$
[heightthree]
produces figure with baseline at about \$y=1.5\$

In addition, smaller versions can be used with the keys [heightones], [heighttwos], and [heightthrees].

## Source code for \tikzstyle key diagram definitions

Here is the source code for the styles:

# Examples

In this section we give the code for several simple TikZ diagrams. More complex examples are at my TikZ Snippets page.

## Simple Diagrams

 regular/tall/kinked: ``````\tikz{\draw(0,0)to[wavyup](.1,1);} \tikz{\draw(0,-.2)to[wavyup](.1,1.2);} \tikz{\draw(.1,0)to[out=90,in=-110](0,.5)to[out=70,in=-110,looseness=2](.4,.5)to[out=70,in=-90,looseness=1](.3,1);}``` ``` trivalent regular/tall/kinked: ``````\tikz[trivalent]{\draw(0,0)to[wavyup](.1,1)node[rightlabel]{\$n\$};} \tikz[trivalent]{\draw(0,-.2)to[wavyup](.1,1.2)node[rightlabel]{\$c\$};} \tikz[trivalent]{ \draw(.1,0)to[out=90,in=-110](0,.5)to[out=70,in=-110,looseness=2](.4,.5) to[out=70,in=-90,looseness=1](.3,1)node[rightlabel]{\$n\$};}``` ```
 ``````\tikz[trivalent]{ \draw(0,.5)circle(.5); \node[basiclabel]at(.5,1){\$c\$};}``` ```

## Trivalent Diagrams

A shortcut is to apply the "basiclabel" style at the outset rather than for each node separately. This only works if these labels are the only types of nodes in the diagram.

 ``````\tikz[trivalent,every node/.style={basiclabel}]{ \draw(0,.5)circle(.4) (0,.1)arc(-145:145:.7); \node at(-.5,.85){\$a\$}; \node at(.5,.85){\$b\$}; \node at(1.2,1.1){\$c\$};}``` ```
 ``````\tikz[trivalent]{ \coordinate(vxa)at(0,.55) edge[bend right]node[leftlabel,pos=1]{\$a\$}(-.4,0) edge[bend left]node[rightlabel,pos=1]{\$b\$}(.4,0) edge[]node[rightlabel,pos=1]{\$c\$}(0,1);}``` ```
 ``````\tikz[trivalent]{ \coordinate(vxa)at(0,.35) edge[]node[leftlabel,pos=1]{\$d\$}(0,-.15) edge[bend left]node[leftlabel,pos=1]{\$a\$}(-.6,1.15); \coordinate(vxb)at(.3,.75) edge[bend left]node[basiclabel,pos=.7,right=2pt]{\$e\$}(vxa) edge[bend left]node[leftlabel,pos=1]{\$b\$}(0,1.15) edge[bend right]node[rightlabel,pos=1]{\$c\$}(.6,1.15);}``` ```
 ``````\tikz[trivalent]{ \coordinate(vxa)at(0,.35) edge[]node[leftlabel,pos=1]{\$d\$}(0,-.15)edge[bend right]node[rightlabel,pos=1]{\$c\$}(.6,1.15); \coordinate(vxb)at(-.3,.75) edge[bend right]node[basiclabel,pos=.7,left=3pt]{\$f\$}(vxa) edge[bend right]node[rightlabel,pos=1]{\$b\$}(0,1.15)edge[bend left]node[leftlabel,pos=1]{\$a\$}(-.6,1.15);}``` ```
 ``````\tikz[trivalent,every node/.style={basiclabel}]{ \draw(0,.5)circle(.3); \node at(-.5,.7){\$a\$}; \node at(.5,.7){\$b\$}; \draw(-.05,-.2)node[anchor=west]{\$c\$}to[wavyup](0,.2) (0,.8)to[wavyup](.05,1.2)node[anchor=west]{\$d\$};}``` ```

Inward leaning edges helps give the impression that the strands are being glued.

 ``````\tikz[trivalent]{ \draw(-.3,-.1)to[wavyup](-.2,.5)to[wavyup](-.3,1.1)node[leftlabel]{\$a\$} (.3,-.1)to[wavyup](.2,.5)to[wavyup](.3,1.1)node[rightlabel]{\$b\$};}``` ```

When several vertices are in the figure, I like to specify and name their positions precisely using \coordinate or \node. The edge command is great for drawing edges between various nodes, but it requires placing the node command within the edge and thus specifying the coordinate precisely. Using the bend left and bend right keys is a nice easy way to curve the edges. The auto key automatically places a label along an edge to one side.

 ``````\tikz[trivalent]{ \coordinate(vxa)at(0,.25) edge[bend right]node[leftlabel,pos=1]{\$a\$}(-.3,-.2) edge[bend left]node[rightlabel,pos=1]{\$b\$}(.3,-.2); \coordinate(vxb)at(0,.75) edge[]node[auto,basiclabel]{\$c\$}(vxa) edge[bend left]node[leftlabel,pos=1]{\$a\$}(-.3,1.2) edge[bend right]node[rightlabel,pos=1]{\$b\$}(.3,1.2);}``` ```
 `````` \tikz[trivalent,shift={(0,.2)}]{ \coordinate(vxa)at(0,.55) edge[bend right](-.3,.2)edge[bend left](.3,.2)edge[]node[rightlabel,pos=1]{\$c\$}(0,1); \draw(-.3,-.4)node[leftlabel]{\$a\$}to[wavyup](.3,.2)(.3,-.4)node[rightlabel]{\$b\$}to[wavyup](-.3,.2); }``` ```
 ``````\tikz[trivalent]{ \coordinate(vxa)at(0,.45) edge[]node[leftlabel,pos=1]{\$a\$}(0,0) edge[bend left](-.2,.7) edge[bend right](.2,.65); \draw(-.2,.7)to[wavyup](-.1,1.1)node[leftlabel]{\$c\$} (.45,0)node[rightlabel]{\$b\$}to[wavyup](.55,.6)to[bend right=90,looseness=2](.2,.65);}``` ```

# Gallery of TikZ snippets with "tracediagram" tag

6j Symbols

(last edited on 26 Jan 2009 15:22 by )

Admissibility

(last edited on 11 Feb 2009 19:55 by )

Central Functions of Rank 1-3

(last edited on 11 Feb 2009 19:45 by )

Dot and Cross Products & Vector Identities

(last edited on 15 Feb 2010 15:44 by )

Examples for Various \tikzstyle keys

(last edited on 15 Feb 2010 15:01 by )

Fundamental Binor Identity

(last edited on 28 Oct 2008 11:49 by )

Gluing an Edge at a Trivalent Trace Diagram Vertex

(last edited on 26 Jan 2009 15:13 by )

Pretty Simple Loop Recurrence

(last edited on 19 Jan 2009 19:21 by )

Simple Tensor Diagrams

(last edited on 11 Feb 2009 19:54 by )

Symmetrizers & Basic Trivalent Diagrams

(last edited on 11 Feb 2009 19:59 by )

Trace, Determinant, and Other Primitive Matrix Invariants

(last edited on 15 Feb 2010 16:36 by )

Trace Relations via Binor Identity

(last edited on 28 Oct 2008 11:53 by )

Triple Vector Product Identity

(last edited on 15 Feb 2010 15:31 by )

Trivalent Spin Networks & Identities

(last edited on 11 Feb 2009 20:01 by )

Vectors and Matrices in Trace Diagrams

(last edited on 15 Feb 2010 15:40 by )

# Figures attached to this page

page revision: 135, last edited: 15 Feb 2010 16:50
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License