TikZ Reference
Fall 2018. All drawings at scale 1. All code is included with TikZ package, no other packages required.
Line Thickness
\ draw (0 ,0) - -(2 ,0); Default line
\ draw [ ultra thin ] (0 ,0) - -(2 ,0); 0.1 pt line width
\ draw [ very thin ] (0 ,0) - -(2 ,0); 0.2 pt line width
\ draw [ thin ] (0 ,0) - -(2 ,0); 0.4 pt line width
\ draw [ semithick ] (0 ,0) - -(2 ,0); 0.6 pt line width
\ draw [ thick ] (0 ,0) - -(2 ,0); 0.8 pt line width
\ draw [ very thick ] (0 ,0) - -(2 ,0); 1.2 pt line width
\ draw [ ultra thick ] (0 ,0) - -(2 ,0); 1.6 pt line width
\ draw [ line width =3 pt ] (0 ,0) - -(2 ,0); Custom line width
Arrows
\ draw [ - >] (0 ,0) - -(2 ,0);
\ draw [ - > >] (0 ,0) - -(2 ,0);
\ draw [ < - >] (0 ,0) - -(2 ,0);
\ draw [| - >] (0 ,0) - -(2 ,0);
\ draw [ < - >] (0 ,0) - -(1 ,0.3) - -(2 ,0);
\ draw [ - latex ] (0 ,0) - -(2 ,0);
\ draw [ - stealth ] (0 ,0) - -(2 ,0);
Path Shapes
\ draw [ thick ] (0 ,0) - -(2 ,0) - -(2 ,0.5) - - cycle ; Closed path, can be filled
\ draw [ thick ] (0 ,0) rectangle (2 ,1); (Lower left) and (upper
right)
\ draw [ thick ] (0 ,0) circle (0.5); (center) and (radius)
\ draw [ thick ] (0 ,0) ellipse (1 and 0.5); (center) and (width height)
\ draw [ thick ] (0 ,0) arc (0:120:3); (start x, start y) arc
(start:stop:rad)
\ draw [ - > , thick ] (0 ,0) to [ bend right =30] (2 ,0); Simple curve between nodes,
right or left, specify angle
\ draw (0 ,0).. controls (0.5 ,1) and (1.5 , -1)..(2 ,0); Bezier
Line Caps
\ draw [ line width =3 pt , line cap = butt ] (0 ,0) - -(2 ,0);
\ draw [ line width =3 pt , line cap = rect ] (0 ,0) - -(2 ,0);
\ draw [ line width =3 pt , line cap = round ] (0 ,0) - -(2 ,0);
Line Joins
\ draw [ line width =6 pt , line join = miter ] (0 ,0) - -(2 ,0) - -(2 ,1) - - cycle ;
\ draw [ line width =6 pt , line join = bevel ] (0 ,0) - -(2 ,0) - -(2 ,1) - - cycle ;
\ draw [ line width =6 pt , line join = round ] (0 ,0) - -(2 ,0) - -(2 ,1) - - cycle ;
Colors, Shading, Opacity
Predefined colors available: red, green, blue, cyan, magenta, yellow, black, gray, darkgray, lightgray, brown, lime,
olive, orange, pink, purple, teal, violet, white
Define custom colors in preamble; rgb, RGB, HEX, CMYK
\ definecolor { mugreen }{ RGB }{15 ,89 ,78}
\ definecolor { mublue }{ RGB }{9 ,60 ,113}
\ definecolor { customOrange }{ rgb }{1 ,0.5 ,0}
\ definecolor { customOrange }{ HTML }{ FF 7 F 00}
\ definecolor { customOrange }{ cmyk }{0 ,0.5 ,1 ,0}
\ draw [ line width =3 pt , teal ] (0 ,0) - -(2 ,0);
\ draw [ line width =3 pt , teal !40! magenta ] (0 ,0) - -(2 ,0);
\ draw [ line width =3 pt , mublue ] (0 ,0) - -(2 ,0);
\ draw [ thick , orange , fill = cyan ] (0 ,0) rectangle (2 ,0.5);
\ draw [ thick ] (0 ,0) - -(2 ,0.5);
\ draw [ thick , black , fill = magenta , opacity =0.6] (0 ,0) rectangle (2 ,0.5);
\ draw [ thick ] (0 ,0) - -(2 ,0.5);
\ draw [ thick , black , fill = magenta , fill opacity =0.6] (0 ,0) rectangle (2 ,0.5);
\ shade [ left color = blue , right color = green ] (0 ,0) rectangle (2 ,0.5);
\ shade [ draw = black , top color = blue , bottom color = yellow ] (0 ,0) rectangle (2 ,0.5);
\ shade [ inner color = blue , outer color = red ] (0 ,0) rectangle (2 ,0.5);
\ draw [ magenta , fill = magenta ] (0 ,0) rectangle (2 ,0.5);
\ draw [ fill = black , very nearly transparent ] (0.25 ,0) rectangle (2 ,0.25);
\ draw [ magenta , fill = magenta ] (0 ,0) rectangle (2 ,0.5);
\ draw [ fill = black , nearly transparent ] (0.25 ,0) rectangle (2 ,0.25);
\ draw [ magenta , fill = magenta ] (0 ,0) rectangle (2 ,0.5);
\ draw [ fill = black , semitransparent ] (0.25 ,0) rectangle (2 ,0.25);
\ draw [ magenta , fill = magenta ] (0 ,0) rectangle (2 ,0.5);
\ draw [ fill = black , nearly opaque ] (0.25 ,0) rectangle (2 ,0.25);
Line Patterns
\ draw [ very thick , dotted ] (0 ,0) - -(2 ,0);
\ draw [ very thick , densely dotted ] (0 ,0) - -(2 ,0);
\ draw [ very thick , loosely dotted ] (0 ,0) - -(2 ,0);
\ draw [ very thick , dashed ] (0 ,0) - -(2 ,0);
\ draw [ very thick , densely dashed ] (0 ,0) - -(2 ,0);
\ draw [ very thick , loosely dashed ] (0 ,0) - -(2 ,0);
\ draw [ very thick , dashdotted ] (0 ,0) - -(2 ,0);
\ draw [ very thick , densely dashdotted ] (0 ,0) - -(2 ,0);
\ draw [ very thick , loosely dashdotted ] (0 ,0) - -(2 ,0);
\ draw [ very thick , dashdotdotted ] (0 ,0) - -(2 ,0);
\ draw [ very thick , densely dashdotdotted ] (0 ,0) - -(2 ,0);
\ draw [ very thick , loosely dashdotdotted ] (0 ,0) - -(2 ,0);
\ draw [ very thick , dash pattern = on 3 off 2] (0 ,0) - -(2 ,0);
\ draw [ very thick , double ] (0 ,0) - -(2 ,0);
\ draw [ very thick , double distance =2 pt ] (0 ,0) - -(2 ,0);
\ draw [ blue , fill = blue ] (0 ,0) rectangle (2 ,0.5);
\ draw [ very thick , draw = white , double = green ] (0 ,0.25) - -(2 ,0.25);
Grids and Plotting
\ draw ( -2 , -2) grid (2 ,2);
\ draw [ very thin , dotted , step =0.25] ( -2 , -2) grid (2 ,2);
\ draw [ very thin , step =1] ( -2 , -2) grid (2 ,2);
\ draw [ thick , stealth - stealth ] ( -2 ,0) - -(2 ,0);
\ draw [ thick , stealth - stealth ] (0 , -2) - -(0 ,2);
\ draw [ very thin , dotted ] ( -6 , -2) grid (6 ,4);
\ draw [ thick , stealth - stealth ] ( -6.3 ,0) - -(6.3 ,0);
\ draw [ thick , stealth - stealth ] (0 , -2.3) - -(0 ,4.3);
% simple parabola
\ draw [ domain = -2:2 , smooth , variable =\ x , ultra thick , orange ] plot ({\ x } ,{\ x *\ x });
% rational function - avoid asymptotes , usually need extra smoothing ( increase samples )
\ draw [ domain = -6: -3.5 , smooth , variable =\ x , dashed , thick , mugreen ] plot ({\ x } ,{1/(\ x +3)});
\ draw [ domain = -2.75:6 , smooth , variable =\ x , dashed , thick , mugreen , samples =300] plot ({\ x } ,{1/(\ x +3)
% trig , expects degrees , so convert from radians
\ draw [ domain = -6:6 , smooth , variable =\ x , ultra thick , blue ] plot ({\ x } ,{ sin ( deg (\ x ))});
Nodes
\ node ( A ) at (0 ,0) {}; Position node called A at the point (0,0), no text
\ node ( B ) at (2 ,0) {}; Position node called B at the point (2,0), no text
\ node ( C ) at (45:2) {}; Polar positioning, (angle:radius from origin)
\ draw ( A ) -- ( B ); Draw path from node called A to node called B
\ node at (2 ,0) { Hello }; Text node at point (2,0), no reference name
\ node [ circle , draw ] at (0 ,0) { Hello }; Hello
\ node [ circle , ultra thick , red , draw = blue ,
fill = yellow , inner sep =3 pt ] at (0 ,0) { Hello }; Hello
\ node [ circle , fill , inner sep =2 pt ] at (0 ,0) {};
\ node [ above right ] at (0 ,0) {$ P = (0 ,0)$}; P = (0, 0)
\ node [ circle , fill = blue , inner sep =2 pt ,
label = right :{$ Q = (0 ,0)$}] at (0 ,0) {}; Q = (0, 0)
\ node [ draw , rectangle , rounded corners ,
text width =6 cm ] at (0 ,0) { To To create a text node with multiple
create a text node with multiple lines of lines of text you need to set the text
text you need to set the text width . The width. The text will then wrap at this
text will then wrap at this width .}; width.
\ node [ draw , rectangle , rounded corners ,
text width =6 cm , align = center ] at (0 ,0) Text can be aligned by left, flush
{ Text can be aligned as {\ tt left } , left, right, flush right, center,
{\ tt flush left } , {\ tt right } , {\ tt flush flush center, justify, and none
right } , {\ tt center } , {\ tt flush center } ,
{\ tt justify } , and {\ tt none }};
Foreach
\ foreach \ i in {0 ,1 ,2 ,3} {
\ draw (\ i , 0) circle (0.2);
}
3
4 2
% label nodes for reference later ; count starts at 1
\ foreach \ angle [ count =\ n ] in {0 ,45 ,... ,315} { 5 1
\ node [ circle , draw = red , inner sep =2 pt ] at (\ angle :1.2) {\ n };
}
6 8
7
% loop within a path :
\ draw (0:1) \ foreach \ x in {45 ,90 ,... ,359} {
-- (\ x :1)
} -- cycle (90:1);
9.0
% use pgfmath to calculate values for colors , text , etc
16.0 4.0
\ foreach \ angle [ count =\ n ] in {0 ,45 ,... ,315} {
\ pgfmathsetmacro \ shade {\ n *12}
\ pgfmathsetmacro \ lab {\ n *\ n } 25.0 1.0
\ node [ circle , white , draw = black , fill = blue !\ shade ! orange ,
inner sep =2 pt ] (\ n ) at (\ angle :1.5) {\ lab };
\ draw (0 ,0) -- (\ n ); 36.0 64.0
} 49.0
\ foreach \ angle [ count =\ n ] in {0 ,5 ,... ,360} {
\ pgfmathsetmacro \ shade {\ n *1.2}
\ pgfmathsetmacro \ r {10*\ n /360}
\ draw [ black !\ shade ! yellow ] (0 ,0) -- (\ angle :\ r );
}
6
% TikZ bar chart , loop over values to be charted
\ foreach \ b [ count =\ n ] in {3 ,6 , -1 ,2 ,4} { 4
\ draw [ thick , black , fill = blue !20! white ] 3
(\ n -1 ,0) rectangle (\ n ,0.4*\ b ); 2
\ node [ above ] at (\ n -0.5 ,0.4*\ b ){\ b };
}
-1
% if else :
\ foreach \ r in {0 ,1 ,2} {
\ foreach \ c in {0 ,1 ,... ,4} {
\ pgfmathsetmacro \ n {\ r +\ c }
\ ifodd \ n
\ draw [ thick , fill = black ] (\ c -1 ,\ r -1) rectangle (\ c ,\ r );
\ else
\ draw [ thick , fill = white ] (\ c -1 ,\ r -1) rectangle (\ c ,\ r );
\ fi
}
}
Foreach, Continued
6
% define multiple variables for each loop
\ foreach \ b /\ c [ count =\ n ] in {3/ green ,6/ blue , -1/ red ,4/ teal } { 4
\ draw [ thick , black , fill =\ c ] 3
(\ n -1 ,0) rectangle (\ n ,0.4*\ b );
\ node [ above ] at (\ n -0.5 ,0.4*\ b ){\ b };
}
-1
π
2
\ foreach \ ang /\ label in {0/$0$ ,45/$\ frac {\ pi }{4}$ ,90/$\ frac {\ pi }{2}$} { π
\ draw [ dotted ] (0 ,0) -- (\ ang :2); 4
90◦
\ node at (\ ang :2.2) {{\ large \ label }};
45◦
\ node [ blue , fill = white ] at (\ ang :1.3) {\ ang $^\ circ $};
}
0◦ 0
Rotate
% rotate by any angle around the origin
\ draw [ fill = blue ] (0 ,0) rectangle (2 ,0.2);
\ draw [ fill = red ] (0 ,0.4) rectangle (2 ,0.6);
\ draw [ fill = gray , rotate =45] (0 ,0.2) rectangle (2 ,0.4);
% rotate by any angle around any point
\ draw [ fill = blue ] (0 ,0) rectangle (2 ,0.2);
\ draw [ fill = red ] (0 ,0.4) rectangle (2 ,0.6);
\ draw [ fill = gray , rotate around ={45:(1 ,0.5)}] (0 ,0.2) rectangle (2 ,0.4);
% rotate in loop
\ foreach \ a in {0 ,30 ,60 ,... ,330} {
\ draw [ fill = orange , rotate =\ a ] (0 ,0) to [ bend left =20] (2 ,0)
- -(1.2 ,0.7) - - cycle ;
}
Shift and Scale
% shift always requires units , even cm :
\ draw [ fill = blue ] (0 ,0) rectangle (2 ,0.25);
\ draw [ fill = red , xshift =1 cm , yshift =0.25 cm ] (0 ,0) rectangle (2 ,0.25);
\ foreach \ i in {2 ,1.8 ,1.6 ,... ,0.2} {
\ draw [ fill = blue !\ i ! white , xshift =\ i cm ] (0 ,0) circle (\ i );
}
% shift within loop
\ foreach \ r [ count =\ row ] in {0 ,0.25 ,... ,2} {
\ foreach \ c in {0 ,1 ,2} {
\ pgfmathsetmacro {\ shiftval }{ Mod (\ row ,2)}
\ draw [ thick , fill = red , xshift =\ shiftval *0.5 cm ]
(\ c ,\ r ) rectangle (\ c +1 ,\ r +0.25);
}
}
% shift within scope , will apply to anything defined in scope
\ draw [ fill = blue ] (0 ,0) rectangle (2 ,0.2);
\ draw [ fill = red ] (0 ,0.4) rectangle (2 ,0.6);
\ begin { scope }[ xshift =1 cm ]
\ draw [ fill = gray ] (0 ,0.2) rectangle (2 ,0.4);
\ draw [ fill = orange ] (0 ,0.6) rectangle (2 ,0.8);
\ end { scope }
% shift and scale within scope
\ draw [ fill = blue ] (0 ,0) rectangle (2 ,0.2);
\ draw [ fill = red ] (0 ,0.4) rectangle (2 ,0.6);
\ begin { scope }[ yshift = -0.5 cm , xscale =2]
\ draw [ fill = gray ] (0 ,0.2) rectangle (2 ,0.4);
\ draw [ fill = orange ] (0 ,0.6) rectangle (2 ,0.8);
\ end { scope }
Clip
% only show what ’ s inside clip region , applies
% to anything defined AFTER clip statement only
\ clip (0 ,0) rectangle (2 ,2);
\ draw [ fill = red ] (0 ,0) rectangle (4 ,4);
\ draw [ ultra thick , blue , fill = yellow ] (0 ,0) circle (2);
% clip can be any shape or path
\ clip (0:1) \ foreach \ x in {72 ,144 ,... ,359} {
-- (\ x :1)
} -- cycle (90:1);
\ foreach \ i [ count =\ n ] in { -1.5 , -1.25 ,... ,1.75} {
\ ifodd \ n
\ draw [ thick , fill = blue ] (\ i -1 , -3) rectangle (\ i ,3);
\ else
\ draw [ thick , fill = red ] (\ i -1 , -3) rectangle (\ i ,3);
\ fi
}
% clip inside scope if only certain parts are to be clipped
\ draw [ fill = yellow ] (0 ,0) circle (1);
\ begin { scope }
\ clip ( -1 ,0) circle (1);
\ draw [ fill = green ] (0 ,0) circle (1);
\ end { scope }