Introduction to TikZ

This tutorial gives a brief introduction in how to draw in LaTeX using the TikZ package. TikZ allows you to create and include vector graphics directly in your LaTeX document.

TikZ setup

In order to use TikZ you must include the package using the \usepackage{tikz} command. Inside the document you create a new picture using \begin{tikzpicture} and \end{tikzpicture}. Everything in between these two commands is interpreted as an image.

Drawing lines

You can use the \draw command to specify the coordinates of the line and -- to specify to draw a straight line. Coordinates are in centimeters by default.
\documentclass{article}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
        \draw (0,0) -- (2,4);
    \end{tikzpicture}
\end{document}
You can specify a sequence of points for the line and draw multiple lines in one picture. The grid command draws a grid to see where the points exactly are.
\documentclass{article}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
        \draw (0,0) grid (4,3);
        \draw (0,0) -- (1,2) -- (3,3) -- (4,2);
        \draw (2,0) -- (3,2);
    \end{tikzpicture}
\end{document}

Scaling pictures

You can scale an image up or down by using the scale option.
\documentclass{article}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}[scale=0.5]
        \draw (0,0) grid (2,2);
        \draw (0,0) -- (1,1);
    \end{tikzpicture}

    \begin{tikzpicture}[scale=2]
    	\draw (0,0) grid (2,2);
    	\draw (0,0) -- (1,1);
    \end{tikzpicture}
\end{document}

Drawing arrows

The draw command accepts several different options that change the appearance of the line endings.
\documentclass{article}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
    	\draw [->] (0,2) -- (1,3);
    	\draw [<-] (1,2) -- (2,3);
    	\draw [|->] (2,2) -- (3,3);
        \draw [<->] (3,2) -- (4,3);
        \draw [<->] (1,1) -- (1,0) -- (3,0);
    \end{tikzpicture}
\end{document}

Line thickness

You can change the thickness of every single line so that it matches your requirements. You can use: ultra thin, very thin, thin, semithick, thick, very thick and ultra thick.
\documentclass{article}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
    	\draw [<->] (0,4) -- (0,0) -- (4,0);
    	\draw [thick] (0,3) -- (3,0);
    	\draw [ultra thick] (0,0) -- (3,3);
    	\draw [ultra thin] (2,0) -- (2,2) -- (0,2);
    \end{tikzpicture}
\end{document}
You can specify your custom width using the line width option of the \draw command. The default unit is pt.
\documentclass{article}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
    	\draw [<->, line width=1] (0,4) -- (0,0) -- (4,0);
    	\draw [line width=0.5mm] (0,3) -- (3,0);
    	\draw [line width=0.1cm] (0,0) -- (3,3);
    	\draw [line width=0.1] (2,0) -- (2,2) -- (0,2);
    \end{tikzpicture}
\end{document}

Coloring lines

You can give every line a different color. There are several predefined ones but you can also use your own by adding \definecolor{mycolor}{rgb}{0,0.6,0.5} in the preamble of your document.
\documentclass{article}
\usepackage{tikz}
\definecolor{mycolor}{rgb}{0,0.6,0.5}
\begin{document}
    \begin{tikzpicture}
    	\draw [thick, gray] (0,1) -- (2,1);
    	\draw [thick, mycolor] (0, 0.5) -- (2,0.5);
    	\draw [thick, blue] (0,0) -- (2,0);
    \end{tikzpicture}
\end{document}

Drawing shapes

You can draw shapes like rectangle, circle, stars, etc. There exist much more. The following example depicts only a few.
\documentclass{article}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
        \draw [blue] (0,0) rectangle (2,1);
        \draw [red, thick] (2,1) circle [radius=0.5];
        \draw [green, ultra thick] (0,0.5) ellipse [x radius = 0.5cm, y radius = 1cm];
    \end{tikzpicture}
\end{document}