TIKZ EXAMPLES
IAN DOUGLAS AND NANDOR SIEBEN
In LATEX load the package with \ newcommand \ pent agonra dius { 1 cm }
\ begin { tikzpicture }
\ usepackage { pgf , tikz } \ tikzstyle { every node }=[ draw ]
\ us etikzl ibrary { arrows } \ foreach \ x in {0 ,... ,4}
\ us etikzl ibrary { shapes } \ node ( P \ x ) at (\ x *72:\ pen tagonr adius ) {\ x };
\ us etikzl ibrary { patterns } \ draw ( P0 ) - -( P1 ) - -( P2 ) - -( P3 ) - -( P4 ) - -( P0 ) ;
\ end { tikzpicture }
\ begin { tikzpicture }[ scale =1.5]
1
2
% absolute coordinates
\ draw [ line width =3 pt , fill = blue !10] 0
(0 ,0) - -(2 ,0) - -(2 ,1) - -(0 ,1) -- cycle ;
% relative coordinates
3
4
\ draw [ pattern color = black !30 ,
pattern = north east lines ]
(3 ,0) - -+(2 ,0) - -+(2 ,1) - -+(0 ,1) -- cycle ; \ begin { tikzpicture }
\ end { tikzpicture } \ draw [ step =.5 cm ] (0 ,0) grid (2.5 ,2) ;
\ draw [ shift ={(6 ,1) } , rotate =45]
( -.5 , -.5) rectangle (.5 ,.5) ;
\ draw (4 ,1) circle (.3 cm ) circle (.5 cm ) ;
\ draw (5 ,1) ellipse (2 cm and 1 cm ) ;
\ end { tikzpicture }
\ begin { tikzpicture }
% polar coordinates
\ coordinate [ label = right : $X$ ] ( P0 ) at (0:1 cm ) ;
\ coordinate ( P1 ) at (1*72:1 cm ) ;
\ node [ draw , circle , inner sep =1.2 pt , label = -30: $v$ ]
( P2 ) at (2*72:1 cm ) {}; \ begin { tikzpicture }
\ node ( P3 ) at (3*72:1 cm ) { $C$ }; \ draw [ dashed , - >]
\ coordinate ( P4 ) at (4*72:1 cm ) ; (0 ,0) .. controls (1 ,1) and (2 , -1) .. (3 ,0) ;
\ draw ( P0 ) - -( P1 ) - -( P2 ) - -( P3 ) - -( P4 ) - -( P0 ) ; \ draw [ densely dotted , < - >]
\ node at ( P1 ) { A }; (4 ,0) to [ out =90 , in = -160]
\ node at ( P3 .180) { $ \ circ$ }; node [ pos =.6 , sloped , above ] { $ \ sum$ }
\ end { tikzpicture } (6 ,0) ;
\ end { tikzpicture }
A P
v
X
◦C
Decorating Lines:
\ begin { tikzpicture }
\ node ( A ) at (0:3 cm ) { $A$ }; upper label
\ node ( D ) at (3*60:3 cm ) { $D$ }; D A
\ draw [ auto ] ( D ) to lower label
node { upper label }
node [ swap ] { lower label }
(A);
\ end { tikzpicture }
1
TIKZ EXAMPLES 2
Decorating Nodes:
\ begin { tikzpicture }[ scale =0.6 , { upper bit \ nodepart { lower } lower half };
circed /. style ={ circle , draw = blue , fill } , \ node [ label = above : $x ^2 $ , draw ] at (9 , -6) { $L$ };
rected /. style ={ rectangle , draw = black , fill = green , thick \ end { tikzpicture }
}]
\ node [ draw , shape = circle ] at (0 ,0) { $A$ }; A B C D
\ node [ draw , circle , thick ] at (3 ,0) { $B$ };
\ node [ draw , rectangle ] at (6 ,0) { $C$ };
\ node [ draw , circle , double ] at (9 ,0) { $D$ };
\ node [ circed ] at (0 , -3) { $E$ };
\ node [ rected ] at (3 , -3) { $F$ }; E F Ge H
\ node [ draw , fill = red , rounded corners ] at (6 , -3)
{ $Ge$ };
\ node [ draw , circle , line width =5 pt ] at (9 , -3) { H };
upper bit x2
\ node [ draw , circle , inner sep =1 pt ] at (0 , -6) { I };
\ node [ draw , circle , inner sep =9 pt , thin ] at (3 , -6) I J L
lower half
{ $J$ };
\ node [ circle split , draw ] at (6 , -6)
Bending:
\ begin { tikzpicture } \ path ( A2 ) edge [ loop left ] ( A2 )
\ node ( A ) at (0 ,0) { A }; ( A2 ) edge [ < - >] ( B2 )
\ node ( B ) at (2 ,0) { B }; ( B2 ) edge [ bend right , - >] ( A2 )
\ node ( C ) at (4 ,0) { C }; ( B2 ) edge [ bend right , - >] ( C2 )
\ node ( A2 ) at (0 , -1) { A }; ( C2 ) edge [ < - >] ( B2 ) ;
\ node ( B2 ) at (2 , -1) { B }; \ end { tikzpicture }
\ node ( C2 ) at (4 , -1) { C };
\ path ( A ) edge ( B )
( B ) edge [ bend right , - >] ( A )
A B C
( B ) edge [ bend right , - >] ( C )
( C ) edge ( B ) A B C
( C ) edge [ loop right ] ( C ) ;
Line types
\ begin { tikzpicture }[ scale =.5] C B
\ tikzstyle { every node }=[ draw , shape = circle ];
\ node ( A ) at (0:3 cm ) { $A$ };
\ node ( B ) at (1*60:3 cm ) { $B$ };
\ node ( C ) at (2*60:3 cm ) { $C$ }; D A
\ node ( D ) at (3*60:3 cm ) { $D$ };
\ node ( E ) at (4*60:3 cm ) { $E$ };
\ node ( F ) at (5*60:3 cm ) { $F$ }; E F
\ draw [ ultra thin , - > >] ( A ) -- ( B ) ;
\ draw [ very thick , < - >] ( B ) -- ( C ) ;
\ draw [ dotted , - >] ( C ) -- ( D ) ;
\ draw [ thick , densely dashed ] ( D ) -- ( E ) ;
\ draw
[ dash pattern = on 1 pt off 2 pt on 3 pt off 2 pt ]
( E ) -- ( F ) -- ( B ) ;
\ draw [ draw = red , double , bend right ] ( A ) -- ( D ) ;
\ draw [ line width =.05 in ] ( F ) -- ( C ) ;
\ end { tikzpicture }
TIKZ EXAMPLES 3
Macros
make sure to include \ usepackage { ifthen } for this ...
% input : x - coord , y - coord , name , radius C B
\ newcommand \ customnode [4]{
\ ifthenelse {\ equal {#4}{1}}
{ \ node [ draw , circle , inner sep =1 pt ]
(#3) at (#1 ,#2) { $ #3 $ }; }{}
\ ifthenelse {\ equal {#4}{2}}
{ \ node [ draw , circle , inner sep =3 pt ]
(#3) at (#1 ,#2) { $ #3 $ }; }{}
\ ifthenelse {\ equal {#4}{3}} AA
{ \ node [ draw , circle , inner sep =8 pt ]
(#3) at (#1 ,#2) { $ #3 $ }; }{}
\ ifthenelse {\ equal {#4}{4}}
{ \ node [ draw , circle , inner sep =10 pt ]
(#3) at (#1 ,#2) { $ #3 $ }; }{}
\ ifthenelse {\ equal {#4}{5}}
{ \ node [ draw , circle , inner sep =16 pt ]
(#3) at (#1 ,#2) { $ #3 $ }; }{}
}
% input : first node , second node thickness
\ newcommand \ customedge [3]{
\ draw [ line width = #3*0.01 in ]
(#1) -- (#2) ;
}
\ begin { tikzpicture }[ scale =.6]
\ customnode {0}{0}{ AA }{5}
\ customnode {5}{5}{ B }{3}
\ customnode {0}{5}{ C }{1}
\ customedge { AA }{ B }{1}
\ customedge { B }{ C }{10}
\ end { tikzpicture }
Making a Tree:
\ begin { tikzpicture } A
\ tikzstyle { edge from parent }=[ draw = none ]
\ tikzstyle { every node }=[ rectangle , draw ]
\ node ( A ) { $A$ }
child { node ( B ) { $B$ } B D E G
child { node ( C ) { $C$ }
child { node ( C1 ) { $C_1$ } }
child { node ( C2 ) { $C_2$ } }
} C
}
child { node ( D ) { $D$ } }
child { node ( E ) { $E$ } }
child { node [ draw = none ] ( none ) {} }
C1 C2
child { node ( G ) { $G$ } };
\ draw ( A ) -- ( B ) ;
\ draw ( B ) -- ( C ) ;
\ draw ( C ) -- ( C1 ) ;
\ draw ( C ) -- ( C2 ) ;
\ draw ( D ) -- ( C ) ;
\ draw ( A ) -- ( E ) ;
\ draw ( A ) -- ( G ) ;
\ end { tikzpicture }
TIKZ EXAMPLES 4
Function Plot:
\ begin { tikzpicture }[ yscale =2] 1.5
% viewing window
\ newcommand \ minx { -3}
\ newcommand \ maxx {3}
1 •f
\ newcommand \ miny { -.9}
\ newcommand \ maxy {1.5} 0.5
\ draw (\ minx ,\ miny ) rectangle (\ maxx ,\ maxy ) ;
% zero axes
\ draw [ dotted ] (\ minx ,0) -- (\ maxx ,0) ; 0
\ draw [ dotted ] (0 ,\ miny ) -- (0 ,\ maxy ) ;
% x ticks •◦ f 0
−.5
\ foreach \ x /\ xtext in { -3 ,0 ,0.785/{\ pi /4} ,2}
\ draw [ shift ={(\ x ,\ miny ) }]
(0 pt ,0 pt ) -- (0 pt , -2 pt )
−3 0 π/4 2
node [ below ] { $ \ xtext$ };
% y ticks
\ foreach \ y /\ ytext in { -.5 ,0 ,... ,1.5}
\ draw [ shift ={(\ minx ,\ y ) }]
(0 pt ,0 pt ) -- ( -2 pt ,0 pt )
node [ left ] { $ \ ytext$ };
% clip to window
\ begin { scope }
\ clip (\ minx ,\ miny ) rectangle (\ maxx ,\ maxy ) ;
% function f
\ draw [ color = blue , smooth , domain =\ minx :2]
plot function { sin ( x ) }
node [ right ] { $f$ };
\ node [ blue ] at (2 ,{ sin (2 r ) }) { $ \ bullet$ };
% function f ’
\ draw [ color = red , smooth , domain =\ minx :2]
plot function { cos ( x ) }
node [ right ] { $f ’ $ };
\ node [ white ] at (2 ,{ cos (2 r ) }) { $ \ bullet$ };
\ node [ red ] at (2 ,{ cos (2 r ) }) { $ \ circ$ };
\ end { scope }
\ end { tikzpicture }