Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 38 ยท TikZ and Graphics

TikZ Basics: Drawing in LaTeX

Start drawing with TikZ: the tikzpicture environment, coordinates, \draw, lines, circles, arcs, curves, colours, line styles and arrows, with a live SVG stand-in.

TikZ is a drawing language for LaTeX. Instead of making a picture in another program and inserting it, you describe the picture with commands, and LaTeX draws it using the same fonts and colours as your text. Diagrams stay sharp at every zoom level, change easily, and can include mathematics in their labels.

TikZ sits on top of PGF, the "portable graphics format". You load it with one line and draw inside a tikzpicture environment.

LATEX
\usepackage{tikz}

\begin{tikzpicture}
  \draw (0,0) -- (3,2);
\end{tikzpicture}

Every command ends with a semicolon. Coordinates are (x,y) pairs; the default unit is 1 cm. The origin is at the bottom left, y grows upwards. You can use other units: (1in, 2pt), (2mm, 1.5cm).

Lines and paths

A path is a sequence of points joined by operations. \draw strokes the path, \fill fills it and \filldraw does both.

LATEX
\begin{tikzpicture}
  \draw (0,0) -- (2,0) -- (2,1.5) -- cycle;   % triangle (cycle closes the path)
  \draw[thick] (3,0) rectangle (5,1.5);        % rectangle: two opposite corners
  \draw (7,0.75) circle (0.75);                % centre and radius
  \draw (9,0.75) ellipse (1 and 0.5);
  \draw (0,3) arc (0:270:0.8);                 % start angle, end angle, radius
  \draw (3,3) to[out=60,in=120] (6,3);         % a curve with tangent angles
  \draw (7,3) .. controls (8,4) and (9,2) .. (10,3);   % Bezier curve
\end{tikzpicture}

Horizontal and vertical moves have shortcuts: -| goes horizontally then vertically, |- the other way round. Relative coordinates use ++(dx,dy) (and move the current point) or +(dx,dy) (and do not):

LATEX
\draw (0,0) -- ++(1,0) -- ++(0,1) -- ++(-1,0) -- cycle;   % unit square

Polar coordinates use an angle and a radius: (30:2) is a point at 30 degrees, 2 cm from the origin.

The stand-in below reads a few TikZ-like commands and draws them as SVG, so you can change numbers and see how paths behave. It supports \draw with lines, rectangle and circle, and an optional colour.

Mini TikZ to SVG

Colours, widths and styles

Options go in square brackets after \draw:

LATEX
\draw[red, thick] (0,0) -- (2,0);
\draw[blue!60, very thick, dashed] (0,0.5) -- (2,0.5);   % 60% blue, rest white
\draw[line width=2pt, green!50!black] (0,1) -- (2,1);    % mix: half green, half black
\draw[dotted] (0,1.5) -- (2,1.5);
\draw[rounded corners=5pt] (3,0) rectangle (5,1.5);
\filldraw[fill=yellow!40, draw=orange] (6,0.75) circle (0.6);
\draw[opacity=0.4, fill=blue] (0,3) rectangle (2,4);

Line width names: ultra thin, very thin, thin, semithick, thick, very thick, ultra thick. Dash styles: dashed, dotted, densely dashed, loosely dotted, dash dot. Colours come from xcolor; define your own with \definecolor{mine}{RGB}{200,60,40}.

Arrows

Add -> to draw an arrowhead at the end, <-> for both ends, or -latex, -stealth, |-|:

LATEX
\draw[->] (0,0) -- (3,0) node[right] {$x$};
\draw[<->, thick] (0,1) -- (3,1);
\draw[-{Stealth[length=4mm]}] (0,2) -- (3,2);

Load \usetikzlibrary{arrows.meta} for the modern arrow tip names (Stealth, Latex, Triangle, To).

Grids, axes and clipping

LATEX
\draw[help lines, step=0.5] (0,0) grid (4,3);       % light grid
\draw[->] (-0.2,0) -- (4.2,0);                      % x-axis
\draw[->] (0,-0.2) -- (0,3.2);                      % y-axis
\begin{scope}
  \clip (0,0) rectangle (2,2);                      % later drawing is cut here
  \draw (1,1) circle (1.5);
\end{scope}

scope groups commands so that options (shifts, colours) apply only within; \begin{scope}[shift={(2,1)}, rotate=30] moves the coordinate system itself. Use \coordinate (A) at (1,2); to name a point and reuse it as (A).

Why not draw everything by hand?

For a single figure, TikZ is the best way to match LaTeX typography. For plots of data, use PGFPlots (see pgfplots); for large diagrams use nodes and styles (nodes and diagrams, loops and styles). TikZ code can be slow to compile for hundreds of elements; for heavy images use the external library, which caches each picture as a PDF.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the default unit in TikZ coordinates?

    Coordinates without a unit are in centimetres.
  2. 2. What does \draw (0,0) -- (2,0) -- (2,1) -- cycle; draw?

    cycle joins the last point back to the first and closes the path, giving a triangle.
  3. 3. Which option draws an arrow at the end of a line?

    -> adds an arrow tip at the end; <-> adds two.
  4. 4. What does ++(1,0) do in a path?

    Double plus means a relative coordinate that also becomes the new current point; a single plus does not move it.

Summary

  • TikZ draws in tikzpicture using paths made of coordinates and operations; every command ends with ;.
  • \draw, \fill, \filldraw with --, rectangle, circle, arc, to and Bezier curves cover most shapes.
  • Options set colour, width, dash, rounded corners, fill and opacity; -> adds arrows.
  • Grids, scopes, clipping and named coordinates keep drawings tidy.

Next: TikZ Nodes and Diagrams