Académique Documents
Professionnel Documents
Culture Documents
An overview
Jan-Philipp Kappmeier
Introduction
TikZ
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
Introduction
For starters
Example
It is easy to draw a thistle .
\tikz{ \filldraw[color=Thistle] circle (0.5ex); }
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
Basic commands
Drawing Graphs
Nodes
Nodes 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
Basic commands
Basic commands
Basic commands
Basic commands
Basic commands
Basic commands
Polar coordinates
Polar coordinates
All coordinates can be defined via polar coordinates
Needed only an angle and the radius (distnace from the origin)
expressed as (angle:radius)
Polar coordinates
Polar coordinates
All coordinates can be defined via polar coordinates
Needed only an angle and the radius (distnace from the origin)
expressed as (angle:radius)
Polar coordinates
Polar coordinates
All coordinates can be defined via polar coordinates
Needed only an angle and the radius (distnace from the origin)
expressed as (angle:radius)
Loops
Loops
Or even to a 7 wheel?
Loops
Or even to a 7 wheel?
\ pgfmathsetmacro {\ n }{5}
\ pgfmath t r u n c a t e m a cro {\ 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 );
\ pgfma t h t r u n c a t e macro {\ j }{ mod ( round (1+\ i ) ,\ n )}
\ draw [ red ] (\ i ) -- (\ j );
}
\ pgfmathsetmacro {\ n }{5}
\ pgfmath t r u n c a t e m a cro {\ 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 );
\ pgfma t h t r u n c a t e macro {\ j }{ mod ( round (1+\ i ) ,\ n )}
\ draw [ red ] (\ i ) -- (\ j );
}
Loops cont.
Example
Want to highlight specific numbers on the real line.
Loops cont.
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 }};
Loops cont.
Example
Want to highlight specific numbers on the real line.
Give it a first try:
Loops cont.
Example
Want to highlight specific numbers on the real line.
Better:
e
0 1 2
Loops cont.
Example
Want to highlight specific numbers on the real line.
Calculate coordinates
Coordinates
Can be defined using coordinate
Like nodes with empty text
Coordinates can be computed (like vector math)
need to add the package calc (\includetikzpackage{calc})
A
= 12
Construction
1 Start with an isosceles triangle with base of length a
G
B
A
= 12
Construction
1 Start with an isosceles triangle with base of length a
2 Draw a copy of AB at F
G
B
A
I
= 12
Construction
1 Start with an isosceles triangle with base of length a
2 Draw a copy of AB at F
3 Draw segment AI of length b with some angle
A
I
= 12
Construction
1 Start with an isosceles triangle with base of length a
2 Draw a copy of AB at F
3 Draw segment AI of length b with some angle
4 and copy it to G
A
I
= 12
Construction
1 Start with an isosceles triangle with base of length a
2 Draw a copy of AB at F
3 Draw segment AI of length b with some angle
4 and copy it to G
5 Connect H and I
H E
G
B
C
F
A
I
= 12
Construction
1 Start with an isosceles triangle with base of length a
2 Draw a copy of AB at F
3 Draw segment AI of length b with some angle
4 and copy it to G
5 Connect H and I
6 Rotate the polygonal path FGHIA around A by 12
H E
G
B
C
F
A
I
= 12
H E
G
B
C
F
A
I
= 12
H E
G
B
C
F
A
I
= 12
H E
G
B
C
F
A
I
= 12
H E
G
B
C
F
A
I
= 12
H
E
G
B
C
F
A
I
= 12
H
E
G
B
C
F
A
I
= 12
H
E
G
B
C
F
A
I
= 12
H
E
G
B
C
F
A
I
= 12
H
E
G
B
C
F
A
I
= 12
H
E
G
B
C
F
A
I
= 12
H
E
G
B
C
F
A
I
= 12
H
E
G
B
C
F
A
I
= 12
H
E
G
B
C
F
A
I
= 12
H
E
G
B
A
I
= 12
H
E
G
B
A
I
= 12
H
E
G
B
A
I
= 12
H
E
G
B
A
I
= 12
H
E
G
B
A
I
= 12
H
E
G
B
A
I
= 12
H
E
G
B
A
I
= 12
H
E
G
B
A
I
= 12
H
E
G
B
A
I
= 12
H
E
G
B
A
I
= 12
H
E
G
B
A
I
= 12
H
E
G
B
A
I
= 12
H
E
G
B
A
I
= 12
H
E
G
B
A
I
= 12
H
E
G
B
A
I
= 12
H
E
G
B
A
I
= 12
= AB = FG
E
G
a
= AI = HG
B
b
C
A
I
= 12
= AB = FG
E
G
a
= AI = HG
B
b
C
A
I
= 12
Example
Define Variables to use:
\ def \ a {0.5}
\ def \ b {0.5}
\ def \ bAngle { -10}
= AB = FG
E
G
a
= AI = HG
B
b
C
A
I
= 12
Example
Compute Distance of Point F from AB:
\ pgfmathsetmacro {\ hyp }{\ a *0.5 / cos (84)}
\ pgfmathsetmacro {\ len }{ sqrt (\ hyp *\ hyp -0.25*\ a *\ a )}
= AB = FG
E
G
a
= AI = HG
B
b
C
A
I
= 12
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 ) $);
= AB = FG
E
G
a
= AI = HG
B
b
C
A
I
= 12
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 ) $);
= AB = FG
E
G
a
= AI = HG
B
b
C
A
I
= 12
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 ) $);
Node features
Multiline nodes: Allows to have several lines of text within one node
Line 1
\node[align=center] {Line 1 \\ Another line}; Another one
Anchors: defines the corner of the node that lies at the specifies
position. Default is center.
\node[draw,anchor=north west] at (0,0) {NW};
\node[draw,anchor=south east] at (0,0) {SE};
SE
NW
Creating own shapes is possible, but not easy. Needs low level
coding of so-called basic layer.
\node[starburst,fill=yellow,draw=red,line width=2pt] at
Example
Example
Example
Example
Example
Example
Example
Example
Scopes
Scopes in TikZ
TikZ allows scopes, just like e. g. J AVA
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
Scopes
Scopes
Scopes
Scopes
Scopes
Scopes
Scopes
Scopes
Scopes
Scopes
Scopes
Scopes
Scopes
Scopes
Scopes
Scopes
Scopes
Scopes
Scopes
Scopes
Scopes
Scopes
Scopes
Scopes
Scopes
Scopes
Scopes
Scopes
Scopes
Scopes
Intersections
Intersections
H E
G
B
C
F
A
I
= 12
Intersections
BIntersections
C H
D
E Nice detail:
G
B
End point of helping line is in the
C
F
middle of the angle.
A
I
= 12
A
I
Jan-Philipp Kappmeier (13. 12. 2010) How to TikZ? 22 / 38
TikZ Basics Advanced TikZ Even more advanced TikZ
Intersections
BIntersections
C H
D
E Nice detail:
G
B
End point of helping line is in the
C
F
middle of the angle.
A
I
= 12
A
I
The good thing:
TikZ can automatically compute these intersection points.
Paths can be arbitrary, not only line segments!
Intersections
Intersections (cont.)
Intersections
Intersections (cont.)
Trees
Trees
Trees
Trees
Trees
Trees
Trees
Trees
Trees
Trees
Trees
Trees
Trees
Trees
Trees
Trees
Trees
Trees
Trees
Defining Trees
Use child{} in a node definition to create a child
Use node and childs iterativeley to create a tree
Example
Trees
Split nodes
Example
\node[rectangle split,rectangle split parts=3,draw]
{1\nodepart{two}2\nodepart{three}3};
1
Trees
Recursive Sorting
5 2 3 7 4 6 1 9
5 2 3 74 6 1 9
5 2 7
3 6
4 1 9
5 3
2
4 7
6
1 9
Trees
Recursive Sorting
Example
\ begin { tikzpicture }[
level 1/. style ={ sibling distance =60 mm } ,
level 2/. style ={ sibling distance =30 mm } ,
level 3/. style ={ sibling distance =20 mm }]
\ end { tikzpicture }
á All siblings on level 1 will have a distance of 60mm
Trees
Recursive Sorting
5 2 3 7 4 6 1 9
5 2 3 7 4 6 1 9
5 2 3 7 4 6 1 9
5 2 3 7 4 6 1 9
Trees
Recursive Sorting
5 2 3 7 4 6 1 9
5 2 3 7 4 6 1 9
5 2 3 7 4 6 1 9
5 2 3 7 4 6 1 9
Trees
Recursive Sorting
5 2 3 7 4 6 1 9
5 2 3 7 4 6 1 9
2 5 3 7 4 6 1 9
5 2 3 7 4 6 1 9
Trees
Recursive Sorting
5 2 3 7 4 6 1 9
2 3 5 7 1 4 6 9
2 5 3 7 4 6 1 9
5 2 3 7 4 6 1 9
Trees
Recursive Sorting
1 2 3 4 5 6 7 9
2 3 5 7 1 4 6 9
2 5 3 7 4 6 1 9
5 2 3 7 4 6 1 9
Trees
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 }
Trees
Styles example
Example
s0 b
a t
s1 c
Trees
Styles example
Example
s0 b
a t
s1 c
Plots
Plotting in TikZ
5
Time (s)
4 RUNTIME
0
23 26 29 212 215 218 221
Size of instance
But: quite lengthy code, as axes and legend have to be drawn manually
Plots
On the following slides, there will be just three examples. For more, have
a look in the manual.
Plots
A starting example
sin(x )
x
+ cos(2x ) 4
90
0
Plots
A starting example
\ begin { tikzpicture }
\ begin { axis }[ domain = -360:360 , samples =80 ,
width =10 cm , height =7 cm , 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 }
Plots
Plots in 3D
0:5
1
0
0
0:5
1
0 200
100
200
300 0
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 memory
limitations!
Plots
0
0 2 4 6 8 10 12 14
Plots
\ begin { tikzpicture }
\ begin { axis }
\ addplot file { charts / data . table };
\ end { axis }
\ end { tikzpicture }
File features
Reads 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
Requirements
Need styles for nodes and edges
Styles should change with the algorithm state
á Good idea to nest styles!
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 =20 pt ]
\ tikzstyle { selected vertex } = [ vertex , fill = Maroon ]
\ tikzstyle { edge } = [ draw , thick , -]
\ tikzstyle { weight } = [ font =\ small ]
\ tikzstyle { selected edge } = [ draw , line width =5 pt , - , Green ]
\ tikzstyle { ignored edge } = [ draw , line width =5 pt , - , Salmon ]
á Style „selected vertex“ based on „vertex“, but changes the fill color
a
7
8
5 b c
7
9 5
15
d e
6 9
8
11
f g
a
7
8
5 b c
7
9 5
15
d e
6 9
8
11
f g
a
7
8
5 b c
7
9 5
15
d e
6 9
8
11
f g
a
7
8
5 b c
7
9 5
15
d e
6 9
8
11
f g
a
7
8
5 b c
7
9 5
15
d e
6 9
8
11
f g
a
7
8
5 b c
7
9 5
15
d e
6 9
8
11
f g
a
7
8
5 b c
7
9 5
15
d e
6 9
8
11
f g
a
7
8
5 b c
7
9 5
15
d e
6 9
8
11
f g
Overview
Outlook
Thank you!