How_to_TikZ

160
How to TikZ? An overview Jan-Philipp Kappmeier Technische Universität Berlin 13. 12. 2010

description

How to TikZ

Transcript of How_to_TikZ

Page 1: How_to_TikZ

How to TikZ?An overview

Jan-Philipp Kappmeier

Technische Universität Berlin

13. 12. 2010

Page 2: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Introduction

TikZ

TikZ ist kein Zeichenprogramm

A language to create images within LaTexTwo components:

1 TikZeasy to use high-level commands

2 PGF (Portable Graphics Format)low-level commands (at aleast: lower level)

Usage

First: include the package tikz. Then:

directly code images. See details in this talk

create images in Inkscape and export

lots of other tools provide TikZ-Output

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 2 / 38

Page 3: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Introduction

For starters

How to add a TikZ picture to your document?

TikZ-code is written in an tikzpicture environment.

Put into a picture environment to add caption, reference etc.

Inline-TikZ: use the \tikz command to create inline graphicslike this nice 5-wheel here.

Example

It is easy to draw a thistle .\tikz{ \filldraw[color=Thistle] circle (0.5ex); }

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 3 / 38

Page 4: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Basic commands

Drawing on paths

Generel principle

Everything is drawn on a so-called path

A sequence of coordinates and drawing commands

General syntax:\draw[options] (coordinate) command (coordinate) ...;

like moving a pencil to some place and start drawing something.

Special commands like \fill also exists.

Example

\draw (1,0) rectangle +(2,1) -- (3,2);\draw (0,0) -- (0,2) -- (1,3.25){[rounded corners] -- (2,2)-- (2,0)} -- (0,2) -- (2,2) -- (0,0) -- (2,0);

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 4 / 38

Page 5: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Basic commands

Drawing Graphs

NodesNodes can be put on any path using the command node

\draw[parameter] node at coordinate {content};

Special draw command for nodes: \node

by default, a node is just a position and no shape is drawn

Example

test v0 colored

\node at (0,0) {test};\node[draw ,circle] at (2,0) {$v_0$};\node[fill] at (4,0) {};\node[draw ,color=red] at (6,0) [green] {colored };

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 5 / 38

Page 6: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Basic commands

Drawing the 5 wheel

We are ready to pimp our slides with the COGA-5-Wheel!

Unfortunately we have do to a lot of computation.á Can be done by PGF!

Also, lines are drawn over the nodes.á Solve this using named nodes.

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 6 / 38

Page 7: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Basic commands

Drawing the 5 wheel

We are ready to pimp our slides with the COGA-5-Wheel!\node[fill ,circle ,draw ,RoyalBlue] at (0,1) {};\node[fill ,circle ,draw ,RoyalBlue] at ( -0.9511 ,0.3091) {};\node[fill ,circle ,draw ,RoyalBlue] at ( -0.5878 , -0.8091) {};\node[fill ,circle ,draw ,RoyalBlue] at (0.5878 , -0.8091) {};\node[fill ,circle ,draw ,RoyalBlue] at (0.9511 ,0.3091) {};\node[fill ,circle ,draw ,RoyalBlue] at (0,0) {};\draw[red] (0,1) to ( -0.9511 ,0.3091) to ( -0.5878 , -0.8091)

to (0.5878 , -0.8091) to (0.9511 ,0.3091) to (0,1);\draw[red] (0,0) to (0,1) (0,0) to ( -0.9511 ,0.3091) (0,0)

to ( -0.5878 , -0.8091) (0,0) to (0.5878 , -0.8091) (0,0)to (0.9511 ,0.3091);

Unfortunately we have do to a lot of computation.á Can be done by PGF!

Also, lines are drawn over the nodes.á Solve this using named nodes.

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 6 / 38

Page 8: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Basic commands

Drawing the 5 wheel

We are ready to pimp our slides with the COGA-5-Wheel!

Unfortunately we have do to a lot of computation.á Can be done by PGF!

Also, lines are drawn over the nodes.á Solve this using named nodes.

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 6 / 38

Page 9: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Basic commands

Drawing the 5 wheel

Computations using PGF\pgfmathsetmacro{\x}{computation}

Creates a variable \x with the result of the computation

\pgfmathparse{computation}

Stores the result in the variable \pgfmathresult

Still we need to know a lot about how to compute coordinates on a circle

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 7 / 38

Page 10: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Basic commands

Drawing the 5 wheel

Computations using PGF\pgfmathsetmacro{\x}{computation}

Creates a variable \x with the result of the computation

\pgfmathparse{computation}

Stores the result in the variable \pgfmathresult

\pgfmathsetmacro {\xa}{cos (90)}\pgfmathsetmacro {\ya}{sin (90)}\pgfmathsetmacro {\xb}{cos (90+72)}\pgfmathsetmacro {\yb}{sin (90+72)}...\node[fill ,circle ,draw ,RoyalBlue] (1) at (\xa ,\ya) {};\node[fill ,circle ,draw ,RoyalBlue] (2) at (\xb ,\yb) {};...\draw[red] (1) to (2) to (3) to (4) to (5) to (1);\draw[red] (0) to (1) (0) to (2) (0) to (3) (0) to (4) (0) to (5);

Still we need to know a lot about how to compute coordinates on a circle

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 7 / 38

Page 11: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Basic commands

Drawing the 5 wheel

Computations using PGF\pgfmathsetmacro{\x}{computation}

Creates a variable \x with the result of the computation

\pgfmathparse{computation}

Stores the result in the variable \pgfmathresult

Still we need to know a lot about how to compute coordinates on a circle

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 7 / 38

Page 12: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

Polar coordinates

Polar coordinatesAll coordinates can be defined via polar coordinates

Needed only an angle and the radius (distnace from the origin)

expressed as (angle:radius)

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 8 / 38

Page 13: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

Polar coordinates

Polar coordinatesAll coordinates can be defined via polar coordinates

Needed only an angle and the radius (distnace from the origin)

expressed as (angle:radius)

\node[fill ,circle ,draw ,RoyalBlue] (1) at (90+0*72:1) {};\node[fill ,circle ,draw ,RoyalBlue] (2) at (90+1*72:1) {};\node[fill ,circle ,draw ,RoyalBlue] (3) at (90+2*72:1) {};\node[fill ,circle ,draw ,RoyalBlue] (4) at (90+3*72:1) {};\node[fill ,circle ,draw ,RoyalBlue] (5) at (90+4*72:1) {};\node[fill ,circle ,draw ,RoyalBlue] (0) at (0,0) {};\draw[red] (1) to (2) to (3) to (4) to (5) to (1);\draw[red] (0) to (1) (0) to (2) (0) to (3) (0) to (4) (0) to (5);

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 8 / 38

Page 14: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

Polar coordinates

Polar coordinatesAll coordinates can be defined via polar coordinates

Needed only an angle and the radius (distnace from the origin)

expressed as (angle:radius)

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 8 / 38

Page 15: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

Loops

What happens when the logo surprisingly changes to a 6 wheel?

Or even to a 7 wheel?

The foreach commandexecutes the same commands for all items of a given set

assigns the value to a variable

\foreach \var in \{ item1, item2, ..., itemN\} { }

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 9 / 38

Page 16: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

Loops

What happens when the logo surprisingly changes to a 6 wheel?

Or even to a 7 wheel?

The foreach commandexecutes the same commands for all items of a given set

assigns the value to a variable

\foreach \var in \{ item1, item2, ..., itemN\} { }

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 9 / 38

Page 17: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

Loops

What happens when the logo surprisingly changes to a 6 wheel?

Or even to a 7 wheel?

The foreach commandexecutes the same commands for all items of a given set

assigns the value to a variable

\foreach \var in \{ item1, item2, ..., itemN\} { }

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 9 / 38

Page 18: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

Putting things together

\pgfmathsetmacro {\n}{5}\pgfmathtruncatemacro {\ nodes }{\n-1}\node[fill ,circle ,draw ,RoyalBlue] (c) at (0,0) {};\foreach \i in {0,...,\ nodes}

\node[fill ,circle ,draw ,RoyalBlue] (\i) at (90+\i*360/\n:1) {};\foreach \i in {0,...,\ nodes} {

\draw[red] (c) to (\i);\pgfmathtruncatemacro {\j}{mod(round (1+\i),\n)}\draw[red] (\i) -- (\j);

}

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 10 / 38

Page 19: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

Putting things together

\pgfmathsetmacro {\n}{5}\pgfmathtruncatemacro {\ nodes }{\n-1}\node[fill ,circle ,draw ,RoyalBlue] (c) at (0,0) {};\foreach \i in {0,...,\ nodes}

\node[fill ,circle ,draw ,RoyalBlue] (\i) at (90+\i*360/\n:1) {};\foreach \i in {0,...,\ nodes} {

\draw[red] (c) to (\i);\pgfmathtruncatemacro {\j}{mod(round (1+\i),\n)}\draw[red] (\i) -- (\j);

}

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 10 / 38

Page 20: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

Loops cont.

\foreach with more variablesThe \foreach command can iterate over tuples

values are assigned to several variables

Example

Want to highlight specific numbers on the real line.

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 11 / 38

Page 21: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

Loops cont.

\foreach with more variablesThe \foreach command can iterate over tuples

values are assigned to several variables

Example

Want to highlight specific numbers on the real line.Give it a first try:

\draw[->] (0,0) to (8,0);\foreach \x in {0, 1, 1.57, 3.14, 2.71} {

\draw (\x,0.1) to (\x, -0.1);\node at (\x, -0.3) {\ footnotesize {\x}};

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 11 / 38

Page 22: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

Loops cont.

\foreach with more variablesThe \foreach command can iterate over tuples

values are assigned to several variables

Example

Want to highlight specific numbers on the real line.Give it a first try:

0 1 1.57 3.142.71

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 11 / 38

Page 23: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

Loops cont.

\foreach with more variablesThe \foreach command can iterate over tuples

values are assigned to several variables

Example

Want to highlight specific numbers on the real line.Better:

0 1 �2 �e

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 11 / 38

Page 24: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

Loops cont.

\foreach with more variablesThe \foreach command can iterate over tuples

values are assigned to several variables

Example

Want to highlight specific numbers on the real line.

\draw[->] (0,0) to (4,0);\foreach \x / \txt in{0, 1, 1.57 / $\frac{\pi}{2}$, 3.14 / $\pi$, 2.71 / $e$} {

\draw (\x,0.1) to (\x, -0.1);\node at (\x, -0.3) {\ footnotesize {\txt}};

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 11 / 38

Page 25: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

Calculate coordinates

CoordinatesCan be defined using coordinate

Like nodes with empty text

Coordinates can be computed (like vector math)

need to add the package calc (\includetikzpackage{calc})

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 12 / 38

Page 26: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - Background

A

B

F

G

I

H

D

C

E

� = 12��

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 13 / 38

Page 27: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - Background

A

B

F

G

I

H

D

C

E

� = 12�

Construction1 Start with an isosceles triangle with base of length a

2 Draw a copy of AB at F3 Draw segment AI of length b with some angle �

4 and copy it to G5 Connect H and I6 Rotate the polygonal path FGHIA around A by 12�

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 13 / 38

Page 28: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - Background

A

B

F

G

I

H

D

C

E

� = 12�

Construction1 Start with an isosceles triangle with base of length a2 Draw a copy of AB at F

3 Draw segment AI of length b with some angle �

4 and copy it to G5 Connect H and I6 Rotate the polygonal path FGHIA around A by 12�

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 13 / 38

Page 29: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - Background

A

B

F

G

I

H

D

C

E

� = 12��

Construction1 Start with an isosceles triangle with base of length a2 Draw a copy of AB at F3 Draw segment AI of length b with some angle �

4 and copy it to G5 Connect H and I6 Rotate the polygonal path FGHIA around A by 12�

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 13 / 38

Page 30: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - Background

A

B

F

G

I

H

D

C

E

� = 12��

Construction1 Start with an isosceles triangle with base of length a2 Draw a copy of AB at F3 Draw segment AI of length b with some angle �

4 and copy it to G

5 Connect H and I6 Rotate the polygonal path FGHIA around A by 12�

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 13 / 38

Page 31: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - Background

A

B

F

G

I

H

D

C

E

� = 12��

Construction1 Start with an isosceles triangle with base of length a2 Draw a copy of AB at F3 Draw segment AI of length b with some angle �

4 and copy it to G5 Connect H and I

6 Rotate the polygonal path FGHIA around A by 12�

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 13 / 38

Page 32: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - Background

A

B

F

G

I

H

D

C

E

� = 12��

Construction1 Start with an isosceles triangle with base of length a2 Draw a copy of AB at F3 Draw segment AI of length b with some angle �

4 and copy it to G5 Connect H and I6 Rotate the polygonal path FGHIA around A by 12�

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 13 / 38

Page 33: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

Example

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 34: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

Example

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 35: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

Example

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 36: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

Example

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 37: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

Example

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 38: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

Example

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 39: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

Example

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 40: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

Example

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 41: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

Example

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 42: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

Example

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 43: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

Example

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 44: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

Example

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 45: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

Example

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 46: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

Example

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 47: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

Example

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 48: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

Example

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 49: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

Example

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 50: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

Example

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 51: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

Example

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 52: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

Example

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 53: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

Example

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 54: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

Example

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 55: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

Example

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 56: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

Example

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 57: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

Example

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 58: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

Example

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 59: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

Example

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 60: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

Example

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 61: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

Example

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 62: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

Example

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 63: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

a = AB = FGb = AI = HG

Example

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 64: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

a = AB = FGb = AI = HG

Example

Define Variables to use:\def\a{0.5}\def\b{0.5}\def\bAngle {-10}

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 65: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

a = AB = FGb = AI = HG

Example

Compute Distance of Point F from AB:

\pgfmathsetmacro {\hyp}{\a*0.5 / cos (84)}\pgfmathsetmacro {\len}{sqrt(\hyp*\hyp -0.25*\a*\a)}

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 66: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

a = AB = FGb = AI = HG

Example

Compute the locations of the points:

\coordinate (A) at (0,0); % start coordinate\coordinate (B) at ($ (A) + (0,\a) $);\coordinate (F) at ($ (B) + (\len ,0.5*\a) $);\coordinate (G) at ($ (F) + (0,\a) $);

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 67: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

a = AB = FGb = AI = HG

Example

Compute the locations of the points:

\coordinate (Htemp) at ($ (G) - (\b,0) $);\coordinate (H) at ($ (G)!1!\ bAngle :( Htemp) $);\coordinate (Itemp) at ( $ (A) + (\b,0)$ );\coordinate (I) at ($ (A)!1!\ bAngle :( Itemp) $);

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 68: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

An example using coordinate calculation - TikZ realization

A

B

F

G

I

H

D

C

E

� = 12��

a = AB = FGb = AI = HG

Example

Compute the locations of the points:

\coordinate (E) at ($ (F)!1! -12:(G) $);\coordinate (D) at ($ (F)!1! -12:(H) $);\coordinate (C) at ($ (F)!1! -12:(I) $);

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 14 / 38

Page 69: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Coordinates and control structures

The complete example

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 15 / 38

Page 70: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

About nodes and edges

Node features

Multiline nodes: Allows to have several lines of text within one node

\node[align=center] {Line 1 \\ Another line};

Line 1Another one

Only works with TikZ 2.10

Node labels: can add a label to all corners of the node

\node at (0,0) [label=below left:\tiny{$note$}] {A};

Anote

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 16 / 38

Page 71: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

About nodes and edges

Node features (cont.)

Anchors: defines the corner of the node that lies at the specifiesposition. Default is center.\node[draw,anchor=north west] at (0,0) {NW};

\node[draw,anchor=south east] at (0,0) {SE};

NWSE

Shapes: The outer appearance of a drawn node.\node[circle] at (0,0) {ellipse};

circle rectangle rounded corners

Creating own shapes is possible, but not easy. Needs low levelcoding of so-called basic layer.

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 17 / 38

Page 72: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

About nodes and edges

If you still miss a powerpoint feature

Do you really miss anything?

Bam! If you really, really need, just do, it’s easy!

I wouldn’t mind havingthese fancy clouds!

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 18 / 38

Page 73: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

About nodes and edges

If you still miss a powerpoint feature

Do you really miss anything?

Bam! If you really, really need, just do, it’s easy!

I wouldn’t mind havingthese fancy clouds!

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 18 / 38

Page 74: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

About nodes and edges

If you still miss a powerpoint feature

Do you really miss anything?

Bam! If you really, really need, just do, it’s easy!

\node[starburst,fill=yellow,draw=red,line width=2pt] at

I wouldn’t mind havingthese fancy clouds!

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 18 / 38

Page 75: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

About nodes and edges

The cloud code

Example

\node[align=center ,draw ,shading=ball ,text=white ,cloud callout ,cloud puffs=17,cloud puff arc=140,callout pointer segments=3,anchor=pointer ,callout relative pointer ={(200:2 cm)},aspect =2.5]at (current page.center){ I wouldn ’t mind having \\these fancy clouds };

I wouldn’t mind havingthese fancy clouds!

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 19 / 38

Page 76: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

About nodes and edges

The cloud code

Example

\node[align=center ,draw ,shading=ball ,text=white ,cloud callout ,cloud puffs=17,cloud puff arc=140,callout pointer segments=3,anchor=pointer ,callout relative pointer ={(200:2 cm)},aspect =2.5]at (current page.center){ I wouldn ’t mind having \\these fancy clouds };

cloud callout – the shape name

I wouldn’t mind havingthese fancy clouds!

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 19 / 38

Page 77: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

About nodes and edges

The cloud code

Example

\node[align=center ,draw ,shading=ball ,text=white ,cloud callout ,cloud puffs=17,cloud puff arc=140,callout pointer segments=3,anchor=pointer ,callout relative pointer ={(200:2 cm)},aspect =2.5]at (current page.center){ I wouldn ’t mind having \\these fancy clouds };

cloud puffs – the number of puffs of the cloud

I wouldn’t mind havingthese fancy clouds!

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 19 / 38

Page 78: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

About nodes and edges

The cloud code

Example

\node[align=center ,draw ,shading=ball ,text=white ,cloud callout ,cloud puffs=17,cloud puff arc=140,callout pointer segments=3,anchor=pointer ,callout relative pointer ={(200:2 cm)},aspect =2.5]at (current page.center){ I wouldn ’t mind having \\these fancy clouds };

cloud puff arc – the angle of two meeting puffs

I wouldn’t mind havingthese fancy clouds!

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 19 / 38

Page 79: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

About nodes and edges

The cloud code

Example

\node[align=center ,draw ,shading=ball ,text=white ,cloud callout ,cloud puffs=17,cloud puff arc=140,callout pointer segments=3,anchor=pointer ,callout relative pointer ={(200:2 cm)},aspect =2.5]at (current page.center){ I wouldn ’t mind having \\these fancy clouds };

callout pointer segments – the number of round bubbles

I wouldn’t mind havingthese fancy clouds!

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 19 / 38

Page 80: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

About nodes and edges

The cloud code

Example

\node[align=center ,draw ,shading=ball ,text=white ,cloud callout ,cloud puffs=17,cloud puff arc=140,callout pointer segments=3,anchor=pointer ,callout relative pointer ={(200:2 cm)},aspect =2.5]at (current page.center){ I wouldn ’t mind having \\these fancy clouds };

callout relative pointer – the angle and distance of the pointer

I wouldn’t mind havingthese fancy clouds!

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 19 / 38

Page 81: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

About nodes and edges

The cloud code

Example

\node[align=center ,draw ,shading=ball ,text=white ,cloud callout ,cloud puffs=17,cloud puff arc=140,callout pointer segments=3,anchor=pointer ,callout relative pointer ={(200:2 cm)},aspect =2.5]at (current page.center){ I wouldn ’t mind having \\these fancy clouds };

aspect – ratio between width and height

I wouldn’t mind havingthese fancy clouds!

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 19 / 38

Page 82: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

About nodes and edges

The cloud code

Example

\node[align=center ,draw ,shading=ball ,text=white ,cloud callout ,cloud puffs=17,cloud puff arc=140,callout pointer segments=3,anchor=pointer ,callout relative pointer ={(200:2 cm)},aspect =2.5]at (current page.center){ I wouldn ’t mind having \\these fancy clouds };

current page.center – absolute coordinate on the page

I wouldn’t mind havingthese fancy clouds!

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 19 / 38

Page 83: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Scopes

Scopes in TikZ

TikZ allows scopes, just like e. g. JAVA

Scopes can alter the drawing projection

That means rotating, moving or scaling etc.

Possible commands are:

xscale, yscale and scale for scaling

rotate for rotation around an angle

xshift, yshift and shift for movements of the origin

\begin{scope}[ rotate =30, xscale =0.5, shift ={(0:\s)}]...\end{scope}

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 20 / 38

Page 84: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Scopes

Example for scopes

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 21 / 38

Page 85: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Scopes

Example for scopes

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 21 / 38

Page 86: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Scopes

Example for scopes

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 21 / 38

Page 87: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Scopes

Example for scopes

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 21 / 38

Page 88: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Scopes

Example for scopes

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 21 / 38

Page 89: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Scopes

Example for scopes

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 21 / 38

Page 90: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Scopes

Example for scopes

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 21 / 38

Page 91: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Scopes

Example for scopes

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 21 / 38

Page 92: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Scopes

Example for scopes

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 21 / 38

Page 93: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Scopes

Example for scopes

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 21 / 38

Page 94: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Scopes

Example for scopes

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 21 / 38

Page 95: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Scopes

Example for scopes

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 21 / 38

Page 96: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Scopes

Example for scopes

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 21 / 38

Page 97: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Scopes

Example for scopes

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 21 / 38

Page 98: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Scopes

Example for scopes

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 21 / 38

Page 99: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Scopes

Example for scopes

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 21 / 38

Page 100: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Scopes

Example for scopes

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 21 / 38

Page 101: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Scopes

Example for scopes

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 21 / 38

Page 102: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Scopes

Example for scopes

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 21 / 38

Page 103: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Scopes

Example for scopes

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 21 / 38

Page 104: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Scopes

Example for scopes

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 21 / 38

Page 105: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Scopes

Example for scopes

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 21 / 38

Page 106: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Scopes

Example for scopes

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 21 / 38

Page 107: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Scopes

Example for scopes

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 21 / 38

Page 108: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Scopes

Example for scopes

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 21 / 38

Page 109: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Scopes

Example for scopes

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 21 / 38

Page 110: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Scopes

Example for scopes

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 21 / 38

Page 111: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Scopes

Example for scopes

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 21 / 38

Page 112: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Scopes

Example for scopes

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 21 / 38

Page 113: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Scopes

Example for scopes

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 21 / 38

Page 114: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Intersections

Intersections

A

B

F

G

I

H

D

C

E

� = 12��

Nice detail:End point of helping line is in themiddle of the angle.

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 22 / 38

Page 115: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Intersections

Intersections

A

B

F

G

I

H

D

C

E

� = 12��

A

B

F

G

I

H

D

C

E

� = 12��

Nice detail:End point of helping line is in themiddle of the angle.

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 22 / 38

Page 116: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Intersections

Intersections

A

B

F

G

I

H

D

C

E

� = 12��

A

B

F

G

I

H

D

C

E

� = 12��

Nice detail:End point of helping line is in themiddle of the angle.

The good thing:TikZ can automatically compute these intersection points.Paths can be arbitrary, not only line segments!

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 22 / 38

Page 117: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Intersections

Intersections (cont.)

How to compute intersections1 Include the package intersections

2 Name paths using the option [name path=pname]

Hint: invisible paths can be drawn using \path

3 Compute intersections as new path:\path [name intersections={of=pname1 and pname2}];

á new intersection points are now available at(intersection-1), (intersection-2) etc.

\path[name path=helpPath] (helpPoint) -- (labelPoint );\path[name path=ai] (B) -- (F);\path [name intersections ={of=helpPath and ai}];\coordinate (inters) at ($ (intersection -1)!0.5!( helpPoint) $);

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 23 / 38

Page 118: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Intersections

Intersections (cont.)

How to compute intersections1 Include the package intersections

2 Name paths using the option [name path=pname]

Hint: invisible paths can be drawn using \path

3 Compute intersections as new path:\path [name intersections={of=pname1 and pname2}];

á new intersection points are now available at(intersection-1), (intersection-2) etc.

\path[name path=helpPath] (helpPoint) -- (labelPoint );\path[name path=ai] (B) -- (F);\path [name intersections ={of=helpPath and ai}];\coordinate (inters) at ($ (intersection -1)!0.5!( helpPoint) $);

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 23 / 38

Page 119: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Trees

Trees

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 24 / 38

Page 120: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Trees

Trees

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 24 / 38

Page 121: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Trees

Trees

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 24 / 38

Page 122: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Trees

Trees

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 24 / 38

Page 123: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Trees

Trees

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 24 / 38

Page 124: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Trees

Trees

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 24 / 38

Page 125: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Trees

Trees

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 24 / 38

Page 126: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Trees

Trees

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 24 / 38

Page 127: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Trees

Trees

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 24 / 38

Page 128: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Trees

Trees – More like this

Defining Trees

Use child{} in a node definition to create a child

Use node and childs iterativeley to create a tree

Example

root

here there

another

\node {\ footnotesize root}child {

node {\ footnotesize here}}child {node {\ footnotesize there}

child {node {\ footnotesize another }}

child {node {}

};

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 25 / 38

Page 129: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Trees

Split nodes

The Rectangle Split Shape

Allows to put more than one text into nodes

Need to include tikzlibrary shapes.multipart

The style is rectangle split

This gives a vertically split. For horizontally split addrectangle split horizontal

Example\node[rectangle split,rectangle split parts=3,draw]

{1\nodepart{two}2\nodepart{three}3};1

2

3

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 26 / 38

Page 130: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Trees

Recursive Sorting

5 2 3 7 4 6 1 9

5 2 3 7

5 2

5 2

3 7

3 7

4 6 1 9

4 6

4 6

1 9

1 9

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 27 / 38

Page 131: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Trees

Recursive Sorting

Unfortunately children overlap

Can be solved via sibling distance

A style for each level of the tree

Example

\begin{tikzpicture }[level 1/. style ={ sibling distance =60mm},level 2/. style ={ sibling distance =30mm},level 3/. style ={ sibling distance =20mm}]\end{tikzpicture}

á All siblings on level 1 will have a distance of 60mm

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 27 / 38

Page 132: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Trees

Recursive Sorting

5 2 3 7 4 6 1 9

5 2 3 7

5 2

5 2

3 7

3 7

4 6 1 9

4 6

4 6

1 9

1 9

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 27 / 38

Page 133: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Trees

Recursive Sorting

5 2 3 7 4 6 1 9

5 2 3 7

5 2

5 2

3 7

3 7

4 6 1 9

4 6

4 6

1 9

1 9

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 27 / 38

Page 134: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Trees

Recursive Sorting

5 2 3 7 4 6 1 9

5 2 3 7

2 5

5 2

3 7

3 7

4 6 1 9

4 6

4 6

1 9

1 9

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 27 / 38

Page 135: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Trees

Recursive Sorting

5 2 3 7 4 6 1 9

2 3 5 7

2 5

5 2

3 7

3 7

1 4 6 9

4 6

4 6

1 9

1 9

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 27 / 38

Page 136: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Trees

Recursive Sorting

1 2 3 4 5 6 7 9

2 3 5 7

2 5

5 2

3 7

3 7

1 4 6 9

4 6

4 6

1 9

1 9

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 27 / 38

Page 137: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Trees

Using styles to clean up code

Styles

Using a lot of parameters creates ugly code

User defined styles help keeping code clean

Style needs to be changed once only

Usage:

\begin{tikzpicture}[stylename /.style ={some commands},otherstyle /.style ={some commands }]

\end{tikzpicture}

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 28 / 38

Page 138: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Trees

Styles example

Example

[default /. style ={draw ,fill ,circle ,shading=ball ,ball color=Dandelion ,text=white},

source /.style={draw ,fill ,circle ,shading=ball ,ball color=ForestGreen ,text=white},

sink/.style={draw ,fill ,circle ,shading=ball ,ball color=BrickRed ,text=white}]

s0

s1

a

b

c

t

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 29 / 38

Page 139: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Trees

Styles example

Example

\node[source] (1) at (0,1) {$s_0$};\node[source] (2) at (0,-1) {$s_1$};\node[default] (3) at (2,0) {$a$};\node[default] (4) at (4,1) {$b$};\node[default] (5) at (4,-1) {$c$};\node[sink] (6) at (6,0) {$t$};

s0

s1

a

b

c

t

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 29 / 38

Page 140: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Plots

Plotting in TikZ

Something like this is possible in TikZ:

Size of instance

Tim

e(s

)

23 26 29 212 215 218 2210

1

2

3

4

5

6

RUNTIME

But: quite lengthy code, as axes and legend have to be drawn manually

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 30 / 38

Page 141: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Plots

Plotting using PGFPLOTS

The PGFPLOTS package

Package specialized for drawing plots

Based upon PGF/TikZ

Available at http://sourceforge.net/projects/pgfplots

The manual is as good as the one of TikZ

On the following slides, there will be just three examples. For more, havea look in the manual.

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 31 / 38

Page 142: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Plots

A starting example

�400 �200 0 200 400 600 800

�8

�6

�4

�2

0

sin(x)x90 + cos(2x)� 4

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 32 / 38

Page 143: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Plots

A starting example

\begin{tikzpicture}\begin{axis}[ domain = -360:360 , samples =80,

width =10cm ,height =7cm,xmax =800]\addplot[color=red ,mark=x] coordinates {

(-200,-1)(-133,-1.2)(-66,-2)(0,-2.5)(66,-4)(133,-5)(200,-7)

};\addplot[color=blue] {sin(x)};\addplot[color=green] {-4+x/90+ cos(x*2)};

\end{axis}\end{tikzpicture}

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 32 / 38

Page 144: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Plots

Plots in 3D

0100

200300 0

200�1

0

1

�0:5

0

0:5

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 33 / 38

Page 145: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Plots

Plots in 3D

\begin{tikzpicture}\begin{axis}

\addplot 3[surf ,domain =0:360 , samples =50]{sin(x)*sin(y)};

\end{axis}\end{tikzpicture}

Take care, high sample values are not possible due to memorylimitations!

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 33 / 38

Page 146: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Plots

Plotting from files

0 2 4 6 8 10 12 14

0

2

4

6

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 34 / 38

Page 147: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Plots

Plotting from files

\begin{tikzpicture}\begin{axis}

\addplot file {charts/data.table };\end{axis}

\end{tikzpicture}

File featuresReads gnuplot style files with datapoints specified as x y i

with x and y beeing floating point values

Also specific rows of a table can be read

For details, see the manual

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 34 / 38

Page 148: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Visualization of Graph Algorithms

Some styles to define a graph and algorithm visualization

Requirements

Need styles for nodes and edges

Styles should change with the algorithm state

á Good idea to nest styles!

\tikzstyle{vertex }=[draw ,circle ,fill=Gray ,minimum size =20pt]\tikzstyle{selected vertex} = [vertex , fill=Maroon]\tikzstyle{edge} = [draw ,thick ,-]\tikzstyle{weight} = [font=\small]\tikzstyle{selected edge} = [draw ,line width=5pt,-,Green]\tikzstyle{ignored edge} = [draw ,line width=5pt,-,Salmon]

á Style „selected vertex“ based on „vertex“, but changes the fill color

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 35 / 38

Page 149: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Visualization of Graph Algorithms

Some styles to define a graph and algorithm visualization

Requirements

Need styles for nodes and edges

Styles should change with the algorithm state

á Good idea to nest styles!

\tikzstyle{vertex }=[draw ,circle ,fill=Gray ,minimum size =20pt]\tikzstyle{selected vertex} = [vertex , fill=Maroon]\tikzstyle{edge} = [draw ,thick ,-]\tikzstyle{weight} = [font=\small]\tikzstyle{selected edge} = [draw ,line width=5pt,-,Green]\tikzstyle{ignored edge} = [draw ,line width=5pt,-,Salmon]

á Style „selected vertex“ based on „vertex“, but changes the fill color

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 35 / 38

Page 150: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Visualization of Graph Algorithms

Defining a graph in four lines...

Using all our so far gained knowledge, we can say:

\foreach \pos/\name in {{(0 ,2)/a}, {(2 ,1)/b}, {(4 ,1)/c},{(0 ,0)/d}, {(3 ,0)/e}, {(2,-1)/f}, {(4,-1)/g}}

\node[vertex] (\name) at \pos {$\ name $};\foreach \source /\dest /\ weight in {b/a/7,c/b/8,d/a/5,d/b/9,

e/b/7,e/c/5,e/d/15,f/d/6,f/e/8,g/e/9,g/f/11}\path[edge] (\ source) -- node[weight] {$\ weight $} (\dest);

Using: styles, node names and foreach with tuples

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 36 / 38

Page 151: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Visualization of Graph Algorithms

Defining a graph in four lines...

a

b c

d e

f g

7

85

97

515

6

8

9

11

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 36 / 38

Page 152: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Visualization of Graph Algorithms

... and an algorithm in another six

\foreach \vertex / \slide in {d/1,a/2,f/3,b/4,e/5,c/6,g/7}\path <\slide -> node[selected vertex] at (\ vertex) {$\ vertex $};

\foreach \source / \dest in {d/a,d/f,a/b,b/e,e/c,e/g}\path <+->[ selected edge] (\ source) -- (\dest);

\foreach \source / \dest / \slide in{d/b/4,d/e/5,e/f/5,b/c/6,f/g/7}

\path <\slide ->[ignored edge] (\ source) -- (\dest);

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 37 / 38

Page 153: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Visualization of Graph Algorithms

... and an algorithm in another six

a

b c

d e

f g

7

85

97

515

6

8

9

11

d

7

85

97

515

6

8

9

11

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 37 / 38

Page 154: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Visualization of Graph Algorithms

... and an algorithm in another six

a

b c

d e

f g

7

85

97

515

6

8

9

11

d

a

7

85

97

515

6

8

9

11

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 37 / 38

Page 155: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Visualization of Graph Algorithms

... and an algorithm in another six

a

b c

d e

f g

7

85

97

515

6

8

9

11

d

a

f

7

85

97

515

6

8

9

11

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 37 / 38

Page 156: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Visualization of Graph Algorithms

... and an algorithm in another six

a

b c

d e

f g

7

85

97

515

6

8

9

11

d

a

f

b

7

85

97

515

6

8

9

11

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 37 / 38

Page 157: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Visualization of Graph Algorithms

... and an algorithm in another six

a

b c

d e

f g

7

85

97

515

6

8

9

11

d

a

f

b

e

7

85

97

515

6

8

9

11

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 37 / 38

Page 158: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Visualization of Graph Algorithms

... and an algorithm in another six

a

b c

d e

f g

7

85

97

515

6

8

9

11

d

a

f

b

e

c

7

85

97

515

6

8

9

11

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 37 / 38

Page 159: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Visualization of Graph Algorithms

... and an algorithm in another six

a

b c

d e

f g

7

85

97

515

6

8

9

11

d

a

f

b

e

c

g

7

85

97

515

6

8

9

11

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 37 / 38

Page 160: How_to_TikZ

TikZ Basics Advanced TikZ Even more advanced TikZ

Overview

Outlook

TikZ really can do a lot of stuff

Much more can be done using some of the many packages

For example object oriented programming

Worth reading a bit in the manual

Thank you!

Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 38 / 38