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 (a,b) represent a point where it is “a” centimeters in the x-direction and “b” centimeters in the y-direction from the origin. TikZ uses one centimeter as the default unit of measure
- Polar coordinates : The syntax (α:r) represent a point where it is at an angle, “α” in degrees, and distance “r” in centimeters from the origin
- 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
\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 numberbed grid with coordinates defined using figHt and figWd variables.
figHt and figWd are the variables, assigned the desired figure height and width dimensions in centimeters.
The grid is drawn between (-0.5,-0.5) and (figHt,figWd) coordinates.
“\foreach” command is used to number the grid along x and y-axes.1 2 3 4 5 6 7 8 9 10 11
\begin{tikzpicture} %--------start graphics code -------- %Grid for intial drawing. %Comment next three lines once typesetting finished \def\figHt{2}; \def\figWd{4}; \draw[step=0.5,very thin, black!20] (-0.5,-0.5) grid (\figWd,\figHt); \foreach \x in {0,...,\figWd} {\node [anchor=north] at (\x,-0.5) {\x};} \foreach \y in {0,...,\figHt} {\node [anchor=east] at (-0.5,\y) {\y};} %--------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
\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
\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
\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
\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
\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 “\draw (a,b) circle (radius=r0);” draws a circle at coordinate (a,b) with radius r0.1 2 3 4 5 6 7
\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
\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
\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
\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
\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 a picture
A picture can be scaled ‘x’ times by passing optional argument [scale=x] to the tikzpicture environment. Here ‘x’ is a number
1 2 3 4 5 6 7 8
\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
\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}