Researa LogoReseara
Start free
Learn Hub/GUIDE 12 / 13Figures & Tables
Quick syntax:\begin{tikzpicture} \draw[->] (0,0) -- (2,0); \end{tikzpicture}

Diagrams in LaTeX with TikZ

TikZ draws diagrams from code, inside your document. They are vector graphics, so they stay sharp at any zoom, and they use your document's fonts. This guide builds up to the diagrams a report needs: flowcharts, architecture diagrams, trees and state machines.

Setup

\usepackage{tikz}
\usetikzlibrary{positioning, arrows.meta, shapes.geometric}

Libraries add features: positioning places boxes relative to each other, arrows.meta gives better arrow tips and shapes.geometric adds diamonds and other shapes.

Lines and shapes

\begin{tikzpicture}
  \draw (0,0) -- (2,0) -- (2,1) -- cycle;          % a triangle
  \draw (3,0) rectangle (5,1);
  \draw (6.5,0.5) circle (0.5);
  \draw[->, thick, blue] (8,0) -- (10,1);
  \draw[dashed, fill=orange!20] (11,0) rectangle (13,1);
\end{tikzpicture}

Coordinates are in centimetres from the origin. -- joins points, cycle closes the path, and the options in brackets set colour, thickness, dashes, fill and arrows. Every command ends with a semicolon; a missing one is the most common TikZ error.

Nodes: boxes with text

\begin{tikzpicture}
  \node[draw, rounded corners] (client) at (0,0) {Client};
  \node[draw, rounded corners] (api)    at (3,0) {API};
  \draw[-Stealth] (client) -- (api);
\end{tikzpicture}

A node is text with an optional shape around it. Naming it, here (client), lets you draw arrows to it, and TikZ stops the arrow at the edge of the box.

Placing nodes relative to each other

Computing coordinates by hand gets tedious. With the positioning library, place each node next to another and let TikZ do the arithmetic:

\begin{tikzpicture}[
  box/.style={draw, rounded corners, minimum width=2.4cm, minimum height=8mm},
  node distance=8mm and 14mm
]
  \node[box]                       (web)    {Web app};
  \node[box, right=of web]         (api)    {API};
  \node[box, right=of api]         (db)     {PostgreSQL};
  \node[box, below=of api]         (worker) {Worker};

  \draw[-Stealth] (web) -- node[above] {HTTPS} (api);
  \draw[-Stealth] (api) -- (db);
  \draw[-Stealth] (api) -- node[right] {queue} (worker);
\end{tikzpicture}

box/.style defines a style once, so every box matches. node distance=8mm and 14mm sets the vertical and horizontal gaps. A node placed on a line, like node[above] {HTTPS}, labels the arrow. Write right=of web, with the equals sign: the older right of=web still works but ignores the node sizes.

Flowcharts

\begin{tikzpicture}[
  node distance=8mm,
  every node/.style={font=\small},
  terminal/.style={draw, rounded corners=3mm, minimum width=2.6cm, minimum height=7mm},
  step/.style={draw, minimum width=2.6cm, minimum height=7mm},
  test/.style={draw, diamond, aspect=2.2, inner sep=1pt},
  arrow/.style={-Stealth}
]
  \node[terminal]               (start) {Start};
  \node[step, below=of start]   (read)  {Read credentials};
  \node[test, below=of read]    (ok)    {Valid?};
  \node[step, below=of ok]      (open)  {Open session};
  \node[terminal, below=of open] (stop) {End};
  \node[step, right=12mm of ok] (err)   {Show error};

  \draw[arrow] (start) -- (read);
  \draw[arrow] (read) -- (ok);
  \draw[arrow] (ok) -- node[left] {yes} (open);
  \draw[arrow] (open) -- (stop);
  \draw[arrow] (ok) -- node[above] {no} (err);
  \draw[arrow] (err) |- (read);
\end{tikzpicture}

|- draws a right-angled path: first vertically, then horizontally. Use -| for the other way round.

Trees

\begin{tikzpicture}[
  every node/.style={draw, rounded corners, font=\small},
  level distance=12mm,
  level 1/.style={sibling distance=34mm},
  level 2/.style={sibling distance=18mm}
]
  \node {App}
    child { node {Auth}
      child { node {Login} }
      child { node {Signup} } }
    child { node {Projects}
      child { node {Editor} }
      child { node {Export} } };
\end{tikzpicture}

Each child hangs below its parent. Adjust sibling distance per level when boxes overlap. For large trees, the forest package lays them out automatically.

State machines

\usetikzlibrary{automata}   % preamble

\begin{tikzpicture}[node distance=28mm, -Stealth]
  \node[state, initial]              (draft)  {Draft};
  \node[state, right=of draft]       (review) {Review};
  \node[state, accepting, right=of review] (pub) {Published};

  \draw (draft)  edge node[above] {submit}  (review)
        (review) edge node[above] {approve} (pub)
        (review) edge[bend left] node[below] {reject} (draft);
\end{tikzpicture}

Repeating with foreach

\begin{tikzpicture}
  \foreach \x in {0,...,4}
    \draw (\x,0) circle (0.3) node {\x};
\end{tikzpicture}

Putting a diagram in a figure

\begin{figure}[htbp]
  \centering
  \begin{tikzpicture}
    ...
  \end{tikzpicture}
  \caption{Overall architecture of the platform}
  \label{fig:architecture}
\end{figure}

A diagram is wider than the page? Shrink the distances, not the picture. Scaling the whole drawing also shrinks its text below the size of the rest of your report.

Useful libraries

LibraryAdds
positioningright=of, below=of and friends
arrows.metaArrow tips such as Stealth and Latex
shapes.geometricDiamonds, ellipses, cylinders (for databases)
automataStates for state machines
calcArithmetic on coordinates, such as midpoints
fitA box drawn around a group of nodes

Where to go next

READY TO WRITE YOUR REPORT OR THESIS?

Write in Researa. Present in Researa Slides.

Draft your LaTeX manuscript with AI assistance and verified citations, then turn your finished PDF into a defense slide deck in minutes.