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}

tikzstyledefs.tex

shapes

decorations.markings

Keys for Nodes, Vectors, and Matrices

This loads in the appropriate packages required for the diagrams, and the custom key definitions, which are defined in. Theandare 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.

TikZ has a

ode command which is used to draw nodes at a specified point. The simplest syntax for this is



ode 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:

{{

ode[key]at(0,0){nodetext};}}

tikzstyledefs.tex

[vertex] Draw a dot at the given coordinate. Sample usage:

ode[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:

ode[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.

Here are the keys defined in thefile:

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);

[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 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:

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] .

\tikzstyle key diagram definitions Source code forkey diagram definitions

Here is the source code for the styles:

+ show block – hide block %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Style definitions for trace diagram tikz macros % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Height styles (sets up proper alignment within a line of text for provided height) % \tikzstyle heightone=[scale=.7,shift={(0,-.3)}] \tikzstyle heightones=[scale=.8,xscale=.35,shift={(0,.1)}] \tikzstyle heightoneonehalf=[scale=.9,shift={(0,-.2)}] \tikzstyle heighttwo=[scale=.9,shift={(0,-.4)}] \tikzstyle heighttwos=[scale=.5,xscale=.6,shift={(0,-.1)}] \tikzstyle heightthree=[scale=.6,shift={(0,-.9)}] \tikzstyle heightthrees=[scale=.4,xscale=.7,shift={(0,-.2)}] % % Generic Arrow Style % \tikzstyle arrowstyle=[blue,semitransparent,scale=2] % % Label Styles % \tikzstyle basiclabel=[draw=none,fill=none,shape=rectangle,inner sep=2pt,scale=.8] \tikzstyle leftlabel=[basiclabel,anchor=east] \tikzstyle rightlabel=[basiclabel,anchor=west] \tikzstyle bottomlabel=[basiclabel,anchor=north] \tikzstyle toplabel=[basiclabel,anchor=south] % % Vertex Styles \tikzstyle vertex=[circle,draw,fill=black,inner sep=1pt] \tikzstyle ciliation=[circle,draw=none,fill=red,inner sep=1pt,semitransparent] \tikzstyle ciliatednode=[vertex,pin={[pin distance=1mm,pin edge={semitransparent,red},ciliation]#1:{}}] % % Vector Styles % \tikzstyle vector=[black,thick,rectangle,draw=gray!50!yellow,top color=yellow!30,bottom color=black!10,scale=.8,inner sep=2pt] \tikzstyle small vector=[vector,scale=.8] \tikzstyle plain vector=[rectangle,draw=none,fill=white,scale=.7] % % Matrix Styles % \tikzstyle my signal=[black,thick,signal,signal pointer angle=120,draw=blue!50,top color=blue!20,bottom color=black!10,scale=.8,inner sep=2pt] \tikzstyle matrix=[my signal,signal from=south,signal to=north] \tikzstyle reverse matrix=[my signal,signal from=north,signal to=south] \tikzstyle small matrix=[matrix,scale=.7] \tikzstyle reverse small matrix=[reverse matrix,scale=.7] \tikzstyle matrix on edge=[small matrix,sloped,rotate=-90] \tikzstyle reverse matrix on edge=[small matrix,sloped,rotate=90] % % Other styles of lines % \tikzstyle trivalent=[very thick] \tikzstyle dotdotdot=[decorate,decoration={markings, mark=at position .3 with{

ode{.};}, mark=at position .5 with {

ode{.};}, mark=at position .7 with {

ode{.};}}] % % Directionality control for lines % \tikzstyle wavyup=[out=90,in=-90] \tikzstyle wavydown=[out=-90,in=90] % % Symmetrizer Styles % \tikzstyle symmetrizer=[rectangle,fill=gray!10,draw=black] \tikzstyle permutation=[symmetrizer] \tikzstyle antisymmetrizer=[rectangle,fill=black,draw=black] \tikzstyle symlabel=[draw=none,fill=none,black,scale=.8] \tikzstyle asymlabel=[draw=none,fill=none,white,scale=.8]

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$};}

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.

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

When several vertices are in the figure, I like to specify and name their positions precisely using \coordinate or

ode . 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.

Gallery of TikZ snippets with "tracediagram" tag