Tutorial Tikz

Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

TikZ Tutorial

KSETA Doktorandenworkshop 2014


Christian Amstutz, Tanja Harbaum, Ewa Holt | July 21, 2014

KIT – University of the State of Baden-Wuerttemberg and www.kit.edu


National Laboratory of the Helmholtz Association
Outline

What is Tikz?
Tikz Commands
Exercises
Outlook: Potential of Tikz
Fancy Examples

TikZ Commands Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 2/37
What is TikZ?

Language for creating vector graphics in LATEX


TikZ = TikZ ist kein Zeichenprogramm
Same author as the Beamer class
√ √ √
√ 68 67 66 √65 √
√ 69
70 64 √
√ 63
71 √
√ 62
72 √
√ 61
73
√ √
74 √ √ 60
√ 26 25 √ √
√ √ 27 24 √ 59
75 28 23
√ √ √
√ 29 22
76 58
√ √
√ 30 21 √
77 57
√ √
31 √ 20 √
√ √
78 √ 4 56
√ 5 3 √
32 √ 19 √
√ √ 2
79 6 55
√ √
33 √ 18
√ √ √
80 7 1 54
√ √
34 17 √
√ √
81 8 √ 53

35 16
√ √ √
82 9 √ 52
√ 15
36 √ √ √
√ 10 √ 14 51

83 √ 11 √ 13
37 12 √
√ 50
84 √
38 √
√ √ 49
85 39 √
√ 48
√ 40 √ √
86 √ 47
41 √ √ 46
√ 42 √43 √ 45
87 44

Source: http://www.texample.net

TikZ Commands Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 3/37
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 easy to learn.

TikZ Commands Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 4/37
Scaling Effects

Raster Graphic (JPG) Vector Graphic (PDF) TikZ

sin(x) sin(x )

x x

sin(x) sin(x )

TikZ Commands Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 5/37
Math Environment

A fancy title eo rem


t’s Last Th
Ferma t
tes tha
To calculate the horizontal position rem sta
t’s La st Theo
the kinematic differential equations Ferma n
n = z
are needed: x +y
n
r
tions fo
er solu
ṅ = u cos ψ − v sin ψ (1)
on -z ero integ
n
ė = u sin ψ + v cos ψ (2) has no en n >
2.
d z wh
♣ x , y an
For small angles the following ap-
proximation can be used:

ṅ = u − v δψ (3)
ė = u δψ + v (4)

TikZ Commands Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 6/37
Loops
Layer 0 Layer 1 Layer 2 Layer 3

Pattern1 word FF word FF word FF word FF

Pattern2 word FF word FF word FF word FF

Output Bus
Pattern3 word FF word FF word FF word FF

Pattern4 word FF word FF word FF word FF

matchline

TikZ Commands Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 7/37
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 Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 8/37
The \draw Command

\ draw (0 ,0) -- (1 ,1);

\ draw (0 ,0) rectangle (1 ,1);

\ draw (0 ,0) circle (0.5);

TikZ Commands Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 9/37
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 (A) at (0 ,0);
\ coordinate (B) at (1 ,1);
\ coordinate (C) at (2 ,0);
\ draw [ blue ] (A) -- ( B ) -- ( C );

TikZ Commands Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 10/37
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 ); Circle

TikZ Commands Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 11/37
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 Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 12/37
Exercise 1: UML Activity Diagram

wake up

yes
tired?

no

start the day drink coffee

TikZ Commands Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 13/37
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 Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 14/37
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 Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 15/37
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 Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 16/37
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 drink coffee

TikZ Commands Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 17/37
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 drink coffee

TikZ Commands Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 18/37
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 drink coffee

TikZ Commands Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 19/37
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

yes
tired?

no

start the day drink coffee

TikZ Commands Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 20/37
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 ){};

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

TikZ Commands Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 21/37
Exercise 1: UML Activity Diagram -
Solution II

\ draw [ - >]( action 3) -- ( action 2);


\ draw [ - >]( action 2) -- ( end );

\ end { tikzpicture }

TikZ Commands Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 22/37
Exercise 2: p-p collision

p e+

p
n

p ν

TikZ Commands Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 23/37
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 ={ - latex , 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 Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 24/37
Exercise 2: p-p collision - Solution II

( collision 1) {};
\ node [ positron , right = 8 cm of proton 1] ( positron 1) {};
\ node [ font =\ Huge ] at ( positron 1) {\ textbf { e $^+$}};
\ node [ neutrino , right = 8 cm of proton 2] ( neutrino 1) {};
\ node [ font =\ Huge ] at ( neutrino 1) {\ 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 1) {};
\ node [ font =\ Huge ] at ( neutron 1) {\ textbf { n }};

\ draw [ myarrow ] ( proton 1) -- ( collision 1);


\ draw [ myarrow ] ( proton 2) -- ( collision 1);
\ draw [ myarrow ] ( collision 1) -- ( positron 1);
\ draw [ myarrow ] ( collision 1) -- ( neutrino 1);
\ draw [ myarrow ] ( collision 1) -- ( proton 3. south west );
\ end { tikzpicture }

TikZ Commands Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 25/37
Feynman Diagram

W
q q
q q

g
q q

TikZ Commands Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 26/37
Feynman Diagram - Code I

\ begin { tikzpicture }[ xscale =0.5 , yscale =0.5]


\ draw [ help lines ] (0 ,0) grid (10 ,10);
\ draw [ thick ] (0.5 ,5) -- (3.5 ,8);
\ draw [ - latex , thick ] (1.5 ,6) -- (2 ,6.5);
\ node [ above ] at (2 ,6.5){ q };
\ draw [ fill ] (3.5 ,8) circle [ radius =0.1];
\ draw [ domain =3.5:6.5 , thick , samples =200] plot (\ x ,{8+0.1* sin (1
\ node [ above ] at (5 ,8){ W };
\ draw [ fill ] (6.5 ,8) circle [ radius =0.1];
\ draw [ thick ] (6.5 ,8) -- (9.5 ,5);
\ draw [ - latex , thick ] (7.5 ,7) -- (8 ,6.5);
\ node [ above ] at (8 ,6.5){ q };
\ draw [ thick , decoration ={ markings , mark = at position 0.6 with {\ ar
(3.5 ,8) to [ out = -90 , in =160] (5 ,5);
\ node [ left ] at (3.7 ,6.5){ q };
\ draw [ thick , decoration ={ markings , mark = at position 0.5 with
(5 ,5) to [ out =20 , in =270] (6.5 ,8);
\ node [ right ] at (6.3 ,6.5){ q };
\ draw [ fill ] (5 ,5) circle [ radius =0.1];
\ draw [ domain =4:5 , thick , samples =200 , rotate =90] plot (\ x ,{ -5+0

TikZ Commands Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 27/37
Feynman Diagram - Code II

\ node [ right ] at (5 ,4.5){ g };


\ draw [ fill ] (5 ,4) circle [ radius =0.1];
\ draw [ thick ] (2 ,3) -- (5 ,4);
\ draw [ - latex , thick ] (2 ,3) -- (3.5 ,3.5);
\ node [ above ] at (3.5 ,3.5){ q };
\ draw [ thick ] (5 ,4) -- (8 ,3);
\ draw [ - latex , thick ] (5 ,4) -- (6.5 ,3.5);
\ node [ above ] at (6.5 ,3.5){ q };
\ end { tikzpicture }

TikZ Commands Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 28/37
Plotting Data
f (x )

5 f (x ) = 1 + 13 x 2

0 x
0 1 2 3 4 5 6

TikZ Commands Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 29/37
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 Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 30/37
Mind Map

algorithms

data
structures

theoretical Computer Science practical

pro-
gramming
languages

software
engineer-
ing

technical applied

databases

WWW

TikZ Commands Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 31/37
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 Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 32/37
Mind Map - Code II

child [ concept color = red ] { node [ concept ] { technical } }


child [ concept color = orange ] { node [ concept ] { theoretical } };
\ end { tikzpicture }

TikZ Commands Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 33/37
Fancy Examples - Polarizing Microscope
z
y

Polarized light

oid ng0

Crystal section
lar np0
Po Polarized and
~
E dephased light

Polaroid
x

Source: http://www.texample.net

TikZ Commands Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 34/37
Fancy Examples - Dipolar magnetic field

B~θ
Sm
~r
B

Nm
B~θ
~r
B

Source: http://www.texample.net

TikZ Commands Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 35/37
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 Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 36/37
Thank you for your attention

TikZ Commands Exercises outlook Fancy Examples

Christian Amstutz, Tanja Harbaum, Ewa Holt – TikZ Tutorial July 21, 2014 37/37

You might also like