An Introduction To TikZ
An Introduction To TikZ
An Introduction To TikZ
1. Define document class - article or standalone (the class for individual exam-
ples). (Use \documentclass{article} or \documentclass{standalone}).
2. Introduce the packages such as graphicx, caption and subcaption. This can be
done by writing the command \usepackage{graphicx,caption,subcaption}
in the preamble.
1
9. patterns- defines patterns for filling areas.
10. plothandlers, plotmarks - to draw nodes and curves in different styles.
11. shapes - uses to define shapes (other than rectangle, circle and co-ordinate).
12. snake - to create curved lines.
13. topaths - to draw paths between nodes.
14. trees- draws tree diagrams.
15. plothandlers, plotmarks - to draw nodes and curves
16. positioning - to adjust positions of nodes and their coordinates and to create
gridlines as in the graph sheets.
17. decorations.markings - for advanced customisations of figures and shapes
\usetikzlibrary{
intersections, arrows.meta,
automata,er,calc,
backgrounds,
mindmap,folding,
patterns,
decorations.markings,
fit,snakes,
shapes,matrix,
positioning,
shapes.geometric,
arrows,through
}
\begin{tikzpicture}
Write your Tikz Code here...!!!
\end{tikzpicture}
\begin{figure}[placement]
\centering
\begin{tikzpicture}
Write your Tikz Code here...!!!
\end{tikzpicture}
\end{figure}
2
\begin{figure}[placement]
\centering
\begin{subfigure}[b]{0.45\textwidth}
\begin{tikzpicture}
Write your Tikz Code for the first figure here...!!!
\end{tikzpicture}
\caption{Subcaption for the first subfigure}
\end{subfigure}
\quad
\begin{subfigure}[b]{0.45\textwidth}
\begin{tikzpicture}
Write your Tikz Code for the second figure here...!!!
\end{tikzpicture}
\caption{Subcaption for the second subfigure}
\end{subfigure}
\caption{Your figure caption here}
\end{figure}
\begin{tikzpicture}
\draw (0,0) --(5,0);
\end{tikzpicture}
\begin{tikzpicture}
\draw (0,0) --(5,0) -- (2.5,2.5);
\end{tikzpicture}
3. The code
\begin{tikzpicture}
\draw (0,0) --(5,0) -- (2.5,2.5) -- (0,0);
\end{tikzpicture}
3
gives us following output:
4
5. The code
\begin{tikzpicture}[yscale=1.5]
\draw [->] (0,0) --(5,0);
\draw [<-] (5,1) -- (2.5,2.5);
\end{tikzpicture}
6. The code
\begin{tikzpicture}
\draw [<->] (0,2) -- (0,0) -- (3,0);
\end{tikzpicture}
\begin{tikzpicture}
\draw [ultra thick] (0,1) -- (2,1);
\draw [thick] (0,0.5) -- (2,0.5);
\draw [thin] (0,0) -- (2,0);
\end{tikzpicture}
5
8. The option “help lines” is made specially to be fine gray lines for showing
special points as shown below:
\begin{tikzpicture}
\draw [<->,thick] (0,2) -- (0,0) -- (4,0);
\draw [dashed] (0,1.5) -- (3,0);
\draw [dotted] (0,0) -- (2,2);
\draw [help lines] (1,0) -- (1,1) -- (0,1);
\end{tikzpicture}
\begin{tikzpicture}
\draw [line width=12] (0,0) -- (2,0);
\draw [line width=0.1cm] (0,1) -- (2,1);
\end{tikzpicture}
\begin{tikzpicture}
\draw [yellow] (0,2.5) -- (4,2.5);
\draw [cyan] (0, 2) -- (4,2);
\draw [purple] (0,1.5) -- (4,1.5);
\draw [green] (0,1) -- (4,1);
\draw [red] (0, 0.5) -- (4,0.5);
\draw [blue] (0,0) -- (4,0);
\end{tikzpicture}
6
Note: We have the following default colors: red , green , blue , cyan
, magenta , yellow , black , gray , darkgray , lightgray
, brown , lime , olive , orange , pink , purple , teal
, violet and white .
In addition to these one can define new colors according to their preferences
using the command \definecolor{myblue}{rgb}{24,40,120}
Here, the colour strips are created using the command
\begin{tikzpicture}
\draw [red, line width=6](0,0) -- (.5,0);
\end{tikzpicture}
3 Drawing Curves
1. The code
\begin{tikzpicture}
\draw [blue] (0,0) rectangle (3,2);
\draw [red, ultra thick] (6,1) circle [radius=1];;
\draw [violet] (10,1) arc [radius=2,start angle=45,end angle= 120];
\end{tikzpicture}
\begin{tikzpicture}
\draw [<->, rounded corners, thick, purple] (0,2) -- (0,0) -- (3,0);
\end{tikzpicture}
7
3. We can do curves without plotting all the points. See an easy one:
\begin{tikzpicture}
\draw[very thick] (0,0) to [out=90,in=195] (2,1.5);
\end{tikzpicture}
which yields
4. The code
\begin{tikzpicture}
\draw [<->,thick, cyan] (0,0) to [out=90,in=180] (1,1)
to [out=0,in=180] (2,0) to [out=0,in=180] (3,1) to [out=0,in=-180] (4,1) ;
\end{tikzpicture}
5. Tikz also has a math engine which enables you to plot functions:
\begin{tikzpicture}[xscale=2,yscale=2]
\draw [<->] (0,2) -- (0,0) -- (2,0);
\draw[green, ultra thick, domain=0:1] plot (\x, {\x*\x*\x+\x-0.025});
\end{tikzpicture}
gives you
8
factorial(\x),
sqrt(\x),
pow(\x,y),
exp(\x),
ln(\x),
log10(\x),
log2(\x),
abs(\x),
mod(\x,y) - (x modulo y),
round(\x) - (rounds x to the nearest integer),
floor(\x),
ceil(\x),
sin(\x) - (sinx in degrees),
sin(\x r) - (sinx in radians),
cos(\x),
cos(\x,r),
tan(\x),
tan(\x,r),
min(\x,y,),
max(\x,y) etc.
6. The code
\begin{tikzpicture}
\draw [help lines, ->] (0,0) -- (8,0);
\draw [help lines, ->] (0,-1.1) -- (0,1.1);
\draw [green,domain=0:2*pi] plot (\x, {2*(sin(\x r)* ln(\x+1))/2});
\draw [red,domain=0:pi] plot (\x, {2*sin(\x r)});
\draw [blue, domain=pi:2*pi] plot (\x, {2*cos(\x r)*exp(\x/exp(2*pi))});
\end{tikzpicture}
4 Filling up Shapes
We can fill figures and shapes with colours. See the following illustrations.
The code
9
\begin{tikzpicture}
\draw [fill=red,ultra thick] (0,0) rectangle (1,1);
\draw [fill=red,ultra thick,red] (2,0) rectangle (3,1);
\draw [blue, fill=blue] (4,0) -- (5,1) -- (4.75,0.15) -- (4,0);
\draw [fill] (7,0.5) circle [radius=0.1];
\draw [fill=orange] (9,0) rectangle (11,1);
\draw [fill=white] (9.25,0.25) rectangle (10,1.5);
\end{tikzpicture}
5 Putting Labels
In almost all figures we have to assign labels to the parts of the figure (marking angles,
lengths etc.). (Here we have to use the library positioning for better placement of
labels). To write labels at certain positions, we need to create nodes using \node
command.
For example, the code
\begin{tikzpicture}
\draw [thick, <->] (0,2) -- (0,0) -- (2,0);
\node at (1,1) {yes};
\end{tikzpicture}
yes
Note that the node is invisible until we define a shape for the node. We shall define
shapes for nodes using \node[shape] command. For this we need to define the char-
acters of the curves in the shape concerned using the \tikzstyle{text} = {definition}
environment. We may have to include the libraries shapes,snakes,matrix etc. accord-
ing to the requirements.
For example, the code
\begin{tikzpicture}
\draw [thick, <->] (0,2) -- (0,0) -- (2,0);
\node[draw,circle] at (1,1) {yes};
\end{tikzpicture}
10
gives us
yes
The code
\begin{tikzpicture}
\draw [thick, <->] (0,2) -- (0,0) -- (2,0);
\node[draw,circle] at (1,1) [label={yes}] {};
\end{tikzpicture}
gives us
yes
The code
\begin{tikzpicture}
\draw [thick, <->] (0,2) -- (0,0) -- (2,0);
\node[draw,circle,fill] at (1,1) {yes};
\end{tikzpicture}
gives us
yes
The code
\begin{tikzpicture}[xscale=3, yscale=3]
\draw [thick, <->] (0,1) -- (0,0) -- (1,0);
\node [below right] at (1,0) {$x$};
\node [left] at (0,1) {$y$};
\draw[fill] (.4,.6) circle [radius=.5pt];
\node[above right] at (.4,.6) {$A$};
\end{tikzpicture}
gives the output
y
x
The code
11
\begin{tikzpicture}
\draw [thick, <->] (0,2) -- (0,0) -- (2,0);
\node[draw,circle,fill=blue] at (1,1) {yes};
\end{tikzpicture}
gives us
yes
The code
\begin{tikzpicture}
\draw [thick, <->] (0,2) -- (0,0) -- (2,0);
\node[draw,rectangle,fill=green] at (1,1) {yes};
\end{tikzpicture}
gives us
yes
The code
\begin{tikzpicture}
\draw [thick, <->] (0,2) -- (0,0) -- (2,0);
\node[draw,diamond,fill=red!50] at (1,1) {yes};
\end{tikzpicture}
gives us
yes
\begin{tikzpicture}[scale=2]
\tikzstyle{ann} = [draw=none,fill=none,right]
\matrix[nodes={draw, ultra thick, fill=blue!20},
row sep=0.3cm,column sep=0.5cm] {
\node[draw=none,fill=none] {Plain node}; &
\node[rectangle] {Rectangle}; &
\node[circle] {Circle};\\
12
\node[ellipse] {Ellipse};&
\node[circle split] {Circle \nodepart{lower} split};&
\node[forbidden sign,text width=4em, text centered]
{Forbidden sign};\\
\node[diamond] {Diamond};&
\node[cross out] {Cross out};&
\node[strike out] {Strike out};\\
\node[regular polygon,regular polygon sides=5] {$n=5$};&
\node[regular polygon,regular polygon sides=7] {$n=7$};&
\node[regular polygon,regular polygon sides=9] {$n=9$};&
\node[ann]{Regular polygon};\\
\node[star,star points=4] {$p=4$};&
\node[star,star points=7,star point ratio=0.8] {$p=7$};&
\node[star,star points=10] {$p=9$};&
\node[ann]{Star};\\
};
\end{tikzpicture}
Circle Forbidden
Ellipse
split sign
\begin{tikzpicture}
\draw (0,0) -- (4,0) -- (4,4) -- (0,4) -- cycle;
\end{tikzpicture}
gives us
2. The code
\begin{tikzpicture}
\draw (0,0) parabola (3,3);
\end{tikzpicture}
gives us
3. To draw a curved line we use control points. We begin with our starting co-
ordinate, then use two dots followed by the keyword controls and then the
co-ordinates of our control points separated by an and.
For example, consider the command \draw (0,0) .. controls (0,4) and (4,0) .. (4,4);
and the output will be as follows:
\begin{tikzpicture}
\draw (0,0) .. controls (0,4) and (4,0) .. (4,4);
\end{tikzpicture}
4. The code
\begin{tikzpicture}
\draw (0,0) .. controls (0,4) and (4,0) .. (4,4) .. controls (5,5)
and (6,2) .. (6,6);
\end{tikzpicture}
5. By the code
\begin{tikzpicture}
\draw (2,2) circle (2cm);
\draw (9,2) ellipse (2cm and 1.5cm);
\draw (15,1.5) arc (0:75:3cm);
\end{tikzpicture}
6. The code \draw[step=1cm,gray,very thin] (-3,-3) grid (8,8); wil give
you the grid lines as follows:
\begin{tikzpicture}
\draw[step=1cm,gray,very thin] (-2.9,-2.9) grid (6.9,6.9);
\end{tikzpicture}
\begin{tikzpicture}
\draw[step=1cm,gray,very thin] (-2.9,-2.9) grid (6.9,6.9);
\fill[blue!40!white] (-1,-1) rectangle (4.5,4.5);
\end{tikzpicture}
\begin{tikzpicture}
\draw[step=1cm,gray,very thin] (-2.9,-2.9) grid (6.9,6.9);
\filldraw[fill=blue!40!white, draw=black] (0,0) rectangle (4,4);
\end{tikzpicture}
10. We can draw different shapes exactly as in a graph sheet and assign colours to
these shapes. For example, the code
\begin{tikzpicture}
\draw[step=1cm,gray,very thin] (-2.9,-2.9) grid (6.9,6.9);
\draw [fill=brown!50] (-1,-1) -- (0,1) -- (2,3) -- (0,4) -- cycle;
\draw [fill=green!40] (2,2) -- (4,4) -- (6,5) --(4,6) -- cycle;
\end{tikzpicture}
\begin{tikzpicture}[scale=0.75]
\draw[step=1cm,gray,very thin] (-2.9,-2.9) grid (6.9,6.9);
\shade[left color=blue,right color=red] (-1,-1) rectangle (6,6);
\end{tikzpicture}
\begin{tikzpicture}[scale=0.75]
\draw[step=1cm,gray,very thin] (-2.9,-2.9) grid (6.9,6.9);
\shade[top color=blue,bottom color=yellow] (-1,-1) rectangle (6,6);
\end{tikzpicture}
Here, we get the output
\begin{tikzpicture}[scale=0.75]
\draw[step=1cm,gray,very thin] (-2.9,-2.9) grid (6.9,6.9);
\shade[inner color=green,outer color=red] (-1,-1) rectangle (6,6);
\end{tikzpicture}
14. As explaied above, we can insert the boarders using \shadedraw command.
See the example:
\begin{tikzpicture}
\draw[step=1cm,gray,very thin] (-2.9,-2.9) grid (6.9,6.9);
\shadedraw[inner color=blue,outer color=red, draw=black] (0,0)
rectangle (4,4);
\end{tikzpicture}
15. The shade can replace \filldraw and the output will be as follows.
\begin{tikzpicture}
\shade[shading=ball, ball color=blue] (0,0) circle (2);
\shade[shading=ball, ball color=white] (5,0) circle (2);
\shade[shading=ball, ball color=black] (10,0) circle (2);
\end{tikzpicture}
7 Coordinate System
16. The coordinate axes can be drawn as follows:
\begin{tikzpicture}%[scale=0.75]
\draw[step=1cm,gray,very thin] (-2.9,-2.9) grid (6.9,6.9);
\draw[thick,->] (-2,0) -- (6.5,0);
\draw[thick,->] (0,-2) -- (0,6.5);
\end{tikzpicture}
\begin{tikzpicture}%[scale=0.75]
\draw[thick,->] (-2,0) -- (6.5,0);
\draw[thick,->] (0,-2) -- (0,6.5);
\draw[step=1cm,gray,very thin] (-2.9,-2.9) grid (6.9,6.9);
\foreach \x in {-2,-1,0,1,2,3,4,5}
\draw (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north] {$\x$};
\foreach \y in {-2,-1,0,1,2,3,4,5}
\draw (1pt,\y cm) -- (-1pt,\y cm) node[anchor=east] {$\y$};
\end{tikzpicture}
5
4
3
2
1
0
−2 −1 0 1 2 3 4 5
−1
−2
18. Graphs can also be plotted in the above grid environment as exlained below:
0
−2 −1 0 1 2 3 4 5
−1
−2
8 Flow Charts
To create the shapes, we use in flowcharts, the libraries shapes.geometric and
arrows. Just add these libaries in the preamble.
To begin with, we need to define the required shapes using
\tikzstyle{shape-name}=[attribute1,atribute2,...]:
Start
3. Now the process boxes and decision boxes can be created as follows:
and
\tikzstyle{arrow} = [thick,->,>=stealth]
\begin{tikzpicture}
\node (start) [ss] at (0,14) {Start};
\node (in1) [io] at (0,12) {Input};
\node (pro1) [proc] at (0,10) {Process 1};
\node (dec1) [dec] at (0,7) {Decision 1};
\node (pro2a) [proc] at (0,4) {Process 2a};
\node (pro2b) [proc] at (4,7) {Process 2b};
\node (out1) [io] at (0,2) {Output};
\node (stop) [ss] at (0,0) {Stop};
\end{tikzpicture}
Input
Process 1
Decision 1 Process 2b
Process 2a
Output
Stop
\begin{tikzpicture}
\node (start) [ss] at (0,14) {Start};
\node (in1) [io] at (0,12) {Input};
\node (pro1) [proc] at (0,10) {Process 1};
\node (dec1) [dec] at (0,7) {Decision 1};
\node (pro2a) [proc] at (0,4) {Process 2a};
\node (pro2b) [proc] at (4,7) {Process 2b};
\node (out1) [io] at (0,2) {Output};
\node (stop) [ss] at (0,0) {Stop};
\draw [arrow] (start) -- (in1);
\draw [arrow] (in1) -- (pro1);
\draw [arrow] (pro1) -- (dec1);
\draw [arrow] (dec1) -- (pro2a);
\draw [arrow] (dec1) -- (pro2b);
%\draw [arrow] (dec1) -- node {yes} (pro2a);
%\draw [arrow] (dec1) -- node {no} (pro2b);
\draw [arrow] (dec1) -- node[anchor=east] {yes} (pro2a);
\draw [arrow] (dec1) -- node[anchor=south] {no} (pro2b);
%\draw [arrow] (pro2b) -- (pro1);
\draw [arrow] (pro2b) |- (pro1);
\draw [arrow] (pro2a) -- (out1);
\draw [arrow] (out1) -- (stop);
\end{tikzpicture}
Start
Input
Process 1
no
Decision 1 Process 2b
yes
Process 2a
Output
Stop
9 More on Shapes
1. Some special types of arrows can be created using the following commands:
2. We can also draw figures using the polar coordinates also. For example, consider
the command:
\begin{tikzpicture}
\draw[color=red] (0,0) -- (40:1);
\draw[color=blue] (0,0) -- (160:1);
\draw[thick] (0,0) -- (90:1);
\draw[color=green] (40:1) arc (40:160:1);
\end{tikzpicture}
4. A path is a series of straight and curved line segments. We can draw paths,
fill them colours or can use for clipping of subsequent drawings. After a \clip
command, all subsequent drawings are clipped, only the parts inside the clip-
ping region are drawn.
For example,
\begin{tikzpicture}
\path[clip, draw] (1,1)--(2,2)--(3,1)--cycle;
\path[fill=blue!50] (2, 1.7) circle (.8);
\end{tikzpicture}
\begin{tikzpicture}
\draw (-2, 1.5) rectangle (2, -1.5);
\begin{scope}
\clip (-0.5, 0) circle (1);
\clip ( 0.5, 0) circle (1);
\fill[color=gray] (-2,1.5)rectangle (2,-1.5);
\end{scope}
\draw (-0.5, 0) circle (1);
\draw ( 0.5, 0) circle (1);
\end{tikzpicture}
-5 -4 -3 -2 -1 0 1 2 3 4 5 R
\begin{tikzpicture}
\draw[->,thick] (0,0) -- (4,2) node[pos=.5,sloped,above] {$x$};
\draw[->,thick] (0,0) -- (4,-2) node[pos=.5,sloped,below] {$y$};
\end{tikzpicture}
\begin{tikzpicture}[scale=2]
\coordinate [label=left:$A$] (A) at (0,0);
\coordinate [label=right:$B$] (B) at (1.25,0.25);
\draw (A) -- (B);
\node (D) [draw,circle through=(B),label=left:$D$] at (A) {};
\node (E) [draw,circle through=(A),label=right:$E$] at (B) {};
\coordinate[label=above:$C$] (C) at (intersection 2 of D and E);
\draw [red] (A) -- (C);
\draw [red] (B) -- (C);
\end{tikzpicture}
generates the diagram (note that the library through is to be used here.)
C
B E
D A
c2 c1 c2 c1 c2 c1 c2
Note that the commands such as [right of=a], [left of=a], [above of=a]
and [below of=a] can be used to position shapes in flowcharts also.
The command node distance=1.8cm creates a distance 1.8cm between two ad-
jacent nodes.
\begin{tikzpicture}
\tikzstyle{every node}=[draw,shape=circle];
\node (v0) at (0:0) {$v_0$};
\node (v1) at ( 0:2) {$v_1$};
\node (v2) at ( 72:2) {$v_2$};
\node (v3) at (2*72:2) {$v_3$};
\node (v4) at (3*72:2) {$v_4$};
\node (v5) at (4*72:2) {$v_5$};
\draw (v0) -- (v1)
(v0) -- (v2)
(v0) -- (v3)
(v0) -- (v4)
(v0) -- (v5);
\end{tikzpicture}
Note that in the command line \node (v0) at (0:0) {$v_0$};, (v0) defines
the name of the corresponding vertex. Putting names for vertices is essential
for drawing edges between vertices. Now, at (a:b) defines the position of that
point at an angle adeg from the horizontal and radius b points.
The command \tikzstyle{every node}=[draw,shape=circle]; defines the
attributes of the vertices. Also, while drawing the edges, note that the semi-
colon is placed only after the last edge is defined.
Then, the above code will provide us the following output:
v2
v3
v0 v1
v4
v5
v2
v3
v1
v4
v5
3. Labeling can be done in a different way so that labels appear outside of the
nodes. See the example:
\begin{tikzpicture}
\tikzstyle{every node}=[draw,shape=circle];
\node (v0) at (0:0) [label=below:$v_0$] {};
\node (v1) at ( 0:3) [label=below:$v_1$] {};
\node (v2) at ( 72:3) [label=below:$v_2$] {};
\node (v3) at (2*72:3) [label=below:$v_3$] {};
\node (v4) at (3*72:3) [label=below:$v_4$] {};
\node (v5) at (4*72:3) [label=below:$v_5$] {};
\draw (v0) -- (v1)
(v0) -- (v2)
(v0) -- (v3)
(v0) -- (v4)
(v0) -- (v5);
\end{tikzpicture}
which gives the output as
v2
v3
v0 v1
v4
v5
4. Labeling can be done in a different way so that labels appear outside of the
nodes. See the example:
\begin{tikzpicture}
\tikzstyle{every node}=[draw,shape=circle];
\node (v0) at (0:0) [fill,label=below:$v_0$] {};
\node (v1) at ( 0:3) [fill,label=below:$v_1$] {};
\node (v2) at ( 72:3) [fill,label=below:$v_2$] {};
\node (v3) at (2*72:3) [fill,label=below:$v_3$] {$v_3$};
\node (v4) at (3*72:3) [fill,label=below:$v_4$] {$v_4$};
\node (v5) at (4*72:3) [fill,label=below:$v_5$] {$v_5$};
\draw (v0) -- (v1)
(v0) -- (v2)
(v0) -- (v3)
(v0) -- (v4)
(v0) -- (v5);
\end{tikzpicture}
v2
v0 v1
v4
v5
We can also customise the vertex attributes with the entire document globally us-
ing some commands like \newcommand{\vertex}{\node[vertex]} in the preamble.
Also, the tikz-style codes can be written in square brackets as shown in the following.
\begin{figure}[h!]
\centering
\begin{tikzpicture}[auto,node distance=1.8 cm,
thick,main node/.style={circle,draw,font=\sffamily\Large\bfseries}]
\vertex [fill,label=below:$c_2$] (1) {};
\vertex [label=below:$c_1$] (2) [right of=1] {};
\vertex [fill,label=below:$c_2$] (3) [right of=2] {};
\vertex [label=below:$c_1$] (4) [right of=3] {};
\vertex [fill,label=below:$c_2$] (5) [right of=4] {};
\vertex [label=below:$c_1$] (6) [right of=5] {};
\vertex [fill,label=below:$c_2$] (7) [right of=6] {};
\path[every node/.style={font=\sffamily\small}]
(1) edge node {} (2)
(2) edge node {} (3)
(3) edge node {} (4)
(4) edge node {} (5)
(5) edge node {} (6)
(6) edge node {} (7);
\end{tikzpicture}
\caption{The path $P_7$ with $\chi^+$colouring}\label{pathmaxorder}
\end{figure}
v5 c1 c1 v 1
c2 c2
v4 v2
c1
v3
Multiple edges and loops can be created using commands as shown in the following
Tikz code
\begin{tikzpicture}[auto,node distance=2.5cm,
thick,main node/.style={circle,draw,font=\sffamily\Large\bfseries}]
\vertex (v1) at (0,0) [fill,label={below:$v_1$}]{};
\vertex (v2) at (3,0) [fill,label={below:$v_2$}]{};
\vertex (v3) at (3,3) [fill,label={above:$v_3$}]{};
\vertex (v4) at (0,3) [fill,label={above:$v_4$}]{};
\path[every node/.style={font=\sffamily\small},line width=1pt]
(v1) edge node {$e_1$} (v2)
(v2) edge node {$e_2$}(v3)
(v3) edge node {$e_3$}(v4)
(v4) edge [bend left] node {$e_4$} (v1)
(v4) edge [bend right] node {$e_5$} (v1)
(v4) edge [loop left,looseness=100] node {$e_6$} (v4)
(v2) edge [loop right,looseness=100] node {$e_7$} (v2)
;
\end{tikzpicture}
e5 e4 e2
e1
e7
v1 v2
The codes loop left, loop right, loop above, loop below, loop above right
etc. can be used for correct positioning of the loop. The command losseness=a de-
cides the shape of the loop.
Graphs with dashed and/or dotted edges can be created using the commands as
given below:
\begin{tikzpicture}
\vertex (1) at (0,0) [label={left:$v_1$}] {};
\vertex (2) at (0.75,0.65) [label={above:$v_2$}] {};
\vertex (3) at (0.75,-0.65) [label={below:$v_3$}] {};
\vertex (4) at (1.5,0) [label={left:$v_4$}] {};
\vertex (5) at (2.5,0) [label={right:$v_5$}] {};
\vertex (6) at (3.25,0.65) [label={above:$v_6$}] {};
\vertex (7) at (3.25,-0.65) [label={below:$v_7$}] {};
\vertex (8) at (4,0) [label={right:$v_8$}] {};
\path[every node/.style={font=\sffamily\small},line width=1pt]
(1) edge node {} (2)
(1) edge node {} (3)
(2) edge node {} (3)
(2) edge [dashed] node {} (4)
(3) edge [dashed] node {} (4)
(2) edge [densely dotted] node {} (6)
(3) edge [densely dashed] node {} (7)
(5) edge [dashed] node {} (6)
(5) edge [loosely dotted] node {} (7)
(6) edge [loosely dashed] node {} (7)
(6) edge node {} (8)
(7) edge node {} (8)
;
\end{tikzpicture}
v1 v4 v5 v8
v3 v7
Set-labels can be assigned to the graphs by using the Tikz code given below:
\begin{tikzpicture}[auto,node distance=1.5cm,
thick,main node/.style={circle,draw,font=\sffamily\Large\bfseries}]
%
\node [circle,fill] (u1) at (0:2.25) [label={right:$\emptyset$}]{};
\node [circle,fill] (u2) at (315:2.25) [label={right:$\{x_1\}$}]{};
\node [circle,fill] (u3) at (270:2.25) [label={below:$\{x_1,x_2\}$}]{};
\node [circle,fill] (u4) at (225:2.25) [label={left:$\{x_2\}$}]{};
\node [circle,fill] (u5) at (180:2.25) [label={left:$\{x_2,x_3\}$}]{};
\node [circle,fill] (u6) at (135:2.25) [label={left:$\{x_3\}$}]{};
\node [circle,fill] (u7) at (90:2.25) [label={above:$\{x_1,x_3\}$}]{};
\node [circle,fill] (u8) at (45:2.25) [label={right:$\{x_4\}$}]{};
\node [circle,fill] (u) at (0:0) [label={above:$\{x_3,x_4\}$}]{};
\path[every node/.style={font=\sffamily\small}]
(u1) edge node {} (u2)
(u2) edge node {} (u3)
edge node {} (u)
(u3) edge node {} (u4)
(u4) edge node {} (u5)
edge node {} (u)
(u5) edge node {} (u6)
(u6) edge node {} (u7)
edge node {} (u)
(u7) edge node {} (u8)
(u8) edge node {} (u1)
edge node {} (u)
;
\end{tikzpicture}
{x3 } {x4 }
{x3 , x4 }
{x2 , x3 } ∅
{x2 } {x1 }
{x1 , x2 }
In a similar manner, we can assign labels to edges also. For example,
\begin{tikzpicture}[scale=1.5,auto,node distance=1.5cm,
thick,main node/.style={circle,draw,font=\sffamily\Large\bfseries},el/.style = {inne
every label/.append style = {font=\tiny}]
\vertex [fill] (1) at (0,0) {};
\vertex [fill] (2) at (2,0) {};
\vertex [fill] (3) at (1.5,2.5) {};
\vertex [fill] (4) at (-0.5,1.5) {};
\vertex [fill] (5) at (1,1) {};
\vertex [fill] (6) at (-1,0) {};
\path[every node/.style={font=\sffamily\small},line width=0.5pt]
(1) edge node [el,below] {$e_4$} (2)
(2) edge node [el,above] {$e_3$} (3)
(3) edge node [el,above] {$e_2$} (4)
(4) edge node [el,above] {$e_5$} (1)
(4) edge node [el,above] {$e_1$} (6)
(5) edge node [el,below] {$e_9$} (1)
(5) edge node [el,below] {$e_8$} (2)
(5) edge node [el,below] {$e_7$} (3)
(5) edge node [el,above] {$e_6$} (4)
;
\end{tikzpicture}
e7
e3
e6
e5
e1
e9 e8
e4
Edges can be given set-labels by using the commands as shown in the below Tikz
code:
\begin{tikzpicture}[scale=1.5,auto,node distance=1.5cm,
thick,main node/.style={circle,draw,font=\sffamily\Large\bfseries},el/.style = {inne
every label/.append style = {font=\tiny}]
\vertex [fill] (1) at (0,0) {};
\vertex [fill] (2) at (2,0) {};
\vertex [fill] (3) at (1.5,2.5) {};
\vertex [fill] (4) at (-0.5,1.5) {};
\vertex [fill] (5) at (1,1) {};
\vertex [fill] (6) at (-1,0) {};
\path[every node/.style={font=\sffamily\small},line width=0.5pt]
(1) edge node [el,below] {$\{4\}$} (2)
(2) edge node [el,above] {$\{1,2\}$} (3)
(3) edge node [el,above] {$\emptyset$} (4)
(4) edge node [el,above] {$\{1,2,3\}$} (1)
(4) edge node [el,above] {$e_1$} (6)
(5) edge node [el,below] {$e_9$} (1)
(5) edge node [el,below] {$e_8$} (2)
(5) edge node [el,below] {$e_7$} (3)
(5) edge node [el,above] {$e_6$} (4)
;
\end{tikzpicture}
∅
e7
{1, 2}
e6
{1, 2
e1
, 3}
e8
e9
{4}
Note that the package er is necessary to use el.
11 Directed graphs Using Tikz
When use the command arrow or the symbol ->, normally arrow marks will appear
at the end of the edges. Using certain edge decorations, we can decide the position
of the arrow mark. For example, see the following code:
\begin{tikzpicture}
\vertex [fill=red] (1) at (0,0) {};
\vertex [fill=green] (2) at (2,0) {};
\vertex [fill=blue] (3) at (1.5,2.5) {};
\vertex [fill=cyan] (4) at (-0.5,1.5) {};
\vertex [fill=purple] (5) at (1,1) {};
\vertex [fill=yellow] (6) at (-1,0) {};
\path[every node/.style={font=\sffamily\small},line width=0.5pt]
(1) edge (2)
(2) edge (3)
(3) edge (4)
(4) edge (1)
(4) edge (6)
(5) edge (1)
(5) edge (2)
(5) edge (3)
(5) edge (4)
;
\end{tikzpicture}
In a similar manner, we can assign colors to the edges also. See the following
example:
The code
\begin{tikzpicture}
\vertex (u3) at (0,0) [fill]{3};
\vertex (u4) at (2,0) [fill]{4};
\vertex (u5) at (4,0) [fill]{5};
\vertex (u6) at (5,0) [fill]{6};
\vertex (u1) at (1,1) [fill]{2};
\vertex (u2) at (3,1) [fill]{1};
\path
(u1) edge [red,line width= 2.5pt ] (u2)
(u1) edge [red,line width= 2.5pt](u3)
(u1) edge [red,line width= 2.5pt](u4)
(u1) edge (u5)
(u2) edge (u3)
(u2) edge[red,line width= 2.5pt] (u4)
(u2) edge (u5)
(u3) edge[red,line width= 2.5pt] (u4)
(u4) edge (u5)
(u5) edge (u6)
;
\end{tikzpicture}
[3] A. Mertz, W. Slough (2007). Graphics with TikZ, The PracTEXJournal, 2007(1),
22 pages.