Basic TikZ

Learning Objectives:

Basic TikZ technique to draw:

  • Straight Line
  • Node
  • Curve
  • Shape
  • Fill

Setup

Loading Package

To load the TikZ package, we just need to use the function \usepackage{tikz}

\documentclass{article}
\usepackage{tikz}
\begin{document}
...
\end{document}

TikZ Environment

First, we need to tell which part of code should be included in the drawing by using begin{tikzpicture} and \end{tikzpiture}.

\begin{tikzpicture}
...
\end{tikzpiture}

Straight line

We first discuss simple straight line and then we discuss its decoration.

Draw a line

To draw a straight line, use \draw, and specify the coordinates. To end drawing, we must end with a semicolon.

\draw(0,0)--(3,3);

To draw connected lines, we specify all the coordinates. Below draws a square:

\draw(0,0)--(4,4)--(4,0)--(0,0);

Line Pattern

To draw a dashed line, we use the option [dashed]:

\draw[dashed] (0,0)--(4,4);

To draw a dotted line, we use the option [dotted]:

\draw[dotted] (4,0)--(0,4);

Line Thickness

We can specify relative thickness:

\draw[ultra thin] (0,6)--(4,6);
\draw[very thin] (0,5)--(4,5);
\draw[thin] (0,4)--(4,4);
\draw[semithick] (0,3)--(4,3);
\draw[thick] (0,2)--(4,2);
\draw[very thick] (0,1)--(4,1);
\draw[ultra thick] (0,0)--(4,0);

We can also specify thickness directly:

\draw[line width=11] (0,1)--(4,1);
\draw[line width=5mm] (0,0)--(4,0);

Line Color

\draw[red] (0,2)--(4,2);
\draw[blue] (0,1)--(4,1);
\draw[green] (0,0)--(4,0);

Line Cap

Now we add arrow to the tip.

\draw[->] (0,5)--(4,5);
\draw[<->] (0,4)--(4,4);
\draw[|->] (0,3)--(4,3);
\draw[<-|] (0,2)--(4,2);
\draw[|-|] (0,1)--(4,1);
\draw[|<->|] (0,0)--(4,0);

Braces

\usetikzlibrary{decorations.pathreplacing}
\draw [decorate,
          decoration={brace,amplitude=4pt,         
          mirror},xshift=0pt,yshift=-3pt]
          (1.4,0) -- (2.4,0) 
          node [black,midway,yshift=-.5cm]
          {abc};

Node

Node is to add description to the graph.

Draw a node

To write description, use \node and put the coordinate after at.

\node at (0,0)  {$\alpha$};
\node at (1,1)  {$\beta$};

Shift location

To shift the relative location of the nodes, we use \node[location] where location can be below/left/right/above/below left.

\node[above] at (1,1)  {$\gamma$};
\node[below] at (1,1)  {$\gamma$};
\node[left] at (1,1)  {$\gamma$};
\node[right] at (1,1)  {$\gamma$};

To shift the relative location of the nodes, we use \node[xshift=1cm, yshift=3cm]:

\node[xshift=3mm] at (1,1) {$\gamma$};
\node[yshift=3mm] at (1,1) {$\gamma$};

Curves

There are many ways to draw a curve:

  • Bending a straight line
  • In and out
  • Right angle bend
  • Arc
  • Smoothing coordinate
  • Beizer Curve

Bending straight line

One way to do is to bend a straight line

\draw (0,0) to [bend right] (3,3);
\draw (1,1) to [bend left] (4,4)

In and out degree

To more accurate control the bending, one way to do is to specify outgoing and incoming coordinates to bend a straight line

\draw (0,0) to [out =90, in =180] (4,4);

Right angle bend

Very often, we need a 90 degree bend to draw digram:

\draw (0,0) -| (3,3);
\draw (1,1) |- (4,4);

Arc

Forth method is by drawing an arc.

\draw (1,0) arc (0:145:2cm);

Smooth coordinate

Fifth method is by smoothing over multiple coordinates:

\draw plot [smooth] coordinates {(0,0) (1,3) (3,2) (4,4)};

Beizer curve

Sixth method is by Beizer curve. It is very common in programming language.

\draw (0,0) .. controls (1,3) and (3,5) .. (4,4);

Basic Shapes

Circle

To draw a circle, we specify the center and the radius.

\draw (0,0) circle [radius =0.6cm];

If we fill the circle with color, then we have dots:

\draw[fill] (0,0) circle [radius =0.06];

Ellipse

\draw (0,0) ellipse (2 and 1);

Rectange

\draw (0,0) rectangle (4,4);

Grid

\draw[step=1,gray,very thin] (0,0) grid (4,4);

Fill

To emphasize the area, we often use color and pattern. Color is good for electronic version and pattern is good for printed version.

Fill Color

\draw[fill=red] (0,0) rectangle (2,2);
\draw[fill=blue] (2,2) rectangle (4,4);

To change opacity, use the option fill opacity.

\draw[fill=red, fill opacity=0.2] (0,0) rectangle (2,2);
\draw[fill=blue, fill opacity=0.2] (2,2) rectangle (4,4);

Fill pattern

To fill pattern, we need to use a library. To do so we need \usetikzlibrary{patterns}

\usetikzlibrary{patterns}
\draw[pattern=horizontal lines] (0,0) rectangle (1,1);
\draw[pattern=vertical lines] (1,1) rectangle (2,2);
\draw[pattern=north east lines] (2,2) rectangle (3,3);
\draw[pattern=north west lines] (3,3) rectangle (4,4);
\draw[pattern=grid] (4,4) rectangle (5,5);
\draw[pattern=crosshatch] (5,5) rectangle (6,6);
\draw[pattern=dots] (6,6) rectangle (7,7);
Next