TikZ Tutorial
KSETA Doktorandenworkshop 2014
Christian Amstutz, Tanja Harbaum, Ewa Holt | July 22, 2014
KIT University of the State of Baden-Wuerttemberg and
National Laboratory of the Helmholtz Association
www.kit.edu
Outline
What is Tikz?
Tikz Commands
Exercises
Outlook: Potential of Tikz
Fancy Examples
TikZ
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
Exercises
Fancy Examples
July 22, 2014
2/34
What is TikZ?
Language for creating vector graphics in LATEX
TikZ = TikZ ist kein Zeichenprogramm
Same author as the Beamer class
70
69
68
67
66 65
64
71
63
72
62
73
75
76
77
27
81
34
10
37
11
12
85
87
16
15
14
13
38
39
86
40
41
42 43
45
44
56
18
17
57
19
20
58
21
36
35
59
22
32
84
60
23
30
83
26
25
24
29
33
82
28
31
79
80
78
61
74
55
54
53
52
51
50
49
48
47
46
Source: http://www.texample.net
TikZ
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
Exercises
Fancy Examples
July 22, 2014
3/34
Why using TikZ?
Single Design Among the Document
One Design Flow
More versatile Image Scaling
Math Environment within Graphics
Automatic Graph Generation (Loops)
combined with LATEX-Beamer class: graphics for presentations
But be warned! It is not so easy to learn.
TikZ
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
Exercises
Fancy Examples
July 22, 2014
4/34
Scaling Effects
Raster Graphic (JPG) Vector Graphic (PDF)
sin(x)
TikZ
sin(x )
sin(x)
sin(x )
x
TikZ
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
Exercises
Fancy Examples
July 22, 2014
5/34
Math Environment
A fancy title
eorem
Last Th
s that
m state
t Theore
ts
Ferma
To calculate the horizontal position
the kinematic differential equations
are needed:
n = u cos v sin
(1)
e = u sin + v cos
(2)
ts
Ferma
Las
n = z
n
x +y
r
tions fo
er solu
ro integ
e
-z
n
o
n
2.
has no
en n >
d z wh
x , y an
For small angles the following approximation can be used:
TikZ
n = u v
(3)
e = u + v
(4)
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
Exercises
Fancy Examples
July 22, 2014
6/34
Loops
Layer 0
Layer 1
Layer 2
Layer 3
word
FF
word
FF
word
FF
word
FF
Pattern2
word
FF
word
FF
word
FF
word
FF
Pattern3
word
FF
word
FF
word
FF
word
FF
Pattern4
word
FF
word
FF
word
FF
word
FF
Output Bus
Pattern1
matchline
TikZ
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
Exercises
Fancy Examples
July 22, 2014
7/34
Setting up the Environment in LATEX
\ documentclass { standalone }
\ usepackage { tikz }
\ us etikzlib rary { ... }
\ begin { document }
\ begin { tikzpicture }
% TikZ c o m m a n d s go here
\ end { tikzpicture }
\ end { document }
TikZ
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
Exercises
Fancy Examples
July 22, 2014
8/34
The \draw Command
\ draw (0 ,0) -- (1 ,1);
\ draw (0 ,0) rectangle (1 ,1);
\ draw (0 ,0) circle (0.5);
TikZ
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
Exercises
Fancy Examples
July 22, 2014
9/34
Coordinates
Cartesian Coordinates (x,y)
\ draw [ blue ] (0 ,0) -- (2 ,1);
Polar coordinates (angle:radius)
\ draw [ blue ] (0 ,0) -- (45:1.7);
Relative Coordinates ++(rel x,rel y)
\ draw [ blue ] (0 ,0) -- ++(0.5 ,0.5) -- ++(1 ,0)
-- ++(0.5 ,0.5);
Define Coordinates
\ coordinate
\ coordinate
\ coordinate
\ draw [ blue ]
TikZ
(A)
(B)
(C)
(A)
at
at
at
--
(0 ,0);
(1 ,1);
(2 ,0);
( B ) -- ( C );
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
Exercises
Fancy Examples
July 22, 2014
10/34
The \node Command
A node is typically a rectangle or circle or another simple shape with some
text on it
\ node [ rectangle , fill = green ]( rect ) { Rectangle };
Rectangle
Node positioning
Rectangle
\ node [ rectangle , fill = green ]( rect ){ Rectangle };
\ node [ circle , fill = purple , below = of rect ]( circ ){ Circle };
Circle
Connect nodes with lines
Rectangle
\ node [ rectangle , fill = green ]( rect ){ Rectangle };
\ node [ circle , fill = purple , below = of rect ]( circ ){ Circle };
\ draw [ - >] ( rect ) -- ( circ );
TikZ
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
Exercises
Circle
Fancy Examples
July 22, 2014
11/34
Style Definitions
Styles are defined by [] behind a command
\ draw [ red , very thick , dashed ] (0 ,0) -- (1 ,0.1);
Styles can be named and defined locally or globally
\ tikzset { my style /. style ={ tikz options }}
\ tikzstyle { my style }=[ tikz options ]
% deprecated
example
\ tikzset { my dot /. style ={ blue , fill = green , thick }}
\ draw [ my dot ] (0 ,0) circle (0.2);
\ draw [ my dot ] (0.1 ,0.6) circle (0.2);
\ draw [ my dot , fill = red ] (0.8 ,0.2) circle (0.2);
TikZ
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
Exercises
Fancy Examples
July 22, 2014
12/34
Exercise 1: UML Activity Diagram
wake up
tired?
yes
no
start the day
TikZ
drink coffee
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
Exercises
Fancy Examples
July 22, 2014
13/34
Exercise 1: UML Activity Diagram
\ tikzset { start /. style ={ circle , minimum width =0.3 cm ,
minimum height =0.3 cm , draw , fill }}
\ node [ start ] ( start ) {};
TikZ
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
Exercises
Fancy Examples
July 22, 2014
14/34
Exercise 1: UML Activity Diagram
\ tikzset { activity /. style ={ rectangle , minimum width =1 cm ,
minimum height =0.5 cm , rounded corners =5 pt , draw }}
\ node [ activity , below of = start ] ( action 1) { wake up };
wake up
TikZ
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
Exercises
Fancy Examples
July 22, 2014
15/34
Exercise 1: UML Activity Diagram
\ tikzset { decision /. style ={ diamond , minimum width =1 cm ,
minimum height =1 cm , draw }}
\ node [ decision , below = of action 1]( decision 1){ tired ?};
wake up
tired?
TikZ
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
Exercises
Fancy Examples
July 22, 2014
16/34
Exercise 1: UML Activity Diagram
\ node [ activity , below = of decision 1] ( action 2) { start the day };
\ node [ activity , right = of action 2] ( action 3) { drink coffee };
wake up
tired?
start the day
TikZ
drink coffee
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
Exercises
Fancy Examples
July 22, 2014
17/34
Exercise 1: UML Activity Diagram
\ tikzset { end /. style ={ draw , double = white , circle ,
inner sep =1 pt , minimum width =0.3 cm , minimum height =0.3 cm
\ node [ end , below of = action 2]( end ){};
wake up
tired?
start the day
TikZ
drink coffee
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
Exercises
Fancy Examples
July 22, 2014
18/34
Exercise 1: UML Activity Diagram
\ draw [ - >]( start ) -- ( action 1);
\ draw [ - >]( action 1) -- ( decision 1);
\ draw [ - >]( action 3) -- ( action 2);
\ draw [ - >]( action 2) -- ( end );
wake up
tired?
start the day
TikZ
drink coffee
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
Exercises
Fancy Examples
July 22, 2014
19/34
Exercise 1: UML Activity Diagram
\ draw [ - >]( decision 1) -- node [ left , very near start ]{ no } ( action 2);
\ draw [ - >]( decision 1) -| node [ above , very near start ]{ yes } ( action 3)
wake up
tired?
yes
no
start the day
TikZ
drink coffee
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
Exercises
Fancy Examples
July 22, 2014
20/34
Exercise 1: UML Activity Diagram Solution I
% \ u s e t i k z l i b r a r y { shapes }
\ begin { tikzpicture }
\ tikzset { activity /. style ={ rectangle , minimum width =1 cm , minimum height
\ tikzset { decision /. style ={ diamond , minimum width =1 cm , minimum height =1
\ tikzset { end /. style ={ draw , double = white , circle , inner sep =1 pt , minimum
\ tikzset { start /. style ={ circle , minimum width =0.3 cm , minimum height =0.3
\ node [ start ] ( start ) {};
\ node [ activity , below of = start ] ( action 1) { wake up };
\ node [ decision , below = of action 1]( decision 1){ tired ?};
\ node [ activity , below = of decision 1] ( action 2) { start the day };
\ node [ activity , right = of action 2] ( action 3) { drink coffee };
\ node [ end , below of = action 2]( end ){};
TikZ
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
Exercises
Fancy Examples
July 22, 2014
21/34
Exercise 1: UML Activity Diagram Solution II
\ draw [ - >]( start ) -- ( action 1);
\ draw [ - >]( action 1) -- ( decision 1);
\ draw [ - >]( decision 1) -- node [ left , very near start ]{ no } ( action 2);
\ draw [ - >]( decision 1) -| node [ above , very near start ]{ yes } ( action 3)
\ draw [ - >]( action 3) -- ( action 2);
\ draw [ - >]( action 2) -- ( end );
\ end { tikzpicture }
TikZ
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
Exercises
Fancy Examples
July 22, 2014
22/34
Exercise 2: p-p collision
e+
p
n
p
TikZ
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
Exercises
Fancy Examples
July 22, 2014
23/34
Exercise 2: p-p collision - Solution I
\ begin { tikzpicture }[ scale =0.7 , transform shape ]
\ tikzset { proton /. style ={ circle , black , thick , fill = red ,
minimum width =1.5 cm , minimum height =1.5 cm , draw }}
\ tikzset { neutron /. style ={ circle , black , thick , fill = gray ,
minimum width =1.5 cm , minimum height =1.5 cm , draw }}
\ tikzset { collision /. style ={ star , star points =8 ,
star point ratio =0.2 , black , thick , fill = yellow ,
minimum width =0.5 cm , minimum height =0.5 cm , draw }}
\ tikzset { neutrino /. style ={ circle , black , thick , fill = blue ,
minimum width =0.8 cm , minimum height =0.8 cm , draw }}
\ tikzset { positron /. style ={ circle , black , thick , fill = yellow ,
minimum width =1.2 cm , minimum height =1.2 cm , draw }}
\ tikzset { myarrow /. style ={ - > , shorten >=0.5 cm , shorten <=0.5 cm ,
very thick }}
\ node [ proton ] ( proton 1) {};
\ node [ font =\ Huge ] {\ textbf { p }};
\ node [ proton , below = 5 cm ] ( proton 2) {};
\ node [ font =\ Huge ] at ( proton 2) {\ textbf { p }};
\ node [ collision , below right = 2.125 cm and 4 cm of proton 1]
TikZ
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
Exercises
Fancy Examples
July 22, 2014
24/34
Exercise 2: p-p collision - Solution II
( collision ) {};
\ node [ positron , right = 8 cm of proton 1] ( positron ) {};
\ node [ font =\ Huge ] at ( positron ) {\ textbf { e $^+$}};
\ node [ neutrino , right = 8 cm of proton 2] ( neutrino ) {};
\ node [ font =\ Huge ] at ( neutrino ) {\ textbf {$\ nu $}};
\ node [ proton , below right = 1.25 cm and 10 cm of proton 1]
( proton 3) {};
\ node [ font =\ Huge ] at ( proton 3) {\ textbf { p }};
\ node [ neutron , below of = proton 3] ( neutron ) {};
\ node [ font =\ Huge ] at ( neutron ) {\ textbf { n }};
\ draw [ myarrow ] ( proton 1) -- ( collision );
\ draw [ myarrow ] ( proton 2) -- ( collision );
\ draw [ myarrow ] ( collision ) -- ( positron );
\ draw [ myarrow ] ( collision ) -- ( neutrino );
\ draw [ myarrow ] ( collision ) -- ( proton 3. south west );
\ end { tikzpicture }
TikZ
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
Exercises
Fancy Examples
July 22, 2014
25/34
Plotting Data
f (x )
f (x ) = 1 + 13 x 2
0
0
TikZ
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
6
Exercises
Fancy Examples
July 22, 2014
26/34
Plotting data - Code
\ begin { tikzpicture }[ domain =0.2:6]
\ draw [ - > , >= stealth ] ( -0.2 ,0) -- (7 ,0) node [ right ] {$ x $};
\ draw [ - > , >= stealth ] (0 , -0.2) -- (0 ,6) node [ above ] {$ f ( x )$};
\ foreach \ x in {0.5 ,1 ,1.5 ,2 ,2.5 ,3 ,3.5 ,4 ,4.5 ,5 ,5.5 ,6 ,6.5}
\ draw (\ x ,2 pt ) -- (\ x , -3 pt );
\ foreach \ x in {0 ,1 ,2 ,3 ,4 ,5 ,6}
\ node at (\ x , -6 pt ) [ anchor = north ] {\ footnotesize $\ x $};
\ foreach \ y /\ ytext in {0.5 ,1 ,1.5 ,2 ,2.5 ,3 ,3.5 ,4 ,4.5 ,5 ,5.5}
\ draw (2 pt ,\ y ) -- ( -3 pt ,\ y cm );
\ foreach \ y /\ ytext in {0 ,1 ,2 ,3 ,4 ,5}
\ node at ( -6 pt ,\ y ) [ anchor = east ] {\ footnotesize $\ ytext $};
\ draw plot [ only marks , mark =x , mark options ={ kit - blue 100 , thick }]
file { working _ material / measurement . dat };
\ draw [ color = kit - green 100] plot [ smooth ] (\ x , {1+ pow ((1/3)*\ x , 2)})
node [ right , xshift =6 mm ] {$ f ( x ) = 1+\ frac {1}{3} x ^{2}$};
\ end { tikzpicture }
TikZ
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
Exercises
Fancy Examples
July 22, 2014
27/34
Mind Map
algorithms
data
structures
Computer Science
theoretical
practical
programming
languages
software
engineering
applied
technical
databases
WWW
TikZ
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
Exercises
Fancy Examples
July 22, 2014
28/34
Mind Map - Code I
\ us etikzlib rary { mindmap , trees }
\ begin { tikzpicture }[ scale =0.5 , transform shape ]
\ path [ mindmap , concept color = black , text = white ]
node [ concept ] { Computer Science }
[ clockwise from =0]
child [ concept color = green !50! black ] {
node [ concept ] { practical }
[ clockwise from =90]
child { node [ concept ] { algorithms } }
child { node [ concept ] { data structures } }
child { node [ concept ] { pro \ - gramming languages } }
child { node [ concept ] { software engineer \ - ing } }
}
child [ concept color = blue ] {
node [ concept ] { applied }
[ clockwise from = -30]
child { node [ concept ] { databases } }
child { node [ concept ] { WWW } }
}
TikZ
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
Exercises
Fancy Examples
July 22, 2014
29/34
Mind Map - Code II
child [ concept color = red ] { node [ concept ] { technical } }
child [ concept color = orange ] { node [ concept ] { theoretical } };
\ end { tikzpicture }
TikZ
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
Exercises
Fancy Examples
July 22, 2014
30/34
Fancy Examples - Polarizing Microscope
z
y
ng0
np0
~
E
Polarized and
dephased light
Polaroid
oid
lar
Po
Crystal section
Polarized light
Source: http://www.texample.net
TikZ
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
Exercises
Fancy Examples
July 22, 2014
31/34
Fancy Examples - Dipolar magnetic field
B~
Sm
~r
B
Nm
B~
~r
B
Source: http://www.texample.net
TikZ
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
Exercises
Fancy Examples
July 22, 2014
32/34
More information
Website with nice TikZ examples:
http://www.texample.net/tikz/examples
A very minimal introduction to TikZ - A short and good introduction:
http://cremeronline.com/LaTeX/minimaltikz.pdf
TikZ PGF Manual (Version 3.0) - great resource written in clear,
comprehensible language:
http://mirrors.ctan.org/graphics/pgf/base/doc/pgfmanual.pdf
TikZ Cheat Sheet - Short cheatsheet far from being complete:
http://home.snc.edu/andershendrickson/tex/TikZcheatsheet.pdf
This tutorial with all the sources:
https://github.com/camstutz/tikz tutorial
TikZ
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
Exercises
Fancy Examples
July 22, 2014
33/34
Thank you for your attention
TikZ
Commands
Christian Amstutz, Tanja Harbaum, Ewa Holt Short title
Exercises
Fancy Examples
July 22, 2014
34/34