# Getting started with TikZ 1

TikZ is a package used for programmatic creation of graphics in LaTex[ more info… ]. Points and paths are the basic elements used to draw pictures in TikZ.

## Points and Paths

Path is drawn between two coordinates in a picture environment by specifying a “path extension operation”. The simplest path operation is a solid line just obtained with “–”. Every path must end with a semicolon(“;”). For drawing the path, we can use \draw command which is an abbreviation for \path[draw]. We can discuss more about the path command in the later sections. The syntax for path is

\begin{tikzpicture} \path[draw] (x1,y1) -- (x2,y2); \end{tikzpicture}

Points are the co-ordinates in the TIkZ picture environment can be specified in any of the following ways:

• Cartesian coordinates : The syntax is ( a, b) where point “a” centimeters in the x-direction and “b” centimeters in the y-direction from origin. TikZ uses one centimeter as the default unit of measure.
• Polar coordinates : The syntax is (α : r) where an angle, “α” in degrees, and distance from the origin, “r” in centimeters
• Named points : It is sometimes convenient to refer to a point by name, especially when this point occurs in multiple \draw commands. The syntax is:
\path (a,b) coordinate (P); assigns name “P” to the Cartesian coordinate ( a, b). \path (α:r) coordinate (Q); assigns name “Q” to polar coordinate (α:r)
• Relative points : A relative point may be defined by providing offsets in the respective units. \path (P) ++(∆x,∆y) coordinate (Q); defines a new relative point “Q” in Cartesian coordinates to point “P” at ∆x and ∆y offset. \path (P) ++(α:rim) coordinate (Q); defines a new relative point “Q” in polar coordinates to point “P” at a angle α and radius r.

## Quick start

• Add the Tikz package to preamble and create a graphic object using TikZ environment.
1 2 3 4 5 6 \usepackage{tikz} \begin{tikzpicture} . . . \end{tikzpicture}
• The first TikZ figure I prefer in this get started is background grid. It is the most helpful item in the process of developing and debugging TikZ based graphics. In the following code snippet Line-6 generates grid between (-1,-1) and (1,1) coordinates and the grid lines are spaced 0.5cm apart.
 1 2 3 4 5 6 7 8 \usepackage{tikz} \begin{tikzpicture} %--------start graphics code -------- %\draw[step=spacing,thickness-of-gridline,color-of-gridline] % (x1,y1) grid (x2,y2); \draw[step=0.5,very thin,black!20] (-1,-1) grid (1,1); %--------end graphics code ---------- \end{tikzpicture} • Draw a horizontal line of 1cm starting at (0,0), red colored and 2pt thickness
 1 2 3 4 5 6 7 \usepackage{tikz} \begin{tikzpicture} %--------start graphics code -------- \draw[step=0.5,very thin,black!20] (-1,-1) grid (1,1); \draw[line width=2pt,color=red] (0,0) -- (1,0); %--------end graphics code ---------- \end{tikzpicture} • To draw both horizontal and vertical line
 1 2 3 4 5 6 7 8 \usepackage{tikz} \begin{tikzpicture} %--------start graphics code -------- \draw[step=0.5,very thin,black!20] (-1,-1) grid (1,1); \draw[line width=2pt,color=red] (1,0) -- (0,0); \draw[line width=2pt,color=red] (0,0) -- (0,1); %--------end graphics code ---------- \end{tikzpicture} • To draw a triangle
 1 2 3 4 5 6 7 8 9 10 11 12 \usepackage{tikz} \begin{tikzpicture} %--------start graphics code -------- %\draw[line width=2pt,color=red] (1,0) -- (0,0); %\draw[line width=2pt,color=red] (0,0) -- (0,1); %\draw[line width=2pt,color=red] (0,1) -- (1,0); % more precise way \draw[step=0.5,very thin,black!20] (-1,-1) grid (1,1); \draw[line width=2pt,color=red] (1,0) -- (0,0) -- (0,1) -- cycle; %--------end graphics code ---------- \end{tikzpicture} • To draw a square filled with color we use fill command instead of draw command
 1 2 3 4 5 6 7 8 \usepackage{tikz} \begin{tikzpicture} %--------start graphics code -------- \draw[step=0.5,very thin,black!20] (-1,-1) grid (1,1); \fill[line width=2pt,color=red] (1,0) -- (0,0) -- (0,1) -- (1,1) -- cycle; %--------end graphics code ---------- \end{tikzpicture} • To draw a filled square with rounded corners
 1 2 3 4 5 6 7 8 \usepackage{tikz} \begin{tikzpicture} %--------start graphics code -------- \draw[step=0.5,very thin,black!20] (-1,-1) grid (1,1); \fill[line width=2pt,color=red] {[rounded corners] (1,0) -- (0,0) -- (0,1) -- (1,1)} -- cycle; %--------end graphics code ---------- \end{tikzpicture} • A circle is drawn by providing its center point or origin and the desired radius. The command is

 1 2 3 4 5 6 7 8 \usepackage{tikz} \begin{tikzpicture} %--------start graphics code -------- \draw[step=0.5,very thin,black!20] (-1,-1) grid (1,1); \draw[line width=2pt,color=red] (0,0) circle[radius=1]; %--------end graphics code ---------- \end{tikzpicture} • To draw an arc
 1 2 3 4 5 6 7 8 9 \usepackage{tikz} \begin{tikzpicture} %--------start graphics code -------- \draw[step=0.5,very thin,black!20] (-1,-1) grid (1,1); \draw[line width=2pt,color=red] (-1,0) arc (180:90:1) ; \draw[fill=red] (0,0)-- +(270:1cm) arc (270:360:1cm) -- cycle; \fill[color=red] (0,0) circle[radius=0.1]; %--------end graphics code ---------- \end{tikzpicture} • To draw an ellipse
 1 2 3 4 5 6 7 \usepackage{tikz} \begin{tikzpicture} %--------start graphics code -------- \draw[step=0.5,very thin,black!20] (-2,-1) grid (2,1); \draw[step=0.5,thin,black!20] (-1,-1) grid (1,1); %--------end graphics code ---------- \end{tikzpicture} • To draw an ellipse
 1 2 3 4 5 6 7 8 \usepackage{tikz} \begin{tikzpicture} %--------start graphics code -------- \draw[step=0.5,thin,black!20] (-2,-1) grid (2,1); \draw[line width=2pt,color=red,rotate=45] (0,0) ellipse (1.5 and 0.75); %--------end graphics code ---------- \end{tikzpicture} • Adding text to the picture
 1 2 3 4 5 6 7 8 9 \usepackage{tikz} \begin{tikzpicture} %--------start graphics code -------- \draw[step=0.5,thin,black!20] (-1,-1) grid (1,1); \node[draw,circle] at (-0.5,-0.5) (start) {start}; \node[draw,circle] at (0.5,0.5) (end) {end}; \draw[red,->] (start) to (end); %--------end graphics code ---------- \end{tikzpicture} • Scaling the picture
 1 2 3 4 5 6 7 8 9 \usepackage{tikz} \begin{tikzpicture}[scale=2] %--------start graphics code -------- \draw[step=0.5,thin,black!20] (-1,-1) grid (1,1); \node[draw,circle] at (-0.5,-0.5) (start) {start}; \node[draw,circle] at (0.5,0.5) (end) {end}; \draw[red,->] (start) to (end); %--------end graphics code ---------- \end{tikzpicture} • Plotting the data
 1 2 3 4 5 6 7 8 9 10 11 12 \usepackage{tikz} \begin{tikzpicture}[domain=0:7,scale=0.5] %--------start graphics code -------- \draw[very thin,color=gray] (0,-1) grid (7,7); \draw[->] (-0.2,0) -- (7,0) node[right] {$x$}; \draw[->] (0,-1.2) -- (0,7) node[above] {$f(x)$}; \draw[thick, color=red] plot[id=x] function{x} node[right] {$f(x) =x$}; \draw[color=blue] plot[id=sin] function{sin(x)} node[right] {$f(x) = \sin x$}; %--------end graphics code ---------- \end{tikzpicture} ## One thought on “Getting started with TikZ”

• rajan

to get relative positions as ordered pairs as (x,y) in tikz