Chapter 2 – First steps creating TikZ images

Here are the Code examples of this chapter. You can compile them online right on this web page by pressing the Typeset / Compile button. You can also edit them for testing, and compile again.

For a better view with the online compiler, I use \documentclass[border=10pt]{standalone} instead of \documentclass{article}. Instead of having a big letter/A4 page, the standalone class crops the paper to see just the visible text without an empty rest of a page.

Any question about a code example? Post it on LaTeX.org, I will answer. As forum admin I read every single question there. (profile link).

Using the tikzpicture environment

Drawing a grid

\documentclass[tikz,border=10pt]{standalone}
\begin{document}
\begin{tikzpicture}
  \draw[thin,dotted] (-3,-3) grid (3,3);
  \draw[->] (-3,0) -- (3,0);
  \draw[->] (0,-3) -- (0,3);
\end{tikzpicture}
\end{document}

Working with coordinates

Cartesian Coordinates

\documentclass[tikz,border=10pt]{standalone}
\begin{document}
\begin{tikzpicture}
  \draw[thin,dotted] (-3,-3) grid (3,3);
  \draw[->] (-3,0) -- (3,0);
  \draw[->] (0,-3) -- (0,3);
  \draw[very thick, blue] (-2,-2) -- (-2,2)
  -- (2,2) -- (2,-2) -- cycle;
\end{tikzpicture}
\end{document}
\documentclass[tikz,border=10pt]{standalone}
\begin{document}
\begin{tikzpicture}
  \draw[thin,dotted] (-3,-3) grid (3,3);
  \draw[->] (-3,0) -- (3,0);
  \draw[->] (0,-3) -- (0,3);
  \draw[very thick, blue] (-2,-2) circle (1) (-2,2)
    circle (1) (2,2) circle (1) (2,-2) circle (1);
\end{tikzpicture}
\end{document}

Polar coordinates

\documentclass[tikz,border=10pt]{standalone}
\begin{document}
\begin{tikzpicture}
  \draw[thin,dotted] (-3,-3) grid (3,3);
  \draw (-3,0) -- (3,0);
  \draw (0,-3) -- (0,3);
  \draw[very thick, blue] (0:2) -- (60:2) -- (120:2)
    -- (180:2) --(240:2) -- (300:2) -- cycle;
\end{tikzpicture}
\end{document}

Three-dimensional coordinates

\documentclass[tikz,border=10pt]{standalone}
\begin{document}
\begin{tikzpicture}[y={(-0.86cm,0.5cm)},x={(0.86cm,0.5cm)}, z={(0cm,1cm)},font=\sffamily]
  \draw[very thick, blue] (-2,-2,0) -- (-2,2,0) -- (2,2,0) -- (2,-2,0) -- cycle;
  \draw[->] (0,0,0) -- (2.5, 0,  0) node [right] {x};
  \draw[->] (0,0,0) -- (0,  2.5, 0) node [left] {y};
  \draw[->,dashed] (0,0,0) -- (0,  0, 2.5) node [above] {z};
  \draw circle (2);
\end{tikzpicture}
\end{document}

Using relative coordinates

\documentclass[tikz,border=10pt]{standalone}
\begin{document}
\begin{tikzpicture}
  \draw[thin,dotted] (-3,-3) grid (3,3);
  \draw[very thick, blue] (-2,-2) -- ++(1,0) -- ++(0,1) --++ (1,0) -- ++(0,1) --++ (1,0) -- ++(0,1) --++ (1,0);
\end{tikzpicture}
\end{document}
\documentclass[tikz,border=10pt]{standalone}
\begin{document}
\begin{tikzpicture}
  \draw[thin,dotted] (-3,-3) grid (3,3);
  \draw[->] (-3,0) -- (3,0);
  \draw[->] (0,-3) -- (0,3);
  \draw[very thick, blue] (-3,-1) -- +(1,0) -- +(2,2) -- +(4,2) --+(5,0) -- + (6,0);
\end{tikzpicture}
\end{document}
\documentclass[tikz,border=10pt]{standalone}
\begin{document}
\begin{tikzpicture}
  \draw[thin,dotted] (-3,-3) grid (3,3);
  \draw[->] (-3,0) -- (3,0);
  \draw[->] (0,-3) -- (0,3);
  \draw[very thick, blue] (-3,-1) -- ++(1,0) -- ++(1,2) -- ++(2,0) --++ (1,-2) -- ++ (1,0);
\end{tikzpicture}
\end{document}

Drawing geometric shapes

\documentclass[tikz,border=10pt]{standalone}
\begin{document}
\begin{tikzpicture}
  \draw (0,0) circle [radius=2];
  \draw (-0.5,0.5,0) ellipse [x radius=0.2, y radius=0.4];
  \draw (0.5,0.5) ellipse [x radius=0.2, y radius=0.4];
  \draw (-1,-1) arc [start angle=185, end angle=355,
    x radius=1, y radius=0.5];
  \draw (-3,-3) rectangle (3,3);
\end{tikzpicture}
\end{document}

Using colors

\documentclass[tikz,border=10pt]{standalone}
\begin{document}
\begin{tikzpicture}
  \draw[fill=yellow] (0,0) circle [radius=2];
  \draw[fill=black] (-0.5,0.5,0) ellipse [x radius=0.2, y radius=0.4];
  \draw[fill=black] (0.5,0.5,0) ellipse [x radius=0.2, y radius=0.4];
  \draw[very thick] (-1,-1) arc [start angle=185, end angle=355,
    x radius=1, y radius=0.5];
\end{tikzpicture}
\end{document}
\documentclass[tikz,border=10pt]{standalone}
\begin{document}
\begin{tikzpicture}
  \draw[shading=ball,ball color=yellow] (0,0) circle [radius=2];
  \draw[shading=ball,ball color=black] (-0.5,0.5,0) ellipse [x radius=0.2, y radius=0.4];
  \draw[shading=ball,ball color=black] (0.5,0.5,0) ellipse [x radius=0.2, y radius=0.4];
  \draw[very thick] (-1,-1) arc [start angle=185, end angle=355,
    x radius=1, y radius=0.5];
\end{tikzpicture}
\end{document}